This demo is used to demonstrate the use of image view. In one section of demo we change color of imageView with the click of particular color button. In second part we try to change images of imageView with the click of button.
1.imageView:
Imageview class can load images from various sources. It take care of computing its measurement from the image so that it can be used in any layout manager.
2.Button:
Button class is used to represent a push-button widget. We can perform a particular action with the click/touch of the button.
3.Drawable:
A Drawable is a general abstraction for “something thak can be drawn”. Whenever we need to use a image for application, We must keep the image in res/drawable folder.
1.MainController.java
package
com.example.sisoftcolorchanger;
import
android.app.Activity;
import
android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class
MainController extends Activity implements OnClickListener{
Button b_colorChange,b_imagechange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_controller);
b_colorChange =
(Button)findViewById(R.id.colorChanger);
b_imagechange =
(Button)findViewById(R.id.imageChanger);
b_colorChange.setOnClickListener(this);
b_imagechange.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==b_colorChange.getId())
{
Intent intt = new
Intent(MainController.this,SiSoftMainActivity.class);
startActivity(intt);
}
if(v.getId()==b_imagechange.getId())
{
Intent intt = new
Intent(MainController.this,SiSoft_ImageChanger.class);
startActivity(intt);
}
}
}
2.Sisoft_ImageChanger.java
package
com.example.sisoftcolorchanger;
import android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
public class
SiSoft_ImageChanger extends Activity implements OnClickListener{
Button btn_horse,btn_fish,btn_tiger;
ImageView img_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sisoft_imagechanger);
btn_horse =
(Button)findViewById(R.id.b_horse);
btn_fish = (Button)findViewById(R.id.b_fish);
btn_tiger =
(Button)findViewById(R.id.b_tiger);
img_view =
(ImageView)findViewById(R.id.img_view);
btn_horse.setOnClickListener(this);
btn_fish.setOnClickListener(this);
btn_tiger.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==btn_horse.getId())
img_view.setBackgroundResource(R.drawable.horse);
if(v.getId()==btn_fish.getId())
img_view.setBackgroundResource(R.drawable.fish);
if(v.getId()==btn_tiger.getId())
img_view.setBackgroundResource(R.drawable.tiger);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the
action bar if it is present.
getMenuInflater().inflate(R.menu.si_soft_main,
menu);
return true;
}
}
3.SiSoftMainActivity.java
package com.example.sisoftcolorchanger;
import android.os.Bundle;
import
android.app.Activity;
import
android.content.Intent;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;
public class
SiSoftMainActivity extends Activity implements OnClickListener{
Button btn_blue,btn_red,btn_yellow,btn_img_changer;
TextView txt_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_si_soft_main);
btn_blue = (Button)findViewById(R.id.b_blue);
btn_red = (Button)findViewById(R.id.b_red);
btn_yellow =
(Button)findViewById(R.id.b_yellow);
txt_view = (TextView)findViewById(R.id.textView1);
btn_img_changer=(Button)findViewById(R.id.img_change);
btn_blue.setOnClickListener(this);
btn_red.setOnClickListener(this);
btn_yellow.setOnClickListener(this);
btn_img_changer.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==btn_blue.getId())
txt_view.setBackgroundResource(R.color.Blue);
if(v.getId()==btn_red.getId())
txt_view.setBackgroundResource(R.color.red);
if(v.getId()==btn_yellow.getId())
txt_view.setBackgroundResource(R.color.yellow);
if(v.getId()==btn_img_changer.getId())
{
Intent intt = new
Intent(SiSoftMainActivity.this,SiSoft_ImageChanger.class);
startActivity(intt);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the
action bar if it is present.
getMenuInflater().inflate(R.menu.si_soft_main,
menu);
return true;
}
}
1.activity_si_soft_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SiSoftMainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:background="@ /Black"
android:layout_alignParentLeft="true"
android:text="" />
<Button
android:id="@+id/b_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b_red"
android:layout_alignBottom="@+id/b_red"
android:layout_centerHorizontal="true"
android:background="@ /Blue"
android:text="@string/blue" />
<Button
android:id="@+id/b_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/b_blue"
android:layout_alignBottom="@+id/b_blue"
android:layout_alignRight="@+id/textView1"
android:background="@ /yellow"
android:text="@string/yellow" />
<Button
android:id="@+id/b_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:background="@ /red"
android:text="@string/red" />
<Button
android:id="@+id/img_change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/imagechanger"
/>
</RelativeLayout>
2.activity_sisoft_imagechanger.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SiSoftMainActivity"
>
<ImageView
android:id="@+id/img_view"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/b_horse"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignBaseline="@+id/b_fish"
android:layout_alignBottom="@+id/b_fish"
android:layout_centerHorizontal="true"
android:background="@drawable/horse"
/>
<Button
android:id="@+id/b_tiger"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignBaseline="@+id/b_horse"
android:layout_alignBottom="@+id/b_horse"
android:layout_alignRight="@+id/img_view"
android:background="@drawable/tiger"
/>
<Button
android:id="@+id/b_fish"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignLeft="@+id/img_view"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:background="@drawable/fish"
/>
</RelativeLayout>
3.main_controller.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:gravity="center"
android:text="Chose
Your Option"
/>
<Button
android:id="@+id/imageChanger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:text="Image Changer" />
<Button
android:id="@+id/ Changer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageChanger"
android:layout_below="@+id/imageChanger"
android:layout_marginTop="58dp"
android:text=" Changer" />
</RelativeLayout>
About the Contributor...
Manoj Mimanshak
Trainee at Sisoft Technologies Indirapuram Ghaziabad