How To Support Multiple Screen Size In Android

There are a lot of android devices with different screen sizes and resolutions. This article will tell you how to make an android program load layout files dynamically based on the device’s screen resolution or size at run time.

1. Use Android Qualifiers.

  1. If you use tablets often, you will find that most of the tablet apps use a two-panel model. This is because the tablet screen size is big, there is enough space to show content. But almost all mobile apps use a single panel model, because of it’s small screen size.
  2. But how to make your single app show a different layout when running on different screen size devices? The answer is to use android qualifiers.
  3. Qualifiers can be used for all android resources such as layout, drawable, mipmap, etc. When Android OS gets the device matched qualifier, it will use the resource files saved in that qualifier directory such as layout file, drawable file, etc.
  4. Let’s see a previous example again Android Multiple Fragments In One Activity Example. If we run this example on a mobile phone, we will find a not user-friendly screen effect.
  5. The above example’s activity layout file name is activity_multiple_fragment.xml, it is saved in the app/res/layout folder. This layout file is ok for tablet devices with a big screen, but for mobile, it is not perfect.
  6. To make the app load different layout files for multiple size screens, we need to follow the below steps.
  7. Modify app / res / layout / activity_multiple_fragment.xml file as below. Now the layout file only contains one left fragment.
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <fragment
            android:id="@+id/fragmentLeft"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="com.dev2qa.example.fragment.multiple.FragmentLeft"/>
    
    </LinearLayout>
  8. Create a folder layout-large under app / src / main / res folder. And copy the original layout file to there, the layout file name does not change. To make the layout-large folder visible in android studio, you need to switch to Project mode in the left panel. large is just one layout qualifier, the layout file under layout-large folder will be used when android OS thinks the device screen size is large.
  9. Now run the MultipleFragmentActivity use mobile and a tablet emulator, you can find that the tablet screen is not changed but the mobile screen is changed more reasonably. You can see the youtube video https://youtu.be/lP-V04Vag-Y for this step demo.
  10. But when you click the button on the mobile screen, the app stopped. Because in the mobile layout file, there is only a left fragment, which does not contain the right fragment.
  11. But the code needs to get the right fragment when the button is clicked. That action triggers a null pointer exception. So we need to change the code to check whether the right fragment exists or not. If the right fragment does not exist, we need to start an activity to show the right fragment content.
    // Do not use fragmentBelongActivity.getFragmentManager() method which is not compatible with older android os version. .
    FragmentManager fragmentManager = fragmentBelongActivity.getSupportFragmentManager();
    
    // Get right Fragment object.
    Fragment rightFragment = fragmentManager.findFragmentById(R.id.fragmentRight);
    
    // If in single panel mode, right fragment do not exist.
    // Then start right fragment activity.
    if(rightFragment==null)
    {
        MultipleFragmentRightActivity rightActivity = new MultipleFragmentRightActivity();
        rightActivity.startActivity(getContext(), "You click Android button.");
    }else {
        // Get the TextView object in right Fragment.
        final TextView rightFragmentTextView = (TextView) rightFragment.getView().findViewById(R.id.fragmentRightTextView);
    
        // Set text in right Fragment TextView.
        rightFragmentTextView.setText("You click Android button.");
    }

    You can see this step demo video on youtube URL https://youtu.be/3HMjgm1vAH0

  12. So we need to create MultipleFragmentRightActivity java class and it’s layout XML file activity_multiple_fragment_right.xml.
  13. MultipleFragmentRightActivity.java

    package com.dev2qa.example.fragment.multiple;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;
    
    import com.dev2qa.example.R;
    
    public class MultipleFragmentRightActivity extends AppCompatActivity {
    
        private final static String INTENT_KEY_MESSAGE = "INTENT_KEY_MESSAGE";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_multiple_fragment_right);
    
            setTitle("dev2qa.com - Right Fragment Activity");
    
            // Get intent and transferred message.
            Intent intent = getIntent();
            String message = intent.getStringExtra(INTENT_KEY_MESSAGE);
    
            // Show the message in the text view.
            TextView textView = (TextView)findViewById(R.id.fragmentRightTextView);
            textView.setText(message);
        }
    
        // This method is used to start this activity. It is called by other activity.
        public void startActivity(Context ctx, String message)
        {
            Intent intent  = new Intent(ctx, MultipleFragmentRightActivity.class);
            intent.putExtra(INTENT_KEY_MESSAGE, message);
            ctx.startActivity(intent);
        }
    }
  14. activity_multiple_fragment_right.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <fragment
            android:id="@+id/fragmentRight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="com.dev2qa.example.fragment.multiple.FragmentRight"/>
    
    </LinearLayout>

2. Android Qualifiers Classification.

2.1 Qualifiers By Screen Size.

  1. small: Small screen size resources.
  2. normal: Normal screen size resources.
  3. large: Large screen size resources.
  4. xlarge: Super large screen size resources.

2.2 Qualifiers By Screen Resolution.

  1. ldpi: Low screen resolution resources ( less than 120 dpi ).
  2. mdpi: Middle screen resolution resources ( 120 dpi – 160 dpi ).
  3. hdpi: High screen resolution resources ( 160 dpi – 240 dpi ).
  4. xhdpi: Super high screen resolution resources ( 240 dpi – 320 dpi ).
  5. xxhdpi: Super super high screen resolution resources ( 320 dpi – 480 dpi ).

2.3 Qualifiers By Screen Orientation.

  1. land: Horizontal screen resource.
  2. port: Vertical screen resource.

3. Use Custom Minimum Width / Height Qualifier.

  1. We can make the qualifier more accurate by specifying the screen minimum width or height value.
  2. When screen size is bigger than the specified width/height it will use one resource ( layout, drawable, mipmap ), when the screen size is smaller than the specified width/height then it will use another resource ( layout, drawable, mipmap ).
  3. Create a folder layout-sw600dp in app / src / main / res folder. This folder tells android OS when run devices that screen width bigger than 600dp then use the layout file in this folder.
  4. Copy the activity_multiple_fragment.xml file into this folder.
    custom-android-qualifiers
  5. Modify the XML file content as follows.
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <fragment
            android:id="@+id/fragmentLeft"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="com.dev2qa.example.fragment.multiple.FragmentLeft"/>
        
    </LinearLayout>
  6. Run the activity again, you can find the custom minimum width qualifier has higher priority than the built-in large qualifier. You can see this step demo video on youtube URL https://youtu.be/Vhb8A2B8x_k.
  7. To create minimum height qualifier, just create folder layout-sh600dp under app / src / main / res folder. And copy related layout files in it.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x