Android 1 function for 3 buttons - android

I have 1 function to print the text of the button, and i use that function for the 3 button, how do i know in what button the user click? (sorry but my english is low)
Example:
public void function(View view) {
TextView text = (TextView) findViewById(R.id.textView);
Button button1= (Button) findViewById(R.id.button3);
Button button1= (Button) findViewById(R.id.button4);
Button button1= (Button) findViewById(R.id.button5);
text.setText(button <?> .getText()); }
is the number of the button the user click
Its any way to know in which button the user click?
Please help, Thanks.

you can define one click listener and set this listener for all buttons. For example:
private OnClickListener genericListerner = new OnClickListener() {
public void Click(View v) {
switch(v.getId())
{
case R.id.button3:
text.setText(button3.getText().toString());
break;
case R.id.button4:
text.setText(button4.getText().toString());
break;
case R.id.button5:
text.setText(button5.getText().toString());
break;
default:
//do something
}
};
and you set the listeners like the following:
button3.setOnClickListener(genericListener);
button4.setOnClickListener(genericListener);
button5.setOnClickListener(genericListener);
I am not sure whether this is what you are asking, but what this code basically does is that, if you click button 3 it sets the textview's text to button 3's text, if you click button 4 it sets the textview's text to button 4's text and if you click button 5, it sets the textview's text to button 5's text.
Hope this helps.

The view that is passed in is the one that was clicked, therefore it has the id.
view.getId() == R.id.button3
If you still need help, share more of your code.

Well you will require some onclick listeners for the button, you can then set an id or name in the onclick e.g
final Button button1 = (Button) findViewById(R.id.button_id);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.settext("Button1 Clicked")
}
});

Related

changing the button text from the edittext state

I have one button and 1 edittext. The button is set to "change", and EditText is enable-false. When you click on a button, its text is changed to "save", and edit text is available for input. After a second press, the text changes to "change" again, and the button becomes Enable-false.
how to implement it?
Use this onClickListener on your button -
Button yourButton = (Button) view.findViewById(R.id.button)
EditText yourEditText = (EditText) view.findViewById(R.id.edittext)
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!yourEdittext.isEnabled()){
yourButton.setText("Save");
yourEditText.setEnabled(true);
counter++;
}else if (yourEdittext.isEnabled()){
yourButton.setText("Change again");
yourButton.setEnabled(false);
}
}
});
Make sure to set the initial button's text and the edit text disabled for the starting point

If one button press does something, other buttons pressed should do something else

I have 5 Buttons in an activity. My code should work as follows :
1 (Correct) button pressed it should do something.
other 4 pressed, something else should be done...
I don't want to used 5 onclicklistener
if(Button1 press) {
do something
}
else if (button2 or button3 or button4 or button5 press)
{
something else to do
}
Why don't you do it this way:
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.equals(button1)) {
// do something
} else {
// do something else
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
There are several ways to do it but if the same buttons will always do the same thing then you can set the onClick() in your xml.
First, define the same function for each Button
<Button
android:id="#+id/button1"
....
android:onClick="functionName"/>
<Button
android:id="#+id/button2"
....
android:onClick="functionName"/>
then in your code
public void functionName(View v)
{
switch (v.getId()) // v is the button that was clicked
{
case (R.id.button1): // this is the oddball
...do stuff
break;
default: // this will run the same code for any button clicked that doesn't have id of button1 defined in xml
...do other stuff
break;
}
}
now your Buttons or onClickListeners don't have to be defined in your code unless you need to do something else with a Button
Edit
#prosperK has pointed out that with the newer ADT passing int to the switch causes errors so you may need an if/else if this is the case. link to SO post about this
You can define two different click listeners. Button 1 gets first listener and others get the second one. Hope this helps.

Dynamically populate a button value android

I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}

Button click to change background image of clicked button

I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}

How to populate the EditText with the button click?

I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).
I am now using:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
String text=(String) b1.getText();
pincode.setText(text);
}
});
with the above code I am getting the values populated, but I have a set of buttons, so is there any simplified way to get the values populated?
*The values currently replacing the previous one , I need multiple values in the edit text like if we press buttons 1,2,3 (values) the values in the edit text should be 123 *
To do this, in your activity implements OnClickListener
then add this listener to all your button as
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
and write onClick event as
public void onClick(View v) {
Button b1 = (Button)v;
editText.setText(editText.getText().toString()+b1.getText().toString());
}
and you are done :)
on your button onClick you got to do this
button1.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"1");
});
button2.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"2");
});
this way your buttons will work
onClick(View v)
{
editText.setText(editText.getText() + v.getText());
//plz adjust casting and spanned as per requirement
}
You can add text like this:
String a = "random text";
input = your_edit_text_box;
input.setText(a);
Link: http://developer.android.com/reference/android/widget/EditText.html#setText%28java.lang.CharSequence,%20android.widget.TextView.BufferType%29
Button btnNO = (Button) findViewById(R.id.btn);
EditTExt edtText = (EditText) findViewById(R.id.editText);
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text= btnNo.getText();
edtText.setText(text);
// or direct use like, edtText.setText(btnNo.getText());
}
});

Categories

Resources