Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Switch image on button click. Open next image when button is clicked. Use image switcher in app in android studio
So, first of all design activity or add this code to you activity layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Image Switcher Example" android:textSize="30sp" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <!-- imageSwitcher --> <ImageSwitcher android:layout_below="@+id/textView" android:id="@+id/imageSwitcher" android:layout_width="match_parent" android:layout_height="400dp" android:layout_marginBottom="28dp" android:layout_marginTop="40dp" /> <!-- Button --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/button" android:layout_marginBottom="47dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Then after designing add this code into your .java file
MainActivity.java
package com.usmtip.demoappbyusmtip; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; public class MainActivity extends AppCompatActivity { ImageSwitcher imageSwitcher; Button nextButton; // Add or call your images here int imageSwitcherImages[] = { R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,R.drawable.pic_4, R.drawable.pic_5, }; int switcherImageLength = imageSwitcherImages.length; int counter = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); nextButton = (Button) findViewById(R.id.button); //set ViewSwitcher imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView switcherImageView = new ImageView(getApplicationContext()); switcherImageView.setLayoutParams(new ImageSwitcher.LayoutParams( ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT )); switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); switcherImageView.setImageResource(R.mipmap.ic_launcher ); //switcherImageView.setMaxHeight(100); return switcherImageView; } }); // set animation Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); imageSwitcher.setOutAnimation(aniOut); imageSwitcher.setInAnimation(aniIn); // set onclick listener on next button nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { counter++; if (counter == switcherImageLength){ counter = 0; imageSwitcher.setImageResource(imageSwitcherImages[counter]); } else{ imageSwitcher.setImageResource(imageSwitcherImages[counter]); } } }); } }
At last click on Resource Manager and simply drag and drop your images then click on next and then import. And then click on Project and start your coding.
That's it now its your turn add this code and run your app and enjoy ... !
0 Comments: