Vahe Muradyan
Vahe Muradyan

Reputation: 1115

Android Java Buttons coordinates

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 .

Upvotes: 0

Views: 140

Answers (1)

Lena Bru
Lena Bru

Reputation: 13947

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!

Upvotes: 1

Related Questions