How to edit Layout to full length of screen? - android

i have an app which has a main RelativeLayout and some LinearLayout within it.
My problem is that the LinearLayout doesn't fit to the full lenght of the screen:
this is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bgApp" >
<LinearLayout
android:id="#+id/mainView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/topBarHeight"
android:background="#drawable/top"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#android:color/transparent" >
<ImageView
android:id="#+id/scoreLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coin_icon"
android:contentDescription="#string/app_name"/>
<TextView
android:id="#+id/mainScreenTotalPoints"
style="#style/pointTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/btnPaddingV"
android:text="#string/startPoint"
android:textSize="#dimen/buttonTextSize" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/logPadding"
android:text="#string/app_name"
android:textColor="#d0105e"
android:textSize="27sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="#dimen/btnPadding"
android:paddingRight="#dimen/btnPadding" >
<Button
android:id="#+id/play"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/button_blue"
android:gravity="center"
android:onClick="onButtonClickPlay"
android:text="#string/play"
android:textColor="#color/btnMain"
android:textSize="#dimen/buttonTextSize" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:orientation="vertical"
android:paddingTop="#dimen/mainbox" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/btn_how"
style="#style/btnCapital"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:layout_marginRight="#dimen/btnPaddingV"
android:layout_weight="1.0"
android:background="#drawable/button_green_how"
android:onClick="onButtonClickHowToPlay"
android:text="#string/howInfo" />
<Button
android:id="#+id/btn_rate"
style="#style/btnCapital"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:layout_marginLeft="#dimen/btnPaddingV"
android:layout_weight="1.0"
android:background="#drawable/button_rate"
android:onClick="onButtonClickRate"
android:text="#string/rateInfo" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/sound"
style="#style/btnCapital"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:layout_marginTop="1.0dip"
android:layout_weight="1.0"
android:background="#drawable/mute_01"
android:gravity="center"
android:onClick="onButtonClickSound"
android:text="#string/mute" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/moreapp"
style="#style/btnCapital"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:layout_marginTop="1.0dip"
android:layout_weight="1.0"
android:background="#drawable/button_blue"
android:gravity="center"
android:onClick="onButtonClickMoreApp"
android:text="#string/moreapp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
android:gravity="bottom" />
</LinearLayout>
</LinearLayout>
</ScrollView>
here is a screenshot :
Screenshot after running app
and here a screenshot of layout before running app:
Screenshot before running app
Thank you in advance !

You need to use the ScrollView attribute called android:fillViewport="true". This attribute causes the scroll view’s child to expand to the height of the ScrollView if needed.
When the child is taller than the ScrollView, the attribute has no effect.
Check out this for detail explanation.

You have to use attribute android:fillViewport="true".in your scrollview.

Related

How to avoid this Android Scrollview from going into infinite scroll?

This is my layout of the scrollView. It just scrolls infinitely. I need the scroll just for the elements that the user sees in the activity. I have tried all my efforts to it however, it didnt work out. Heres the layout..
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginLeft="-20dp"
android:layout_marginStart="-20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="100dp"
android:layout_marginStart="100dp"
android:gravity="center"
android:text="Create event" >
</Button>
</LinearLayout>
Set the height of the LinearLayout (id parentLayout) to wrap_content.
The ScrollView is set to wrap_content, but its child is set to fill_parent. This makes no sense, as something needs to provide a size.

Android App 50 percent width

I am trying to build a little Android App and I need two LinearLayout side by side. (50/50)
How it looks:
http://i.stack.imgur.com/C9yCF.png
I already try to use: android:layout_width="0dp" android:layout_weight="1" but it didn't work.
This is my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/part">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="9pt"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:baselineAligned="true"
android:background="#ff7f7f7f"
android:id="#+id/head">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="123456789"
android:id="#+id/textView"
android:paddingStart="3pt"
android:paddingLeft="3pt"
android:textColor="#ffffffff"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/left"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/head"
android:background="#ffff0100" >
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/head"
android:background="#fffcff00" />
How it should look:
http://i.stack.imgur.com/tup01.png
Have one more LinearLayout with horizontal orientation below your #+id/head LinearLayout.
Set its width to match_parent. Keep the last two LinearLayouts inside the new LinearLayout and have android:layout_weight=0.5 for both. Something like:
<RelativeLayout>
<LinearLayout> </LinearLayout>
<LinearLayout
...
android:orientation:"horizontal"/>
<LinearLayout
android:weight="0.5"
android:width="0dp"
... />
</LinearLayout>
<LinearLayout
android:weight="0.5"
android:width="0dp"
... />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
You can't use layout_weight with RelativeLayouts children. Put/Wrap both LinearLayouts in another LinearLayout.
Hey try this code...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/part"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="9pt"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff7f7f7f"
android:baselineAligned="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3pt"
android:paddingStart="3pt"
android:singleLine="false"
android:text="123456789"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/head"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff0100"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#fffcff00"
android:orientation="vertical" />
</LinearLayout>

Android: TextView not appearing below ListView

I checked out a similar question to this but it didn't solve my problem.
From top to bottom, I'm try to display:
a TextView (header)
a ListView with an index strip to the right of it (I use horizontal LinearLayout for this pair)
a TextView (footer)
Everything appears except my footer TextView.
Can somebody please tell me what is wrong with my XML?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tvHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:background="#00F"
android:textColor="#android:color/white"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
<ListView
android:id="#android:id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="#+id/sideIndex"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:background="#0F0"
android:textColor="#F00"
android:textStyle="bold" />
</LinearLayout>
Try this..
change android:layout_height="wrap_content" to android:layout_height="0dp" and add android:layout_weight = "1" for the middle LinearLayout
Change this..
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
to
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight = "1"
android:layout_height="0dp" >
LinearLayout
1) http://developer.android.com/reference/android/widget/LinearLayout.html
2) http://developer.android.com/guide/topics/ui/layout/linear.html
RelativeLayout
1) http://developer.android.com/reference/android/widget/RelativeLayout.html
2) http://developer.android.com/guide/topics/ui/layout/relative.html
Better you try RelativeLayout. Check this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tvHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00F"
android:text="TextView"
android:textColor="#android:color/white"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tvFooter"
android:layout_below="#+id/tvHeader"
android:orientation="horizontal" >
<ListView
android:id="#android:id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="#+id/sideIndex"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/tvFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#0F0"
android:text="TextView"
android:textColor="#F00"
android:textStyle="bold" />
</RelativeLayout>
add this attribute to your 2nd Linear layout.
android:layout_weight = "1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tvHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:background="#00F"
android:textColor="#android:color/white"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_weight = "1" <!-- here -->
>
.......
You could use a RelativeLayout
<?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" >
<TextView
android:id="#+id/tvHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:background="#00F"
android:textColor="#android:color/white"
android:textStyle="bold" />
<TextView
android:id="#+id/tvFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#0F0"
android:text="TextView"
android:textColor="#F00"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_below="#+id/tvHeader"
android:layout_above="#+id/tvFooter"
android:orientation="horizontal"
android:layout_height="wrap_content" >
<ListView
android:id="#android:id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="#+id/sideIndex"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Use RelativeLayout, it will be easier to show or reposition other items.

3 layouts - middle is flexible

I need the following thing:
3 layouts:
Header
Content
Footer
Header must be on the top footer must be sticked to the bottom (android:layout_alignParentBottom="true").
But how to make middle to occupy the whole other screen? Thanks.
Here is my solution (with android:layout_weight and ScrollView):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#DDDDDD"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"
tools:ignore="HardcodedText" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Content"
tools:ignore="HardcodedText" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#DDDDDD"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer"
tools:ignore="HardcodedText" />
</LinearLayout></LinearLayout>
And the result picture :
Hi Use the following lines
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Button" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/linearfooter"
android:layout_below="#+id/button1"
android:background="#android:color/darker_gray"
>
</LinearLayout>
<LinearLayout
android:id="#+id/linearfooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer" />
</LinearLayout>
</RelativeLayout>
hope this helps you.
Use a height of 0dp and
android:layout_weight
set to 1
Finally I've got the next solution:
To the middle layout I added:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:layout_weight="1">
And it works!

No widget shows after scrollView in android

My design is like this: "Linearlayout start" "scrollview start" "scrollview end" "LinearLayout start" "LinearLayout end" "LinearLayout end". But second linear layout does not show in the screen.
code:
<?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="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:fillViewport="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nasa" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/description" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button android:id="#+id/refresh" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/refresh">
</Button>
</LinearLayout>
</LinearLayout>
Thanks in advance
I found the solution. I need to add an extra attribute in ScrollView
<ScrollView android:layout_weight="1">
which prevent scroller to fill entire screen.
When your are using scrollView it needs to be as parent for the layout.
As per your code, follow the following code which was edited as per your requirement....
code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nasa" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/description" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button android:id="#+id/refresh" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/refresh">
</Button>
</LinearLayout>
</ScrollView>
Run the following code. It will execute...

Categories

Resources