1

I am developing an android app using the ConstraintLayout. It shows properly on different screen sizes, however, I would like for the textviews to increase in size and spacing between the elements to take advantage of larger screen real estate on bigger screen phones.

I've read that you can create different XML layouts for different densities or sizes (xxhdpi, xhdpi), but I thought one of the main advantages of ConstraintLayout over relative layouts was that it would adjust spacing/size automatically. Can you do this with constraint layout, or do you need to create individual xml layouts for each screen size/density? Thanks!

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="19dp"
        android:backgroundTint="@color/colorAccent"
        app:layout_constraintBottom_toTopOf="@+id/inView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/lbsView"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/weightView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="27dp"
        android:layout_marginTop="21dp"
        android:layout_marginEnd="14dp"
        android:text="Weight:"
        android:textSize="26sp"
        app:layout_constraintEnd_toStartOf="@+id/weight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

This is a sample of my constraint layout.

ADM
  • 20,406
  • 11
  • 52
  • 83
Ahmedev
  • 51
  • 7
  • 4
    You need to understand this [concept](https://developer.android.com/training/multiscreen/screensizes) and follow [this link](https://stackoverflow.com/questions/32860815/how-to-define-dimens-xml-for-every-different-screen-size-in-android) – Pratik Butani Feb 01 '19 at 04:40
  • check this answer here https://stackoverflow.com/a/58349912/3141738 – Arpit J. Nov 27 '20 at 06:26

3 Answers3

0

Put your margin values in dimens.xml, from there you can adjust size in different screen size like values-hdpi, values-xhdpi`,..

In values-hdpi/dimens.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <dimen name="spinner_margin_top">16dp</dimen>
</resources>

In values-xhdpi/dimens.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <dimen name="spinner_margin_top">20dp</dimen>
</resources>

And in your xml file:

<Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spinner_margin_top"
        .... />
Bach Vu
  • 2,298
  • 1
  • 15
  • 19
0

You can different size for your tex using values-[smallestWidth].

SW : The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for it UI.

Example: Let's confider 3 type different layout , small , medium , large

values       : <dimen name="text_size">10sp</dimen>
values-sw375 : <dimen name="text_size">11sp</dimen>
values-sw600 : <dimen name="text_size">18sp</dimen>

enter image description here

Farid Haq
  • 3,728
  • 1
  • 21
  • 15
0

As @Farid said.. you can use dimens for different-2 size of devices.

This question is also helpful for you to understand dimens.

If you are not want to declare dimens for every size then you can use this libraries sdp & ssp.

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51