Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle **Grid View** on configuration change?

I am using a grid view in my app's activity. Each grid view row contains three check boxes which can be set to selected/ not selected based on what user wants to query from database. The activity also includes an editText which causes an onScreenKeyboard to appear every time activity starts and here I am having problem. OnScreenKeyboard, when appears disturbs gridview and some of its checkboxes simply disappear. My idea was to refresh grid view every time configuration changes. I tried handling this by returning an object through onRetainNonConfigurationInstance(). Object contains an array list to populate my gridview rows with but onCreate() when I use getLastNonConfigurationInstance() to retrieve returned object it shows null. Can anyone please suggest me that how to handle this issue or if there is any other approach available by which I can make my gridView behave normal on configuration change. Following is the code and i want to make it clear I have added keyboardHidden configuration change in manifest file but when key board appears it sometimes don't trigger onConfigurationChanged()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.failureselection);
    findLocalWidgets(); //Initializes Layout Objects

    if(configchanged){
                    //Re populate grid view
                        customDataGridRows = (ArrayList<CustomGridViewRow>) getLastNonConfigurationInstance();
                        dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
                                customDataGridRows));

                                configchanged = false;
                      }else{
                            fillFailuresList(customDataGridRows);
                            dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
                                    customDataGridRows));
                    }

    }


@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.failureselection);

    configchanged = true;
}


@Override
public Object onRetainNonConfigurationInstance() {

    return customDataGridRows;
}

private boolean fillFailuresList(
        ArrayList<CustomGridViewRow> customDataGridRows) {
    boolean isFilled = false;
    try {
        // Adding Failures data
        customDataGridRows.add(new CustomGridViewRow(false, "Hood", false,
                "Assembly Defect", false, "Masking Poor"));
        customDataGridRows.add(new CustomGridViewRow(false, "Floor", false,
                "Forget Work", false, "Assembly Defect"));
        customDataGridRows.add(new CustomGridViewRow(false, "Grill", false,
                "Incorrect Assembly", false, "Bad Company"));
        customDataGridRows.add(new CustomGridViewRow(false,
                "R Right Frame", false, "Interference", false,
                "Fix Large Effort"));
        customDataGridRows.add(new CustomGridViewRow(false, "R Left Frame",
                false, "Leakage", false, "High Incidence"));
        customDataGridRows.add(new CustomGridViewRow(false, "R Frame",
                false, "Dirt", false, "Recurrence"));
        customDataGridRows.add(new CustomGridViewRow(false,
                "Outside R Frame", false, "Decal", false, "Checking"));
        customDataGridRows.add(new CustomGridViewRow(false,
                "F Right Frame", false, "Other", false, "Foreign Body"));
        customDataGridRows.add(new CustomGridViewRow(false, "F Left Frame",
                false, "", false, "Not Caulking"));
        customDataGridRows.add(new CustomGridViewRow(false, "F Frame",
                false, "", false, "Painting Defect"));
        customDataGridRows.add(new CustomGridViewRow(false,
                "Outsie F Frame", false, "", false, "Other"));
        customDataGridRows.add(new CustomGridViewRow(false, "", false, "",
                false, ""));

        // Populating Failures grid view
//      dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
//              customDataGridRows));

        isFilled = true;

    } catch (Exception e) {
        e.getMessage();
    }
    return isFilled;

}
like image 280
Abdul Rehman Avatar asked Dec 19 '11 08:12

Abdul Rehman


2 Answers

A couple of options:

  1. How to prevent Custom Views from losing state across screen orientation changes for your CustomGridViewRow.
  2. Create a method in your Adapter and put the values in a bundle and restore it on create using the savedInstanceState

Tip: Remove any <requestFocus /> from your EditText layout. Although Android will set focus to the first focusable view.

Another hack: How to remove focus without setting focus to another control?

like image 121
ejohansson Avatar answered Sep 19 '22 05:09

ejohansson


If your onConfigurationChanged is not called always, consider adding

android:configChanges="keyboardHidden|orientation"

in your Manifest. This will also trigger the onConfigurationChanged call, when the orientation of the device changes. Also, i'm not sure with the setContentView inside your onCOnfigurationChanged. Good luck

like image 2
Entreco Avatar answered Sep 21 '22 05:09

Entreco