1

I have created the below layout using Linear Layout , its running fine but i have one doubt that it can be optimized because i have created this UI using nested Linear Layout , i think its not well.

So Can you please help me to optimize this Linear Layout either by using Relative Layout or else ?

I have defined a layout like:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="6dip">

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dip">

        <TextView
            android:id="@+id/txtViewTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textSize="18dip"
            android:text="hello"
        />
        <TextView
            android:id="@+id/txtViewDescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textSize="14dip"
            android:text="hello"
            />
    </LinearLayout>       

     <ImageView
      android:id="@+id/image"
      android:layout_width="50dip"
      android:layout_height="fill_parent" 
      android:src="@drawable/arrow"
      android:layout_marginLeft="3dip"
      android:scaleType="center"/>

</LinearLayout>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • @PM:Can you explain your problem in detail, I mean what you want to do ??? – Sankar Ganesh PMP Feb 23 '11 at 08:55
  • explain your expected design, attach an image if possible. – Tushar Vengurlekar Feb 23 '11 at 08:59
  • @Tushar i dont want to change the design, but want to optimize the above code as i have taken nested Linear layout. – Paresh Mayani Feb 23 '11 at 09:01
  • 1
    @PM - PareshMayani: I do not see what is wrong here. For some complex layouts I had to have several levels of nesting Linear/Relative layouts and it worked fine and fast enough, even after I rotate the screen. But, if you like it can be solved with one RelativeLayout, just need to specify relationships (not circular!) among elements. – Zelimir Feb 23 '11 at 09:09
  • @Zelimir Thanx for the detailed help, but dear i am using the layout as a custom-layout for the item inside the listview, and suppose there are more number of items thats why i want to optimize it. I think i made clear now. Thanx – Paresh Mayani Feb 23 '11 at 09:11
  • This code is looking fine for me. Using relative layout etc would give this layout unneeded complexity. You can think about optimization in code.. – Vivienne Fosh Feb 23 '11 at 09:14
  • @Markiz Lonkly I dont have more experience to use Relative Layout but still I have tried enough to set this with Relative Layout to implement the same, but i failed to implement this. So please help me to implement the same using Relative Layout. Thanx – Paresh Mayani Feb 23 '11 at 09:18

5 Answers5

2

Here is a refactoring of your code using RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:padding="6dip">
    <ImageView android:layout_marginLeft="3dip" android:id="@+id/image"
        android:src="@drawable/arrow_cell"
        android:layout_width="50dip" android:scaleType="center"
        android:layout_alignParentRight="true" android:layout_height="wrap_content" android:layout_centerVertical="true"></ImageView>
    <TextView android:textSize="18dip" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/txtViewTitle"
        android:layout_marginLeft="10dip" android:text="hello"
        android:gravity="center_vertical" android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/image"></TextView>
    <TextView android:textSize="14dip" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/txtViewDescription"
        android:layout_marginLeft="10dip" android:text="hello2"
        android:gravity="center_vertical" android:layout_below="@+id/txtViewTitle"
        android:layout_toLeftOf="@+id/image"></TextView>
</RelativeLayout>  

As you said you want to use it as a row in a ListView, I changed the hight of the parent layout to "wrap_content".

Adinia
  • 3,722
  • 5
  • 40
  • 58
  • Not really..there are some more differences; did you tried your code in Eclipse before posting it? it still gives me errors, beside the one at the end you just edited. – Adinia Feb 23 '11 at 09:32
  • Thanx a lot for the support, but i am confused as here almost the same 3 answers are posted, so i have to gone through all the answers. I need your help to implement optimize one as i am seeing your above comment may gets right. – Paresh Mayani Feb 23 '11 at 09:37
  • 1
    @Tushar Vengurlekar Indeed, it is working, there was just the missing at the end. But still, you set some unnecessary layout_align properties(I suspect you just used drag-and-drop in Graphical Layout with the new ADT, maybe :) ?) – Adinia Feb 23 '11 at 09:45
1

You can try this but I dont think you need this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="6dip">


<TextView android:id="@+id/txtViewTitle" android:layout_width="250dip"
    android:layout_height="200dip" android:gravity="center_vertical"
    android:textSize="18dip" android:text="hello" />
<TextView android:id="@+id/txtViewDescription"
    android:layout_below="@+id/txtViewTitle" android:layout_width="250dip"
    android:layout_height="200dip" android:gravity="center_vertical"
    android:textSize="14dip" android:text="hello" />

<ImageView android:id="@+id/image" android:layout_width="50dip"
    android:layout_height="fill_parent" android:layout_marginLeft="3dip"
    android:scaleType="center" android:layout_alignParentRight="true" />  </RelativeLayout>
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
0

maybe you could use the relativelayout to replace the linearlayout,because for such an not very complicated UI,the relativelayout has the better performance.

kaosmys
  • 1
  • 1
  • @PareshMayani In the question, it says "Help me optimize using RelativeLayout or else" So the OP is clearly aware of it. Besides just saying its a good thing to try ("maybe"), it doesn't help the OP at all. I don't see it as an answer without a *lot* of fleshing out. Just my opinion of course. – BradleyDotNET Jul 19 '14 at 04:39
0

Hope this helps

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
android:padding="3dip">
<ImageView  android:id="@+id/image" 
android:layout_width="50dip" 
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="3dip" 
android:scaleType="center"
android:src="@drawable/arrow" 
android:gravity="center" />


<TextView android:id="@+id/txtViewDescription"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:textSize="14dip" 
android:text="hello"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/image"
/>

<TextView android:id="@+id/txtViewTitle" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:textSize="18dip" 
android:text="hello"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/image"
android:layout_above="@+id/txtViewDescription"
/>  

Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48
0
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
        android:layout_width="wrap_content"       
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtViewTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"           
        android:gravity="center_vertical"
        android:textSize="18dip"
        android:text="hello"
    />
    <TextView
        android:id="@+id/txtViewDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:textSize="14dip"
        android:text="hello"
        />      

 <ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="fill_parent" 
  android:src="@drawable/arrow"
  android:layout_marginLeft="3dip"
  android:scaleType="center"/>

</LinearLayout>
dcanh121
  • 4,665
  • 11
  • 37
  • 84