How to handle configuration changes (Screen orientation, App display size etc) in Android programatically

313 Views Asked by At

Yes, there are already a lot of answers available on stack overflow on the same topic, most of them stating the following approach:

Manifest.xml:

<activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
</activity>

Manually handling config changes:

override fun onConfigurationChanged (newConfig: Configuration) {

    super.onConfigurationChanged (newConfig)
    
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        // TODO block 1:
        
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        // TODO block 2:
    }
}

Now, let's say I have a simple Android app with one RelativeLayout that contains a button and a text field(EditView). The complete app has been programmatically designed.

In portrait mode: The text field and button are one below the other as shown,

enter image description here

In landscape mode, I want the text field and button to be adjacent to each other, to do so I'll be changing the x, y, height, width, etc inside the TODO block 2 in above mentioned onConfigurationChanged method.

similarly, to handle changes in the app display size (multi-window, multi-screen flip devices) I need to do something similar. (Right now, I don't know how to handle these changes).

Fow now there are just two views in my relative layout: a button and a text field (EditView), let's say when there are 50-60 views in my activity I need to change the x, y, height, and width for each of the view and for each of the scenarios (screen orientation, app display size, etc).

This seems very tedious to do and maintain for a long time, Is there any other way to handle the configuration changes with less efforts?

Note: My whole application is design programmatically and without jetpack compose.

PS: Android docs also does mention that above way isn't the prefered one

enter image description here

I also went through this link, but didn't seem to be useful for the above scenarios. (correct me if I'm wrong)

2

There are 2 best solutions below

0
Rashid On

use following liberties

  implementation 'com.intuit.sdp:sdp-android:1.1.0'
  implementation 'com.intuit.ssp:ssp-android:1.1.0'
3
Rameswar Tarai On
  • You need to have at least one root layout to create your activity. let it be LinearLayout

  • add your views (TextView, Button) programmatically to your LinearLayout

  • Add respective parameters(margin, padding, etc.)

  • add different parameters according to your requirement

  • in the end add the programmatically created views to your root view.

eg:-

private TextView getTextView(String text) {
    TextView textView = new TextView(MyLeaveActivity.this);
    textView.setText(text);
    textView.setPadding(20, 20, 20, 20);
    textView.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);

    return textView;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_leave);
    LinearLayout rootView=findViewById(R.id.maintable);
    TextView l1 = getTextView(data.getSrNo());
    final TableRow row = new TableRow(MyLeaveActivity.this);
    row.addView(l1, new TableRow.LayoutParams());
    rootView.addView(row, new TableLayout.LayoutParams());

}