You are on page 1of 3

T.Y.B.Sc(I.T) Advanced Mobile Programming (Practical No.

2) Date : 30/03/19

Practical No.2 : Programming Resources

Aim : Android Resources: (Color, Theme, String, Drawable, Dimension, Image),

Content_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayoutxmlns: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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android"/>
<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image"/>
</LinearLayout>

MainActivity.java:

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivityextends AppCompatActivity {


Button button;
ImageViewimage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

1
T.Y.B.Sc(I.T) Advanced Mobile Programming (Practical No.2) Date : 30/03/19

addListenerOnButton();
}

public void addListenerOnButton() {

image=(ImageView)findViewById(R.id.imageview1);
button=(Button)findViewById(R.id.btnChangeImage);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.untitled);
}
}
);

}
}

Output:

2
T.Y.B.Sc(I.T) Advanced Mobile Programming (Practical No.2) Date : 30/03/19

You might also like