How to set OnClick on Programmatically TableRow? - android

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.

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("")){
}

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

Approach to get values of a dynamically generated textView

I successfully created a dynamic TextView and a Button now when ever the button was clicked the value of a TextView changes.
But the problem is I have a final "submit button" outside a loop which should get the INDIVIDUAL values of each TextView and I cant think of a way how to do it can someone pls give me an approach for this thanks!. pls be nice..
code
Cursor citem= sdb.rawQuery("SELECT * FROM ITEM INNER JOIN CATEGORY ON item.categoryid = category.id where category.categoryname='"+fcat+"'", null);
ScrollView scrollView= new ScrollView(this);
LinearLayout mainLayout= new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button border = new Button(this);
border.setId(Integer.parseInt(cuser.getString(cuser.getColumnIndex("id"))));;
border.setText("ORDER");
while (citem.moveToNext())
{
byte[] blob =citem.getBlob(citem.getColumnIndex("itemimage"));
int id = Integer.parseInt(citem.getString(citem.getColumnIndex("id")));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setTag(id);
int i;
ImageView iv = new ImageView(this);
iv.setId(id);
iv.setImageBitmap(dh.getPhoto(blob));
final TextView txtquantity = new TextView(this);
txtquantity.setId(id);
txtquantity.setText("0");
txtquantity.setTextSize(20);
final TextView txtprice = new TextView(this);
txtprice.setId(id);
txtprice.setText(citem.getString(citem.getColumnIndex("itemprice")));
txtprice.setTextSize(30);
ImageButton btn1 = new ImageButton(this);
btn1.setId(id);
final int id_ = btn1.getId();
btn1.setImageResource(R.drawable.ic_launcher);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
i=Integer.parseInt((String) txtquantity.getText())+1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())+(Integer.parseInt(txtprice.getText().toString())*1)));
}
});
ImageButton btn2 = new ImageButton(this);
btn2.setId(id);
final int id2_ = btn2.getId();
btn2.setImageResource(R.drawable.ic_launcher);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(Integer.parseInt((String)txtquantity.getText())<=0)
{
}
else
{
int i=0;
i= Integer.parseInt((String) txtquantity.getText())-1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())-(Integer.parseInt(txtprice.getText().toString())*1)));
}
}
});
linearLayout.addView(iv);
linearLayout.addView(txtprice);
linearLayout.addView(btn1);
linearLayout.addView(txtquantity);
linearLayout.addView(btn2);
mainLayout.addView(linearLayout);
}
mainLayout.addView(totalprice);
mainLayout.addView(border);
scrollView.addView(mainLayout);
setContentView(scrollView);
Not quite sure what problem you're having? If it's just reading all the TextViews you created in the loop, then you should just keep a list, and send it off for processing when you submit...
List<TextView> tv_list = new ArrayList<TextView>();
while(...){
//In loop..add your tv's
TextView some_tv = new TextView()
tv_list.add(some_tv);
...
}
//In the submit, send them off for processing...
private void process_tvs(List<TextView> tv_list){
for(TextView tv:tv_list){
//Assuming your tv's have numbers...
int val = Integer.valueOf(tv.getText().toString());
//do something....
}
}

Click Event On Dynamically Added TextViews in Android

I am trying to add 20 textviews and assign a onclick to dynamically added textviews. Problem is whenever i try to click any of the dynamic textiview. It always fires up click event of last added textview.
Here is my code:
EditText s;
EditText t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
dynaText.setTag("" + i);
dynaText.setOnClickListener(btnClickListener);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
}
OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout ll = (RelativeLayout) v.getParent();
TextView tv = (TextView) ll.getChildAt(2);
// Integer pos = (Integer) tv.getTag();
Toast.makeText(v.getContext(), "Toast " + v.getTag(),
Toast.LENGTH_SHORT).show();
}
};
}
1) you should make your TextViews Clickable first :
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
dynaText.setTag("" + i);
dynaText.setClickable(true);//make your TextView Clickable
dynaText.setOnClickListener(btnClickListener);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
2) modify your onClickListener :
OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("btnClickListener", ""-----TextView Clicked : "+v.getTag());
Toast.makeText(YourActivity.this, "TextView Clicked : "+v.getTag(),
Toast.LENGTH_SHORT).show();
}
};
EDIT : you are adding your TextViews one over one . that's why the click event fires on the last one added.
try to use a LinearLayout with android:orientation="vertical" , or try to add each new TextView bellow the previous one :
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setId(i+1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
if(i!=0)
params.addRule(RelativeLayout.BELOW, i);
dynaText.setLayoutParams(params);
dynaText.setTextSize(30);
dynaText.setTag("" + i);
dynaText.setOnClickListener(btnClickListener);
containerLayout.addView(dynaText);
}
Try this:
OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Toast " + v.getTag(),
Toast.LENGTH_SHORT).show();
}
};
Update:
The reason why you are clicking the last TextView is because it is just so big.. You are increasing the padding with each loop and when you click somewhere, it will always hit the last View because it is overlaying the others. Try a LinearLayout and remove the padding like so:
public class MainActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
dynaText.setTag("" + i);
dynaText.setOnClickListener(this);
containerLayout.addView(dynaText);
}
}
public void onClick(View v) {
// do stuff
}
}
But having your problem solved.. I think a ListView would suit your purpose :)
Do in the following way to get the clicked data
Declare a variable
int sCount = 0;
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setId(1000+sCount);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
dynaText.setTag("" + i);
dynaText.setOnClickListener(btnClickListener);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout ll = (RelativeLayout) v.getParent();
TextView tv = (TextView) ll.getChildAt(2);
tv.addFocusables(null, v.getId(), 0);
Toast.makeText(v.getContext(), "Toast " + tv.getText(),
Toast.LENGTH_SHORT).show();
}
};
This works fine for you.
You can try to use anonymous instances for OnClickListener and create different instance of listener for each view. Something like this:
dynaText.setPadding(0, (i * 30), 0, 0);
dynaText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((TextView) v).getText();
}
});
containerLayout.addView(dynaText);

Setting background image makes the button unclickable

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

Categories

Resources