Onclicklistener programmatically created Imagebutton - android

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

Related

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.

Adding onClick to buttons that are created programmatically

A TextView and Button are programmatically created and added to a pre-existing vertical layout making it look like a vertical list of views. These views are only created based off the user entering data into an edittext and that data being saved into an ArrayList.
How can I add an onClick function to my 'trash' buttons that are programmatically created that would allow them to remove the view they are associated with.
public static ArrayList<String> deckNameArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
for(int i = 0; i < deckNameArray.size(); i++)
{
LinearLayout layout = new LinearLayout(this);
if ((i % 2) == 0) {
layout.setBackgroundColor(Color.CYAN);
} else {
layout.setBackgroundColor(Color.WHITE);
}
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setPadding(10, 5, 10, 5);
layout.setWeightSum(5);
mainLayout.addView(layout);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 4f);
TextView deckName = new TextView(this);
deckName.setText(deckNameArray.get(i));
deckName.setTextColor(Color.BLACK);
deckName.setTextSize(18);
deckName.setGravity(Gravity.CENTER_VERTICAL);
deckName.setLayoutParams(textViewParams);
layout.addView(deckName);
LinearLayout.LayoutParams imageButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
ImageButton remove = new ImageButton(this);
remove.setImageResource(R.mipmap.trash);
if ((i % 2) == 0) {
remove.setBackgroundColor(Color.CYAN);
} else {
remove.setBackgroundColor(Color.WHITE);
}
remove.setLayoutParams(imageButtonParams);
layout.addView(remove);
}
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
See http://developer.android.com/reference/android/widget/Button.html
You can set a click listener on any View, using code similar to the sample below:
View myView;
myView.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
// TODO your logic here
}
}

how do i align image view left of button programically?

how do i align image view left of button programically? in this code below i genrate button and image view programicallybut image view is show below of buton how do i set right of button image view??
public class MainActivity1 extends Activity {
public static Bitmap yourSelectedImage = null;
ImageView imageView1;
ContentValues newCon = new ContentValues();
public static byte[] blob = null;
public static long reqId;
int i;
LinearLayout btnLO;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
btnLO = new LinearLayout(MainActivity1.this);
LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// button margins
paramsLO.setMargins(0, 0, 0, 0);
// button height/width *pixels*
paramsLO.height = 75;
paramsLO.width = 75;
btnLO.setOrientation(LinearLayout.VERTICAL);
btnLO.setBackgroundColor(5); // not working correctly
//buttons
for (i =0;i <j;i++)
{
final Button b1 = new Button(MainActivity1.this);
final ImageView imageView = new ImageView(MainActivity1.this);
b1.setText(reqdata[i].getSpinnerText());
b1.setTag(reqdata[i].getValue());
btnLO.addView(b1, paramsLO);
btnLO.addView(imageView, paramsLO);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
reqId =
Long.valueOf(reqdata[btnLO.indexOfChild(b1)/2].getValue()).longValue();
Toast.makeText(MainActivity1.this, reqId+"",
Toast.LENGTH_SHORT).show();
}
final Button b2 = new Button(MainActivity1.this);
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
}
});
btnLO.addView(b2, paramsLO);
btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
this.addContentView(btnLO, new LayoutParams());
}
LinearLayout li=new LinearLayout(this);
li.setOrientation(LinearLayout.HORIZONTAL);
final Button b1 = new Button(MainActivity1.this);
final ImageView imageView = new ImageView(MainActivity1.this);
b1.setText(reqdata[i].getSpinnerText());
b1.setTag(reqdata[i].getValue());
li.addView(b1, paramsLO);
li.addView(imageView, paramsLO);
btnLO.addView(li);
ScrollView sc=new ScrollView(this);
sc.addView(btnLO);
remember this must be done outside for loop.
this is how you need to add button and image to your layout inside for loop

refresh activity with dynamic content

I have an activity with some dynamic content.
For each item of an array, there is a different content (number of textViews, checkboxes, ..)
The user clicks on a "save" button and then the screen has to refresh itself to display the content of the next item.
I was wondering if it was a good practice to reload my activity like this :
Intent refresh = new Intent(this, conflictActivity.class);
startActivity(refresh)
finish()
instead of removing all the views in my layouts, etc ...
I think it's easier to reload it.
Is there another way to do it ?
EDIT : added Code.
public class ConflicFieldTest extends Activity {
private int nbField;
private int nbChecked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conflict);
final Button saveButton = (Button) findViewById(R.id.save);
final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
final TextView fieldName = new TextView(this);
final LinearLayout editLayoutRight = new LinearLayout(this);
final LinearLayout editLayoutLeft = new LinearLayout(this);
final LinearLayout fieldRight = new LinearLayout(this);
final LinearLayout fieldLeft = new LinearLayout(this);
final ArrayList<String> allIdInConflict = getAllIdInConflict();
for (int i = 0; i < allIdInConflict.size(); i++) {
new AccountSyncTask() {
#Override
public void onPostExecute(
final ArrayList<ArrayList<String>> result) {
nbField = 0;
nbChecked = 0;
final Map<String, Object> fieldsInConflict = conflict
.conflictWithFields(memoAccountMap,
serverAccountMap, modifiedAccountMap);
for (Iterator<Map.Entry<String, Object>> field = fieldsInConflict
.entrySet().iterator(); field.hasNext();) {
final Map.Entry<String, Object> entry = field.next();
fieldName.setText(entry.getKey() + " : ");
final EditText[] editTextArrayRight = new EditText[fieldsInConflict
.size()];
final EditText[] editTextArrayLeft = new EditText[fieldsInConflict
.size()];
final CheckBox[] checkboxRight = new CheckBox[fieldsInConflict
.size()];
final CheckBox[] checkboxLeft = new CheckBox[fieldsInConflict
.size()];
checkboxRight[nbField] = new CheckBox(
ConflicFieldTest.this);
checkboxLeft[nbField] = new CheckBox(
ConflicFieldTest.this);
editLayoutLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
fieldRight.setOrientation(LinearLayout.VERTICAL);
fieldLeft.setOrientation(LinearLayout.VERTICAL);
rightLayout
.addView(
editLayoutRight,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
leftLayout
.addView(
editLayoutLeft,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editTextArrayRight[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayRight[nbField].setText((String) entry
.getValue());
editTextArrayRight[nbField].setTextColor(Color.BLACK);
editTextArrayRight[nbField].setFocusable(false);
editTextArrayRight[nbField].setClickable(false);
editTextArrayRight[nbField].setWidth(180);
editTextArrayRight[nbField].setPadding(0, 10, 0, 0);
editTextArrayLeft[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayLeft[nbField]
.setText((String) serverAccountMap.get(entry
.getKey()));
editTextArrayLeft[nbField].setTextColor(Color.BLACK);
editTextArrayLeft[nbField].setFocusable(false);
editTextArrayLeft[nbField].setClickable(false);
editTextArrayLeft[nbField].setWidth(180);
editTextArrayLeft[nbField].setPadding(0, 10, 0, 0);
final int i1 = nbField;
checkboxLeft[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
checkboxRight[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
editLayoutLeft.addView(fieldName);
editLayoutRight
.addView(
editTextArrayRight[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutLeft
.addView(
editTextArrayLeft[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutRight.addView(checkboxRight[nbField]);
editLayoutLeft.addView(checkboxLeft[nbField]);
nbField++;
}
saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//save data & update UI
}
}
.execute(appState.getSessionName(), id, "getObjectOnServer");
}
}
Sorry for the code, it is a litle bit dirty (i have deleted some parts), i am reworking it.
int flag = 0;
public void updateUI() {
if(flag == 0) {
//your normal codes...
} else {
//display view with user given data
}
}
Call this function in oncreate. At first flag will be set to zero. When u enter the data in textview and tick checkboxes, update the flag with some value. Then on save Button click call this function again. Now flag is set. So it will display the updated UI.
I think this will give u an idea..

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