0

I actually have fixed margin_Top, which is 50dp. But when I tested with different size of screens, the imageview will not be in the same position as each other. Therefore, I searched something about how to adjust the dimen.xml in values. But I have no idea how to edit, create new dimen.xml, and calculate the size in different screens.

<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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:layout_centerInParent="true"
  tools:context="com.example.mygames.MainActivity$PlaceholderFragment">

<ProgressBar

    android:id="@+id/circularProgressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="350dp"
    android:layout_height="750dp"
    android:indeterminate="false"
    android:max="100"
    android:progress="50"
    android:progressDrawable="@drawable/circular"
    android:secondaryProgress="100"
    android:layout_marginBottom="110dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@drawable/whitecircle"
    android:id="@+id/imageView3"
    android:layout_alignTop="@+id/tv"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp" />

<TextView
    android:id="@+id/tv"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:gravity="center"
    android:text="25%"
    android:textColor="#ffffff"
    android:textSize="35sp"
    android:textStyle="bold"
    android:layout_marginBottom="57dp"
    android:layout_alignBottom="@+id/circularProgressbar"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:textSize="20sp"
    android:text="Progress" />


</RelativeLayout>

My dimen.xml:

<resources>
 <!-- Default screen margins, per the Android Design guidelines. -->
 <dimen name="activity_horizontal_margin">16dp</dimen>
 <dimen name="activity_vertical_margin">16dp</dimen>
 <dimen name="fab_margin">16dp</dimen>
 <dimen name="appbar_padding_top">8dp</dimen>
</resources>
androidnewbie
  • 374
  • 1
  • 6
  • 23

1 Answers1

0

This is done in a different way. Like you can put a layout for landscape mode in a folder layout-land you can put values files (like dimens.xml) in different folders too. Example is the values-w820p folder which is created in a default project. the -wxxxx is the screen dimension (for tablets and the like).

So you just need copies of your dimens.xml in different values folders, each containing the values for the screen size.

I am not native english, I hope you understand what I mean.

Android will automatically pick the file which fits the currently running device best.

Read through the resource features here: https://developer.android.com/guide/topics/resources/providing-resources.html

Here is a simple example:

in your values folder is dimens.xml which holds

<dimen name="examplewidth">50dp</dimen>

now create a folder values-mdpi and just copy the dimens.xml file into that folder.

in that copy you change the value from 50dp to... say 20dp. Then examplewidth will be 50dp on all devices except on low-res screens (mdpi) where it will resolve to 20dp. Automatically you do not need to write a single line of code.

Grisgram
  • 3,105
  • 3
  • 25
  • 42
  • It would be highly appreciated if you could give example, as I have read many sources, and none of them teach how to do it step by step. Thanks – androidnewbie Mar 11 '17 at 16:08
  • The comment given by @rafsanahmad007 is what you are looking for. I will edit my answer with one example, but its really as easy as copying a file... there is no black magic involved. – Grisgram Mar 11 '17 at 16:22