I am a French noob in Android and I have a problem which I do not find a solution. In fact, I can't save my LinearLayouts'states during my tablet's rotation. I have tried a lot of things and I made this code:
public class Repas extends Activity {
private LinearLayout area1, area2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.repas);
area1 = (LinearLayout) findViewById(R.id.area1);
area2 = (LinearLayout) findViewById(R.id.area2);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int width_screen=dm.widthPixels;
area2.setMinimumWidth(width_screen);
if( savedInstanceState != null ) {
int count_item = savedInstanceState.getInt("number_of_item");
String image = savedInstanceState.getString("Sauvegarde_repas0");
//area2.addView(image);
}
else {
TypedArray arrayResources = getResources().obtainTypedArray(
R.array.resicon);
for (int i = 0; i < arrayResources.length(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setOnTouchListener(myOnTouchListener);
area2.addView(imageView);
}
arrayResources.recycle();
}
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
int count = area1.getChildCount();
View v = null;
outState.putInt("number_of_item", count);
for(int i=0; i<count; i++) {
v = area1.getChildAt(i);
outState.putString("Sauvegarde_repas"+i, v.toString());
}
super.onSaveInstanceState(outState);
}
My current problem, assuming my logic is right, is I cannot convert my String into ImageView during my tablet's rotation. Can you help me ?
I would also like to know if what I did is right: I mean To got my layout's elements one by one(I didn't find how to save the whole layout)?
I hope I am clear enough : if you need more information, don't hesitate !
Thank you very much for your help !