I have three views that are part of a list view cell.
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/postArea"
android:layout_below="#+id/date"
android:layout_alignLeft="#+id/date"
android:layout_alignStart="#+id/date"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="5dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date"
android:layout_alignLeft="#+id/date"
android:layout_alignStart="#+id/date"
android:id="#+id/shareView">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/shareView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/commentsView">
</RelativeLayout>
</RelativeLayout>
In some cells "shareView" is set to shareView.setVisibility(View.GONE);
When this is the case I need to change commentsView so that it is below postArea instead.
Its there a way to do this within my getView method.
When you set your shareView's visibility to View.GONE, you should change the layout_below rule for your commentsView. Like this:
RelativeLayout commentsView = (RelativeLayout)findViewById(R.id.commentsView);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)commentsView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.postArea);
commentsView.requestLayout();
Related
RelativeLayout in Android Studio
i have a horizontal LinearLayout in which i have a RelativeLayout and i want another RelativeLayout to be below it and likewise throughout the xml file but the Layout is appearing at the immediate right of the previous Layout
i have tried to set the top margin of the RelativeLayout to a certain random value e.g 60sp it positions below the previous RelativeLayout but when i add TextView the TextView Does Not Show up in the design view
After adding textview to RelativeLayout
How do i position the Layout below the previous RelativeLayout ? and also how do i add TextViews to this RelativeLayout and display them ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/calcbackground"
android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="53dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="SHIFT"
android:id="#+id/shifttextiew"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="DEG"
android:id="#+id/degtextview"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/shifttextiew"
android:layout_toEndOf="#+id/shifttextiew"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RAD"
android:id="#+id/radtextview"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/degtextview"
android:layout_toEndOf="#+id/degtextview"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="GRAD"
android:id="#+id/gradtextview"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/radtextview"
android:layout_toEndOf="#+id/radtextview"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12345678910"
android:id="#+id/calculationtextview"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/shifttextiew"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60sp">
</RelativeLayout>
</LinearLayout>
You told it to be at the right here: android:orientation="horizontal".
Make your LinearLayout vertical instead.
I see your code you don't add any orientation on root layout.if you don't add any orientation then it will work on horizontal.Please add the below code on your root layout.Hope it will work.
android:orientation="vertical"
make to seprate layout and using include layout tag place both layout according to your need
changes required in topmost viewgroup(LinearLayout)
android:orientation="vertical"
instead of
android:orientation="horizontal"
use orientation="vertical" property for your parent Linear layout
Sorry for the long question..
I have Relativelayout of elements I am adding dynamically into Linearlayout of Horizontalscrollview.
When I see the layout the preview seems great, but when I am adding it programmatically it ruins the view.. the Text moved to another locations on the layout, and the padding is ignored..
This is the Relativelayout element deal_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:id="#+id/deal_layout"
>
<ImageView
android:layout_width="310dp"
android:layout_height="130dp"
android:src="#drawable/deal_square"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="deals background" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:id="#+id/from_to_deals_txt"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="from "
android:textAppearance="#style/from_to_deals"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Source"
android:textAppearance="#style/source_deals"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" to "
android:textAppearance="#style/from_to_deals"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="destination"
android:textAppearance="#style/dst_deals"
android:layout_below="#id/from_to_deals_txt"
android:layout_alignRight="#id/from_to_deals_txt"
android:paddingTop="10dp"
android:id="#+id/dst_deals_txt"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1099 X"
android:textAppearance="#style/price_deals"
android:layout_below="#id/dst_deals_txt"
android:layout_alignRight="#id/from_to_deals_txt"
android:layout_alignEnd="#id/from_to_deals_txt"
android:paddingTop="10dp"
android:id="#+id/price_deal_txt"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/ic_launcher"
android:background="#000000"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
/>
</RelativeLayout>
This is the Horizontal scroll view:
<?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="fill_parent"
android:background="#drawable/bg"
android:padding="10dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dealsView"
android:paddingTop="10dp"
>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I am adding the Relativelayout elements with this java function:
LinearLayout dealsView;
dealsView = (LinearLayout)findViewById(R.id.dealsView);
for(int i=0;i<5;i++)
{
LayoutInflater mInflater ;
mInflater = LayoutInflater.from(this);
View cur_deal = mInflater.inflate(R.layout.deal_layout, null);
dealsView.addView(cur_deal);
}
In the preview it looks like this:
On the application it looks like this:
Does anyone has idea what am I doing wrong?
When I am adding the elements with copy paste to the linearlayout it seems great, and the margin is works fine, only from the code it not works..
Thanks!
Instead of passing null to the inflater, pass the Views intended parent (dealsView) so that the LayoutParams from the root can be used to inflate.
View cur_deal = mInflater.inflate(R.layout.deal_layout, dealsView, false);
I believe the issue is caused by all of your Views using width="wrap_content" but without the inflater knowing the root it cannot properly inflate that value.
I'm looking for a solution on how to add dynamic relative layout on a parent relative layout after a pattern for dynamic layout.
For example the parrent rl have 2 other rl. Those rl I want to add them dynamically
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/providerServiceRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/dynamic1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="96dp"
android:layout_marginTop="123dp" >
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zzzz" />
<TextView
android:id="#+id/section_label44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/section_label"
android:text="kkkkk" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dynamic1"
android:layout_marginLeft="96dp" >
<TextView
android:id="#+id/section_label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zzzz122" />
<TextView
android:id="#+id/section_label448"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/section_label2"
android:text="kkkkk" />
</RelativeLayout>
</RelativeLayout>
Something like TableRow, but with the row split into two horizontal rows (I've tried that without success).
Or any other idea will be great.
try this;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.zzzzRL);
for(int i=0;i<2;i++){
RelativeLayout rlDynamic=new RelativeLayout(this);
rl.addView(rlDynamic);
}
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 ^_^
Why my TextView doesn't go right?
Update: Well, now I don't just need to set TextView to the right. Now it is very interesting why layout_gravity doesn't work as expected, namely - set the View to the position inside it parent container.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
style="#style/activated_item"
android:orientation="horizontal">
<CheckBox
android:id="#+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
style="?android:attr/starStyle"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="8dp"
android:layout_gravity="right" //////// HERE I AM
android:textStyle="bold"
android:textColor="#color/item_text_color"/>
</LinearLayout>
Try to use gravity instead of layout_gravity.
I have found 2 solutions to that:
Set android:orientation="vertical".
Use FrameLayout instead of LinearLayout as the parent for your text/checkbox widgets. It is not primary usage of FrameLayout but that advice is also noted in Android documentation:
... You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
Because you set orientation to horizontal, which means you cannot manually change horizontal position of your child views.
try this...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
style="#style/activated_item"
android:orientation="horizontal">
<CheckBox
android:id="#+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
style="?android:attr/starStyle"/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="40dp"
android:gravity="right" //////// HERE I AM
android:textStyle="bold"
android:textColor="#color/item_text_color"/>
you could use weight also try this one
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/star"
style="?android:attr/starStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_weight="1" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:text="TEST"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
the gravity is just to center the text ,what makes it go right is the weight