PeakGen PeakGen - 6 months ago 7
Android Question

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>

Answer

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>