Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make layout to be 80% of the width of the screen?

Tags:

java

android

xml

How can I make some layout to be for example 80% or 90% of the width of the screen, no matter what is the screen size or DPI? I tried with " but I get 80% of the height then.

EDIT:

    <?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:background="@drawable/background" >

    <Button
        android:id="@+id/bNivoi1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        android:background="@drawable/button_nivoi_back" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvOpis15Nivoa"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="15sp"
        android:layout_weight="0.8"
        android:text="text" />
    </LinearLayout>

</LinearLayout>

I only need textview to be 80% of the screen, not the rest of the activity.

like image 822
marjanbaz Avatar asked Jul 23 '13 18:07

marjanbaz


People also ask

How do you find the width of a layout?

If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0. Show activity on this post. Which onMeasure() call are you talking about?

What is Layout_weight and weightSum in android?

android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

What does layout weight do?

Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.


1 Answers

android:layout_weight is based on the android:orientation of your LinearLayout - if you want your layout to take up 80% of the width, you need a LinearLayout with an android:orientation="horizontal"

<LinearLayout
    android:id="@+id/your_outer_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" > <!-- note that vertical is the default -->

    <!-- Other elements here -->

    <LinearLayout
        android:id="@+id/eighty_percent_layout_holder
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">
        <View
            android:id="@+id/your_eighty_percent_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8" />
     </LinearLayout>

    <!-- Other elements here -->
</LinearLayout>
like image 197
ianhanniballake Avatar answered Nov 14 '22 23:11

ianhanniballake