Translate To Your Own Langauge
ImageView & ImageButton
Today I will show you how to
place ImageView and ImageButton into your project as well as how to perform a
task when someone click on ImageButton or IamgeView. It is similar to the
simple Button provided in Android. You can also see the implementation of
Button by clicking here. See the following output which will be implemented
in this tutorial.
Steps for Implementation
Step 1: Drag and drop the ImageView and ImageButton control
from the pallete provided on left side of your Eclipse IDE. Note that this will
be visible only when you have opened an xml file. If you don’t know how to drag
and drop then just copy the following code into your .xml file.
<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"
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=".MainActivity"
>
<ImageView
android:id="@+id/imageViewAT4U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="@drawable/at4u"
/>
<ImageButton
android:id="@+id/imageButtonAT4U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageViewAT4U"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
android:src="@drawable/at4u"
/>
</RelativeLayout>
|
Step 2:
Now make the following changes into your .java file so that functionality can be added
to both ImageView and ImageButton.
package com.example.iamgeviewimahebutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
// for imageView
ImageView iv;
// for
imageButton
ImageButton ib;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
Initialization
iv
= (ImageView) findViewById(R.id.imageViewAT4U);
ib
= (ImageButton) findViewById(R.id.imageButtonAT4U);
//
clcik event on imageButton
ib.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View arg0) {
//
TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"ImageButton
is pressed", 0).show();
}
});
//
clcik event on imageView
iv.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View arg0) {
//
TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"ImageView is pressed",
0).show();
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
//
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,
menu);
return
true;
}
}
|
Your App is ready. Now you can run it on your Virtual
device. To make a new virtual device, click here
Subscribe to:
Post Comments (Atom)
You may be interested in:
- Registration Form in Android with Validation
- Login with Validation (Android Example)
- Implementing Tab Example in android using TabActivity
- Splash screen in Android using Thread
- Showing Map
- button in android example
- Spinner Android Example
- Change the Profile Mode Programmatically (Android Example)
- Login Example (Android)
- Generating Google APIs
1 Responses to “ImageView & ImageButton”
28 September 2022 at 03:10
Share to mrecw.imransk@gmail.com also send please the registratio form as it's not being downloade.
Post a Comment
Comment your feedback for the post. You can also ask your queries regarding android.