I have following UI hierarchy
Scroll View -->TableLayout-->Rows [each row has TextView+EditText]
Problem
Inside a row, each View should not exceed 50% of width of parent (TableRow). if an EditText is created to enter, say Username with max characters N; then width of EditText ideally should be exactly N chars long but must not exceed 50% limit.
I've tried many permutations and combination and could not get it right so far.
I have to do it programmatically as per the project requirement.
thanks.
Try using layout weight like this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textview1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
<EditText
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</TableRow>
To do it programmatically, try using LayoutParams to accomplish the same like this:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
TableLayout.LayoutParams trparams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(trparams);
textview1 = new TextView(this);
edittext1 = new EditText(this);
TableRow.LayoutParams fieldparams = new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tr.addView(textview1, fieldparams);
tr.addView(edittext1, fieldparams);
table.addView(tr);
i saw your comment to ask's answers.i think you can set the width of edittext or textview as wrap content.and set the max width property of those controls as 50% of the screen.then it's width will not exceed the 50% of the parent width.
I had to scrap TableLayout in order to be my own layouts effective. Layout params in TableRows seems ineffective. LinearLayout looks more flexible.
Now I have following Layout hierarchy and it works.
Scroll View -->LinearLayout instead of TableLayout--> [Individual fields encapuslated in LinearLayout instead of TableRows]
Related
Adding text view to row and thus row to table layout. But the row gets cut from the screen.
TextView point = new TextView(activity);
TextView time = new TextView(activity);
point.setTextSize(15);
time.setTextSize(15);
point.setPadding(5, 5, 5, 5);
time.setPadding(5, 5, 5, 5);
point.setText("smthing something")
time.setText("smthing smthing")
row.addView(point);
row.addView(time);
tableLayout.addView(row);
How to wrap the textview in the row so that the row gets wrapped in the screen itself..?
Set your textView height wrap_content.
point.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Thanks Mukesh, that solved most of the part of my problem. :) The text crossing the screen got wrapped inside screen.
Since my first column contained long text and second column has short text.. So first column must fit in multiple lines and 2nd column in single line. (Setting the textview to single line or multiple line didnt help)
So I did it by adding shrink column to my '0th' and stretch column to my '1st'. This solved my whole problem and setting layout params also not required now.
XML for table layout:
<TableLayout
android:id="#+id/route_table"
android:layout_width="match_parent"
android:shrinkColumns="0"
android:padding="5dp"
android:stretchColumns="1">
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/row1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stop"
android:background="#drawable/table_row"
android:padding="10dp"
android:text="Stop: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/time"
android:layout_gravity="right"
android:background="#drawable/table_row"
android:padding="10dp"
android:text="Time: "/>
</TableLayout>
`
TextView point = new TextView(activity);
TextView time = new TextView(activity);
LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
point.setLayoutParams(layoutParams);
time.setLayoutParams(layoutParams);
point.setTextSize(15);
time.setTextSize(15);
point.setPadding(5, 5, 5, 5);
time.setPadding(5, 5, 5, 5);
point.setText("smthing something")
time.setText("smthing smthing")
row.addView(point);
row.addView(time);
tableLayout.addView(row);
I'm trying to programmatically add many TextView to a RelativeLayout but I am unable to do that when TextView reach the end of the display right next TextView inflate in a new line.
RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/tag_cloud"
android:padding="10dp">
</RelativeLayout>
Code:
if (categoriesCursor.moveToFirst()){
do {
TextView tagElement = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
tagElement.setText(categoriesCursor.getString(2));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, pixels, pixels); // llp.setMargins(left, top, right, bottom);
tagElement.setLayoutParams(llp);
tagCloudLayout.addView(tagElement);
} while (categoriesCursor.moveToNext());
}
tag.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#ff000000"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
Thanks
It´s not really clear what You are asking, but If I understand You the right way, You want to have only one line with textViews, or? Also, You are using wrong Layout Params, if You want to add some views to a relativeLayout side by side, I think You will get it with:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
Where the last parameter stands for the layout weight attribute. With LayoutWeight and MATCH_PARENT, all views will be drawn with equal size.
Consider using a ListView which contains TextView, and that way you'll be able to take advantage of cell re-use etc. Here's a good tutorial
Finally I put one LinearLayout vertical oriented and inside LinearLayouts horizontal oriented with three TextView inside. Isn't the best solution but it works for me.
I know this has been asked several times over, but can't seem to get it to work correctly in my situation. I am trying to get the last column to align right in a table that is generated programatically. I know I need to apply the LayoutParams to the row and all the inner children, and I know that I need to set the Gravity to the TextView, not the row, but I have tried all the permutations I can think of and can't seem to get the last column to align right.
Here is my XML layout:
<!--Open Hours-->
<LinearLayout
android:id="#+id/llOpenHourDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/llWebsiteDetail"
android:paddingBottom="10dp"
android:visibility="gone"
android:weightSum="4">
<TextView
android:id="#+id/tvOpenHourDetailIcon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/fa_clock"
android:textColor="#color/cp_blue" />
<TableLayout
android:id="#+id/tlOpenHoursDetail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
</LinearLayout>
And then in my activity I have the following code in a loop
String currentPeriod = formattedOpen.format(open.getTime()) + " - " +
formattedClose.format(close.getTime());
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT );
TableRow tableRow = new TableRow(getBaseContext());
tableRow.setLayoutParams(params);
TextView tvDayOfWeek = new TextView(getBaseContext());
tvDayOfWeek.setText(open.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()));
tvDayOfWeek.setTextColor(getResources().getColor(R.color.black));
tvDayOfWeek.setLayoutParams(params);
tableRow.addView(tvDayOfWeek);
TextView tvPeriodHours = new TextView(getBaseContext());
tvPeriodHours.setText(currentPeriod);
tvPeriodHours.setTextColor(getResources().getColor(R.color.black));
tvPeriodHours.setGravity(Gravity.RIGHT);
tvPeriodHours.setLayoutParams(params);
tableRow.addView(tvPeriodHours);
tlOpenHoursDetail.addView(tableRow);
In order for setGravity() to work you must first modify the width field of yourTextView as follows in your layout:
android:layout_width="fill_parent"
Now you should be able to call:
tvPeriodHours.setGravity(Gravity.RIGHT)
I think there are two ways of doing this.
If your TableLayout has a width not based on it's contents (such as if the columns widths are set using android:stretchColumns) you can set the TextView width to match_parent and then assign the gravity on the TextView using textView.setGravity(Gravity.END).
If your TextView width is smaller than the bounds of the TableLayout cell you can call the gravity on the TableRow instead using tableRow.setGravity(Gravity.END).
I have a TableLayout where my TextViews fill the width of the cell but not the height so I'm using:
textView.setGravity(Gravity.CENTER);
tableRow.setGravity(Gravity.CENTER_VERTICAL);
To get my text in the very centre of the cell.
In case it helps anyone, I have spent ages trying to get the TextView aligned to the bottom of the cell but nothing seems to work, including tableRow.setGravity(Gravity.CENTER_VERTICAL), no idea why but I have given up and in the centre is fine.
I'm trying to create a list of clickable image buttons w/ text that fit inside a HorizontalScrollView. The images/content will be set programmatically. The best way to do this seemed to be a LinearLayout, that then contained a series of RelativeLayouts that contained the views to display the relevant content. However, I'm having trouble getting space between each RelativeLayout. Even though I've set margins in xml and programmatically they seem to be ignored and the RelativeLayout objects are squished together.
Some code:
<RelativeLayout
android:id="#+id/details_image_button"
android:layout_width="75dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="#00ff78">
<ImageView
android:id="#+id/loadable_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/details_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#b083ef"
android:text="PH - Info about title"
android:layout_alignParentBottom="true"
/>
//Code below is looped through several times
RelativeLayout imageButtonLayout = (RelativeLayout) inflater.inflate(R.layout.details_image_button, null);
RelativeLayout.LayoutParams imageButtonLayoutParams = new RelativeLayout.LayoutParams(100, 100);
imageButtonLayoutParams.setMargins(10, 10, 10, 10);
imageButtonLayout.setLayoutParams(imageButtonLayoutParams);
The current result that I am getting is a solid green (background color of the RelativeLayout) rather than the expected result of a group of RelativeLayouts with a space between each. How can I best get a margin or buffer between each RelativeLayout?
If your RelativeLayout is inside a LinearLayout, the LayoutParams you need to use would be LinearLayout.LayoutParams:
RelativeLayout imageButtonLayout = (RelativeLayout)
inflater.inflate(R.layout.details_image_button, null);
LinearLayout.LayoutParams imageButtonLayoutParams = new
LinearLayout.LayoutParams(100, 100);
imageButtonLayoutParams.setMargins(10, 10, 10, 10);
imageButtonLayout.setLayoutParams(imageButtonLayoutParams);
LayoutParams come from the parent, not the child.
I got some trouble with the TableLayout Android. I wanna create a table with symmetric rows/columns, i.e. a layout where every TextView in it gets allocated exactly the same amount of space like all the others, no matter what text has been appended to it.
It's important that this can be done by the use of Java-code, not the XML-files.
It would be probably good to know that this layout requires two tables to be next to each other, i.e. every table has just 50% of the screen size. I suppose this makes several LayoutParameter unavailable (e.g. FILL_PARENT)?
This is what I tried:
for (String s : buffer) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(0x00, LayoutParams.WRAP_CONTENT, 1f));
tv.append(s);
row.addView(tv, new TableRow.LayoutParams(LayoutParams.FILL_PARENT));
}
Still looks bad though.
This will create two columns that share's the width in an orderly fashion.
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<!-- Set the width to 0dp and set layout_weight=1! on both Views-->
<TextView
android:text="This is text1, its pretty long but that shouldn't be a problem"
android:layout_marginLeft="1px"
android:background="#ff0000"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Shorter"
android:background="#00ff00"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
Edit: Same thing, but now in code:
TableLayout layout = new TableLayout(this);
TextView t1 = new TextView(this);
t1.setBackgroundColor(Color.BLUE);
t1.setText("This is text1, its pretty long but that shouldn't be a problem");
TextView t2 = new TextView(this);
t2.setBackgroundColor(Color.GRAY);
t2.setText("Shorter");
TableRow row = new TableRow(this);
row.addView(t1, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
row.addView(t2, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
layout.addView(row);