Programmatically set TextView gravity right in a TableRow - android

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.

Related

How to dynamically add textviews in a linear layout with additional constraints?

I am developing an app in which I want to add textviews in a column-wise order. Atmost 3 textviews can fill horizontally the screen width. If there are more than 3 textviews to be added, the new ones should be added in the next row, and so on. See the diagram below-
---1st---------------2nd--------------3rd
---4th----------------5th-----and so on
I know we need to use a nested linear layout with different orientation parameters. But how do I get around this "maximum 3 textviews per row constraint". I know that this has to do something with the weight parameter. The code I have written so far is-
LinearLayout out = (LinearLayout) findViewById(R.id.linear_params);
TextView mView = new TextView(this);
mView.setText("placeholder");
mView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
out.addView(mView);
And for the layout I have written-
<LinearLayout
android:id="#+id/out
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<LinearLayout
android:id="#+id/in
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView ... />
</LinearLayout>
</LinearLayout>
How should I modify the above xml and code so that I am able to place the views dynamically as required ?
The correct way to do this will be to use a GridView Component with three columns. It will allow you to add the views as well.
If you use LinearLayout you will have to use weights and keep adding new LinearLayout per row, this will take a lot of processing for calculations and overdrawing as well.
Using GridView will help to avoid writing the code to calculate and add every row and every view and will keep the layout optimized as well.

Adding a few TextViews on RelativeLayout

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.

How do I get space between consecutive RelativeLayouts inside a LinearLayout?

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.

UI controls' width

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]

Make a view full-width with a ListAdapter

I'm trying to make a View in a ListView (through a ListAdapter) with full width.
Here is the current test code :
LinearLayout result = new LinearLayout(context);
TextView testView = new TextView(context);
testView.setText("Aaah");
TextView test2View = new TextView(context);
test2View.setText("Eeeeh");
result.addView(testView, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
result.addView(test2View, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
// To make it easier to see borders
result.setBackgroundColor(Color.BLUE);
(It has to be put in the ListAdapter's createView)
Unfortunately, the two TextViews are small and both stuck on the left.
The limiter is actually the result view itself, not taking full width, so the two TextViews simply share this little space.
Doesn anyone know how to make them take full width ? Thanks ! :)
EDIT : simplified (and almost identical) problem :
Code :
http://pastebin.com/6pn1hXnT
I want the TextView to be full-sized, and I center the text so I can see when it is full-width and when it is only wrapping content.
This code shows the text on the left, so the MATCH_PARENT is not doing anything...
What should I do ?
Thanks !
I faced the similar problem and I've followed the advice on this thread. The linked topic refers to a ListView inside a dialog, but I've experienced the same behaviour in a pop-up list. In order to make the TextView full-width, I had to wrap it inside a RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="14dip"
android:paddingRight="15dip"
>
<TextView
android:id="#+id/list_dialog_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
</RelativeLayout>
Why don't you add them with fill_parent, but with '0'?
result.addView(testView, new LinearLayout.LayoutParams(FILL_PARENT ,
LayoutParams.WRAP_CONTENT, 1));

Categories

Resources