I'm trying to add a Linear Layout inside a programmatically generated Table Row layout. The linear layout never seems to show. I have tried setting the width and height in the program yet nothing helped.
TableLayout ll = (TableLayout) view.findViewById(R.id.info_table);
TableRow row=new TableRow(getContext());
row.setBackgroundColor(RED);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=20;
int rightMargin=20;
int bottomMargin=10;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
row.setLayoutParams(tableRowParams);
TextView address = new TextView(getContext());
address.setMinimumWidth(320);
address.setMaxWidth(320);
address.setText("Rating");
address.setTypeface(null, Typeface.BOLD);
address.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
row.addView(address);
View dummyView = new View(getContext());
LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,300);
dummyParams.weight = 1f;
dummyView.setLayoutParams(dummyParams);
dummyView.setBackgroundColor(Color.BLACK);
row.addView(dummyView);
row.bringChildToFront(dummyView);
ll.addView(row,0);
I can see only the red color being set by the row but not the black color being set by the Layout.
you can do like below example, you can create child view in parent view :
my code working in this manner may be it helps you .
LinearLayout linear=new LinearLayout(this);
linear.setLayoutParams(new
android.view.ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tableRowParams.addView(linear);
Related
I'm trying to develop similar to grid but using LinearLayout. I would like to have 3 images and exact bottom text after images in single row.
What I have tried:
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
ImageView icon = new ImageView(context);
Item item = getItem(page, index);
Log.e("tag", item.getDrawable());
imageLoader.displayImage(item.getDrawable(), icon, R.drawable.ic_launcher);
icon.setPadding(15, 15, 15, 15);
layout.addView(icon);
TextView label = new TextView(context);
label.setTag("text");
label.setText(item.getName());
label.setTextColor(Color.BLACK);
label.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.setWeightSum(3f);
layout.addView(label);
I've a view method which returns Viewso I return return layout; at the end of the method.
Here I 've given weight 3 button this is not working for me. And code show more then 3 images in row with text but would like to have weight 3 images and bottom text .
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
or
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
I would define a TableLayout with as many TableRows as you need. On each of those TableRows, I'd add a LinearLayout with VERTICAL orientation consisting of the two Views you need: ImageView and TextView.
This is the LinearLayout where you should set a weight of 1 (to all of them). You will have to get the screen's width and see whether the new LinearLayout to be added still fits the current row. If not, simply start a new TableRow.
To get the width of the screen you can use this:
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
The screen's width might be accessed via the params.width attribute, just compare it with your LinearLayout's .getWidth() method and keep an incremental account (a float) of the current TableRow's width to help you determine if the current item should be placed in an existing row or a new one.
Programmtically setting Linear Layout weight
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
I'm having a tablelayout, where columns are added dynamically. I'm unable to set width of the column/TextView dynamically.
This is my code:
TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);
TableRow header = new TableRow(this);
header.setId(100);
header.setBackgroundColor(Color.rgb(255, 165, 0));
//header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView empCodeHeader = new TextView(this);
empCodeHeader.setId(200);
empCodeHeader.setText("Employee Code");
empCodeHeader.setTextSize(16);
empCodeHeader.setPadding(2,2,2,2);
//empCodeHeader.setWidth(200);
header.addView(empCodeHeader);
TextView empNameHeader = new TextView(this);
empNameHeader.setId(201);
empNameHeader.setText("Employee Name");
empNameHeader.setTextSize(16);
empNameHeader.setPadding(2,2,2,2);
//empNameHeader.setWidth(300);
header.addView(empNameHeader);
I'm able to set using setWidth, but i want to set it dynamically, say 30% for column1 and 70% for column2.
Use LayoutParams for set height and width dynamically.
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);
I have a TableLayout in my XML, and I'd like to dynamically add a TableRow with an ImageView to that TableLayout. What I have so far is this:
TableRow tr = new TableRow(this);
ImageView imgView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(lp);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.icon_test));
tr.addView(imgView);
tlCollection.addView(tr);
What am I doing wrong? If I want to add a Textview to the same TableRow, it works, but adding an ImageView doesn't work..
The code for adding a TextView:
TextView myTextView = new TextView(this);
myTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
myTextView.setText("Test");
tr.addView(myTextView);
Any idea?
I fixed it, using an LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow)inflater.inflate(R.layout.collection_row, tlCollection, false);
//fill textview
TextView content = (TextView)row.findViewById(R.id.txtCollection);
content.setText("Test");
//fill imageview
ImageView myImgView = ImageView)row.findViewById(R.id.imgCollection);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.my_icon));
//add row to tablelayout
tlCollection.addView(row);
I think it has to do with the layout params. You're setting both dimensions to WRAP_CONTENT but the image view doesn't have "content" per se, it has a source drawable. Try playing around with adjustViewBounds, setScaleType, and the layout params. Notice that in your example of being able to add a TextView, you're setting the width to FILL_PARENT?
The only example I can find quickly in my open projects of adding an ImageView dynamically in this way was a case in which I was using FILL_PARENT in both directions. That might not work for you since I'm not doing it exactly the same, but it's worth playing around with these settings as well as the two others mentioned above.
try this
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
tr.addView(imgView);
I Had the same problem now its fixed using the below code
CandFlag =new ImageView(this);
CandFlag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
CandFlag.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.flag));
LayoutParams layoutParams = (LayoutParams) CandFlag.getLayoutParams();
layoutParams.width = 75;
layoutParams.height = 75;
CandFlag.setLayoutParams(layoutParams);
Tablerow.addView(CandFlag);
I want to show a list of flights on a listview. And I want to have them all of them align. I have used a LinearLayout, with gravity and padding. But I want to reduce the space for the 4th column, so then I can do bigger the size of the text. I have tried to set the layout_weight of this element to 0 and all of the rest to 1. But it doesnt work. So far I have this
Any ideas?
If you are going to use ListView with a list of LinearLayout view's (I would go with a TableRow list put into a TableLayout), then I would suggest predefining the width of each column in percentage of the total space (i.e. how much of the screen would you allow the column to allocate). Something among the lines:
// basically you build your adapter here
LinearLayout.LayoutParams wrapWrapLinearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] columnWidths = new int[]{20, 20, 20, 20, 20};
ArrayList<LinearLayout> tableRows = new ArrayList<LinearLayout>();//this is the adapter
LinearLayout row = new LinearLayout(this);
//add a header
row.setLayoutParams(wrapWrapLinearLayoutParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(headerColor);
row.addView(makeTableRowWithText(headerForCol1, columnWidths[0]));
row.addView(makeTableRowWithText(headerForCol2, columnWidths[1]));
row.addView(makeTableRowWithText(headerForCol3, columnWidths[2]));
row.addView(makeTableRowWithText(headerForCol4, columnWidths[3]));
row.addView(makeTableRowWithText(headerForCol5, columnWidths[4]));
tableRows.add(row);
for(int i = 0; i < numberOfItemsInTheFlightsTable; i++) {
row = new LinearLayout(this);
row.setLayoutParams(wrapWrapLinearLayoutParams);
row.setGravity(Gravity.CENTER);
row.addView(makeTableRowWithText(col1Text, columnWidths[0]));
row.addView(makeTableRowWithText(col2Text, columnWidths[1]));
row.addView(makeTableRowWithText(col3Text, columnWidths[2]));
row.addView(makeTableRowWithText(col4Text, columnWidths[3]));
row.addView(makeTableRowWithText(col5Text, columnWidths[4]));
tableRows.add(row);
}
//util method
private TextView recyclableTextView;
public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
return recyclableTextView;
}
I mean, the trick is that you predefine the percentage of screen allocated by each column.
Do the following
LinearLayout LL1 containing the first 4 columns
LinearLayout LL2 containing the last column
RelativeLayout RL contains LL1 and LL2
You can now position LL2 to the right of the container
EDITED