Setting background image makes the button unclickable - android

I have created an array of buttons. When I set the background image to each button it becomes unlockable (disabled). Why this happen? Please, anyone, suggest it to me.
My code:
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[6][7];
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT,1);
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(int j=0;j<7;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText("1");
buttons[i][j].setBackgroundResource(R.drawable.but_blue_clicked);
rowLayout.addView(buttons[i][j],param);
buttons[i][j].setClickable(true);
}
}
}

There is no code adding listener to button. This is probably the problem.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});

Please look below code it helpful to you
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
Button[][] buttons = new Button[6][7];
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
for (int i = 0; i < 6; i++) {
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for (int j = 0; j < 7; j++) {
buttons[i][j] = new Button(this);
buttons[i][j].setText("1");
buttons[i][j].setBackgroundResource(R.drawable.btn_yes);
rowLayout.addView(buttons[i][j], param);
buttons[i][j].setClickable(true);
buttons[i][j].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "Click Button", 5000).show();
}
});
}
}

very silly mistake.. you forget to add
btnNAme.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
//some code
}
});

this is good practice to make the Listener for array of buttons
for (int ImgBtnID = 0; ImgBtnID <=8; ImgBtnID++)
{
ImgBtnArray[ImgBtnID].setOnClickListener(myListener);
}
OnClickListener myListener = new View.OnClickListener() {
// #Override
public void onClick(View v) {//your code
}
}

Related

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

How to set OnClick on Programmatically TableRow?

I have programmatically TableRow. Then I loop it like this:
int countData = dbData.readRows();
for(int i = 0;i<countData;i++) {
TableRow tr = new TableRow(getActivity());
TextView tvMessage = new TextView(getActivity());
tvMessage.setText("Click Here");
tr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Clicked", Toast.LENGTH_SHORT).show();
}
});
tr.addView(tvMessage);
}
Any help is appreciated. Thanks.

Get the value of the checkboxes after clicking the transmit button

I have checkbox list and I am trying to get the result after the user selected multiple chexkboxes and clicked the transmit button
how can I get the multiple checkboxes values after clicking the trasmit button?
I appreciate any help.
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(items.get(i) + "");
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
}
Edit 1:
private void createRadioButton(final ArrayList items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(items.get(i) + "");
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
//here I am getting this error: OnClickListener cannot be resolved to a type
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i: items){
//here I am getting `cb cannot be resolved`
if(cb.getId(i).isChecked()){
}
}
}
});
}

Onclicklistener programmatically created Imagebutton

I create different Imagebuttons dynamically like this:
for (int i=0 ; i<articlesArray.size() ; i++){
table=(TableLayout)findViewById(id.showOuvragesTableLayoutCreateDevis);
LinearLayout layout = new LinearLayout(ShowCreateOuvrages.this);
layout.setOrientation(LinearLayout.VERTICAL);
BitmapDrawable drawableDelete = (BitmapDrawable)getApplicationContext().getResources().getDrawable(R.drawable.deleterows);
Bitmap bitmapDelete = drawableDelete.getBitmap();
bitmapDelete = Bitmap.createScaledBitmap(bitmapDelete, 16, 16, true);
tR = new TableRow(ShowCreateOuvrages.this);
TextView TextViewTVA = new TextView(ShowCreateOuvrages.this);
ImageButton ImgButtonDelete = new ImageButton(ShowCreateOuvrages.this);
Articles articles = articlesArray.get(i);
TextViewTVA.setText("Remise.toString()");
TextViewTVA.setBackgroundResource(R.drawable.cellborder);
TextViewTVA.setPadding(5, 5, 5, 5);
ImgButtonDelete.setBackgroundColor(80000000);
ImgButtonDelete.setImageBitmap(bitmapDelete);
ImgButtonDelete.setOnClickListener(mListener);
tR.addView(ImgButtonDelete);
tR.addView(TextViewTVA);
table.addView(tR);
}
and the Class Listener like:
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
View myView = findViewById(R.id.showOuvragesTableLayoutCreateDevis);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
}
};
But it doesn't work. It seems that the onClickListener worked only at one ImageButtons has .
How can I get my click listeners to work?
ImgButtonDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" Click me",
Toast.LENGTH_LONG).show();
}
});
table=(TableLayout)findViewById(R.id.showOuvragesTableLayoutCreateDevis);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
BitmapDrawable drawableDelete = (BitmapDrawable)getApplicationContext().getResources().getDrawable(R.drawable.deleterows);
Bitmap bitmapDelete = drawableDelete.getBitmap();
bitmapDelete = Bitmap.createScaledBitmap(bitmapDelete, 16, 16, true);
tR = new TableRow(this);
TextView TextViewTVA = new TextView(this);
ImageButton ImgButtonDelete = new ImageButton(this);
TextViewTVA.setText("Remise.toString()");
TextViewTVA.setBackgroundResource(R.drawable.cellborder);
TextViewTVA.setPadding(5, 5, 5, 5);
ImgButtonDelete.setBackgroundColor(80000000);
ImgButtonDelete.setImageBitmap(bitmapDelete);
tR.addView(ImgButtonDelete);
tR.addView(TextViewTVA);
table.addView(tR);
ImgButtonDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) table.getParent();
parent.removeView(table);
}
});

Dynamically create button using data in array and setOnClicklistener

im trying to add the OnClickListener to the button that it generated by the value in array, but m fail to do so, any suggestion here?
long lprice = Long.parseLong(searchId.getText().toString());
List<String> buttonNameOne = dbc.getItemNameRbPrice(lprice);
List<String> buttonPriceOne = dbc.getPriceRbPrice(lprice);
List<String> buttonDateOne = dbc.getCurrentDateRbPrice(lprice);
for(int i = 0 ; i < buttonNameOne.size() ; i ++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i);
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.v("Value of element "+i, buttonNameOne.get(i));
Log.v("Value of element "+i, buttonPriceOne.get(i));
Log.v("Value of element "+i, buttonDateOne.get(i));
}
What you want to do is add the btn.setOnClickListener
for(int i = 0; i < buttonnameOne.size(); i++)
{
Button btn = new Button(this);
btn.setId(2000+i);
btn.setText(buttonNameOne.get(i) + i));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(view v) {
// TODO Whatever you want onclick to do
}
});
//Log stuff
linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

Categories

Resources