PeakGen
PeakGen

Reputation: 23025

Making the ImageView at least 50% of the screen

Please have a look at the below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:id="@+id/image_background"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/mmm"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"/>

    <LinearLayout
        android:id="@+id/l22Parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/image_background"
        android:orientation="horizontal">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/step18button1"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:background="@drawable/button_border"
            android:layout_below="@+id/step18textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/step18button2"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignTop="@+id/step18button1"
            android:layout_toRightOf="@+id/step18button1"
            android:layout_toEndOf="@+id/step18button1"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="3"
            android:id="@+id/step18button3"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button2"
            android:layout_toRightOf="@+id/step18button2"
            android:layout_toEndOf="@+id/step18button2"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/step18button4"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button3"
            android:layout_toRightOf="@+id/step18button3"
            android:layout_toEndOf="@+id/step18button3"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="5"
            android:id="@+id/step18button5"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button4"
            android:layout_toRightOf="@+id/step18button4"
            android:layout_toEndOf="@+id/step18button4"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="6"
            android:id="@+id/step18button6"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button5"
            android:layout_toRightOf="@+id/step18button5"
            android:layout_toEndOf="@+id/step18button5"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/step18button7"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button6"
            android:layout_toRightOf="@+id/step18button6"
            android:layout_toEndOf="@+id/step18button6"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="8"
            android:id="@+id/step18button8"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button7"
            android:layout_toRightOf="@+id/step18button7"
            android:layout_toEndOf="@+id/step18button7"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="9"
            android:id="@+id/step18button9"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button8"
            android:layout_toRightOf="@+id/step18button8"
            android:layout_toEndOf="@+id/step18button8"
            android:background="@drawable/button_border"/>
    </LinearLayout>
</RelativeLayout>

The UI generated by the above is below enter image description here

However I wanted the image to take at least 50% of the screen. The layout doesn't matter, any layout is fine, however in any mobile device it should be around 50% of the screen (50% is a rough value, it can be 60% etc). How to achieve my requirement?

The image I use can be found here - http://iespressolife.com/wp-content/uploads/2016/03/Malabar-InHouse-9423_1450_906px-1024x640.jpg

UPDATE

User "Arpit Patel"s answer is correct but it was incomplete. Below is the correct version.

<?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="@android:color/white">


    <ImageView
        android:id="@+id/image_background"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_weight="2"
        android:adjustViewBounds="true"
        android:background="@drawable/mmm" />

    <LinearLayout
        android:id="@+id/l22Parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_background"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/step18button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/step18textView"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:text="1"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/step18button1"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button1"
            android:layout_toRightOf="@+id/step18button1"
            android:layout_weight="1"
            android:text="2"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button2"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button2"
            android:layout_toRightOf="@+id/step18button2"
            android:layout_weight="1"
            android:text="3"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button3"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button3"
            android:layout_toRightOf="@+id/step18button3"
            android:layout_weight="1"
            android:text="4"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button5"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button4"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button4"
            android:layout_toRightOf="@+id/step18button4"
            android:layout_weight="1"
            android:text="5"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button6"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button5"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button5"
            android:layout_toRightOf="@+id/step18button5"
            android:layout_weight="1"
            android:text="6"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button7"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button6"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button6"
            android:layout_toRightOf="@+id/step18button6"
            android:layout_weight="1"
            android:text="7"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button8"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button7"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button7"
            android:layout_toRightOf="@+id/step18button7"
            android:layout_weight="1"
            android:text="8"
            android:textSize="13dp" />

        <Button
            android:id="@+id/step18button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step18button8"
            android:layout_marginLeft="3dp"
            android:layout_toEndOf="@+id/step18button8"
            android:layout_toRightOf="@+id/step18button8"
            android:layout_weight="1"
            android:text="9"
            android:textSize="13dp" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Views: 93

Answers (4)

Arpit Patel
Arpit Patel

Reputation: 8057

See with proof 50% of screen covered by image.

enter image description here

<ImageView
    android:id="@+id/image_background"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:background="@drawable/a" />

<LinearLayout
    android:id="@+id/l22Parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image_background"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/step18button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/step18textView"
        android:layout_marginLeft="3dp"
        android:layout_weight="1"
        android:text="1"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/step18button1"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button1"
        android:layout_toRightOf="@+id/step18button1"
        android:layout_weight="1"
        android:text="2"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button2"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button2"
        android:layout_toRightOf="@+id/step18button2"
        android:layout_weight="1"
        android:text="3"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button3"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button3"
        android:layout_toRightOf="@+id/step18button3"
        android:layout_weight="1"
        android:text="4"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button4"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button4"
        android:layout_toRightOf="@+id/step18button4"
        android:layout_weight="1"
        android:text="5"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button5"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button5"
        android:layout_toRightOf="@+id/step18button5"
        android:layout_weight="1"
        android:text="6"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button6"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button6"
        android:layout_toRightOf="@+id/step18button6"
        android:layout_weight="1"
        android:text="7"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button7"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button7"
        android:layout_toRightOf="@+id/step18button7"
        android:layout_weight="1"
        android:text="8"
        android:textSize="13dp" />

    <Button
        android:id="@+id/step18button9"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="39dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/step18button8"
        android:layout_marginLeft="3dp"
        android:layout_toEndOf="@+id/step18button8"
        android:layout_toRightOf="@+id/step18button8"
        android:layout_weight="1"
        android:text="9"
        android:textSize="13dp" />
</LinearLayout>

Upvotes: 1

Muhammad Waleed
Muhammad Waleed

Reputation: 2601

try this xml code.

<?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="2"
    android:background="@android:color/white">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_weight="1"
        android:src="@drawable/mmm"
        android:id="@+id/image_background"
        android:adjustViewBounds="true"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:id="@+id/l22Parent"
       android:layout_marginTop="10dp"
       android:layout_below="@+id/image_background">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/step18button1"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:background="@drawable/button_border"
            android:layout_below="@+id/step18textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/step18button2"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignTop="@+id/step18button1"
            android:layout_toRightOf="@+id/step18button1"
            android:layout_toEndOf="@+id/step18button1"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="3"
            android:id="@+id/step18button3"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button2"
            android:layout_toRightOf="@+id/step18button2"
            android:layout_toEndOf="@+id/step18button2"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/step18button4"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button3"
            android:layout_toRightOf="@+id/step18button3"
            android:layout_toEndOf="@+id/step18button3"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="5"
            android:id="@+id/step18button5"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button4"
            android:layout_toRightOf="@+id/step18button4"
            android:layout_toEndOf="@+id/step18button4"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="6"
            android:id="@+id/step18button6"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button5"
            android:layout_toRightOf="@+id/step18button5"
            android:layout_toEndOf="@+id/step18button5"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/step18button7"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button6"
            android:layout_toRightOf="@+id/step18button6"
            android:layout_toEndOf="@+id/step18button6"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="8"
            android:id="@+id/step18button8"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button7"
            android:layout_toRightOf="@+id/step18button7"
            android:layout_toEndOf="@+id/step18button7"
            android:background="@drawable/button_border"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="39dp"
            android:layout_height="wrap_content"
            android:text="9"
            android:id="@+id/step18button9"
            android:textSize="13dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/step18button8"
            android:layout_toRightOf="@+id/step18button8"
            android:layout_toEndOf="@+id/step18button8"
            android:background="@drawable/button_border"/>
    </LinearLayout>
</LinearLayout>

Upvotes: 1

yennsarah
yennsarah

Reputation: 5517

Pin the LinearLayout at the bottom and tell your ImageView to appear above it. You'll need to delete the layout_below attribute, of course.

<RelativeLayout>
    <ImageView 
        layout:above_layout="@+id/yourLinearLayout"/>
    <LinearLayout
        android:alignParentBottom="true"/>
</RelativeLayout>

You can tell the ImageView to center itself in the space, crop it etc. Also, you can give your ´LinearLayout` a top margin to become bigger.

Upvotes: 0

user6363583
user6363583

Reputation: 104

Just set

android:weight="0.5"

Both inside your ImageView and LinearLayout tag. You will get your image fill with half of your screen and everything others which have inside the linear layout will take other half of the screen.

Upvotes: 0

Related Questions