1. Check out our app, Forums for Android! Download and leave feedback here!

LinearLayout usage

Discussion in 'Application Development' started by joao.sauer, Aug 11, 2010.

  1. joao.sauer

    joao.sauer Member
    Thread Starter
    5

    Aug 11, 2010
    8
    0
    5
    Hello Guys!

    It's my first post here and I hope that it will not be the last one! :)

    First of all, I already programmed for android, but using absoluteLayout (it's so simple! :p ), now, I'm moving to dynamic positions for obvious reasons (We don't have just few screen sizes now and the future Android's "ipad" are starting to appear with huge screens).

    So, my concern here is to understand and work with LinearLayouts. I tried to use the RelativeLayout, but everybody told me to start with the LinearLayouts, because it's simple.

    What I just want to do it to put some texts in the screen, together with a border that I'm doing(this border will be in the entire screen.)
    The problem is that the LinearLayout is adding some texts to OUTSIDE of the screen, and this I cannot understand why. Why the LinearLayout cannot just keep the elements in the screen size, even if will be very small sizes.
    take a look:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     android:background = "@color/White">
    8.  
    9.     <ImageView android:layout_height="wrap_content" android:minHeight="2px" android:maxHeight="2px" android:layout_gravity= "top" android:src = "@drawable/horizontal_bar" android:scaleType = "fitXY" android:layout_weight="1" android:layout_width="fill_parent"/>
    10.      <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
    11.             <ImageView android:src = "@drawable/vertical_bar" android:minWidth="2px" android:maxWidth="2px" android:scaleType = "fitXY" android:layout_gravity= "left" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
    12.             <TextView  android:text="testing" android:layout_height="fill_parent"  android:layout_width="fill_parent" android:paddingLeft="15px"    android:textStyle="bold" android:textColor="@color/grey"    android:textSize="20px" android:layout_weight="1" android:layout_gravity="center_horizontal|center"/>
    13.              <TextView  android:text="blablabla" android:layout_height="fill_parent"  android:layout_width="fill_parent" android:paddingLeft="15px"    android:textStyle="bold" android:textColor="@color/grey"    android:textSize="20px" android:layout_weight="1" android:layout_gravity="center_horizontal|center"/>
    14.             <TextView  android:text="0000" android:id="@+id/number" android:layout_height="fill_parent"  android:layout_width="fill_parent" android:layout_gravity="left" android:textStyle="bold" android:textColor="@color/green" android:textSize="20px" android:layout_weight="1"/>
    15.             <ImageView android:src = "@drawable/vertical_bar" android:minWidth="2px" android:maxWidth="2px" android:scaleType = "fitXY" android:layout_gravity= "right" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
    16.     </LinearLayout>
    17.     <ImageView android:layout_height="wrap_content" android:minHeight="2px" android:maxHeight="2px" android:layout_gravity= "top" android:src = "@drawable/horizontal_bar" android:scaleType = "fitXY" android:layout_weight="1" android:layout_width="fill_parent"/>
    18. </LinearLayout>
    where horizontal_bar is a image with 6x4 and vertical_bar is a image with 4x6.
    This image needs to be stretch to be able to became a border.

    Any idea why the LinearLayout is going out of the screen size?

    Thanks a lot for your help,
    Joao
     

    Advertisement

  2. blundell

    blundell Well-Known Member
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    Read the first 2 lines of your post.

    Absolute layout is deprecated, don't ever use it ever again pls k thnx
     
  3. blundell

    blundell Well-Known Member
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    Ok read the rest now.

    Omg your linear layout is horrible :p

    For what you are attempting to achieve even tho everyone said start with linear,

    I would make a framelayout with your borders in and then a linearlayout with everything else inside.

    Basically a framelayout dumps everything on top of each other. so your border will be at the back then the linearlayout will be on top. Linearly (one after the other) showing whatever items you put in it
     
  4. joao.sauer

    joao.sauer Member
    Thread Starter
    5

    Aug 11, 2010
    8
    0
    5
    Hi,

    Maybe I was not clear in my question.
    I'm not understanding why the size of the elements are going to outside of the screen.
    How can I ""say" that I want to only use the physical screen? Doesn't matter what I add, it will always be inside of the screen.

    Thanks,
    Joao
     
  5. blundell

    blundell Well-Known Member
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    Hmm I don't think you can. Just think if you had 50 EditText boxes, how would Android force them to show all on one screen? They would have to be drawn off the bottom and as a side note to see them all you would wrap the linearlayout in a scrollview
     
  6. joao.sauer

    joao.sauer Member
    Thread Starter
    5

    Aug 11, 2010
    8
    0
    5
    Actually,

    I was expecting the screen being divided in 50 parts, in the example that you gave me.
    I know that this means that I will not be able to use them. But once that I'm not using the ScrollView, I was expecting this approach.
    Right now, without using the ScrollView, I'm not able to see the elements out of the screen, so, there are no reason to have it.
    Anyway, about the code above:
    The text is very small, it's possible to show the entire 3 parts of text in the screen. But the division made by eclipse is increasing the size of the first word in double. So, the text "testing" is using a space like "testingtesting". And this is causing the other words to be out of the screen.
    Any clue why this is happening?

    PS: Thanks for your help!

    Thanks,
    Joao
     
  7. blundell

    blundell Well-Known Member
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    I would advise you explain with screen shots. As your code is impossible to read, I would recommend setting layouts like:
    Code (Text):
    1.  
    2. <LinearLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res/com.blundell"
    5.     android:orientation="vertical"
    6.     android:layout_width="fill_parent"
    7.     android:layout_height="fill_parent"
    8.     android:gravity="center"
    9.     >
    10.     <Button
    11.         android:id="@+id/open"
    12.         android:layout_width="fill_parent"
    13.         android:layout_height="wrap_content"
    14.         android:text="Take Another Pic"
    15.     />
    16.     <com.admob.android.ads.AdView  
    17.       android:id="@+id/ad3"
    18.       android:layout_width="fill_parent"
    19.       android:layout_height="wrap_content"
    20.       app:backgroundColor="#000000"
    21.       app:primaryTextColor="#FFFFFF"
    22.       app:secondaryTextColor="#CCCCCC"
    23.       app:keywords="Toy Fun"
    24.     />
    25.     <View
    26.         android:layout_height="20dip"
    27.         android:layout_width="fill_parent"
    28.         android:background="#000000" />
    29.     <com.admob.android.ads.AdView  
    30.       android:id="@+id/ad4"
    31.       android:layout_width="fill_parent"
    32.       android:layout_height="wrap_content"
    33.       app:backgroundColor="#000000"
    34.       app:primaryTextColor="#FFFFFF"
    35.       app:secondaryTextColor="#CCCCCC"
    36.       app:keywords="Picture Photo Image"
    37.     />
    38. </LinearLayout>
    39.  
    atleast then you can read what element is doing what, without unlimited scrolling.

    Finally I will try an helpyou fix your problem if you do these things :) but the simpler answer is to used a Framelayout like I said:
    Code (Text):
    1.  
    2. <FrameLayout>
    3.     <LinearLayout>
    4.        <ImageView
    5.           android:id="topBorder"
    6.           />
    7.        <ImageView
    8.           android:id="bottomBorder"
    9.         />
    10.        <ImageView
    11.           android:id="leftBorder"
    12.         />
    13.        <ImageView
    14.           android:id="rightBorder"
    15.         />
    16.      </LinearLayout>
    17.      <LinearLayout>
    18.         <TextView  android:text="testing" />
    19.         <TextView  android:text="testing2" />
    20.     </LinearLayout>
    21. </FrameLayout>
    22.  
    Looks prettier as well :)
     
  8. cp1

    cp1 Well-Known Member
    53

    Apr 30, 2010
    375
    35
    53
    All your layout heights are fill parent -- is that intentional?
     
  9. hossaindoula

    hossaindoula New Member
    5

    Jun 18, 2011
    1
    0
    5
    May be this will help you... Java Guru...
     

Share This Page

Loading...