Two buttons with same id and same behavior in android - android

I want to navigate two buttons to the same class. In one XML, I have two buttons, both should work the same way. How can I achieve this?

Define two button with different id and point to same function in onClick event
<Button
android:id="#+id/button1"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
And then implement your logic here:
public void executeSameBehavior(View view) {
// Your logic
}

You can simply assign one clickListener interface to both setOnClickListeners
Button a = findViewById(R.id.a);
Button b = findViewById(R.id.b);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Something for both
}
};
a.setOnClickListener(listener);
b.setOnClickListener(listener);

Related

How to Create Radio button functionality using <Button> Tag in Android

I need to create this type of UI.
But it should work similar to Radio Button that is when one button got selected other two should be deselected.
There are various approaches like using three Images for each button and also images for pressed and unpressed state.
But how to achieve this using Button Tag.
Perhaps here may be useful to library work
https://github.com/pucamafra/android-segmentedtab
You can create 3 buttons and control other buttons with OnClickListener. For example:
layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
class:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(true);
btn2.setSelected(false);
btn3.setSelected(false);
//Do something
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(true);
btn3.setSelected(false);
//Do something
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(false);
btn3.setSelected(true);
//Do something
}
});
Sure, here .. keep it in selected state in OnClickListener when event x is fired. when event y is fired, keep button2 in selected state *& don't forget to remove button1 from selected state, Just try to improve this, it will work
I just made this , you are welcome to use & commit changes too!

Android Studio Anonymous class even listener fails

I have set an event listener for two buttons and event listener for a EditText control in my java code:
public void doNewButtonClick( View view ) {
View.OnClickListener onSnap = new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView calculatorTextView = (TextView) findViewById(R.id.textView);
calculatorTextView.setText( "GO TO SLEEP" );
Log.d(TAG, "In the Handler for NEW BUTTONS");
}
};
Button button1 = (Button) findViewById(R.id.buttonA);
Button button2 = (Button) findViewById(R.id.buttonB);
button1.setOnClickListener( onSnap );
button2.setOnClickListener( onSnap );
}
public void onEditTextInput( View view ){
EditText myMessage = (EditText) findViewById(R.id.userText);
myMessage.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionID, KeyEvent event) {
Log.d(TAG, "BEFORE THE SEND");
EditText userMessage = (EditText) textView;
TextView phone = (TextView)findViewById(R.id.textView2);
if( actionID == EditorInfo.IME_ACTION_GO){
Log.d(TAG, "IN THE SEND");
phone.setText( userMessage.getText() );
}
return false;
}
});
}
This is my xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonA"
android:layout_below="#+id/button6"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:layout_marginTop="119dp"
android:drawableLeft="#drawable/abc_ic_star_black_16dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonB"
android:layout_alignBottom="#+id/buttonA"
android:layout_alignRight="#+id/button6"
android:layout_alignEnd="#+id/button6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/userText"
android:layout_above="#+id/buttonB"
android:layout_alignLeft="#+id/buttonA"
android:layout_alignStart="#+id/buttonA"
android:imeOptions="actionGo"
android:inputType="text"
android:layout_alignRight="#+id/buttonB"
android:layout_alignEnd="#+id/buttonB" />
I have two issues:
1) The event handlers don't work unless I also go into the control's properties and set the OnClick property to the desired method. In other words, I'll click a control many times and nothing will happen unless I set the OnClick property to a function, then the controls and their event handlers work as expected.
2) Setting the OnClick property of each control to the desired event handler( callback function) forces me to have to click the control twice for it to work. This happens since I am setting the OnClick property and also defining the OnClick function in the event handler.
Why are my event handlers not responding? Why do I have to set the OnClick property? Am I forced to set the OnClick property and not use the Anonymous class method to make event handlers?
Thank you!!
You can simple use onClick property on button as shown below
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonB"
android:onClick="doNewButtonClick" //<--- Like this
android:layout_alignBottom="#+id/buttonA"
android:layout_alignRight="#+id/button6"
android:layout_alignEnd="#+id/button6" />
And your method will be
public void doNewButtonClick(View v){
// you can handle different views based on the
//'id' you get v.getId() and handle different functionalities for each button.
}
Or simply
Button btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do anything
}
});
You can use either one of them.

Is it possible to add method invoked by clicking on a button in Android (not adding listener)

I wanted to invoke method when the button is clicked. I have found this solution
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
});
but this solution is horrible.
Is it possible to add method invocation in XML when the:
<Button
android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/actionButtonUnclicked"
/>
is clicked? In similar manner that it can be done in Windows Phone's and WPF's XAML? Something like android:onClick=clickMethod().
You can add method in xml using android:onClick="clickMethod"
In xml:
<Button
android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/actionButtonUnclicked"
android:onClick="clickMethod"
/>
and in activity/ fragment add
your method should receive View v
public void clickMethod(View v){
// do smth
}
PS: If you want to use same method for multiple buttons
public void clickMethod(View v){
// check for id
if(v.getId() == R.id.button1){
//operation for button 1 click
} else if(v.getId() == R.id.button2){ //operation for button 1 click
}
}
Here:
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myButtonClick" />
<!-- even more layout elements -->
and in your class:
public void myButtonClick(View v) {
// does something very interesting
}

screen to screen command

Hi am new to android and have spent over 10 hours looking for this answer but i cant seem to find and understand it. I am at the main xml page. I am trying to create a button that goes to another page. I need the easiest and simplest way to do this. Can anyody please help me? Heres my code for my main xml.
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
Go to your activity Initialize your button
Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your action
Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
startActivity(newActivityIntent);
}
});
You can also do it different...
You can instantiate a Button in your Activity:
private Button button;
Inside the onCreate(Bundle bundle) method you find your button, as defined at this Activity XML, which is the one you used setContentView(R.layout.yourxml);:
button = (Button) findViewById(R.id.button);
Then you use an OnClickListener:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Inside the onClick method, you instantiate an Intent
Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);
CurrentActivity = the one you are, and ActivityToGo the one you want to load.
startActivity();
Read the basics on Android here.
public class MyActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
// Use a switch on v.getId() if you have multiple clickable views.
// Since there's just one button, ...
Intent intent = new Intent(this, TargetActivity.class;
startActivity(intent);
}
}
You have to learn How to switch between Activities/Screens in Android, you can find here a well explained tutorial for beginners
This process is documented on the website concerning buttons. Google search Android Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button"
android:clickable="true"
android:onClick"insertMethodNameHere"
/>
This will call the method you define in the onClick tag, then start a new activity or update the view. Whatever you need to do

Add dynamically input text and buttons in activity android

I have an application with an input text where the users have to insert an information and a button "+" beside to input text.
I would like to make my form dynamic in a way that when a user pushes on "+" button appears dynamically another text input and another "+" button beside this one, the process is repeated in the same way.
I created and xml file, sample_content:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/attempt"
android:layout_alignParentBottom="true"
android:text="TextView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="36dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:text="+" />
<EditText
android:layout_width="229dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/addKey"
android:background="#drawable/inputtext_corner"
android:ems="10"
android:textSize="18sp" >
<requestFocus />
</EditText>
</RelativeLayout>
and in my Activity, AddDeviceActivity I put:
inflater = LayoutInflater.from(AddDeviceActivity.this);
Button addKey = (Button) findViewById(R.id.addKey);
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});
But this solution doesn't work because when I add the first input text and the first button, I don't know how to make the second button work in my AddDeviceActivity dynamicly
Just wondering whether you can do this:
Have your activity implement OnClickListener and add this method to your activity:
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
canvas.addView(childView);
((Button)childView.findViewById(R.id.addKey)).setOnClickListener(AddDeviceActivity.this);
}
And then change your initial code to use
addKey.setOnClickListener(this);
instead of an anonymous inner class.
I haven't tested this, but don't see why it wouldn't work.
check out this, pass null instead of canvas object in inflate() method
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, null, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});

Categories

Resources