4

I have Some layout Problem regarding my Application. This is My Application Layout:

<?xml version="1.0" encoding="utf-8"?>

<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:src="@drawable/tax_calculator_logo"/>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="vertical" android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp" android:layout_gravity="center" 
    android:layout_marginTop="10dp" android:layout_marginBottom="10dp">

    <Button android:id="@+id/contactUs" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Contact Us"
        android:textSize="20dp"/>
    <Button android:id="@+id/contactUs" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Contact Us"
        android:textSize="20dp"/>   
    <Button android:id="@+id/contactUs" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Contact Us"
        android:textSize="20dp"/>   
    <Button android:id="@+id/contactUs" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Contact Us"
        android:textSize="20dp"/>   
</LinearLayout>

Here, I want to set this all button to be complete fit with the height of the Screen of any device. Yes i can show all the Button. but i want them all to be have equat space between them and they must be fit complete to the Screen height. So what should i have to do for that ??

Edited:

And there is, My tax_calculator_logo is of 600x239 resolution and i have set the ImageView height as wrap_content and width with fill_parent then why the Image is croping from bottom and top ???

2 Answers 2

13

Give equal weight to each of your buttons, they will share height automatically.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical" >

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

</LinearLayout>
4
  • Wow. .. Its Work great. Thanks. Will you please give me some idea about weight ? I mean how i have use it ? and What it will do ??? Oct 3, 2011 at 10:06
  • please See my Edited Question also. . . Oct 3, 2011 at 10:55
  • Please Help me regarding the Edited Question also. Oct 3, 2011 at 11:23
  • use android:scaleType="fitXY", for your imageView may be. Oct 3, 2011 at 11:26
2

To do this we have to get he display(screen) width and height.Then through java code assign the height. For exmple

The xml file is like this

<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:src="@drawable/tax_calculator_logo"/>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" android:layout_gravity="center" 
android:layout_marginTop="10dp" android:layout_marginBottom="10dp">

<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   

In the Activity class

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

after setContentView(..);

 imageview.getLayoutParams().height=height;
 button1.getLayoutParams().height=height;
 button2.getLayoutParams().height=height;
 button3.getLayoutParams().height=height;
 button4.getLayoutParams().height=height;

I think this may help you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.