Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, how to set imageview with percentage in relativelayout

I want to build a UI like this http://postimg.cc/image/fihidv1rv/ . Here is my xml code for it, for my design, I want "EFFECT" & "CAMERA" to combine as one ImageView like "SHOP" in the link so there will be total 5 ImageViews, and I set the id as they named in the link

the problem is, how can I set the height and width with percentage?

effect+camrea: height 25%,width 100%

collage: height 25%,width 50%

draw: height 25%,width 50%

photo: height 50%,width 50%

shop: height 25%,width 100%

<RelativeLayout android:id="@+id/mainContent" android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:background="#ffffff">
    <ImageView 
        android:id="@+id/img_effect+camera" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:src="@drawable/a" />
    <ImageView 
        android:id="@+id/img_collage" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:layout_below="@+id/img_effect+camera"
        android:src="@drawable/b" />
    <ImageView 
        android:id="@+id/img_draw" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/img_effect+camera" 
        android:layout_toRightOf="@+id/img_collage"
        android:src="@drawable/c" />
    <ImageView 
        android:id="@+id/img_photo" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/img_collage"
        android:layout_below="@+id/img_draw"
        android:src="@drawable/d" />
    <ImageView 
        android:id="@+id/img_shop" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:layout_below="@+id/img_photo"
        android:src="@drawable/e" />
</RelativeLayout>
like image 973
Kennett Avatar asked Jul 05 '13 04:07

Kennett


2 Answers

You can consider using android:layout_weight param in the layout http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight

<LinearLayout android:id="@+id/mainContent" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:background="#ffffff">
<!-- height 25% -->
<LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <ImageView 
        android:id="@+id/img_effect" 
        android:layout_width="0dp" 
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/a" />
    <ImageView 
        android:id="@+id/img_camera" 
        android:layout_width="0dp" 
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/a" />    
</LinearLayout>

<!-- height 50% -->
<LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="horizontal">

    <!-- width 50% -->

    <LinearLayout
        android:layout_width="0dp" 
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <!-- height 50%% -->
        <ImageView 
            android:id="@+id/img_collage" 
            android:layout_width="match_parent" 
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/a" />

        <!-- height 50%% -->    
        <ImageView 
            android:id="@+id/img_draw" 
            android:layout_width="match_parent" 
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/a" />    
    </LinearLayout>   

    <!-- width 50% -->
    <ImageView 
        android:id="@+id/img_photo" 
        android:layout_width="0dp" 
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:src="@drawable/b" />
</LinearLayout>
<!-- height 25%% -->
<ImageView 
    android:id="@+id/img_shop" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/e" />

like image 155
Ethan Avatar answered Oct 16 '22 05:10

Ethan


Its hard to it in Xml, you can do it by code doing some calculations. How to do:

  • Get mainContent height & width, which is your parent view.
  • GetLayoutparams of each imageVIew, and set height & width,by calculating percentage height & width

Sample Code: (in activity)

final View parentView= findViewById(R.id.mainContent);
final ImageView effect_camera= (ImageView)findViewById(R.id.img_effect+camera);

effect_camera.post(new Runnable(){
  @Override
  public void run(){
   int height=parentView.getHeight();
   int width=parentView.getWidth();


   RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams)effect_camera.getLayoutParams();
   int percentHeight=height*.25;
   int percentWidth= width*1;
   lp.height=percentHeight;
   lp.width=percentWidth;
   effect_camera.setLayoutParams(lp);
 }
});
like image 6
Sreejith B Naick Avatar answered Oct 16 '22 06:10

Sreejith B Naick