0

Im trying to design a CardView to implement it in a RecyclerView whith a GridLayout. I designed the cardView but when I prove my design in different mobiles with different screen sizes the design doesn't scale itself. I used "dp" unity and "sp" unity like "Android developers" page shows.I don't know if I must create a design for each screen size or each screen density.

Can you help me?

Code:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:cardCornerRadius="6dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
android:id="@+id/dlvsr_CardView">


<LinearLayout
    android:layout_width="170dp"
    android:layout_height="170dp"
    android:orientation="vertical"
    android:padding="4dp">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Text view tittle"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text view subtittle" />
</LinearLayout>

Here are the examples:

https://i.stack.imgur.com/nRpBY.png

https://i.stack.imgur.com/scL7K.png

https://i.stack.imgur.com/lrCyZ.png

https://i.stack.imgur.com/REzt7.png

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ProRiderZ115
  • 115
  • 2
  • 9

3 Answers3

3

You don't have to create different design for each screen size. But at least you have to use different dimension for each screen.

To do that

  1. You have to create Different values folder for different screens . Like

    values-sw720dp          10.1” tablet 1280x800 mdpi        
    values-sw600dp          7.0”  tablet 1024x600 mdpi  
    
    values-sw480dp          5.4”  480x854 mdpi     
    values-sw480dp          5.1”  480x800 mdpi   
    
    values-xxhdpi           5.5"  1080x1920 xxhdpi    
    values-xxxhdpi           5.5" 1440x2560 xxxhdpi  
    
    values-xhdpi            4.7”   1280x720 xhdpi     
    values-xhdpi            4.65”  720x1280 xhdpi 
    
    values-hdpi             4.0” 480x800 hdpi    
    values-hdpi             3.7” 480x854 hdpi        
    
    values-mdpi             3.2” 320x480 mdpi        
    
    values-ldpi             3.4” 240x432 ldpi    
    values-ldpi             3.3” 240x400 ldpi
    values-ldpi             2.7” 240x320 ldpi
    
  2. Create a new dimens.xml file by right clicking all the values folder and choosing New > Values resource file. Write dimens for the name. (You could also call it dimen or dimensions. The name doesn't really matter, only the dimen resource type that it will include.)

  3. Add a dimen name and value.

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

    Values can be in dp, px, or sp.

  4. Use the value in xml

    <TextView
        android:padding="@dimen/my_value"
        ... />
    

    or in code

    float sizeInPixels = getResources().getDimension(R.dimen.my_value);
    
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
1

You can use this library for sp and this for dp for all screen sizes. It has different dimen.xml with values respected to the different dpi.

This is the simplest solution to your problem.

Add all the ssp and sdp XML in your res->values-> to your project and then in UI xml use them like below.

<android.support.design.widget.TextInputLayout
        android:id="@+id/textEmailAddressInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="@dimen/_10sdp"
        android:textSize="@dimen/_14ssp">

In above usage example you can see I have used 14ssp as android:textSize and 10sdp for android:layout_marginTop which will be adjusted according to the dpi of all screen, this will work for all mobile size, tablet and even with Android TV.

Please let me know if you need more info on this.

Pankaj Mundra
  • 1,401
  • 9
  • 25
  • this two libraries work as I need, but trying some designs, when I change landscape to portrait or viceversa the resizing is not good for me. Maybe is better for that to create different layouts for each views(Landscape and Portrait)? – ProRiderZ115 Aug 09 '18 at 19:44
  • @ProRiderZ115 Yes you can create the layout-land folder for landscape layouts. – Pankaj Mundra Aug 10 '18 at 04:00
  • @ProRiderZ115 Have a look at this https://stackoverflow.com/questions/4572496/different-layouts-for-horizontal-and-vertical-for-an-android-app – Pankaj Mundra Aug 10 '18 at 04:00
0

You can use ConstraintLayout instead of LinearLayout and constraints view vertically and horizontally. it will be compatible with the different screen sized automatically.

Emadi_mehrdad
  • 146
  • 1
  • 2
  • 16