admob ads overlapping textview - android

I'm trying to add an Admob banner (at the top) to a the main activity (my main app screen), but I face tiny problem which is the admob ad overlapping textview
this is my main activity xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adView"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_top_margin"
tools:context="${relativePackage}.${activityClass}" >
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-xxxxxxxxx/xxxxxx"
ads:adSize="BANNER"/>
<LinearLayout
android:id="#+id/linearLayoutHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- First header line -->
<TextView android:id="#+id/header_priority"
android:layout_width="31dp"
android:layout_height="wrap_content"
android:textColor="#00F"
android:gravity="center"
android:textSize="#dimen/font_size_tiny"
android:text="#string/pri_label"
/>
<!-- First header line -->
<TextView android:id="#+id/header_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00F"
android:textSize="#dimen/font_size_tiny"
android:text="#string/desc_label"
/>
</LinearLayout>
<ListView
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llPriority"
android:layout_below="#+id/linearLayoutHeader" >
</ListView>
<LinearLayout
android:id="#+id/llPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/etPriority"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/hint_priority"
android:inputType="number"
android:text="#string/num_1" />
<EditText
android:id="#+id/tvItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="#string/add_hint">
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAdd"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:onClick="onClickAddItem"
android:text="#string/add_button_label" />
</LinearLayout>
</RelativeLayout>

Yes, RelativeLayout allows overlapp, but within RelativeLayout you can arrange the alignments of the view elements, too. Just figure out the id of the view element that overlaps with your adview and add the following attribute to your AdView (I am assuming it's overlapping with linearLayoutHeader)
android:layout_above="#+id/linearLayoutHeader"
You (or someone else that works on the same project?) actually already did make use of these attributes to arrange the views. Study the for example the following part of the code ;-)
<ListView
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llPriority"
android:layout_below="#+id/linearLayoutHeader" >
</ListView>

That's because the root layout is a relativelayout which allows overlap, the other layouts you use are linear which are meant for stacking controls.

Related

alignParentBottom appears Offscreen in Fragment with Scroll View

For those wanting to have an adView in their app that is comprised of Fragment layouts, hope the below helps you!
Basically, I was originally trying to place the adView inside the Layout XML for each fragment. This caused the adView to either be pushed off the screen or not play nice with the relative layout commands (ex. alignParentBottom).
The solution was to move the adView into the Main Activity Layout outside of the the Coordinator Layout used for my fragments. Then I wrapped the Coordinator Layout and adView in a Relative Layout.
This then allowed me to fully control the adView and present on each fragment pinned to the bottom of the screen.
I have found that when I change the background of your ScrollView, it paints OVER the AdView. You MUST place your AdView below the ScrollView in code. That way, it is drawn AFTER the ScrollView and is therefore on top.
Here is your edited code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/dummyLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" />
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="4"
android:hint="#string/hint"
android:nextFocusLeft="#id/editText1"
android:nextFocusUp="#id/editText1"
android:singleLine="true"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linearLayout1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView4"/>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button1"
android:background="#color/colorAccent0"
android:stretchColumns="*"/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tableLayout1"
android:layout_above="#+id/adView"
android:background="#color/colorGray">
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"/>
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

Android layout objects placement

I have this simple activity in my android app but I don't know how to place my button so it appears at the same position on various screen sizes.
I want the button to always be placed at the bottom of the screen. The screenshot shown is from a phone screen. When displayed on a larger screen the button is higher up.
How can I fix this?
Thanks
See this screenshot
You can use RelativeLayout with this parameter (android:layout_alignParentBottom="true")
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Click Me!"/>
</RelativeLayout>
You should use a RelativeLayout
If you have a RelativeLayout that fills the whole screen you should be able to use android:layout_alignParentBottom to move the button to the bottom of the screen.
If your button at the bottom doesn't show then probably the layout above it takes all the space. In this case you can put the button first in your layout file and position the rest of the layout above the views with android:layout_above.
Have a look a RelativeLayout. Use this ViewGroup and add a Button inside it with alignParentBottom
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentBottom="true"
android:text="HORAIRE"
android:layout_margins="<your_margin_in_dp>" />
</RelativeLayout>
Make footerButton.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="HORAIRE"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
And add it in all your layout file like below line:
Android Activity_Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Footer aligned to bottom -->
<include layout="#layout/footer" />
<!-- Content above footer -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content goes here"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
Hope it will solve your problem
This is what I ended up doing. Thanks to everyone who posted, it helped me get in the right direction!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context="xxxx.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dernières nouvelles Twitter"
android:id="#+id/textView"
android:textStyle="bold"
android:textSize="17dp"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:layout_alignParentTop="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/listView_tweets"
android:layout_above="#+id/buttonDisplay"
android:choiceMode="none"
android:paddingTop="25dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Horaire"
android:layout_margin="5dp"
android:id="#+id/buttonDisplay"
android:onClick="buttonGotoDisplay"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

How would I use a ScrollView inside a FrameLayout?

I'm having a lot of trouble getting my ScrollView to function properly. I'm using a Master-Detail design. The Master scrolls, since it's a listView. The Detail when selected will display the scrollbar implemented in my XML for a couple seconds when the Activity is inflated before disappearing (Stackoverflow won't allow me to post the screenshot of the error since this is my first post)
When I try to scroll the Detail Activity with touch input nothing happens. I've tried copying and pasting Mel's solution to what seems like an identical problem in the post at ScrollView doesn't work. I've also tried every conceivable combination of match_parent and fill_parent for the parent, child, and ScrollView itself. Any help is appreciated. My XML code is below:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="true"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_marginTop="20dp"
android:stretchColumns="1, 2"
>
<ImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/bmp1"
android:layout_alignParentTop="true"
/>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginTop="20dp"
android:stretchColumns="1, 2">
<!-- Table Row 0 -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tableRow0">
<TextView
android:id="#+id/priWeapon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priWeapon" android:textColor="#000" android:textStyle="bold"
android:gravity="left"
android:padding="5dp"/>
<EditText
android:id="#+id/priWeaponEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NA" android:textSize="14sp" android:textStyle="bold"
android:gravity="center"
android:focusable="false"
android:layout_weight="1"
android:cursorVisible="false"
android:longClickable="false"/>
</TableRow>
<!-- Table Row 1 -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tableRow1">
<TextView
android:id="#+id/priWeaponRange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priMaxRange" android:textColor="#000"
android:gravity="left"
android:padding="5dp"/>
<EditText
android:id="#+id/priWeaponRangeEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NA" android:textSize="14sp"
android:gravity="center"
android:focusable="false"
android:layout_weight="1"
android:cursorVisible="false"
android:longClickable="false"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
I found a solution! Instead of trying to apply the formatting to the Activity XML itself, I cut and pasted it into one of the Activity's Fragment XMLs, which didn't have a frameLayout. Because of this change it now scrolls perfectly while maintaining the title bar on top throughout the scroll, exactly the behavior that I wanted. I've posted the final code below. If you have a solution that would still allow me to use a scrollView inside a FrameLayout though I'd be more than happy to see it for future reference. Thank you for your help!
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="true"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_marginTop="20dp"
android:stretchColumns="1, 2"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textIsSelectable="true"
tools:context=".ItemDetailFragment"/>
<ImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/bmp1"
android:layout_alignParentTop="true"
/>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginTop="20dp"
android:stretchColumns="1, 2">
<!-- Table Row 0 -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tableRow0">
<TextView
android:id="#+id/priWeapon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priWeapon" android:textColor="#000" android:textStyle="bold"
android:gravity="left"
android:padding="5dp"/>
<EditText
android:id="#+id/priWeaponEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NA" android:textSize="14sp" android:textStyle="bold"
android:gravity="center"
android:focusable="false"
android:layout_weight="1"
android:cursorVisible="false"
android:longClickable="false"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
Your goal is to use the maximal screen size for the ImageView and then scroll down to the (at first invisible) TableLayout? Thats afaik not possible. When you set android:layout_height="fill_parent" on ImageView it uses the the maximum possible (in your example all of the screen size) space. So no TableLayout is rendered and there is no need for scrolling. You should set the android:layout_height="wrap_content" in the ImageView.
Some notes on the source code:
Use match_parent instead of fill_parent. It's renamed since API Level 8
You have a useless android:layout_below="#id/picture" in the TableLayout. May an artifact from a previous version with RelativeLayout?
Remove the namespace definitions in the TableLayout

ScrollView starts from tabbar, but needs to scroll from top of the layout

I have an issue over Scroll bar in my layout.
My layout contains a scrollbar which contains two layouts. The first layout has static height. And the second latout contains tabHost for tabgroup. Second layout also contains Expandable Gridview.
The issue is, when I load my layout, It shows Tabbar directly and hides the first layout. We can scroll But initially I need to show the top layout instead of tabHost. Can anyone help me to slove this issue?
Thanks in Advance...
I have added my code snippet from xml file.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true" >
<include
android:id="#+id/cover"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/coverphoto" />
<ImageView
android:id="#+id/ximg_logo"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_below="#+id/cover"
android:layout_marginLeft="16dip"
android:layout_marginTop="-42dip" />
<TextView
android:id="#+id/xtxt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ximg_logo"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#+id/ximg_logo"
android:ellipsize="end"
android:ems="5"
android:singleLine="true"
android:textColor="#color/sixty_black"
android:textSize="20sp" >
<requestFocus />
</TextView>
<Button
android:id="#+id/xbtn_follow"
style="#style/follow_button_normal"
android:layout_width="95dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/cover"
android:layout_marginRight="7dip"
android:background="#drawable/buttons_shape_fill_orange"
android:text="#string/str_follow" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cover"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/fifty_black"
android:orientation="vertical"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:weightSum="3" >
<RelativeLayout
android:id="#+id/xlay_following"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentTop="true"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:onClick="following" >
<TextView
android:id="#+id/xtxt_imfollowing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/xtxt_imfollowing_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/xtxt_imfollowing"
android:layout_centerHorizontal="true"
android:padding="5dip"
android:text="#string/str_im_following"
android:textColor="#color/white"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/xlay_follower"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:onClick="follower" >
<TextView
android:id="#+id/xtxt_myfollower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/xtxt_myfollowers_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/xtxt_myfollower"
android:layout_centerHorizontal="true"
android:padding="5dip"
android:text="#string/str_myfollowers"
android:textColor="#color/white"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/xlay_interests"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:layout_below="#+id/xlay_follower"
android:layout_centerHorizontal="true"
android:layout_weight="1" >
<TextView
android:id="#+id/xtxt_interests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/xtxt_interests_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/xtxt_interests"
android:layout_centerHorizontal="true"
android:paddingBottom="5dip"
android:text="#string/str_interest"
android:textColor="#color/white"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/xrl_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/fifty_black"
android:padding="10dip" >
<ImageView
android:id="#+id/ximg_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_msg" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relay2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relay1"
android:layout_marginTop="10dip" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip" >
</TabWidget>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#color/orange" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Of course, the RelativeLayout rl1 overlaps with rl2. They're both match_parent on layout_width. You need to align both layouts with left/right or top/bottom using android:layout_alignParentTop or android:layout_alignParentLeft="true".
However you cannot use it for RelativeLayout. It is used for the UI elements inside RelativeLayout or LinearLayout.
Since you did not post the full layout file with your tab elements or any UI, I cannot give you any xml example.
I have a similar issue and one way that works for my layout is to change the height and visibility like below (using your layout schema as a good example):
<RelativeLayout
android:id="#+id/relay1"
android:layout_width="match_parent"
android:focusable="true"
android:layout_height="0dp"
android:visibility="gone"
>
Notice the android:layout_height and visibility. Of course I know you don't want your first layout to be absolutely hidden always. But this is the idea, and you can do the same with your second layout relay2.
Here's a sample code to hide/show a layout, dynamically. Object relayLayout is your relay1 view.
if "hide the layout" {
relayLayout.setVisibility(LinearLayout.GONE);
}
else {
relayLayout.setVisibility(LinearLayout.VISIBLE);
}
Notice setVisibility and the valid constants.
Now, sample code to set the height of a view. This may be the only code you need and may actually work better.
ViewGroup.LayoutParams layoutParams = relayLayout.getLayoutParams();
layoutParams.height = 0;
relayLayout.setLayoutParams(layoutParams);
Notice ViewGroup.LayoutParams and height attribute, mainly. In this code, the layout will be hidden and the second layout should rise to the top.
Another solution and possibly the best is the Google's answer to show/hide 2 different layouts. In your layout file, I see 2 very different layout designs and so this is applicable in your case.
One webpage stating this is Building a Dynamic UI with Fragments.
It's good reading.
Here's a sample code, making this idea work:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
The sample code is from Fragments.
One advantage of this code and layout design is that your 2 layouts are separated in 2 files, at least, and forces separation for future code changes.
Have fun with this :-) Tommy Kwee.

ListView hidden behind button

The contents of my list view get hidden behind the button as follows:
The xml file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:text="Send"
android:id="#+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addItems"
>
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_toLeftOf="#id/Button"
android:layout_height="wrap_content"
>
</EditText>
</RelativeLayout>
</RelativeLayout>
The TextView for each row is as follows:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content
android:layout_gravity="right"
android:gravity="right"
android:textSize="20sp"
/>
What should i do to align it properly (as in above the button)?
Also when the listView is with just one or two entries, and when the keyboard is opened to type, the whole view shifts? How do I fix that as well? Thanks in advance
Cleaned up and corected the code a bit. Perhaps this is what you were looking for. Let me know if it works. Cheers !
<?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="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" />
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toLeftOf="#id/Button" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/InnerRelativeLayout" />
</RelativeLayout>
adding android:layout_above="#+id/InnerRelativeLayout" to your ListView.
RelativeLayout has child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
For put your InnerRelativeLayout layout at Top add android:layout_alignParentTop="true" to your InnerRelativeLayout and remove android:layout_alignParentBottom="true"
Update: set up your Layout like below:
<?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="fill_parent"
android:background="#color/Beige "
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" >
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/Button"
android:hint="Enter text" >
</EditText>
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/InnerRelativeLayout"
android:drawSelectorOnTop="false" />
</RelativeLayout>
Output:
use android:layout_above="#+id/InnerRelativeLayout" in your listview
Make the outer RelativeLayout into a LinearLayout with orientation vertical. Then, for the ListView set layout_height as 0dp and layout_weight as 1. The ListView will fill up all the remaining space in the LinearLayout without overlapping other views.
Also, stop using fill_parent, it's been deprecated and you should use match_parent instead.

Categories

Resources