ImageButton is not visible when adding to a tablerow dynamically - android

I am creating a form where the user enters his/her address and when the user saves the form, the flow goes to another activity where the users' name is shown one below the other. I am using TableLayout and in each TableRow I have a TextView which shows the name and an ImageButton beside the textview which can be used to delete the entry. Now when I run the app I can only see the TextView in the layout, the ImageButton is not visible.Can someone please help me where I am going wrong?
Here is part of my code:
if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
RecipientArray = (ArrayList<Person>) data
.getSerializableExtra("RecArray");
TableLayout tbl = new TableLayout(this);
TextView[] tv = new TextView[RecipientArray.size()];
ImageButton delete_btns[] = new ImageButton[RecipientArray.size()];
TableRow tr[] = new TableRow[RecipientArray.size()];
for (int i = 0; i < RecipientArray.size(); i++) {
tv[i] = new TextView(this);
tv[i].setBackgroundResource(R.drawable.fill_rece);
Person p = RecipientArray.get(i);
tv[i].setText(p.getName());
tv[i].setTextColor(Color.WHITE);
delete_btns[i] = new ImageButton(this);
delete_btns[i]
.setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tr[i] = new TableRow(this);
tr[i].addView(tv[i]);
tr[i].addView(delete_btns[i]);
tbl.addView(tr[i]);
}
recs_layout.addView(tbl);//I add the TableLayout to a RelativeLayout
}

delete_btns[i] = new ImageButton(this);
delete_btns[i].setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Drawable d = delete_btns[i].getDrawable();
d.setBounds(0, 0, delete_btns[i].getWidth(), delete_btns[i].getHeight());

I think your textview is taking all the space in the table row try setting layout params for that as well
For ex:
tv[i].setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2f));
And similarly for image button
delete_btns[i].setLayoutParams(new TableRow.LayoutParams(
0, LayoutParams.WRAP_CONTENT,1f));
And to table row
tr[i].setWeightSum(3f)
Here the third argument of float type is weight of the view as it is set to 1f for both your image view and textview both will occupy equal space you can change it and set it what you need.
While using weight set width to 0dp for both textview and imageview

Related

Adding Buttons below TextViews dynamically

I have the following:
Button[] buttons = new Button[forSale.size()];
TextView[] textViews = new TextView[forSale.size()];
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < forSale.size(); i++) {
rl = (RelativeLayout)findViewById(R.id.marketView);
textViews[i] = new TextView(this);
textViews[i].setText("\n" + forSale.get(i).getTradeGoodType()
+ "\t$"
+ forSale.get(i).getTradeGoodType().getBasePrice() +
"\n");
textViews[i].setId(i);
goodsForSaleText.append(textViews[i].getText());
buttons[i] = new Button(getApplicationContext());
buttons[i].setText("BUY");
// add the rule that places your button below your TextView object
params.addRule(RelativeLayout.BELOW, textViews[i].getId());
buttons[i].setLayoutParams(params);
rl.addView(buttons[i]);
}
My problem is that my the buttons are being added on top of each other at the top of the screen rather than one after the other below the TextViews.
Basically, it should look like:
TextView
Button
TextView
Button
TextView
Button
I need to dynamically add buttons for each number of TextViews I have (derived from a list). Can anyone help me out?
Use LinearLayout with vertical orientated instead of RelativeLayout. You will get automatically vertical position of objects

Android LinearLayout programmatically one column left, one column float right

My Android layout consists of 2 TextView inside a LinearLayout. However, I want the second text view to be align right.
Explanation:
// LinearLayout A
LinearLayout linearLayoutA = new LinearLayout(this);
linearLayoutA.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayoutWorkoutPlansSessionsMain.addView(linearLayoutA);
// Title
TextView textViewExerciseTitle = new TextView(this);
textViewExerciseTitle.setText("Pull ups");
textViewExerciseTitle.setTextSize(16);
textViewExerciseTitle.setTextColor(Color.BLACK);
linearLayoutA.addView(textViewExerciseTitle);
// Reps & sets
TextView textViewSetsReps = new TextView(this);
textViewSetsReps.setText("8 x 4");
textViewSetsReps.setTextSize(16);
textViewSetsReps.setTextColor(Color.BLACK);
textViewSetsReps.setPadding(20,0,0,0);
linearLayoutA.addView(textViewSetsReps);
TextView textViewSetsReps = new TextView(this);
textViewSetsReps.setText("8 x 4");
textViewSetsReps.setTextSize(16);
textViewSetsReps.setTextColor(Color.BLACK);
textViewSetsReps.setPadding(20,0,0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textViewSetsReps.setLayoutParams(params);
textViewSetsReps.setGravity(Gravity.RIGHT);
ll.addView(textViewSetsReps);
you could edit your sencond TextView like this !

Adding TableRows dynamically in Android doesn't work

I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.

Align components in TableRow

First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);

How to set the row in a table layout?

So far, I have code that dynamically creates my table within a horizontal scrollview. I want each item to appear in the same y location and so that you can only see one at a time. Right now, each column gets set, but the visual is like a staircase, where the next row is underneath and only moved over about 50px. Is there a way to force each column to take up the entire screen width? Also, can you set the row that the content appears in?
TableLayout tl = (TableLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
ImageView iv = new ImageView(this);
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
else
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.noimage);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tl.addView(tr);
}
As you have use the TableLayout and now you want each cell to consume appropriate same size throughout the table respectfully by comparing itself via row and column.
So, you have to set WEIGHT to each item and specify weight value = "1.0" or below it.

Categories

Resources