Reputation: 769
Well i have one button and one ImageView in my app. What i am trying to do is when i pressing on the button then the image at the ImageView will change. All i have are two pics file.
What i am trying to do is - if the first pic is linked to the ImageView than change it to pic2 by clicking on the button, and if pic2 is linked than a click on the button will change it back to the first pic file.
here's the onClick method i tried to use:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
Thanks for any kind of help
Upvotes: 5
Views: 5766
Reputation: 1
You can also create a Boolean variable and assign it true and then if the boolean variable is true you change the picture of image button and set the boolean variable to false:- here my code example in java:-
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton play_pause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_pause = findViewById(R.id.play_pause_button);
final boolean[] play_or_pause = new boolean[1];
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
play_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (play_or_pause[0]) {
play_pause.setImageResource(R.drawable.ic_baseline_pause_24);
play_or_pause[0] = false;
}else {
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
}
}
});
}
}
and xml code:-
<?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"
android:background="@color/black"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="252dp"
android:layout_height="48dp"
android:gravity="center"
android:text="TBT Music Player"
android:textColor="@color/white"
android:textSize="30sp"
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.087" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="247dp"
android:layout_height="263dp"
android:src="@drawable/ic_baseline_library_music_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.124" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="327dp"
android:layout_height="41dp"
android:thumbTint="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:layout_constraintVertical_bias="0.163"
android:secondaryProgressTint="@color/white"/>
<ImageButton
android:id="@+id/play_pause_button"
android:layout_width="64dp"
android:layout_height="77dp"
android:backgroundTint="@color/black"
android:src="@drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar"
app:layout_constraintVertical_bias="0.213" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 146
private ImageView ib1;
private int currentImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib1 = (ImageView) findViewById(R.id.imageView1);
currentImage = R.drawable.pic1;
}
public void onClick(View view){
currentImage = (currentImage == R.drawable.pic1) ? R.drawable.pic2 : R.drawable.pic1;
ib1.setImageResource(currentImage);
}
Upvotes: 0
Reputation: 13436
Rather than checking the image, I would suggest set the information tag of the ImageView
each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
Upvotes: 12