ImageButton Click Action Android - android

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);
}
});

Related

OnClickListener for Dynamically created buttons

I'm new to android Development and I hope you can help me.I created Buttons Dynamically ( Based on the contents of my Database). I also made onclicklistener for those buttons. The problem now is, If I click the buttons, Nothing happens. There is also no error shown in logcat. Why do you think this happened? Any response will be appreciated.
Here is my code on creating buttons:
final LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
cursorCol = scoresDataBaseAdapter.queueCrit(mRowId);
for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
int Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("_id")));
Log.i("_id","_id : "+Id);
String CriteriaButton = cursorCol.getString(cursorCol.getColumnIndex("Criteria"));
Log.i("CriteriaButton","CriteriaButton : " + CriteriaButton);
Button btn = new Button(this);
btn.setText(" " + CriteriaButton + " ");
btn.setId(Id);
btn.setTextColor(Color.parseColor("#ffffff"));
btn.setTextSize(12);
btn.setPadding(10, 10, 10, 10);
btnlayout.addView(btn,params);
btn.setOnClickListener(getOnClickDoSomething(btn));}
Now after my OnCreate, I have the following method to set the onclicklistener
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
String criteria = button.getText().toString();
if ("Exams".equals(criteria)){
Toast.makeText(getApplicationContext(),"Exams Selected",2).show(); }
else if ("Quizzes".equals(criteria)){
Toast.makeText(getApplicationContext(),"Quizzes Selected",2).show(); }
}
};
}
Change
String criteria = button.getText().toString();
to
String criteria = button.getText().toString().trim();
Inside onClick method use View parameter of onClick method to get Text from pressed button as:
public void onClick(View v) {
Button button = (Button)v;
String selectedText = button.getText().toString();
....your code here
}

Random Button enabling in Android

I need to enable a button using a random number. The names of the buttons are button1, button2, button3. Here's my code:
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
b.setBackgroundColor(R.color.redwue);
setNextButton(b);
}
});
public void setNextButton(Button str){
System.out.println("&&&&&&&&&&& SETNEXTBUTTON");
str.setEnabled(false);
int zufall = (int) (Math.random()*2);
int buttonid = str.getId();
int buttonname = (int) str.getId();
System.out.println("&&&&&&&&&&&" + getResources().getResourceEntryName(buttonid));
Button bnew = new Button(this);
bnew.setTag(buttonname);
System.out.println("&&&&&&&&&&&" + getResources().getResourceEntryName(bnew.getId()));
bnew.setEnabled(true);
Now with this solution, I get an Unable to find resource id error. I know why that is but I can't find a solution for how to randomly enable a different button?
When a Button is created by Button bnew = new Button(this); no id is assigned to that Button. You must do it by yourself:
bnew.setId(yourId);
Create an array that save the id for all buttons you want to work. Match the randon number with the position on array to get the id for that button.
int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3};
Button bnew = (Button) findViewById(buttonIds[zufall]);
bnew.setEnabled(true);
You must ensure zufall is in [0, buttonIds.length - 1] range

How to change specify previous clicked image button view when the 2nd click button is occur

I have doubt in when I click the 2nd time image button, I want to change the both button image view in simultaneously.
Example:
1. first time press the button 1 and change already the first time image view. (get work)
2. second time press the other button 2 and I want change image view for the both button 1 and 2 in simultaneously. But I can only get the ibutton as variable signal to change the button 2 image view and button 1 can't get.
Question:
1. How do I change the button image view for button 1 when I click the button 2?
2. How do I can keep the button variable in array?
My code like this:
public class CheckersTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final ImageView iv_new_game = (ImageView) findViewById(R.id.new_game);
iv_new_game.setOnClickListener(welcome_listener);
}
OnClickListener welcome_listener = new View.OnClickListener() {
public void onClick(View v) {
final ImageView iv = (ImageView) v;
if (iv.getId() == R.id.new_game) {
setContentView(R.layout.checkers_board);
final ImageButton b2 = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton b4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton b6 = (ImageButton) findViewById(R.id.imageButton6);
// set the OnClickListeners.
b2.setOnClickListener(button_listener);
b4.setOnClickListener(button_listener);
b6.setOnClickListener(button_listener);
// Re-enable the Click-able property of buttons.
b2.setClickable(true);
b4.setClickable(true);
b6.setClickable(true);
}
};
};
OnClickListener button_listener = new View.OnClickListener() {
public void onClick(View v) {
ImageButton ibutton = (ImageButton) v;
ibutton.setImageResource(R.drawable.green_bol);
}
};
In the second Button's onclick method get instance of first button and set image.

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());
}
});

android get Colour

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

Categories

Resources