Center text in TextView and Button - android

I am trying to center the text in my TextView and Button. I currently have the following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="#string/CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:src="#drawable/coupon" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="#string/continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
However, the text in the Button and TextView are to the right and hence some of the text is off the screen... I am also getting the following warning for my LinearLayout:
This LinearLayout layout or its RelativeLayout parent is possibly useless
Any help would be greatly appreciated!!

Try this
[<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.15"
android:gravity="center"
android:hint="asdasasdasdd"
android:text="adasdasdasd"
android:textAlignment="gravity"
android:textColor="#FFffff"
android:textSize="30sp"
android:typeface="monospace" />]

Try this,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/couponImage"
android:layout_width="wrap_content"
android:layout_weight="0.7"
android:layout_height="wrap_content"
android:src="#drawable/coupon" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:gravity="center"
android:text="continueButton"
android:textSize="30sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="CouponFinder"
android:textColor="#FF0000"
android:textSize="30sp"
android:typeface="monospace" />
</RelativeLayout>

Try this..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:src="#drawable/icon" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>

You should make the RelativeLayout a LinearLayout, and get rid of the internal LinearLayout, unless you plan to make modifications and add more things inside the RelativeLayout and outside of the LinearLayout.
Try setting the layout-gravity on the items instead of gravity.
Hope this helps :)

Try this way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:gravity="center"
android:text="CouponFinder"
android:textColor="#FF0000"
android:textSize="30sp"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:src="#drawable/icon" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:gravity="center"
android:text="continueButton"
android:textSize="30sp" />
</LinearLayout>

The warning is because you are using only one LinearLayout as the Child View for RelativeLayout.So remove your RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:gravity="center"
android:text="#string/CouponFinder"
android:textSize="30sp"
android:textColor="#FF0000"
android:typeface="monospace" />
<ImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:text="#string/CouponFinder" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_weight="0.15"
android:layout_height="0dp"
android:text="#string/continueButton"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
Try the above data.I got this by the above data

What you've done is correct, but to see the changes, you just need to restart your IDE. I found the answer here.

Related

How to place two Seeksbars at one place?

Here what I am doing is , I want to show the seekbar running in between the RangeSeekBar (between the selected range). For that, I want to place these two seekbars at one position.
I don't know how to do that. I have tried RelativeLayout and FrameLayout but nothing happened.
Thank you.
activity_play.xml
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<SeekBar
android:id="#+id/pSeekBar"
android:layout_width="match_parent"
android:layout_height="37dp" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/screen_background_light"
android:orientation="vertical"
android:padding="10dip">
<TextView
android:id="#+id/selectedfile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="Not file selected"
android:textColor="#android:color/black" />
<com.yahoo.mobile.client.android.util.rangeseekbar.RangeSeekBar
android:id="#+id/rangeSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:paddingBottom="10dip"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/screen_background_light"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="#+id/MinEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_media_previous" />
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_media_play" />
<ImageButton
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_media_next" />
<EditText
android:id="#+id/MaxEdiText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
u can use this library its hard to write it yourself
https://github.com/syedowaisali/crystal-range-seekbar

Why layout_weight and weightSum aren't working for me?

I have a small question about linearlayouts.
Let's say i have, 1 horizontal linearlayout that contains 6 vertical linearlayouts.
I can make all linearslayouts the same width size if i use weightsum=12(of horizontal ll) and layout_weight=2(for vertical ll)
Result :
Now i want the "test1" layout to be 2 or 3 times smaller than the others.
I tried with weightsum=11, and layout_weigth=1(for the test1 layout) and layout_weight=2(for the others). I can't make it work, here is my code...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#drawable/border"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test"
/>
</LinearLayout>
</LinearLayout>
How can i do it?
Thank you.
You need to use : android:layout_width="0dp" for the Linearlayout with the android:layout_weight="1" set as 1.
Now i want the "test1" layout to be 2 or 3 times smaller than the
others
You can reduce the layout weight attribute of test1. to 0.5..
use this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="6">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test1" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Output

Android centering linear layout

I read this: How to align linearlayout to vertical center?
But it didn't help me
Basically I am trying to center 2 ImageButtons in the middle of my screen. The two buttons are horizontally orientated in a linearLayout.
I tried to use a relativeLayout with vertical orientation to center the two imagebuttons in the center in the vertical direction but that doesnt seem to do the job.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
The two buttons keep sticking to the top button bar
Put
android:layout_height="match_parent"
to the RelativeLayout,
and replace android:layout_gravity="center" with
android:layout_centerInParent="true"
in its child LinearLayout
Make it like -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout> </LinearLayout>
your question seems bit unclear but hope this tip will help you
android:layout_gravity
android:layout_gravity is used to set the position of an element in its parent (e.g. a child View inside a Layout).
Supported by LinearLayout and FrameLayout
android:gravity
android:gravity is used to set the position of content inside an element (e.g. a text inside a TextView).
to get and idea copy this XML and understand how it works
<?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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="left"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorPrimary"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorPrimary"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorPrimary"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorAccent"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorAccent"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorAccent"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="right"
android:gravity="center_vertical">
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/first"
android:background="#color/colorPrimaryDark"
android:gravity="left"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/second"
android:background="#color/colorPrimaryDark"
android:gravity="center"/>
<TextView
android:layout_width="#dimen/fixed"
android:layout_height="wrap_content"
android:text="#string/third"
android:background="#color/colorPrimaryDark"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
Make sure all parent layouts of Button's have set android:layout_height="match_parent"
Then apply android:layout_centerVertical="true" to the RelativeLayout.
Try this way it will work
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#android:color/white"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#ffffff"
android:textSize="16sp"
android:background="#ff00"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_gravity="center">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#ffffff"
android:src="#mipmap/ic_launcher" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#ffffff"
android:src="#mipmap/ic_launcher"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Change your layout like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/white"
tools:context="com.example.max.testcase.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:padding="5sp"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/white"
android:textSize="16sp"
android:background="#color/blue"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:adjustViewBounds="true"
android:layout_gravity="left"
android:gravity="center_horizontal"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/white"
android:src="#drawable/icon1" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:padding="20dp"
android:paddingTop="50dp"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/white"
android:src="#drawable/icon2"
/>
</LinearLayout>
</RelativeLayout>
add layout_gravity and gravity attribute in root linear layout with value center vertical it will make all his child align in center vertical.
Note: make child height wrap_content, to achieve this. if your child height is match_parent than this will not work.
A simple layout design for center two image button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loadSomething"
android:layout_margin="20dp"
android:textAlignment="center"
android:text="Click here to load the site"
android:textColor="#color/WhiteSmoke"
android:textSize="16sp"
android:background="#color/Blue"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/icon1"
android:onClick="method1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:adjustViewBounds="true"
android:scaleType = "fitCenter"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:background="#color/WhiteSmoke"
android:src="#mipmap/minus_icon" />
<ImageButton
android:id="#+id/icon2"
android:onClick="method2"
android:layout_width="0dp"
android:layout_weight= "1"
android:layout_height="wrap_content"
android:padding="20dp"
android:adjustViewBounds="true"
android:maxWidth="350dp"
android:maxHeight="350dp"
android:scaleType="fitCenter"
android:background="#color/WhiteSmoke"
android:src="#mipmap/plus_icon"
/>
</LinearLayout>
</RelativeLayout>

ListView overlaps textview and background image in android

I am trying to design layout but its not rendering properly. Background logo is not visible at all. When phone goes to landscape mode then textview below listview is not visible.
Below is my xml.
<ScrollView 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:background="#E0ECF8"
android:fillViewport="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.view.MainMenuActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E0ECF8" >
<LinearLayout
android:id="#+id/note1Wrapper_note1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="start"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2_note1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="#string/note1"
android:textColor="#000"
android:textSize="13sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#0c2d4e"
android:src="#drawable/banner640" />
<TextView
android:id="#+id/textView1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="#string/menu_title"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/View1_hr"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/textView1_title"
android:layout_marginTop="15dp"
android:background="#80000000" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/View1_hr"
android:layout_marginTop="0dp"
android:background="#E0ECF8" >
</ListView>
<View
android:id="#+id/View2_hr"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#android:id/list"
android:layout_marginTop="5dp"
android:background="#80000000" />
</RelativeLayout>
</ScrollView>
You problem may come from using ListView inside ScrollView. You should avoid it. To overcome your problem, I think you can set a specific height for your ListView or using a vertical LinearLayout to add row views programmatically.
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead. - from Romain Guy - the developer who write ListView in GG.
ok the problem is there with your definition, just define your LinearLayout below View (which is belowListView). check this
<RelativeLayout 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:background="#E0ECF8"
android:fillViewport="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.view.MainMenuActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#0c2d4e"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="MEnu"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/View1_hr"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/textView1_title"
android:layout_marginTop="15dp"
android:background="#80000000" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/View1_hr"
android:layout_marginTop="0dp"
android:background="#E0ECF8" >
</ListView>
<LinearLayout
android:id="#+id/note1Wrapper_note1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#E0ECF8"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2_note1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="Nofghnjfgjtyjjyjdtyjte"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</RelativeLayout>
output :
May be that is what you need?
UPDATE:
<ScrollView 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:background="#E0ECF8"
android:fillViewport="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.view.MainMenuActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E0ECF8" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/View1_hr"
android:layout_marginTop="0dp"
android:background="#E0ECF8"
android:layout_above="#+id/note1Wrapper_note1">
</ListView>
<LinearLayout
android:id="#+id/note1Wrapper_note1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="start"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2_note1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="#string/note1"
android:textColor="#000"
android:textSize="13sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#0c2d4e"
android:src="#drawable/banner640" />
<TextView
android:id="#+id/textView1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="#string/menu_title"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/View1_hr"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/textView1_title"
android:layout_marginTop="15dp"
android:background="#80000000" />
<View
android:id="#+id/View2_hr"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#android:id/list"
android:layout_marginTop="5dp"
android:background="#80000000" />
</RelativeLayout>
</ScrollView>

XML Android layout problems

So I am having problems with some XML layout coding... I am supposed to replicate this image
but so far I seem to be only able to get this far and the checkout button refuses to stay to the right even when I do use android:gravity="right" to float right within its container... :/
This is the code I have so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".CartActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Shopping Cart"
android:background="#android:color/holo_blue_dark" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/holo_blue_light"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtotal:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Checkout" >
</Button>
</LinearLayout>
</LinearLayout>
use a android:weightSum="3" for your inner
Layout with the three elements, and after that,
android:layout_weight="1" to each view
this will make the views to hold 1/3 of the available space
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/holo_blue_light"
android:weightSum="3"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtotal:"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???"
android:layout_weight="1" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Checkout"
android:layout_weight="1">
</Button>
</LinearLayout>
EDIT fill_parent is deprecated use match_parent
Try to use a Relativelayout
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Subtotal:" />
<TextView
android:id="#+id/textView2"
android:layout_toRightOf="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Checkout" >
</Button>
</RelativeLayout>

Categories

Resources