6

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;

}
Abdul Rehman
  • 373
  • 6
  • 15
  • It really helps, if you put code samples in your question. This way, users might copy and paste it in a project, to actually see what's going on. Consider doing that, to generate more valuable answers for your questions – Entreco Dec 19 '11 at 23:31
  • Hope new edit will help you understand my problem. Thank you – Abdul Rehman Dec 20 '11 at 05:48
  • Why dont you simply prevent the keyboard from appearing when the activity starts ? I am sure you will get the code if you google a little more. – Rat-a-tat-a-tat Ratatouille Jan 07 '14 at 16:20
  • The problem and solution are both here:
    http://stackoverflow.com/questions/3121153/baseadapter-causing-listview-to-go-out-of-order-when-scrolled
    – Dharmendra Jan 09 '14 at 06:00

3 Answers3

4

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?

Community
  • 1
  • 1
ejohansson
  • 2,832
  • 1
  • 23
  • 30
2

Rather than doing this, you can over write onSaveInstanceState(Bundle bundle) & in this bunde save all the present state of all UI components(like bundle.putBoolean("check_box_1", true)). When there will any configuration change Android system will call this method.

After configuration change when the system will recreate your activity by calling onCreate(Bundle saveInstanceState) the same Buldle instance will be provided so this time saveInstanceState is not null. From this Bundle extract previously saved values(like saveInstanceState.getBoolean("check_box_1")) & set those to respective fields.

This is the satndard way to handle this & because of this onCreate() have a Bundle as input parameter.

CR Sardar
  • 921
  • 2
  • 17
  • 32
2

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

Entreco
  • 12,738
  • 8
  • 75
  • 95
  • The configchanges tag has already been added in application's manifest with the entries android:configChanges="locale|keyboardHidden|Orientation" but still its not triggering __onconfigurationChanged()__ when keyboard appears but works fine for orientation changes. – Abdul Rehman Dec 20 '11 at 09:04
  • I guess, you also want the call when the SoftKeyboard is shown/hidden. The keyBoardHidden attribute is only for a physical keyboard. Try the solution from this answer: http://stackoverflow.com/questions/2150078/android-is-software-keyboard-shown – Entreco Dec 20 '11 at 10:08
  • try adding screenSize attribute as well...this has changed with 4.0 – onlythoughtworks Feb 17 '12 at 13:15