Easy Tutorial
❮ Android Tutorial Spinner Csharp Enum ❯

3.6 Responding to System Configuration Events (Configuration Class)

Category Android Basic Tutorial

Introduction to This Section:

>

This section introduces the Configuration class, which is used to describe the configuration information of mobile devices, such as screen orientation, touch methods of the touch screen, etc. Friends who have customized ROMs should know that we can find this class at: frameworks/base/core/java/android/content/res/Configuration.java and then change some related settings, such as adjusting the default font size! If you are interested, you can learn about it on your own! This section explains the use of the Configuration class in our Android development~ API Documentation: Configuration


1. List of Methods Provided by Configuration

>

-densityDpi: Screen density

-fontScale: The current user-set font scaling factor

-hardKeyboardHidden: Determines if the hardware keyboard is visible, with two possible values: HARDKEYBOARDHIDDEN_NO, HARDKEYBOARDHIDDEN_YES, which are hexadecimal 0 and 1 respectively

-keyboard: Retrieves the current keyboard type: The return values of this property are: KEYBOARD_12KEY (a small keyboard with only 12 keys), KEYBOARD_NOKEYS, KEYBOARD_QWERTY (a standard keyboard)

-keyboardHidden: This property returns a boolean value indicating whether the current keyboard is available. This property not only judges the system's hardware keyboard but also the system's soft keyboard (located on the screen).

-locale: Retrieves the current user's language environment

-mcc: Retrieves the mobile signal country code

-mnc: Retrieves the mobile signal network code

-navigation: Determines the type of navigation device on the system. The return values of this property are: NAVIGATION_NONAV (no navigation), NAVIGATION_DPAD (DPAD navigation) NAVIGATION_TRACKBALL (trackball navigation), NAVIGATION_WHEEL (wheel navigation)

-orientation: Retrieves the system screen orientation. The return values of this property are: ORIENTATION_LANDSCAPE (landscape screen), ORIENTATION_PORTRAIT (portrait screen)

-screenHeightDp, screenWidthDp: The available height and width of the screen, represented in dp

-touchscreen: Retrieves the touch method of the system touch screen. The return values of this property are: TOUCHSCREEN_NOTOUCH (no touch screen), TOUCHSCREEN_STYLUS (stylus touch screen), TOUCHSCREEN_FINGER (touch screen that accepts fingers)


2. Write a Simple Example to Test:

Screenshot of Running:

Code Implementation:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txtResult = (TextView) findViewById(R.id.txtResult);
        StringBuffer status = new StringBuffer();
        //①Get the system's Configuration object
        Configuration cfg = getResources().getConfiguration();
        //②Check whatever you want
        status.append("densityDpi:" + cfg.densityDpi + "\n");
        status.append("fontScale:" + cfg.fontScale + "\n");
        status.append("hardKeyboardHidden:" + cfg.hardKeyboardHidden + "\n");
        status.append("keyboard:" + cfg.keyboard + "\n");
        status.append("keyboardHidden:" + cfg.keyboardHidden + "\n");
        status.append("locale:" + cfg.locale + "\n");
        status.append("mcc:" + cfg.mcc + "\n");
        status.append("mnc:" + cfg.mnc + "\n");
        status.append("navigation:" + cfg.navigation + "\n");
        status.append("navigationHidden:" + cfg.navigationHidden + "\n");
        status.append("orientation:" + cfg.orientation + "\n");
        status.append("screenHeightDp:" + cfg.screenHeightDp + "\n");
        status.append("screenWidthDp:" + cfg.screenWidthDp + "\n");
        status.append("screenLayout:" + cfg.screenLayout + "\n");
        status.append("smallestScreenWidthDp:" + cfg.smallestScreenWidthDp + "\n");
        status.append("touchscreen:" + cfg.touchscreen + "\n");
        status.append("uiMode:" + cfg.uiMode + "\n");
        txtResult.setText(status.toString());
    }
}

3. Override onConfigurationChanged to Respond to System Setting Changes

>

This method is used to monitor changes in system settings and is a callback

WeChat Follow

❮ Android Tutorial Spinner Csharp Enum ❯