0

There are two different xml files in layout and layout-land for the respective modes. As I want to set the values back in the edittexts even when orientation changed, I am using android:configChanges="orientation|keyboardHidden" in manifest file. This deactivates the execution of onCreate() method again and the present activity is not destroyed.

I have two frames in my app. First frame contains 25 edittexts and second contains 10 buttons. So in portrait mode, these frames are one after the other and in landscape mode one next to the other. To get this view according to modes, I wrote setContentView(R.layout.main); in the onConfigurationChanged() method. But my problem here is I am able to store the values that entered already in the editTexts in onConfigurationChanged(). But I am unable to set the values back to the edittexts after setting to the respective layout. Because I am getting respective layout, but with empty edittexts. The following code snippet gives overview of my problem.

public void onCreate()
{
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new EditTextAdapter(this)); //This creates 25 edittexts in the gridview which is in first frame.

} 

public void getEditTexts()//getting the edittext objects here in the activity class
{
editText1=gridView.getChildAt(k);
...........
...........
}

public void onConfigurationChanged()
{
String s1=editText1.getText().toString();//Here I am using technique to store the values.

setContentView(R.layout.main);
gridView.setAdapter(new EditTextAdapter(this));
getEditTexts();
editText1.setText(s1);    //here is my problem. I am unable to set values back in the editText.

}

Where I am going wrong? How to set the values back like this in 25 edittexts in the present mode if they are set in previous mode. Please suggest. Here I guess the values are set in the old edittext objects itself even though the mode is changed. How to set the values in the new edittext objects? How come the values are set in old edittext objects if they are not present in the current layout? Or there is another reason for this problem?

Spike
  • 147
  • 3
  • 17
  • You are using grid view and edit text as it's item. How are you handling GridView recycling?? cause each time you scroll the grid the view will get recycled. – karn Sep 12 '12 at 19:02
  • I set the verticalScrollbarEnabled false. So, the scroll bar is disabled. – Spike Sep 13 '12 at 02:53
  • look into this post..it will solve your recycle problem.. http://stackoverflow.com/questions/4523609/grid-of-images-inside-scrollview/4536955#4536955. – karn Sep 13 '12 at 09:48

1 Answers1

1

I have created an alternative application for your requirement. I have not used grid view still I can add as many views as I want without XML's. The code checks the device configuration and the view size and add that many rows and columns to the layout. This is a basic example you can modify it for your requirement.
Here is a link to the code

Feel free to discuss in case there is any doubt.

karn
  • 5,963
  • 3
  • 22
  • 29
  • Karn, Thanks for the response. I have tried in the other way and even I will implement your code too. – Spike Sep 15 '12 at 03:48
  • I have doubt in which you gave the file. – Spike Sep 17 '12 at 04:23
  • It is showing force close to me. lds[i].root = (LinearLayout) inflater.inflate(R.layout.row_layout, null); What is the layout here. It is showing error to me here. If I change this to R.id.row_layout, it is showing force close. – Spike Sep 17 '12 at 10:13
  • Here i have tried to create a grid of views in which you can enter the number of views to be added and the width of a view and it will adjust itself the number of rows and columns depending on the device resolution. Here row_layout.xml is an empty linear layout i.e a container to which the views(edit text) are added. This container forms the row. The this container is added to the parent layout to form a grid. Here views are added dynamically. If you have downloaded the file correctly then it should run. – karn Sep 17 '12 at 17:46
  • This layout adjusts itself on rotation i.e number of rows and columns. It also saves the input values of edit text on rotation and then initializes them with the saved values – karn Sep 17 '12 at 17:50
  • Thanks for your time. Though I approached another way, +1 for your contribution and accepting as an answer. – Spike Sep 21 '12 at 17:21