Search code examples
javaandroidimageviewandroid-6.0-marshmallowimagebutton

Cant get swap 2 images with a imagebutton


Im trying that when i click on the ImageButton swap both the view and the button for others that i have on drawable

I can see the principal layout with the TextView, the ImageButton and the Image View. When i click on the button my app get close. I try different methods and anything works. I should declare the onClick on the xml file.

MAINACTIVITY.JAVA

package com.example.practica_walter_sin_layouts;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView walter;
    private ImageButton telefono;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    private void llamando(View vista){
        walter=findViewById(R.id.walter) ;
        telefono =findViewById(R.id.llamar) ;
        walter.setImageResource(R.drawable.walterwhite2);
        telefono.setImageResource(R.drawable.colgar);


    }

}

ACTIVITY_MAIN.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/encabezado"
        android:layout_width="207dp"
        android:layout_height="47dp"
        android:text="Puedes llamar a walter white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.096" />

    <ImageView
        android:id="@+id/walter"
        android:layout_width="213dp"
        android:layout_height="211dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.779"
        app:srcCompat="@drawable/walterwhite" />

    <ImageButton
        android:id="@+id/llamar"
        android:layout_width="106dp"
        android:layout_height="116dp"
        android:src="@drawable/llamar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.265"
        app:srcCompat="@drawable/llamar"
        android:onClick="llamando"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

The app should swap view and button for another view and another button, but always close. There are no compile errors


Solution

  • Your method llamando needs to be public