0

I want to resize buttons if the screen is big . So I have done it using this method . I got screen size and put button width screenWidth/4 . But now I can't put buttons position . I used relative layout and I put their position from xml but when I change their sizes the are lying each on other at 0,0 position. tihs is on nexus S this is on nexus 10

Here is XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/pink"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vahe_muradyan.yourquote.MainActivity" >

    <Button
        android:id="@+id/openCamera"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/camera" />

    <Button
        android:id="@+id/openGallery"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/openDefaults"
        android:layout_below="@+id/openDefaults"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/openDefaults"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/openCamera"
        android:layout_centerVertical="true"
        android:background="@drawable/roundbuttom"
        android:text="@string/defaults" />

</RelativeLayout>

I have upploaded images on Nexus S and nexus 10 .

5
  • make your buttons match parent, why do you need to resize them? this is not iphone - android has very good resizing techniques, with both relative and linear layout. Can you post the xml of your buttons? Commented Jul 13, 2014 at 16:35
  • If I give them match parent they would be big Commented Jul 13, 2014 at 16:37
  • How are you setting their positions? and what do you want the layout to look like? Commented Jul 13, 2014 at 16:43
  • what about wrap content? Commented Jul 13, 2014 at 18:24
  • wrap content would be small on 10 inch Commented Jul 13, 2014 at 18:26

1 Answer 1

1

Add dimensions per screen size

you need to create in your res folder new folders named

values-sw600dp

and in the normal values folder create an xml file called

dimens.xml

in that file write

<dimen name="button_width">70dp</dimen>
<dimen name="button_height>70dp</dimen>
<dimen name="button_text_size">14sp</dimen>

in your values-sw600dp create another dimens.xml file

in it write

<dimen name="button_width">140dp</dimen>
<dimen name="button_height>140dp</dimen>
<dimen name="button_text_size">20sp</dimen>

//play with the sizes to find the good fit

in your xml file write this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/pink"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vahe_muradyan.yourquote.MainActivity" >

    <Button
        android:id="@+id/openCamera"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:background="@drawable/roundbuttom"
        android:textSize="@dimen/button_text_size"
        android:text="@string/camera" />

    <Button
        android:id="@+id/openGallery"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openDefaults"
        android:layout_below="@+id/openDefaults"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/openDefaults"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openCamera"
        android:layout_centerVertical="true"
        android:background="@drawable/roundbuttom"
        android:text="@string/defaults" />

</RelativeLayout>

The system will choose the correct values based on the device you run the app on.

The buttons will be big enough (providing you give good values in values-sw600dp/dimens.xml file)

This is the folder tree: enter image description here

have fun!

Sign up to request clarification or add additional context in comments.

3 Comments

I will try it but why the folder name is sw600dp?
7 inch tablets 10 inch tablets, devices that were made in mid 2013 and devices that were made in 2014. (2014 devices are mostly xhdpi and xxhdpi even though they are phone sized) however, if you don't create a sw720dp folder, your sw600dp will be used . the term sw600dp means screen width **at least ** 600dp
so sw720dp would mean "at least 720dp screen width"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.