Change RelativeLayout from Java in Android - android

I have this XML RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeBackGround"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<ImageView
android:id="#+id/img1"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/sample" />
<LinearLayout
android:id="#+id/linout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/img1"
android:background="#drawable/bubble_left" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="text2"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I'm trying to to do is to change the gravity of the RelativeLayout from right to left based on the row like so
wrapper = (RelativeLayout) view.findViewById(R.id.relativeBackGround);
if (even) {
wrapper.setGravity(Gravity.LEFT);//this one isn't working
//How can I change the alignment of linout to be on the left of the img1?
} else {
wrapper.setGravity(Gravity.RIGHT);
}
the setGravity isn't working, and I'm not sure how to do the alignment, can you help me with that?

You can set the layout of the LinearLayout with something similar to the following:
LinearLayout ll = (LinearLayout) view.findViewById(R.id.linout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ll.setLayoutParams(lp);
Then just switch the rule based on whether it's odd or even row.

Related

Android display half TextView and half ImageView if true else display only TextView

I'm trying to display a text inside a TextView in the left side of the screen and an image inside ImageView in the right side when "true":
<RelativeLayout
android:id="#+id/displayMessageCenter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/displayMessageTop"
android:layout_centerHorizontal="true"
android:background="#android:color/white">
<TextView
android:id="#+id/textMessageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:layout_weight="0.5"
android:layout_alignBottom="#+id/dividerId" />
<ImageView
android:id="#+id/messagePicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/dividerId"
android:layout_weight="0.5"
android:gravity="right"/>
<View
android:id="#+id/dividerId"
android:layout_width="400dp"
android:layout_height="1dp"
android:background="#000"
android:layout_above="#id/leftTextGenericDialog"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>
<TextView
android:id="#+id/leftTextGenericDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:layout_centerHorizontal="true"
android:textColor="#FF72CCCC"
android:text="SEND RESPONSE"
android:textSize="40dp"
android:drawableLeft="#drawable/sendicon"
android:layout_above="#+id/closeButton"
android:onClick="sendMessage"/>
<ImageButton
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5sp"
android:layout_gravity="center"
android:background="#drawable/button_close_animation"
android:onClick="closeActivity"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>
</RelativeLayout>
and if "false" to hide the image and display just the TextView with the text in the center. The hiding will be something like:
if(true) imageView.setVisibility(View.GONE);
else imageView.setVisibility(View.VISIBLE);
but I don't know how to align the ImageView and the TextView to be easier to hide/show just the ImageView when needed.
I'm new in Android development so please be gentle :)
When using Relative layout on these cases, you can use alignParentLeft=true for the TextView and alignParentRight=true for the ImageView, but if you wanted the TextView to adjust whenever the ImageView is visible or not. Use something like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="text"
android:layout_weight="0.5"
android:gravity="center_horizontal"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:visibility="gone"
/>
</LinearLayout>
For true condition, you can just set
android:alignParentLeft="true"
and
android:alignParentRight="true"
to your TextView and ImageView corresponsdingly.
For false condition, you can programmatically set rules to your TextView. Just get the TextView using findViewById and set rules like following:
TextView tv = (TextView) findViewById(R.id.textMessageText);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)tv.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
tv.setLayoutParams(layoutParams);

Textview inside custom layout of ActionBar doesn't go rtl

I have this custom layout in my app bar.
My problem is that not only the text is not shown completely, but also it sticks to the left instead of right.
I have set all gravity, direction layout_gravity attributes to right and rtl, both in code and in layout.xml files. But no success.
Here is the screenshot: (as can be seen only the "right" part of the text "Should stick right" inside the textview is shown.)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="#color/black1"
android:gravity="right|center_vertical"
android:layoutDirection="rtl"
android:orientation="horizontal">
<ImageView
android:id="#+id/search_mag_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue4main"
android:src="#drawable/abc_ic_search_api_mtrl_alpha" />
<com.jahanjooy.haftdong.views.DelayAutoCompleteTextView
android:id="#+id/text_view_vicinity"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#color/black1"
android:gravity="right"
android:imeOptions="flagNoExtractUi|actionSearch"
android:inputType="textCapSentences"
android:layoutDirection="rtl"
android:shadowColor="#color/white1"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/text_view_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="#color/blue1"
android:gravity="right"
android:layoutDirection="rtl"
android:text="should go right"
android:textColor="#color/white1"
android:textSize="15dp" />
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="20dp"
android:layout_height="20dp"
android:indeterminate="true"
android:indeterminateTint="#android:color/white"
android:indeterminateTintMode="src_atop"
android:visibility="gone" />
<ImageView
android:id="#+id/ic_filter_cone"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_filter" />
Activity:
/**/
/**/
Toolbar parent =(Toolbar) layout.getParent();//first get parent toolbar of current action bar
parent.setContentInsetsAbsolute(100,10);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
ActionBar.LayoutParams lp = (ActionBar.LayoutParams) layout.getLayoutParams();
lp.rightMargin= 0;
lp.width = metrics.widthPixels/2;
lp.gravity = Gravity.RIGHT;
layout.setLayoutParams(lp);
layout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
TextView hint = (TextView) findViewById(R.id.text_view_hint);
LinearLayout.LayoutParams ll= (LinearLayout.LayoutParams) hint.getLayoutParams();
ll.rightMargin = 0;
ll.width = metrics.widthPixels/2;
ll.gravity = Gravity.RIGHT;
hint.setLayoutParams(ll);
hint.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
hint.setTextDirection(View.TEXT_DIRECTION_RTL);
/**/
android:shadowColor="#color/white1"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/text_view_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="#color/blue1"
android:gravity="right"
android:layoutDirection="rtl"
android:text="should go right"
android:textColor="#color/white1"
android:textSize="15dp" />
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleSmall"
Replace the TextView with a RelativeLayout and put the TextView in it.as shown below
<RelativeLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/blue1">
<TextView
android:id="#+id/text_view_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="should go right"
android:textColor="#color/white1"
android:textSize="15dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
give the required width to RelativeLayout, wrap_content to TextView. Set your TextView background to RelativeLayout. Add
android:layout_alignParentRight="true"
will move the TextView to right side.And removed other gravity attributes.

How to display Relative layout content from top of the screen

I am having following layout in file mylayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/yellow"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:id="#+id/Layout5"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Layout6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="top">
<ImageView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/green"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="5dp"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="#+id/imageView">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_2"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
With this layout when i am doing setContentView(R.layout.mylayout) for my activity, which has transparent theme, my layout is getting displayed in the center of the screen.
I want to change this layout positioning dynamically, so center(default) and bottom(by using following code) display is working.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
But somehow i am not able to figure out how to display this layout dynamically to the top of the screen.
Any help will be appreciated.
Try removing the rule that you made earlier (setting it to align bottom), and add a new rule:
//your code.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
//remove rule
relativeParas.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lLayout.setLayoutParams(relativeParas);
Note that removeRule() is available from API 17 onwards.
Try modifying following way:
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Layout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/yellow"
android:padding="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#color/green"/>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/imageView"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button2"
/>
</LinearLayout>
</RelativeLayout>
class:
RelativeLayout Layout5 = (RelativeLayout)findViewById(R.id.Layout5);
Layout5.setGravity(Gravity.BOTTOM);
I found the following fix for this issue.
I added filler Liner layout in the end under Relative layout.
To display in center don't do anything.
To display in top use following code. Just move filler layout to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
To display in bottom use following code. i.e. move layout6 to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);

Keep Buttons from shifting depending on text in TextView

How can I make it so that the Buttons are not shifted to the bottom depending on the content a TextView above it?
If the TextView has a lot of text, the buttons are pushed down.
It may need to use RelativeLayout? Could you please explain how can i do it?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mainbackground">
<TextView
android:id="#+id/tvBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tv_company"
android:layout_marginLeft="30dp"
android:textColor="#002060"
android:layout_marginTop="20dp"
android:textSize="15sp"
>
</TextView>
<Button
android:id="#+id/button_operations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_blue"
android:drawableLeft="#drawable/ic_purchase"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="left|center"
android:text=" oper1"
android:layout_marginTop="15dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#001B51"
android:textSize="30sp"
android:textStyle="bold"
/>
....... //4 more buttons
<Button
android:id="#+id/button_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_blue"
android:paddingLeft="10dp"
android:drawableLeft="#drawable/ic_exit"
android:gravity="left|center"
android:text=" Exit"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#001B51"
android:textSize="30sp"
android:textStyle="bold"/>
</LinearLayout>
Yes, RelativeLayout would work better for this. Notice the new attributes added.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mainbackground">
<TextView
android:id="#+id/tvBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tv_company"
android:layout_marginLeft="30dp"
android:textColor="#002060"
android:layout_marginTop="20dp"
android:textSize="15sp"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</TextView>
<Button
android:id="#+id/button_operations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_blue"
android:drawableLeft="#drawable/ic_purchase"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="left|center"
android:text=" oper1"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#001B51"
android:textSize="30sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
Another way is to set their positions programmatically like this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1 = (Button)findViewById(R.id.button_operations);
button1.setLayoutParams(params);
//or set its position relative to another button:
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2 = (Button)findViewById(R.id.secondBtn);
button2.setLayoutParams(params);
//or set its margins like this (left, top, right, bottom):
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(200, 600, 0, 0); //if it is possible don't use hard coded numbers, use parameters based on screen resolution instead
Button button3 = (Button)findViewById(R.id.thirdBtn);
button3.setLayoutParams(params);
By default, your TextView is going to grow as much as it needs. You can place restrictions on it such as using maxLines
<TextView
android:maxLines="3"
...
</TextView>
There is also android:lines
If you wish, you can also use android:ellipsize to show, for instance, at the end of the TextView that there is more text.
You can read through the documentation to see which attributes are best for you.

Every View stick together when loop LayoutInflater

I have a layout problem. my code have a LayoutInflater set the following xml as layout, and will loop few time to show my school time table. Eventhough i have set the relativelayout's marginBottom and Top to 50dp, but why every layout still stick together?
here's the xml:
<RelativeLayout
android:id="#+id/left_part"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_day"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_above="#+id/table_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_day"
android:textSize="#dimen/table_day_size" />
<TextView
android:id="#+id/table_date"
android:layout_width="match_parent"
android:layout_height="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_date"
android:textSize="#dimen/table_date_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/left_part"
android:layout_alignTop="#+id/left_part"
android:layout_toRightOf="#+id/left_part" >
<RelativeLayout
android:id="#+id/center_part1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/table_time"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/table_timelocationclass_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/center_part1" >
<TextView
android:id="#+id/table_subject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="1dp"
android:gravity="center_vertical"
android:text="#string/table_subject"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/table_subject_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/center_part2" >
<TextView
android:id="#+id/table_lecturer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/table_lecturer"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="#dimen/table_lecturer_size" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/right_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/center_part"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/center_part"
android:layout_toRightOf="#+id/center_part" >
<RelativeLayout
android:id="#+id/right_part1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/table_location" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/right_part2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/right_part1" >
<TextView
android:id="#+id/table_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/table_class" />
</RelativeLayout>
</RelativeLayout>
Here is the print screen:
here is the loop:
LinearLayout ll = (LinearLayout)findViewById(R.id.main_table);
ll.removeAllViews();
//set table layout
for(int i=0;i<tableRow.size();i++)
{
LayoutInflater inflater = getLayoutInflater();
View perClassTable = inflater.inflate(R.layout.perclass_table, null);
TextView table_day = (TextView)perClassTable.findViewById(R.id.table_day);
TextView table_date = (TextView)perClassTable.findViewById(R.id.table_date);
TextView table_time = (TextView)perClassTable.findViewById(R.id.table_time);
TextView table_location = (TextView)perClassTable.findViewById(R.id.table_location);
TextView table_class = (TextView)perClassTable.findViewById(R.id.table_class);
TextView table_subject = (TextView)perClassTable.findViewById(R.id.table_subject);
TextView table_lecturer = (TextView)perClassTable.findViewById(R.id.table_lecturer);
table_day.setText(date[i].substring(0, 3));
table_date.setText(date[1].substring(4,13));
table_time.setText(time[i]);
table_location.setText(loca[i]);
table_class.setText(clas[i]);
table_subject.setText(subj[i]);
table_lecturer.setText(lect[i]);
//testing
ll.addView(perClassTable);
}
Set some LayoutParams with some margins for your inflated view at the end of the for loop:
//...
table_subject.setText(subj[i]);
table_lecturer.setText(lect[i]);
// set some proper LayoutParams for the inflated View which is going to be added
// to the ll LinearLayout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// set some margins to distance the rows
lp.topMargin = 100;
lp.bottomMargin = 100;
// add the inflated view with to ll with the LayoutParams above.
ll.addView(perClassTable, lp);
}
The LayoutParams is a special class designed to be use with a ViewGroup subclass which generally holds layout attributes like width/height, margins, layout positioning rules etc.
Also, you could improve the layout file by removing all those wrappers RelativeLayouts and set the TextViews directly in the parent RelativeLayout. If the number of rows is big you may also want to have a look at the ListView widget as galex said in his answer.
A RelativeLayout evaluates it's children from top to bottom. So, if you use something like android:layout_above="#+id/table_date", it must come after table_date.
<RelativeLayout
android:id="#+id/left_part"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_date"
android:layout_width="match_parent"
android:layout_height="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_date"
android:textSize="#dimen/table_date_size" />
<TextView
android:id="#+id/table_day"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_above="#+id/table_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_day"
android:textSize="#dimen/table_day_size" />
</RelativeLayout>
Here, I have swapped the textviews.
Looks like a list to me, look into ListView.
It will even recycle views if you implement your adapter correctly!

Categories

Resources