I have this xml file where there are 4 image views, with one logo at the centre and the other three are just circles with radius greater than the previous one surrounding the logo. I want to display this as an animation until my page gets loaded. In want that the three circles appear one after the other in loop in a sequence. How can I do that ?
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/center" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim1" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim2" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have also attached a picture for a better visualization if needed :
enter image description here
#Srijan,
There are more ways to achieve this.
Please refer to objectAnimator or animation-list drawable for more details.
As a quick answer to your problem, please see the below sample code.
package com.jrh.testanim;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
int count= 0;
int imgArr[] = new int[]{
R.drawable.circle1,
R.drawable.circle2,
R.drawable.circle3,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.animation_imageview);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
imageView.setImageResource(imgArr[count]);
handler.postDelayed(this, 500);
count++;
if (count > 2) {
count = 0;
}
}
}, 500);
}
}
Please check this and mark accepted, if it solves your problem.
Thanks
JRH
Related
I'm a beginner, but I want to learn and I'm developing my first app!!I would also like to use the "toaster" function in the app. Unfortunately no longer works !!
Not only in my app, but no matter where I want to use it. Should I reinstall android studio?
Thank you, Stefan
Hi, no error message! Just not work!!
MainActivity:
package de.havadinagy.toaster_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void info(View view){
Toast.makeText(this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main :
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="243dp"
android:layout_height="172dp"
android:onClick="info"
android:text="ToasterTest"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this
Toast.makeText(requireContext(), "message", Toast.LENGTH_SHORT).show()
do try this
try
public void info(View view){
Toast.makeText(MainActivity.this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
and change "info"
I am using motionlayout to handle animations since it's seems like a good choice. One of the animation i should implement required to alter the src of the image and i couldnt animate that.(seems like we cant change src attribute of an image in constraintsets of motionlayour scene file) Then i found its posssible to crossfade two images with ImageFilterView. I finish the job and switch to another activity, there i need same resource and as you expect i used it but it didn't appeared. I thought this is related with something else, i try many different ways to find out but after some time I found its because motion layout. when i use the image as altsrc with imagefilterview and if its somehow ended up faded state this image will not be visible even if you use it in another activity. it keeping its state across the whole app. What i am wondering is this a bug or some "supposed to be" way logic?
Edit:
upon #hoford request I create a new project just to test it. and it happened again.
Basically what i am try to do is move an image from right side of the screen to left side when clicked. meanwhile its should fading to another image. and doing the opposite on second click
Here is what i do:
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MotionLayout _motionlayout;
boolean isForward = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_motionlayout = findViewById(R.id.rootlayout_motionlayout);
ImageFilterView imageFilterView = findViewById(R.id.about_imagefilterview);
isForward = true;
imageFilterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isForward) {
Log.d("TAG", "onClick: inside if");
_motionlayout.setTransition(R.id.mainactivity_transition_about_icon_1);
_motionlayout.transitionToEnd();
isForward = false;
} else {
Log.d("TAG", "onClick: inside else");
_motionlayout.setTransition(R.id.mainactivity_transition_about_icon_2);
_motionlayout.transitionToEnd();
isForward = true;
}
}
});
findViewById(R.id.open_second_activity_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="#+id/rootlayout_motionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d80"
app:layoutDescription="#xml/activity_main_scene"
tools:context=".MainActivity">
<!--
damn overlay!
https://stackoverflow.com/questions/52996035/motionlayout-using-crossfade-altsrc-appears-on-top-of-src-original-image-does
-->
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="#+id/about_imagefilterview"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/ic_about_icon_normal_filled"
app:altSrc="#drawable/ic_about_icon_normal_empty"
app:layout_constraintBottom_toTopOf="#id/mainactivity_guidline_about_icon_bottom"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toStartOf="#id/mainactivity_guidline_about_icon_right_end"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toEndOf="#id/mainactivity_guidline_about_icon_left_end"
app:layout_constraintTop_toBottomOf="#id/mainactivity_guidline_about_icon_top"
app:overlay="false" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainactivity_guidline_about_icon_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.038" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainactivity_guidline_about_icon_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.079" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainactivity_guidline_about_icon_right_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/mainactivity_guidline_about_icon_left_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />
<Button
android:id="#+id/open_second_activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
activity_main_scene.xml
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
android:id="#+id/mainactivity_transition_about_icon_1"
motion:constraintSetEnd="#+id/mainactivity_constraintset_state2"
motion:constraintSetStart="#id/mainactivity_constraintset_state1"
motion:duration="500" />
<Transition
android:id="#+id/mainactivity_transition_about_icon_2"
motion:constraintSetEnd="#+id/mainactivity_constraintset_state1"
motion:constraintSetStart="#id/mainactivity_constraintset_state2"
motion:duration="500" />
<!-- ===================================== CONSTRAINTSETS ===================================== -->
<ConstraintSet android:id="#+id/mainactivity_constraintset_state1">
<Constraint
android:id="#+id/about_imagefilterview"
android:layout_width="0dp"
android:layout_height="0dp"
motion:layout_constraintBottom_toTopOf="#id/mainactivity_guidline_about_icon_bottom"
motion:layout_constraintDimensionRatio="1"
motion:layout_constraintEnd_toStartOf="#id/mainactivity_guidline_about_icon_right_end"
motion:layout_constraintHorizontal_bias="1"
motion:layout_constraintStart_toEndOf="#id/mainactivity_guidline_about_icon_left_end"
motion:layout_constraintTop_toBottomOf="#id/mainactivity_guidline_about_icon_top">
<CustomAttribute
motion:attributeName="crossfade"
motion:customFloatValue="0" />
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="#+id/mainactivity_constraintset_state2">
<Constraint
android:id="#+id/about_imagefilterview"
android:layout_width="0dp"
android:layout_height="0dp"
motion:layout_constraintBottom_toTopOf="#id/mainactivity_guidline_about_icon_bottom"
motion:layout_constraintDimensionRatio="1"
motion:layout_constraintEnd_toStartOf="#id/mainactivity_guidline_about_icon_right_end"
motion:layout_constraintHorizontal_bias="0"
motion:layout_constraintStart_toEndOf="#id/mainactivity_guidline_about_icon_left_end"
motion:layout_constraintTop_toBottomOf="#id/mainactivity_guidline_about_icon_top">
<CustomAttribute
motion:attributeName="crossfade"
motion:customFloatValue="1" />
</Constraint>
</ConstraintSet>
</MotionScene>
SecondActivity.Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_second.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"
android:background="#b40"
tools:context=".SecondActivity">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/ic_about_icon_normal_empty"
app:layout_constraintBottom_toTopOf="#id/secondactivity_imageview_bottom"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toStartOf="#id/secondactivity_imageview_right_end"
app:layout_constraintStart_toEndOf="#id/secondactivity_imageview_left_end"
app:layout_constraintTop_toBottomOf="#id/secondactivity_imageview_top" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/secondactivity_imageview_left_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.40" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/secondactivity_imageview_right_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.60" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/secondactivity_imageview_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.40" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/secondactivity_imageview_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.60" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am doing a project that requires me to change an image in the same form content page with a button onclick or another button method if you have one. I normally program in C# with visual studio and I am not used to the java so I would greatly appreciate it, Thank you.
Here is a sample image changer code
Xml -
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="#+id/changeBtn"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="151dp"
android:layout_marginLeft="151dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="4dp"
android:onClick="change"
android:text="Change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="456dp"
android:layout_height="632dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cats01" />
</android.support.constraint.ConstraintLayout>
Main Activity code
public class MainActivity extends AppCompatActivity {
private int i =0;
public void change(View view)
{
ImageView imageView2 = (ImageView)findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.cats01); // this function
changes the image
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have coded an app to learn about Custom List View. While the app has no errors, I have one issue. I have shown it in the images as follows.
IMAGE 1
IMAGE 2
The problem is I have to scroll a long way before i can find the next list item.The list items do not appear one below the other.How to resolve this issue?
Here is my code:
package com.example.hp.customlistview;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int IMAGES[]={R.drawable.adsense, R.drawable.allo, R.drawable.chrome, R.drawable.firebase, R.drawable.youtube};
String[] NAMES={"AdSense","Allo","Chrome","Firebase","YouTube"};
String [] DESCRIPTIONS={"Money through ads","Video calling","Web Browser","Cloud Database","Video Streaming"};
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
Log.d("Length of Array ","The length is "+IMAGES.length);
return IMAGES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater=(LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.customlayout, null);
Log.d("Image ID","The id is "+R.id.imageView);
ImageView imageView=(ImageView)view.findViewById(R.id.imageView);
TextView textView_name=(TextView)view.findViewById(R.id.textView);
TextView textView_desc=(TextView)view.findViewById(R.id.textView2);
if(imageView==null)
Log.d("NULL?","YES IT IS NULL");
else
Log.d("NULL?","NO IT IS NOT NULL");
imageView.setImageResource(IMAGES[i]);
textView_name.setText(NAMES[i]);
textView_desc.setText(DESCRIPTIONS[i]);
Log.d("Hello","Hello there "+textView_name.getText().toString());
return view;
}
}
}
customlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="120dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="161dp"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="237dp"
android:layout_height="57dp"
android:layout_marginBottom="380dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="44dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.hp.customlistview.MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="386dp"
android:layout_height="409dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Ok"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/listView" />
</android.support.constraint.ConstraintLayout>
To resolve this, i tried the following
1)android:layout_height="match_parent" to a specific height of 120 dp.
(It is in customlayout.xml)
But it did not work
2)Used some other layout, such as Absolute and Relative Layout, but image fills up the list content much beyond expected size
EDIT:
I have the list items displayed in a compact manner, as follows,
after suggestions by Ben P. in the comments below.
IMAGE 3
But the description is not appearing,although clearly i have set it programatically in MainActivity.java
I observed that setting android:layout_height to some custom value makes the description text view disappear in the preview.
If someone is downvoting,please give the reason for doing so. This will help me improve the quality of my questions.
Ok, I figured it out. Posting it for anyone who may have similar doubts in future.
Set layout height as 120dp or some finite value even before dragging and dropping items.
Now start drag and drop operation on this reduced layout.
Pretty simple i guess. This helped me resolve it.
Here it is:)
Image 5
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
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");
}
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);
}
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>