-2

Please consider the code given code. How to make these image heights are of 1/3 of screen size. if i use it with an adapter. Please refer the image

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           android:paddingTop="0dip" android:layout_gravity="top"
         >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"    
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingTop="5dp" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="left|top"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
            <ImageView
                android:id="@+id/imaget"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|top"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

</TableLayout>
Glorfindel
  • 21,988
  • 13
  • 81
  • 109

3 Answers3

0

You can get the width and height of the screen and set one parameter for both layout_width and layout_height of button. How??

Display display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).
                                      getDefaultDisplay();
    int width=display.getWidth();
    int height=display.getHeight();

//Calculate width and height

Say For screen 240*320

buttonWidth=width/5;     result: buttonWidht=48;
 buttonHeight=height/10   result: buttonHeight=32;

Using LayoutParams object, set width and height to the button.

Now, if you run this code on device having screen size 320*480

 buttonWidth=width/5;     result: buttonWidht=64;
 buttonHeight=height/10   result: buttonHeight=48;

and so on for other devices too.

Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

Maybe look at the SAME QUESTION you posted an hour ago that has been answered?

Is it possible to Adjust height of listview with respect to screen size when it is used with adapter

Community
  • 1
  • 1
yedidyak
  • 1,964
  • 13
  • 26
0

Try this.

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int density = metrics.densityDpi;

        int width = 0, height = 0;
        metrics = new DisplayMetrics();        
         getWindowManager().getDefaultDisplay().getMetrics(metrics);

         //height is actual screen height
         height = metrics.heightPixels;     
         width = metrics.widthPixels;


         CustomAdapter mAdapter;
         //pass h to your adapter class and it will gives you the height as per you need
         h=height/3;
         mAdapter = new CustomAdapter(this,ImageArrayList1, ImageArrayList2,h);
cracker
  • 4,900
  • 3
  • 23
  • 41