How to put 50% for width

13,076

Solution 1

Write following code to do that in both views inside LinearLayout.

android:layout_width="0dp"
layout_weight="1"

Solution 2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" 
    android:weightSum="100"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/logo_c"
        android:layout_width="0dp" 
        android:layout_weight="50" 
        android:layout_height="wrap_content"
        android:src="@drawable/logo_animado" />

    <ImageView
        android:id="@+id/logo_t" 
        android:layout_height="wrap_content" 
        android:layout_width="0dp"    
        android:layout_weight="50" 
        android:src="@drawable/logo_animado2" />


</LinearLayout>

Solution 3

Use android:weightSum and android:layout_weight

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:weightSum="2"
        android:orientation="horizontal" >


            <ImageView
                android:id="@+id/logo_c"
                android:layout_width="0dp" android:layout_weight="1" 
                android:layout_height="wrap_content"
                android:src="@drawable/logo_animado" />

            <ImageView
                android:id="@+id/logo_t"
                android:layout_width="0dp" android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/logo_animado2" />


    </LinearLayout>

Solution 4

Add android:layout_weight="1" in both the ImageView and make the width if image view as fill_parent I think it will solve your problem

But remember it will stretch your image as because image view will streach in every screen resolution so as your image.

Share:
13,076
Guilherme Gregores
Author by

Guilherme Gregores

#SOreadytohelp SOreadytohelp

Updated on July 22, 2022

Comments

  • Guilherme Gregores
    Guilherme Gregores almost 2 years

    I have a LinearLayout in horizontal orientation and 2 ImageView and I want to make that ImagesView fill 50% of the screen on width, to work in every cellphone or tablet with diferent sizes.

    Something like this:

     +-------------+   
     |_____________|     
     |      |      |  
     | 50%  | 50%  |  
     |      |      |   
     |-------------|
     |             |
     +-------------+
    

    Best so far:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >
    
    
            <ImageView
                android:id="@+id/logo_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo_animado" />
    
            <ImageView
                android:id="@+id/logo_t"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/logo_animado2" />
    
    
    </LinearLayout>
    
  • Guilherme Gregores
    Guilherme Gregores about 11 years
    Thanks Chintan that was very helpful =D.
  • Chintan Rathod
    Chintan Rathod about 11 years
    @Guilherme its my pleasure... :-)