0

I found many solutions, but nothing works for me. I am creating a language learning application. So in that I am using imageviews, buttons, textviews and webviews. There will be more than 15 layout files. And the solutions i have found seem like I have to create layouts like below.

layout-small

layout-normal

layout -land

layout-land-small

So I have to modify those 15 layout files for these 4 folders. It will be difficult to code as my file count is higher. Instead of modifying files is there any other way to do? If so please help me. Or suggest me how I can handle this for a large number of files.

Simon Zyx
  • 6,503
  • 1
  • 25
  • 37
tweety
  • 11
  • 5

4 Answers4

1

There is no need to create separate layout directories

You can refer to this:

For Layouts Consistency refer and use (example is given below): https://github.com/intuit/sdp

Note: Margin , Padding , Height, Width, etc can be used with sdp for best results.

in your gradle : implementation 'com.intuit.sdp:sdp-android:1.0.5'

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/_200sdp"
    android:orientation="vertical"
    android:padding="@dimen/_4sdp">
      <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_marginBottom="@dimen/_12sdp"          // SDP used
        android:layout_marginEnd="@dimen/_12sdp"             // SDP used
        android:layout_marginStart="@dimen/_12sdp"           // SDP used
        android:background="@drawable/shape_button_background"
        android:foreground="?android:selectableItemBackground"
        android:textColor="@color/white"
        android:textSize="18sp"                              // SP used
        android:text="@string/str_confirm_place_order"/>
</LinearLayout>

For Font Size Consistency : I will recommend you to use sp instead of others.

Note: textSize in Button,EditText,TextView etc can be used with sp for best results.

               <TextView
                    android:id="@+id/tvFbShare"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="@dimen/_6sdp"             // SDP used
                    android:text="@string/str_share_on_facebook"
                    android:textColor="@color/fb_blue"
                    android:textSize="14sp"                   // SP used />
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
0

You can also use values/dimens.xml to define dimensions for different screen sizes. Then you only need to use these dimensions in your layout files. As for landscape/portrait, you will probably have to deal with 2 layout files.

Rak
  • 16
  • 3
0

no need use 4 folder for each layout best solution use sdp

Ahmad Nemati
  • 309
  • 2
  • 12
-1

First of all Define these two methods in your Utility class

// Method is used to get the height of the screen
public static int getScreenHeight(Context context) {

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    assert wm != null;
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.heightPixels;
}

// Method is used to get the width of the screen
public static int getScreenWidth(Context context) {

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    assert wm != null;
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.widthPixels;
}

Use above two methods to get the height and width of the screen in onCreate()

int height = Utility.getScreenHeight(this);
int width = Utility.getScreenWidth(this);

Now you can use these dimensions in onResume() method like

your_ui_component.getLayoutParams().height = (int) (height * .25);
your_ui_component.getLayoutParams().width = (int) (height * .25);

Change .25 with your required dimension

Ankit
  • 309
  • 2
  • 8