Android Layout issue of Portrait and Landscape - android

Hello friends I'm new to Android Development and Stackoverflow,
I'm facing some issues in android layout,
I have created two layout folders one for portrait and one for landscape(both for tablet TVDPI)
layout-sw600dp-land-tvdpi & layout-sw600dp-port-tvdpi
The issue I'm facing is when I run my program in portrait mode it shows me correct layout of portrait but when I turn my device portrait to landscape it shows me same layout of portrait, and same case when I run it in landscape mode it run correctly and turn to potrait it shows me the landscape layout...
Why?

Try this
#region Handle State on Orientation
//this has been done using better technique
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
//adding spinner/dropdownlist selected item
if (ViewModel.IsLoading == true)
{
isLoadingState = true;
AndroidHUD.AndHUD.Shared.Dismiss(this);
}
var preferences = GetSharedPreferences("TmsAppData", FileCreationMode.Private);
var editor = preferences.Edit();
//editor.PutString("DeviceId", registrationId);
editor.PutBoolean("IsOrientationChange", true);
editor.Commit();
//outState.PutInt("_AlreadySelectedPostion", _AlreadySelectedPostion);
outState.PutBoolean("_isLoadingState", isLoadingState);
}
protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreInstanceState(savedInstanceState);
//setting a flag to manage spinner selected state
_IsStateViewActive = true;
isLoadingState = savedInstanceState.GetBoolean("_isLoadingState");
if (ViewModel.IsLoading == true)
{
isLoadingState = false;
ViewModel.IsLoading = true;
}
//getting the previous selected item from the saved state for spinner / dropdownlist
//_AlreadySelectedPostion = savedInstanceState.GetInt("_AlreadySelectedPostion");
}
#endregion

Related

VISIBLE EditText goes to INVISBLE on orientation change

I have a spinner that has numbers from 1 - 10. If the user selects anything above 1, a certain EditText that is INVISBLE becomes VISIBLE, this is what I want. But when I change it from portrait to landscape, it goes back to INVISIBLE.
This is where the changing happens
btn_Calc.setOnClickListener(new View.OnClickListener() {
#Override
if (split > 1) {
et_APP.setVisibility(View.VISIBLE);
tv_app.setVisibility(View.VISIBLE);
vis = 1;
}
else{
et_APP.setVisibility(View.INVISIBLE);
tv_app.setVisibility(View.INVISIBLE);
vis = 2;
}
});
And I tried to do onSaveInstanceState like this
if(savedInstanceState != null) {
int isVis = savedInstanceState.getInt("vis", 2);
if(isVis == 1){
et_APP.setVisibility(View.VISIBLE);
tv_app.setVisibility(View.VISIBLE);
}
else{
et_APP.setVisibility(View.INVISIBLE);
tv_app.setVisibility(View.INVISIBLE);
}
}
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("getVisible", vis);
}
Sorry for poor formatting, I cut out unrelated code.
It works that if the selected item in the spinner is greater than 1 and I click the calc button, it shows the fields I want but when I change orientation they dissapear again. Any ideas on how I can get to stay visible when I change the orientation?
The names of your keys don't match between save and load.
Saving:
savedInstanceState.putInt("getVisible", vis);
Restoring:
int isVis = savedInstanceState.getInt("vis", 2);
The layout is being recreated if you don't want Android to do anything add the following line:`android:configChanges="keyboardHidden|orientation|screenSize"' to the activity in the manifest.

Rotation Toggle Not Working

I am trying to make a togglebutton that will endable or disable Auto-Rotation on my android device. I was able to get the setting the setting is on or off. But I can't seem change the setting.
public void toggleOrientation(View view)
{
ToggleButton tgOrientation = (ToggleButton) findViewById(R.id.tgOrientation);
String orientationOption = Settings.System.ACCELEROMETER_ROTATION;
int orientation = android.provider.Settings.System.getInt(getContentResolver(),orientationOption, 0);
if(orientation == 1)
{
android.provider.Settings.System.putInt(getContentResolver(),orientationOption,0);
tgOrientation.setChecked(false);
}
else
{
android.provider.Settings.System.putInt(getContentResolver(), orientationOption,1);
tgOrientation.setChecked(true);
}
}
Could anyone help me if there is a problem in my code?
My testing device is a Tablet. I am not sure if it helps.
To disable the orientation change based on the orientation of the device:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
To let device/user decide the orientation:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
To force device to use orientation sensor:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
Just call on button click event. Check out http://developer.android.com/reference/android/R.attr.html#screenOrientation for full details

CheckBoxes in ListView disappear when the screen rotates

What my application first does is it loads ListView whose items have invisible CheckBoxes by setting its visibility View.Gone. When the user tabs a menu button then it will turn on and off the CheckBox visibility and some other layouts. Below is the code, I removed some unnecessary parts:
private void editmodeSwitch(boolean flag){
// get topbar, bottombar, and bottombar2
LinearLayout topbar = (LinearLayout) findViewById(R.id.task_topbar_linearLayout);
LinearLayout bottombar = (LinearLayout) findViewById(R.id.task_bottombar1_linearlayout);
LinearLayout bottombar2 = (LinearLayout) findViewById(R.id.task_bottombar2_linearlayout);
if(flag){
isEditmodeOn = true;
// make topbar and bottombar2 visilble, but bottombar gone
topbar.setVisibility(View.VISIBLE);
bottombar.setVisibility(View.GONE);
bottombar2.setVisibility(View.VISIBLE);
// make checkboxes visible in listview visible as well
for(int i=0; i<listView.getChildCount(); i++){
LinearLayout ll = (LinearLayout) listView.getChildAt(i);
CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1);
cb.setVisibility(View.VISIBLE);
}
}
else{
isEditmodeOn = false;
topbar.setVisibility(View.GONE);
bottombar.setVisibility(View.VISIBLE);
bottombar2.setVisibility(View.GONE);
// set each checkbox false and its visibility gone
for(int i=0; i<listView.getChildCount(); i++){
LinearLayout ll = (LinearLayout) listView.getChildAt(i);
CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1);
cb.setVisibility(View.GONE);
cb.setChecked(false);
}
}
}
It works fine but the problem is the application doesn't work when the screen rotates(changes the screen orientation). Everything worked fine as it displayed some layouts but only CheckBoxes in list items. Below is the code inonCreate()`:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_layout);
initialize();
loadDB();
updateListAdapter(list_title, list_date);
// in case of screen rotation
if(savedInstanceState != null){
isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK);
isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK);
if(isEditmodeOn){
if(!isItemChecked){
Log.i(tag, "item NOT checked");
editmodeSwitch(true);
} else{
//this is something different so please don't mind
deditmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK));
}
}
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save values for rotation
outState.putBoolean(EDITMODE_CHECK, isEditmodeOn);
outState.putBoolean(ITEM_CHECK, isItemChecked);
outState.putBooleanArray(LIST_CB_CHECK, list_cb_check);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(tag, "you're in onRestoreInstanceState()");
// in case of screen rotation
if(savedInstanceState != null){
isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK);
isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK);
if(isEditmodeOn){
if(!isItemChecked){
Log.i(tag, "item NOT checked");
editmodeSwitch(true);
} else{
// this is for something else so please ignore this part
editmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK));
}
}
}
What I guessed is the ListView is being loaded at the end. Therefore, even if the code in onCreate() makes CheckBoxes visible, the CheckBoxes will become invisible again as its initialization in xml will do so. However, I'm stuck here and need your advice to solve this problem. Can anyone help me?
Just in case, below is the checkbox code of layout xml file for getview.
<CheckBox android:id="#+id/task_row_checkBox1" android:gravity="right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:visibility="gone"
/>
Override onSaveInstanceState for saving value on screen rotation and onRestoreInstanceState as:
protected void onCreate(Bundle savedInstanceState) {
if(null != savedInstanceState)
{
Boolean IntTest = savedInstanceState.getBoolean("ITEM_CHECK");
Boolean StrTest = savedInstanceState.getBoolean("ITEM_CHECK");
Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save away the CheckBoxes states, so we still have it if the activity
// needs to be killed while paused.
savedInstanceState.putBoolean(EDITMODE_CHECK, 0);
savedInstanceState.putBoolean(ITEM_CHECK, 0);
super.onSaveInstanceState(savedInstanceState);
Log.e(TAG, "onSaveInstanceState");
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Boolean IntTest = savedInstanceState.getBoolean(EDITMODE_CHECK);
Boolean StrTest = savedInstanceState.getBoolean(ITEM_CHECK);
Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
Similar to how you override onCreate, you can override onConfigurationChanged(...) which you can setup to run when the screen changes orientation.
In order for OnConfigurationChanged(...) to be trigger when the screen rotates, you need to to edit your manifest and put that relationship/rule in.
It's easy to do but takes a bit of explaining and it was answered before in this question:
Activity restart on rotation Android
Edit: Here is the dev guide on how to handle configuration changes
http://developer.android.com/guide/topics/resources/runtime-changes.html
Edit #2: First, let me suggest using Imran's solution. It follows the Developer Guide better and the end results will be the same.
Now, for the onConfigurationChanged solution.
Look at what you are doing with your onCreate:
1) Set the view. (Checkboxes are hidden at this point. Right?)
2) Call your DB and determine if you should display checkboxes (edit mode)
3) Make all the checkboxes visible.
Now, onConfigurationChanged also calls setContentView, at which point all your checkboxes are hidden again. So you need to repeat the process of making your checkboxes visible (#3 above). You probably don't need to repeat step #2 because the value should be retained, but I'm not sure how the logic of your app works, so you may need to re-do step #2.
Does that make sense?
Based on my experience, getview seems to be triggered at the end and it was why 'onRestoreInstanceState()' and 'onConfigurationChanged()' could not make it as getview will reset my checkboxes invisible as initialization in the layout xml file.
Therefore, the only solution I could find out was I must control them in getview for the answer.

Force Close of Android Popupwindow when changing orientation to portrait at run time, why?

Here's my MAIN ACTIVITY
public static boolean popupStatus=false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
popupStatus = savedInstanceState.getBoolean("Open");
}
setContentView(R.layout.main);
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("Open", DateTimePicker.openPopup);
super.onSaveInstanceState(savedInstanceState);
}
I have DateTimePicker.java class which has 1 button and 1 Textview. Clicking on button, my another class Calendar.java get populated in PopupWindow and this Popup window displays my class Calendar.java . I have created different layouts of my Calendar.java class for portrait and landscape mode. Here's DateTimePicker.java some snippet of code,
public static boolean openPopup = false;
textView = new TextView(this.getContext());
this.addView(textView, layoutParams);
button = new Button(this.getContext());
button.setText("C");
this.addView(button, layoutParams1);
button.setOnClickListener(this);
if(Main.popupStatus){
button.performClick();
}
public void onClick(View v) {
if(Main.popupStatus){
new Handler().postDelayed(new Runnable() {
public void run() {
openCalendar();
}
}, 100);
}
else{
openCalendar();
}
private void openCalendar() {
Calendar calendar = new Calendar(this.getContext());
if(portrait.equals(orientation)){
pw = new PopupWindow(calendarLayout, 245, 284, true);
}
else{
pw = new PopupWindow(calendarLayout, 295, 240, true);
}
pw.setOutsideTouchable(false);
pw.showAtLocation(this, Gravity.NO_GRAVITY, 10, 80);
openPopup = true;
}
public void closeCalendar(){
pw.dismiss();
openPopup = false;
}
Main.XML contain DateTimePicker .
Actually I wanted my Popup window to be opened up even when orientation gets changed at run time, so I have done it through setting flag openPopup = true; in openCalendar() method and if it is opened and orientation gets changed at run time, this flag will be saved in onSaveInstanceState() method. After orientation will change, it will be checked in onCreate() and popup will be opened up for respective orientation mode. I hope you got my point.
PROBLEM: Initially When I click on button in Portrait mode, popup window pops up for portrait layout. then without dismissing popup window, I change the orientation to landscape. And after changing, I can see my popup window as intact and appears on screen of landscape layout. Till now it works fine. But IF popup window is opened up in landscape mode and then I change the orientation to portrait, popup window of portrait layout didn't come up and I see FORCE CLOSE message:/ Please help since I am working behind it so long and getting no clue. I would be very grateful to you all. Thanks!
P.S.: Changing orientation means I am pressing ctrl+F11 and changing orientation of Emulator
The emulator has an odd feature (some consider it a bug) in which changing from landscape to portrait in the emulator causes two configuration changes and two restarts of your activity. (One configuration change is the orientation and the other is an emulated change in the keyboard state.) The timing of the configuration changes frequently causes crashes like this. Try adding this attribute:
android:configChanges="keyboard|keyboardHidden"
to your <activity> tag in the manifest. See if that improves the situation.
Make sure you have your layout defined in layout-land folder and ensure onCreate is not called again and again. android:configChanges="keyboard|keyboardHidden" Put this in your manifest file so that the state is retained when you change orientation

Problem saving WebView scroll position during orientation change in Android 2.1

I have a problem preserving the current scroll position of content loaded in Android's WebView control during orientation changes. The issue currently exist only in Android 2.1 and Android 2.1 Update 1
The code that I use is based on the default Browser application and looks like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
mWebView.setWebViewClient(mWebViewClient);
if (savedInstanceState != null)
{
final WebBackForwardList list = mWebView.restoreState(savedInstanceState);
if (list == null)
return;
if (savedInstanceState.containsKey("currentPicture"))
{
final File f = new File(savedInstanceState.getString("currentPicture"));
mWebView.restorePicture(savedInstanceState, f);
f.delete();
}
}else
mWebView.loadUrl("http://slashdot.org/");
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
final WebBackForwardList list = mWebView.saveState(outState);
File mThumbnailDir = getDir("thumbnails", 0);
if (list != null)
{
final File f = new File(mThumbnailDir, mWebView.hashCode() + "_pic.save");
if (mWebView.savePicture(outState, f))
outState.putString("currentPicture", f.getPath());
}
}
This code works in Android 1.5, 1.6 and 2.0.1. In Android 2.1 the same code restores the position for a split second but then the page reloads and scrolls to position 0,0.
In Android 2.1 Update 1, it not only scrolls back to 0,0, but also changes the Zoom to fit the page width (similar to the browser Overview mode introduced in 2.1)
Does anyone know of a workaround or has any idea what may cause such behavior?
Just load the WebViewState in onRestoreInstanceState.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
Changing the orientation from landscape to portrait or vise-versa, it starts the new activity. Hence you the open as refreshed activity.
Put the the code in your AndroidManifest.xml and reload app.
"activity android:name=".Main
android:configChanges="orientation"
in activity tab. Thanks

Categories

Resources