First of all create New Project and give the Name And MainACtivity name all that.
Now open the MainActivity.
MainActivity
- public class MainActivity extends AppCompatActivity {
- ImageView img;
- Button b;
- int flag = 0;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- img = (ImageView) findViewById(R.id.imageView1);
- b = (Button) findViewById(R.id.button1);
-
- b.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- if (flag == 0) {
- img.setImageResource(R.drawable.home1);
- flag = 1;
- } else if (flag == 1) {
- img.setImageResource(R.drawable.home2);
- flag = 2;
- } else if (flag == 2) {
- img.setImageResource(R.drawable.home3);
- flag = 0;
- }
- }
- });
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
-
- if (id == R.id.action_settings) {
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
- }
Now Open Your activity_main.xml file.
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <TextView
- android:id="@+id/textView1"
- android:textSize="20dp"
- android:textColor="#FF0000"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="110dp"
- android:text="Static ImageView" />
-
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:src="@drawable/home1" />
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="80dp"
- android:text="Change" />
-
- </RelativeLayout>
In your Drawable Folder add below name Image. The Image are contain in Rar file.
home1.jpg
home2.jpg
home3.jpg
Or you can use your image by giving this Name to Image.
Now Run the Application the following Output is Generated.
Click on Button the following output is generated
And then,
The Image is in the Drawable file