Every View stick together when loop LayoutInflater - android

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!

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);

Change RelativeLayout from Java in 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.

RelativeLayout params for main screen widget

What's up guys!
Here is an example of my layout xml:
<RelativeLayout
android:id="#+id/conf_RLayoutMultiSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<ImageView
android:id="#+id/widget_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/transparent100" />
<ImageView
android:id="#+id/widget_scar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/scar1" />
</RelativeLayout>
<ImageView
android:id="#+id/widget_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/dial1" />
</RelativeLayout>
The question is - is it possible to change the conf_RLayoutMultiSize size through the code?
I have the cofigurantion layout where I did it easy, but can't realise how to do it on manescreen vidget.
I've tried this
widgetView.setInt(R.id.conf_RLayoutMultiSize,"setLayoutParams",150+widgetBodySize);
but it wasn't succesful
You can do it as follows:
RelativeLayout rlay = (RelativeLayout) findViewById(R.id.conf_RLayoutMultiSize);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(value_width, value_height); // Set whatever parameters you want here
rlay.setLayoutParams(params); // Assign parameters to relative layout

Add a view that occupies the whole screen

I have a relative layout with transparent background and a some views arranged in the middle, i inflate this layout and add it to the main layout with addView(), the inflated view just adds the middle elements in the corner to the parent layout, how can i make the added view occupy all the screen (tried fill_parent but it just gets ignored)
This is the view i inflate
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#color/info_bar_bg_color" >
<ImageView
android:id="#+id/channel_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/channel_pic"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/title_color"
android:textSize="#dimen/title_text_size" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#color/date_color"
android:textSize="#dimen/date_text_size" />
<TextView
android:id="#+id/hourStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/date"
android:textColor="#color/title_color"
android:textSize="#dimen/title_text_size" />
<TextView
android:id="#+id/hourEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/hourStart"
android:textColor="#color/title_color"
android:textSize="#dimen/title_text_size" />
<ImageView
android:id="#+id/share_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/epgshare_0" />
<ImageView
android:id="#+id/bell_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/share_pic"
android:src="#drawable/epgsharebell_0" />
</RelativeLayout>
</RelativeLayout>
and this is the main layout i add the view to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"
tools:context=".MainActivity" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="openInfoBar"
android:text="Open InfoBar" />
</RelativeLayout>
i inflate this layout and add it to the main layout with addView(),
the inflated view just adds the middle elements in the corner to the
parent layout,
That is happening(most likely) because you don't setup the proper LayoutParams for the inflated layout view. You probably use something like this:
View v = inflater.inflate(R.layout.to_fill_parent, null);
which combined with the simple addView(v) method will make the view behave like you see.
The correct way would be:
View v = inflater.inflate(R.layout.to_fill_parent, (ViewGroup)findViewById(R.id.layout), false);
//add the view with addView
Change the :
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#color/info_bar_bg_color" >
such as :
android:layout_width="500dp"
android:layout_height="600dp"
You must try another numbers not the 500 and 600 ,,,, when you have the best size save it ^_^

Problems positioning with layout Inflate

I have a layout A composed by a set of 2 texts and one image followed by another set of 2 texts and one image (they are separated by a line). I want to inflate this layout (lets say A) and put it inside another one (lets say B). The problem is that the images and the separator are getting in the middle of B, while I wanted them in the middle of A. My code is:
Layout A (to be inflated):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="#drawable/bg_numeros"
android:orientation="vertical" >
<ImageView
android:id="#+id/separator"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/divisoria_numeros" />
<ImageView
android:id="#+id/artistsDoll"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/separator"
android:scaleType="fitCenter"
android:src="#drawable/boneco_artistas_numeros" />
<ImageView
android:id="#+id/songsDoll"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="#drawable/bonecos_numeros" />
<TextView
android:id="#+id/songsNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/artistsDoll"
android:layout_toLeftOf="#+id/songsDoll"
android:layout_toRightOf="#+id/separator"
android:text="blabla"
android:textColor="#333333"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/songsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/songsNumbers"
android:layout_toLeftOf="#+id/songsDoll"
android:layout_toRightOf="#+id/separator"
android:text="SOME SONGS"
android:textColor="#999999"
android:textSize="11dp" />
<TextView
android:id="#+id/artistsNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/artistsDoll"
android:layout_toLeftOf="#+id/artistsDoll"
android:text="blabla"
android:textColor="#333333"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/artistsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/artistsNumbers"
android:layout_toLeftOf="#+id/artistsDoll"
android:text="SOME ARTISTS"
android:textColor="#999999"
android:textSize="11dp" />
</RelativeLayout>
I am inserting the inflated layout like this:
RelativeLayout A = (RelativeLayout) Patterns.mainContext
.getLayoutInflater()
.inflate(R.layout.A, null);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, someTopLayout.getId());
B.addView(A, rlp);
Note that although the attributes android:layout_height="fill_parent" and android:layout_centerHorizontal="true" are set for the images, I expected them to centered in A, not B.
Any help??
The solution found here: How to use layoutinflator to add views at runtime?
Solved my problem.
RelativeLayout A = (RelativeLayout) Patterns.mainContext
.getLayoutInflater()
.inflate(R.layout.A, parentLayout, false);

Categories

Resources