0% found this document useful (0 votes)
25 views2 pages

Exercise Que 2: Activity - Main - XML

The document describes code for an Android application that changes the image displayed in an ImageView when a button is clicked. The XML layout contains an ImageView and Button. The Java code finds the views, declares an array of images, and uses a click listener on the button to change the image, cycling through the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Exercise Que 2: Activity - Main - XML

The document describes code for an Android application that changes the image displayed in an ImageView when a button is clicked. The XML layout contains an ImageView and Button. The Java code finds the views, declares an array of images, and uses a click listener on the button to change the image, cycling through the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise Que 2:

iv=(ImageView)
Activity_main.xml findViewById(R.id.img1);
b1=(Button)
<?xml version="1.0" encoding="utf-8"?> findViewById(R.id.button);
<RelativeLayout
xmlns:android="http://schemas.android.co flag=true;
m/apk/res/android"
b1.setOnClickListener(new
xmlns:tools="http://schemas.android.com/ View.OnClickListener() {
tools" @Override
android:id="@+id/activity_main" public void onClick(View v)
android:layout_width="match_parent" {
android:layout_height="match_parent"
tools:context="MainActivity"> iv.setImageResource(images[i]);
i++;
<ImageView if(i==3)
android:layout_width="200dp" i=0;
android:layout_height="500dp" }
});
android:layout_centerHorizontal="true" }
android:id="@+id/img1"/> }

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_below="@+id/img1"

android:layout_centerHorizontal="true"
android:text="Change Image"
android:id="@+id/button"/>
</RelativeLayout>

MainActivity.java

package com.example.pr_14_2;

import
androidx.appcompat.app.AppCompatActivity
;

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

public class MainActivity extends


AppCompatActivity {
Button b1;
ImageView iv;
boolean flag;
int
images[]={R.drawable.s6,R.drawable.s6,R.
drawable.s4};
int i=0;

@Override
protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Output:

You might also like