relative layout position - android

I have a vertical linearlayout, it has inside two linearlayout with diferent elements, I would like to fix the first one to the top, and the second one centered, I'm trying it but doesn't work:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_bg"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#3b5998"
>
elements
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="300dip"
android:layout_height="300dip"
android:background="#drawable/background_resto"
android:gravity="center"
>
elements
</LinearLayout>
</LinearLayout
Why is it not working? Thank you in advance

I'd advice to choose a RelativeLayout for that purpose.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/holo_blue_bright"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:background="#fff" >
</LinearLayout>
<LinearLayout
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_centerInParent="true"
android:background="#android:color/holo_green_light"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
This will help you to set the exact "relative" position in relation to other views.
Best wishes,
Tim

should use RelativeLayout instead of LinearLayout
<?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="#drawable/background_bg"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#3b5998"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
elements
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="300dip"
android:layout_height="300dip"
android:background="#drawable/background_resto"
android:layout_centerInParent="true"
>
elements
</LinearLayout>
</RelativeLayout>

<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:background="#ff0000" >
</LinearLayout>
<LinearLayout
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_centerInParent="true"
android:background="#ff0000"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>

Related

Displaying a Gridview and ViewPager in the same layout

I would like to display on the same page a viewpager and a gridview like this one ![What I would like :)][1]
[1]: http://i.stack.imgur.com/Xzg7T.jpg but what i get is that ![what I got][1]
[1]: http://i.stack.imgur.com/iZxwU.png .
There are my layouts.The Gridview Item
<?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" >
<ImageView
android:id="#+id/frag_Acc_gridView_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#215487"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo"/>
<TextView
android:id="#+id/frag_Acc_gridview_textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/frag_Acc_gridView_imageView"
android:layout_alignParentLeft="true"
android:background="#80000000"
android:gravity="center"
android:paddingLeft="15dp"
android:textColor="#ffffffff"
android:textSize="20sp"
android:text="#string/hello_world" />
</RelativeLayout>
GridView layout
<?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:orientation="vertical" >
<GridView
android:id="#+id/fragment_acc_gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
the main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_acc_viewpager"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="6">
</FrameLayout>
<FrameLayout
android:id="#+id/fragment_accueil_gridview"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="4"
>
</FrameLayout>
</LinearLayout>
thanks
Take a look of this. You will get some help:-
http://www.rogcg.com/blog/2013/10/20/working-with-fragments-and-viewpager-on-android

Text percentage

I put this code but the problem with the percentage gives me a warning, where shall I put them?
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="14">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3">
</RelativeLayout>
try as below...
<?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:orientation="vertical"
android:weightSum="20" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:background="#FF00FF" >
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="14"
android:background="#FFFF00" >
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:background="#00FFFF" >
</RelativeLayout>
</LinearLayout>

android:layout_weight with vertical linearlayout

I'm trying to create a android layout like:
[####EditText###][Button]
[####listview#####]
For that i'm using weight, but the following xml is giving me something very strang like:
[editText][button][ListView]
Someone know why?
<?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"
android:background="#drawable/background"
android:weightSum="10"
>
<EditText android:id="#+id/nova_lista"
android:inputType="text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="9"
android:hint="#string/new_list_hint">
</EditText>
<Button android:id="#+id/nova_lista_botao"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="#string/new_list"/>
<ListView
android:id="#+id/list_of_lists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You are using android:orientation="vertical"
and you are specifying android:layout_width equal to 0dp.
For vertical layout layout_height should be 0 dp and for Horizontal layout layout_width should be 0 dp if you are using weight in LinearLayout.
Put EditText and Button in separate Horizontal Layout. Use below 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"
android:background="#drawable/background"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText android:id="#+id/nova_lista"
android:inputType="text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="9"
android:hint="#string/new_list_hint">
</EditText>
<Button android:id="#+id/nova_lista_botao"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="#string/new_list"/>
</LinearLayout>
<ListView
android:id="#+id/list_of_lists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Maybe this is what you are looking for:
<?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"
android:background="#ffffff" >
<EditText
android:id="#+id/nova_lista"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toLeftOf="#id/nova_lista_botao"
android:inputType="text"
android:hint="something">
</EditText>
<Button
android:id="#+id/nova_lista_botao"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="Hello"/>
<ListView
android:id="#+id/list_of_lists"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/nova_lista" >
</ListView>
</RelativeLayout>
Hope that helps.
1.Set your main layout is RelativeLayout
2.Create LinarLayout and move EditText and Button to LinerLayout
3.Set LinerLayout is "#+id/top_layout" and set listview is
android:layout_below="#+id/top_layout"
<?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"
android:background="#drawable/background"
android:weightSum="10" >
<LinearLayout
android:id="#+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/nova_lista"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:hint="#string/new_list_hint"
android:inputType="text" >
</EditText>
<Button
android:id="#+id/nova_lista_botao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/new_list" />
</LinearLayout>
<ListView
android:id="#+id/list_of_lists"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_layout" >
</ListView>
</RelativeLayout>

Why LinearLayout looks like this (a white space between elements)?

This is the basic XML file
<?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"
android:background="#color/white"
>
<!--top part-->
<include layout="#layout/top" android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1"/>
<!--content area-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#ff6600"
>
</LinearLayout>
<!--bottom part-->
<include layout="#layout/bottom" android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1"/>
</LinearLayout>
When I load it in the emulator, it looks like on the image below.
What am I doing wrong?
EDIT
This is top and bottom XML. Basic as well.
<?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"
>
<ImageView
android:id="#+id/bottom_image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:src="#drawable/bottom"
/>
</LinearLayout>
-----------------------------------------------------------
<?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"
>
<ImageView
android:id="#+id/top_image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:src="#drawable/top"
/>
Please try following
Top.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/top"
>
<ImageView
android:id="#+id/top_image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:src="#drawable/top"
android:scaleType="fitXY"/>
</LinearLayout>
Bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom"
>
<ImageView
android:id="#+id/bottom_image"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:src="#drawable/bottom"
android:scaleType="fitXY"/>
</LinearLayout>
and finally Main.xml
<?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"
android:background="#000000"
>
<!--top part-->
<include layout="#layout/top" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!--content area-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff6600"
>
</LinearLayout>
<!--bottom part-->
<include layout="#layout/bottom" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This will solve your problem
remove the android:layout_weight from your top and bottom parts (and set it to anything you want, different than 0, in the content area). This way the content will take all the available space.
On what you did, you only told the content to take 5/7 of the available space, which will behave as you show depending on your screen proportions.
And on a real Smartphone? Is there a space, too?
Have you got the newest Version of the SDK Platform tools?
Check for layout_bottom and layout_top definition. Most probably these layouts have marginTop and marginBottom set.

How to set below layout in Android?

I want to do like this, which layout should i use? so that it should be compatible for all screen?
thank you
use LinearLayout Like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#color/blue"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="220px"
android:background="#color/gray"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="40px"
android:background="#color/blue"
android:layout_width="220px"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:background="#color/gray"
android:layout_width="fill_parent"
android:layout_height="380px"/>
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="20px"
android:background="#color/blue"
android:layout_width="fill_parent"
android:layout_height="380px"/>
</LinearLayout>
i hope you understand, if not let me know.
I'd use a RelativeLayout if I were you. Here's an example.
Make use of layout weight and for spacing just give layout margin
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="1" android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout03"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout android:id="#+id/LinearLayout04"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
</LinearLayout>

Categories

Resources