Hello i have lots of button with the same OnClickListener, the buttons have different colours, how can i get the colour(or the colour resource) of the pressed button ?
Here is the code i use
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener colorButtonListener = new View.OnClickListener(){
public void onClick (View v){
textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button
dialog.dismiss();
}
};
Button button1 = (Button) dialog.findViewById(R.id.button1);
Button button2 = (Button) dialog.findViewById(R.id.button2);
Button button3 = (Button) dialog.findViewById(R.id.button3);
Button button4 = (Button) dialog.findViewById(R.id.button4);
Button button5 = (Button) dialog.findViewById(R.id.button5);
Button button6 = (Button) dialog.findViewById(R.id.button6);
Button button7 = (Button) dialog.findViewById(R.id.button7);
Button button8 = (Button) dialog.findViewById(R.id.button8);
Button button9 = (Button) dialog.findViewById(R.id.button9);
/*for changing the colour when the user clicks on a button*/
button1.setOnClickListener(colorButtonListener);
button2.setOnClickListener(colorButtonListener);
button3.setOnClickListener(colorButtonListener);
button4.setOnClickListener(colorButtonListener);
button5.setOnClickListener(colorButtonListener);
button6.setOnClickListener(colorButtonListener);
button7.setOnClickListener(colorButtonListener);
button8.setOnClickListener(colorButtonListener);
button9.setOnClickListener(colorButtonListener);
/**for the round corner*/
Resources res = this.getResources();
button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1)));
button2.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2)));
button3.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x3)));
button4.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x1)));
button5.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x2)));
button6.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x3)));
button7.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x1)));
button8.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x2)));
button9.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x3)));
//now that the dialog is set up, it's time to show it
dialog.show();
As far as i know, You can get the color values(for eg: R.color.green) but you can get a drawable object of the button.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Drawable d = v.getBackground()
}
});
I've found this method, but i have never try it :
int color = v.getSolidColor();
TextView txt = (TextView)findViewById(R.id.txtColor);
txt.setText("Color Of Button ");
View.OnClickListener colorButtonListener = new View.OnClickListener(){
public void onClick (View v){
txt.setTextColor(v.getSolidColor());
textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button
dialog.dismiss();
Log.i("Color of Button","Color = "+v.getSolidColor() );
}
};
Note : follow this link : getSolidColor()
in your Code , try to replace the :
button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1)));
with :
button1.setBackgroundRessource(R.color.color1x1);
download this project , i've created 4 buttons, and i get the color of the background ;) , enjoy it
Get Color of Buttons
Hope it helps
Related
I need a way to know which of my buttons was pressed first. The app layout is more or less like this.:
Button1 Button2
Button3 Button4
Button5 Button6
Button7 Button8
Button9 Button10
Button11 Button12
And if one of the buttons of it's "line" is pressed, the other disappears. The thing is, I have no idea how to know which one of all these 12 buttons was pressed first, then pressed second, then pressed third and so on...
The code I have for hiding buttons works well, but that's pretty much the easy part.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
}
});
I searched but maybe I don't know what exactly to search for, and I didn't find a good answer.
You can do it like this:
1) Have a HashMap where you link both of your buttons on the same line to each other. 2) Have an ArrayList of button id's where you can hold the order of presses. 3) Implement a method which will perform the mapping and call it in your Activity's #onCreate method. 4) Set your global listener instance to all of your buttons.
private HashMap<Integer, Integer> buttonMap = new HashMap<>();
private ArrayList<Integer> buttonPressedOrder = new ArrayList<>();
// A global listener instance to be set to all of your buttons
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View selectedButton) {
int selectedButtonId = selectedButton.getId();
//Add pressed button to pressed buttons list
buttonPressedOrder.add(selectedButton.getId());
//Find button to hide and hide it
int hidingButtonId = buttonMap.get(selectedButtonId);
Button hidingButton = findViewById(hidingButtonId);
hidingButton.setVisibility(View.GONE);
}
}
//Put these inside your activity#onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mapButtons();
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
...
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
// A method for mapping your buttons in the same line to each other
private void mapButtons(){
buttonMap.put(R.id.button1, R.id.button2)
buttonMap.put(R.id.button2, R.id.button1)
buttonMap.put(R.id.button3, R.id.button4)
buttonMap.put(R.id.button4, R.id.button3)
...
}
Whenever you need to see in which order the buttons are pressed, use this method
public void getButtonPressedOrder(){
Resources res = getResources();
int numberOfPressedButtons = buttonPressedOrder.size();
for(int i=0; i<numberOfPressedButtons; i++){
Log.i("PressOrder", res.getResourceEntryName(buttonPressedOrder.get(i))
+ " is pressed at " + (i+1) + " order");
}
}
which will log something like:
I/PressOrder: button1 is pressed at 1 order
I/PressOrder: button5 is pressed at 2 order
I/PressOrder: button10 is pressed at 3 order
Hope this helps!
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")
}
});
I've stored the imagebutton info in an xml. It's image path,width,height and so on.
But since there are multiple images I want to store data for each imagebutton while it's clicked. for example, if there are two buttons named male and female, i want to set String value = "male"; if male image button is clicked. But it did not work. Can somebody help me?
public void AddAllImageButtons() throws IOException {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
ImageButton btn = new ImageButton(this);
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn
.getLayoutParams();
absParams.x = x;
absParams.y = y;
btn.setLayoutParams(absParams);
btn.setBackgroundDrawable(null);
btn.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(path)));
if(type.equals("imagebutton")){
if(elementId.equals("female")){
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = elementId;
GenerateAlertBoxes(gender);
}
});
}
Id is the image button id which is created in xml file.
AlertBox text does not show "female" for this example.
you can setTag("value") to set the value in the ImageButton btn and on the onclick event you can use getTag() function to retrieve the value and set it to gender.
use btn.setTag(elementid); whenever you add a btn to the layout.......
inside btn.setOnClickListener you can write v.getTag().toString() to get the value and set it to gender......
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = v.getTag().toString();
GenerateAlertBoxes(gender);
}
});
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.
I have a edit_text field in activity_X.xml. I have a button B in activty_X.xml. On clicking button B i need to assign a value to edit_text field. How to do?
you should try something on your own then post any question if you are getting any problem in that...anyways here's how you can implement this
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.textidinxml);
editText1.setText("Some text to show on edittext");
}
});