NoobieNoob
NoobieNoob

Reputation: 907

Linearlayout should not cut off

sorry for this newbie question. I have googled a while and cannot find an answer.

I have 10 buttons in a row. This looks ok on my tablet. But when I start the app on my phone only the first 5 buttons are shown, the rest is cut off.

If there is not enough space the remaining buttons should be displayed in a second row. How can I do that?

Thanks.

<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="horizontal"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="7"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="8"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="9"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10"
    />

Upvotes: 1

Views: 2019

Answers (4)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this way
<?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:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              tools:context=".MainActivity">

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"/>

    <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.2"
            android:text="10"/>
</LinearLayout>

Upvotes: 0

Dave
Dave

Reputation: 6104

You have a few options:

  1. Place all your buttons into a GridView - this will work across any screen size and is relatively straightforward and neat.

  2. Put them into a ScrollView. This is quite popular but a bit annoying for the user as it can lead to mis-clicks and they may not realise you can scroll the buttons.

  3. If you only ever want exactly 1 or 2 rows depending on screen size, put each row of buttons into a separate layout file. Then create 2 additoinal screen-dependent layouts containing LinearLayout which include both rows, but give the portrait version a vertical orientation. So when that layout is loaded, you get two rows.

  4. Work out if you can present the buttons in a different way - maybe using just icons, or combine them into dropdowns.

Upvotes: 0

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="7"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="8"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="9"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10"
    />
</LinearLayout>
</ScrollView>

Upvotes: 1

RobinHood
RobinHood

Reputation: 10969

Interesting! take scrollview parents of LinearLayout@

<ScrollView...
<LinearLayout...//android:orientation="horizontal"
<Button... // your all buttons
</Button>  
</LinearLayout>
</ScrollView>

Upvotes: 2

Related Questions