How To Obtain And Pass The Value Of A Button
Solution 1:
Your class should implement OnClickListener and override onClick()
Buttonbutton1= (Button) findViewById(R.id.button1);
Buttonbutton2= (Button) findViewById(R.id.button2);
Buttonbutton3= (Button) findViewById(R.id.button3);
button1.setOnClickListener(newOnClickListener(this));
button2.setOnClickListener(newOnClickListener(this));
button3.setOnClickListener(newOnClickListener(this));
@OverridepublicvoidonClick(View v){
switch(v.getId()) //get the id which is an int
{
case R.id.button1 : //if its button1 that is clicked
Intent i= newIntent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
// use intents to pass information to secondActivity and display the image therebreak;
case R.id.button2 :
Intent i= newIntent("com.example.secondActivity");
startActivity(i)
//use intents to pass information to secondActivity and display the image therebreak;
case R.id.button3 :
Intent i= newIntent("com.example.secondActivityy");
startActivity(i)
//use intents to pass information to secondActivity and display the image therebreak;
}
}
To pass values using intents
On Button click
Intent i= newIntent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
In Second Activity retrieve it as below
Bundleextras= getIntent().getExtras();
if(extras!=null)
{
int values= extras.getInt("key");
}
Solution 2:
Why not use a switch case
to determine which button was clicked by the user and show the corresponding / relevant image in the next activity? For example:
First, make your Activity implement the OnClickListener
. Then, in the onCreate()
cast your Buttons
and set their setOnClickListener
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
....
ButtonImage1= (Button) findViewById(R.id.Image1);
Image1.setOnClickListener(this);
.... // THE REST OF THE BUTTONS
}
I am assuming you are passing a Bundle
in the Intent for starting the next Activity. Change that code to pass information that contains which button was pressed.
For example: Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class); showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1"); startActivity(showPhoto);
@Override
publicvoid onClick(View v) {
// Perform action on clickswitch(v.getId()) {
case R.id.Image1:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1");
startActivity(showPhoto);
break;
case R.id.Image2:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE2");
startActivity(showPhoto);
break;
case R.id.Image3:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE3");
startActivity(showPhoto);
break;
}
}
}
Solution 3:
I have 3 buttons Image1, Image2 & Image3 on my MainActivity. Now based on what button the user clicks (Image1, Image2 & Image3), a corresponding image is displayed on a new activity.
=> As you already said yo know how to create buttons and initiate it.
Now you just need to assign OnClickListener
to every buttons and then pass Image id or URL into the Intent by which you are calling new activity.
Check the code posted by @IceMAN above, now as I mentioned above, put ImageURL or Image ID into Intent by using putExtra() method.
For example:
Intent intent= newIntent(CurrentClass.this, ImageActivity.class);
intent.putExtra("ImageURL",strImageURL);
startActivity(i);
Solution 4:
For example you have 3 Button
s and ImageView
in Main
layout:
Main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/btn1"
android:text="button 1"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/btn2"
android:text="button 2"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/btn3"
android:text="button 3"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv"
/>
I your activity metod onClick handle events from three buttons. And we need to recognize that the button has been pressed.
MyActivity.java
publicclassMyActivityextendsActivityimplementsView.OnClickListener {
Button btn1;
Button btn2;
Button btn3;
ImageView iv;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn1);
btn3 = (Button) findViewById(R.id.btn1);
iv = (ImageView) findViewById(R.id.iv);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v) {
switch (v.getId()){
case R.id.btn1:
{
Toasttoast= Toast.makeText(this, "onClickButton1", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_1);
break;
}
case R.id.btn2:
{
Toasttoast= Toast.makeText(this, "onClickButton2", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_2);
break;
}
case R.id.btn3:
{
Toasttoast= Toast.makeText(this, "onClickButton3", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_3);
break;
}
}
}
}
Where my_image_1
, my_image_2
, my_image_3
- images, from your Drawable
folder.
Hope its Help.
Solution 5:
Add this in onCreate of your activity that contains the 3 buttons
Buttonimage1= (Button) findViewById(R.id.image1);
image1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
startActivity(newIntent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image1"));
}
});
Buttonimage2= (Button) findViewById(R.id.image2);
image2.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
startActivity(newIntent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image2"));
}
});
Buttonimage3= (Button) findViewById(R.id.image3);
image3.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
startActivity(newIntent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image3"));
}
});
Add this in onCreate of your activity where you will set the image:
String imageName = getIntent().getStringExtra("ImageName");
if(imageName.equals("Image1")) {
//set image 1
}
elseif(imageName.equals("Image2")) {
//set image 2
}
elseif(imageName.equals("Image3")) {
//set image 3
}
Post a Comment for "How To Obtain And Pass The Value Of A Button"