Add a view that occupies the whole screen - android

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 ^_^

Related

RelativeLayout doesn't match parent's height

The relativelayout inside main layout won't fill the parent height when it's displayed on device. In fact i want the left-side layout with the yellow color to fill the row vertically.
here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/verses_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.ad.holyquran.extra.TextViewEx
android:id="#+id/textView_Verse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/versesRelativeLayout"
android:gravity="right"
android:layout_marginTop="-15dp"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:text="Text"
android:textSize="#dimen/versesTextSize"
android:textColor="#ff000000" />
<TextView
android:id="#+id/textView_VerseTranslation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView_Verse"
android:paddingBottom="5dp"
android:gravity="fill_horizontal"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#+id/versesRelativeLayout"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="Text Text Text Text Text Text"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/versesRelativeLayout"
android:layout_alignParentLeft="true"
android:background="#fff3f27f">
<RelativeLayout
android:id="#+id/verseNumberIcRL"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/versesImageView"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/verse_number_ic" />
<TextView
android:id="#+id/textView_verseNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/versesImageView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="280"
android:paddingBottom="28dp"
android:textSize="14sp"
android:textColor="#ff000000" />
</RelativeLayout>
</RelativeLayout>
Thanks for reading.
If you remove the line
android:layout_toRightOf="#+id/versesRelativeLayout"
It will stop being on the right. If you need the yellow as background, then in the main layout (which is the first relativeLayout) set it as the background using
android:background="#drawable\image"
You may need to put all the other elements inside the main RelativeLayout.
Hope this helps.
I couldn't see the end tag of the main RelativeLayout.
Just add the tag in layout versesRelativeLayout:
android:layout_alignBottom="#+id/textView_VerseTranslation"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/versesRelativeLayout"
android:layout_alignParentLeft="true"
android:background="#fff3f27f"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="false"
android:layout_alignBottom="#+id/textView_VerseTranslation"
>

Inflating RelativeLayout View into horizontal scroll view deletes layout settings

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.

Correct RelativeLayout usage please

I have a layout for a listview item and it's displayed incorrectly (book descr goes over the last update date). Can you please help me to fix?
If I set
android:layout_above="#+id/lastUpdatedDt"
to the bookDescr item the whole text disappears...
EDIT: I've updated sample according to comments
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_margin="10dp"
android:background="#drawable/big_card_details"
android:clickable="true"
android:focusable="false">
<CheckBox
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/starStyle"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/bookTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book title"
android:textSize="16sp"
android:layout_alignBaseline="#id/icon"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/icon" />
<TextView
android:id="#+id/bookSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="470k"
android:layout_alignBaseline="#id/icon"
android:textSize="16sp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/lastUpdatedDt"
android:layout_width="match_parent"
android:layout_height="26dip"
android:singleLine="true"
android:text="Updated 27.04.2014 17.10"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#id/icon" />
<TextView
android:id="#+id/bookDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="10sp"
android:text="here should be long description"
android:layout_alignEnd="#id/bookSize"
android:layout_below="#+id/bookTitle"
android:layout_marginTop="20dp"
android:layout_alignLeft="#+id/icon" />
<!-- this is the button that will trigger sliding of the expandable view -->
<ImageButton
android:id="#+id/expand_details_button"
android:src="#drawable/slide_button_details"
android:layout_alignTop="#id/lastUpdatedDt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_alignBaseline="#id/lastUpdatedDt"
android:layout_alignParentBottom="true"
android:layout_alignRight="#id/bookSize"
android:layout_alignEnd="#id/bookSize" />
</RelativeLayout>
<!-- this is the expandable view that is initially hidden and will slide out when the more button is pressed -->
<LinearLayout
android:id="#+id/details_expandable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
>
<!-- put whatever you want in the expandable view -->
<Button
android:layout_width="match_parent"
android:layout_weight="0.3"
android:drawableTop="#drawable/ic_action_read"
android:text="#string/ReadBook"
style="#style/SlideButtonTheme" />
<Button
android:layout_width="match_parent"
android:layout_weight="0.3"
android:drawableTop="#drawable/ic_action_open_browser"
style="#style/SlideButtonTheme"
android:text="#string/OpenInBrowser" />
</LinearLayout>
</LinearLayout>
Hope your aware that the child view in the RelativeLayout need to placed below/above any of the sibling views or aligned from the parent.
The problem here is that , lastUpdatedDt is aligned to parent bottom , since the parent RelativeLayout height is set to wrap_content height will be decided based on the child views , so that lastUpdatedDt is placed at the bottom depends on the parent height, which overlap the sibling view.
FIX : Just set the android:layout_below="#+id/bookDescription" for the lastUpdatedDt TextView , so that the lastUpdatedDt will always be placed bottom of the parent and below bookDescription TextView sibling.

android child layout not showing

I have a LinearLayout with three RelativeLayout inside. The first two children layouts show. The third one does not show. When I change the children from RelativeLayout to LinearLayouts, the third one still does not show. Any ideas on how to fix this? The one not showing contains the ImageView.
Here is the code:
<?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="60dp"
android:clickable="true"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/back1" >
<View
android:id="#+id/fract"
android:layout_width="60dp"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="#00a5e5"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<TextView
android:id="#+id/nume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/fract"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:singleLine="true"
android:text="58 apple"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="#+id/denom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/fract"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:singleLine="true"
android:text="46 orange"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/back2" >
<TextView
android:id="#+id/red_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:singleLine="true"
android:text="11 apple "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="#+id/red_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/red_1"
android:layout_centerHorizontal="true"
android:layout_marginRight="5dp"
android:ellipsize="end"
android:singleLine="true"
android:text="13 oranges "
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ffffff" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="#drawable/guess" />
</RelativeLayout>
</LinearLayout>
Your second Relative Layout's height is
android:layout_height="match_parent"
try putting:
android:layout_height="wrap_content"
--edit - try adding a weight attribute.
</RelativeLayout>
<RelativeLayout
android:weight=1
>
</RelativeLayout>
<RelativeLayout
android:weight=1>
</RelativeLayout>
android:layout_centerInParent="true" remove this from the view, if you want to add a view you can add separately out of the RelativeLayout with this your Layout is already in the center of the parent, For debug purpose set the width of each view to certain level so that you can see. Your two RelativeLayout already took the width of the Linear Layout, so no space left for the third Layout.
I decide to do it programmatically. I get the width of the device using context.getResources().getDisplayMetrics().widthPixels. Then from there I layout my pieces pixels by pixels, so to speak.

adding elements to scrollview using java when there is more then 1 child

I am trying to add elements to my scrollview,
this scroll view has a vertical linear layout inside of it.
(i added another linear layout - horizontl, inside that will manage 2 textviews and an image according to the attributes i assigned them),
i am trying to add elements of this inner layout to my scroll view (id=insideLineLayout)
(each element added to scrollview will contain these 2 texts and an image)
because of the fact scrollview can only have 1 child i am unable to do this.
would appreciate some help, nothing seems to be working here.
Thanks.
my current xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchScreenOuterLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/catagoryselectedText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="selected catagory"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/searchText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/search_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="5dp"
android:text="Search Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/radiusBar"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<ScrollView
android:id="#+id/searchScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/searchScrollViewLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dp" >
<LinearLayout
android:id="#+id/insideLineLayout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="100"
>
<TextView
android:id="#+id/linesPrice"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="tmp1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/linesInfo"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="tmp2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageInfo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#drawable/tv_icon" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Consider how long your list can get as ListView has been optimized in many ways which a regular scroll view is not. If you have too many elements you might start get out of memory exceptions.
In order to dynamically add views to a ScrollView you first need to get the container view inside the ScrollView (most likely a LinearLayout), then you simply do following for each view you add:
View view = inflater.inflate(R.layout.view_to_add, container, false); // this is used if you create from xml, if you make it in code you'd use the regular constructor.
// update the view content (view.findViewById(); etc.)
container.addView(view);

Categories

Resources