Passing parameter from a button to android:onClick method - android

Hi I have something like this (3 buttons) in my activity xml pointing to same method:
<Button
android:id="#+id/Button_1"
android:onClick="printNo"
android:text="#string/Button_1" />
<Button
android:id="#+id/Button_2"
android:onClick="printNo"
android:text="#string/Button_2" />
<Button
android:id="#+id/Button_3"
android:onClick="printNo"
android:text="#string/Button_3" />
Is there any way I could determine which button was pressed while in the printNo method ?

public void printNo( View v ) {
switch (v.getId()) {
case (R.id.Button_1):
//stuff
break;
case (R.id.Button_2):
//stuff
break;
case (R.id.Button_3):
//stuff
break;
}

As #user1106018 said - you can use tag in xml like that:
<Button android:onClick="f" android:tag="0"/>
Then it is really simple to get this tag in this way:
public void f(View v) {
String value = v.getTag();
}

Simply switch over the ID:
public void printNo(View v){
switch (v.getId()){
case R.id.Button_1:
break;
case R.id.Button_2:
break;
case R.id.Button_3:
break;
}

Working in my end
public void printNo(View v) {
switch (v.getId()) {
case R.id.Button_1:
break;
case R.id.Button_2:
break;
case R.id.Button_3:
break;
}

In xml add tag, np with name of button.
public void printNo(View V){
view.getTag();
// now you can recognize view with getTag()
}
Other answers seems also good;)

Related

How to detect Circle Menu's animation has finished in android?

Im using CircleMenu
Layout
<com.hitomi.cmlibrary.CircleMenu
android:id="#+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Activity
circleMenu = (CircleMenu) findViewById(R.id.circle_menu);
circleMenu.setMainMenu(Color.parseColor("#CDCDCD"), R.mipmap.icon_menu, R.mipmap.icon_cancel)
.addSubMenu(Color.parseColor("#258CFF"), R.mipmap.icon_home)
.addSubMenu(Color.parseColor("#30A400"), R.mipmap.icon_search)
.addSubMenu(Color.parseColor("#FF4B32"), R.mipmap.icon_notify)
.addSubMenu(Color.parseColor("#8A39FF"), R.mipmap.icon_setting)
.addSubMenu(Color.parseColor("#FF6A00"), R.mipmap.icon_gps)
.setOnMenuSelectedListener(new OnMenuSelectedListener() {
#Override
public void onMenuSelected(int index) {
switch (i){
case 0:
((SomeScreenSlidePagerActivity)getActivity()).refresh();
break;
case 1:
((SomeScreenSlidePagerActivity)getActivity()).logout();
break;
case 2:
((SomeScreenSlidePagerActivity)getActivity()).showNotifications();
break;
default:
break;
}
}
});
When i press on a subMenu button animation starts but it finishes, OnMenuSelected option executes so it looks very bad. i want animation to finish then start executing OnMenuSelected option. How to do it ?
You can use addListener() of Animation of the class CircleMenu.java

Android - Unexpected layout

i haven't many experience in android development. this is my code:
public void onClick(View v) {
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
}
myAvatar is an ImageView.
when i click in one of the four imageButton , the result is always the same: Case R.id.categoriaA4.
Why??
You need to add break to each case. like
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
break;
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
break;
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
break;
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
break;
}

onClick event buttons

I have 5 buttons,each of them open another activity. How to make clickeble only one of them at current moment?When i click 2 different buttons simultaneusly onClick event calls 2 times and 2 different activities opens. How can i resolve my problem. Thanks.My code
public void onClick(View v)
{
switch (v.getId())
{
case R.id.play_button:
onPlayClick();
setButtonsEnable(false);
break;
case R.id.difficalty_button:
onDifficultyClick();
setButtonsEnable(false);
break;
case R.id.hight_scores_button:
onHighScoresClick();
setButtonsEnable(false);
break;
case R.id.share_button:
setButtonsEnable(false);
break;
case R.id.turn_off_button:
onLeaderboardClick();
setButtonsEnable(false);
break;
default:
break;
}
}
After return to main activity i make my buttons clickable
protected void onResume() {
super.onResume();
setButtonsEnable(true);
}
public void setButtonsEnable(boolean config)
{
playBtn.setClickable(config);
difficultyBtn.setClickable(config);
hScoreBtn.setClickable(config);
shareBtn.setClickable(config);
turnOffButton.setClickable(config);
}
setOnClickListener(null)
would do the trick or you can make
setClickable(false)
Maybe what you really need is RadioGroup? It allows out of the box to select click/select only one button at the time.
Doc reference
Tutorial
Create a variable clicked in Activity:
boolean clicked=false;
Set this value in onClick code:
public void onClick(View v)
{
if(!clicked){
clicked=true;
switch (v.getId())
{
case R.id.play_button:
onPlayClick();
setButtonsEnable(false);
break;
case R.id.difficalty_button:
onDifficultyClick();
setButtonsEnable(false);
break;
case R.id.hight_scores_button:
onHighScoresClick();
setButtonsEnable(false);
break;
case R.id.share_button:
setButtonsEnable(false);
break;
case R.id.turn_off_button:
onLeaderboardClick();
setButtonsEnable(false);
break;
default:
break;
}}
}
If you want to open an activity on each click, use a onclicklistener for each button or add a method call in the xml layout via android:onClick="someMethod", when you click a button and call another activity the UI should block itself...have you tried it this way?

Switch Statement with Button pressed

So I push either button1, button2, button3, or button4. Pushing any of these Buttons calls method buttonPressed(View v). I want to use a different case depending on the button pressed. For example:
public void buttonPressed(View v){
switch(v){
case button1:
//do something
case button2:
//do something
case button3:
//do something
case button4:
//do something
}}
Obviously, this does not work. I cannot use a switch statement with a View. What is the easiest and most efficient solution to this problem? Thanks!!
Note: I am using android:onClick="buttonPressed" in my .xml
You need to switch on the View's ID, not the View itself
public void buttonPressed(View v){
switch(v.getId()) {
case R.id.ButtonOneID:
//do something
break;
case R.id.ButtonTwoID:
//do something
break;
case R.id.ButtonThreeID:
//do something
break;
case R.id.ButtonFourID:
//do something
break;
}
}
public void buttonPressed(View v){
int id = v.getId();
switch(id){
case button1.getId();
//do something
break;
case button2.getId();
//do something
break;
case button3.getId();
//do something
break;
case button4.getId();
//do something
break;
}
}

Use onOptionsItemSelected with a button in layout

I trying to personalize the Android tutorial about Layout Changes (http://developer.android.com/training/animation/layout.html), but in the code there is this code
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Navigate "up" the demo structure to the launchpad activity.
// See http://developer.android.com/design/patterns/navigation.html
// for more.
NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
return true;
case R.id.action_add_item:
// Hide the "empty" view since there is now at least one item in the
// list.
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
return true;
}
return super.onOptionsItemSelected(item);
}
In my case I don't have a button in the menu, but I woud use an external button. I trying to use this button:
<Button
android:id="#+id/buttonPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/editTextComment"
android:text="Post"
android:onClick="add" />
With the method
public void add(View view) {
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
}
But this solution doesn't work. Any ideas?
onOptionsItemSelected is for an options menu used with onCreateOptionsMenu(Menu menu). Simply move this code to your onClick if you want it to work for the Buttons and switch on the Button id.
public void add(View v) {
switch (v.getId()) {
case R.id.buttonPost:
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
break;
case R.id.another_button_id:
// do something else
break;
}
}
Put an OnClickListener on your button instead:
findViewById(R.id.buttonPost).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do something here
}
});

Categories

Resources