Funny layout problem :)

Put your problem here if it does not fit any of the other categories.

Funny layout problem :)

Postby xjaphx » Mon Sep 19, 2011 5:45 am

Well, it's kinda interesting problem.
I create an application just having only one Button in order to set Screen Orientation, switching between Portrait and Landscape.
If the current is Portrait, the button text should be: "Set Landscape"; also, if Landscape, the text should be: "Set Portrait".

Here the fun part, the coding:

Code: Select all
package pete.android.study;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
   
   Button mBtnSetOrientation = null;
   
   boolean mScreenState = false;

   static final String ORIENT_LANDSCAPE = "Set Landscape";
   static final String ORIENT_PORTRAIT  = "Set Portrait";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mBtnSetOrientation = (Button)findViewById(R.id.btnOrient);
          mBtnSetOrientation.setText(ORIENT_LANDSCAPE);

        mBtnSetOrientation.setOnClickListener(new OnClickListener() {           
         @Override
         public void onClick(View v) {            
            if(mScreenState == false) {
               mBtnSetOrientation.setText(ORIENT_PORTRAIT);
               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);               
            } else {
               mBtnSetOrientation.setText(ORIENT_LANDSCAPE);
               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);               
            }
            mScreenState = !mScreenState;
         }
      });
    }
   
   
}


The question is: Can you see where the problem is? and how to solve?
If you find it interesting, give it a try :D
[Android - Newbie]: contains all the basic stuffs that help you learn/study Android.
Just click to visit: ..:: Android Code Breaker Diary ::..

>>> YOU'RE ALL WELCOME <<<
User avatar
xjaphx
Junior Developer
Junior Developer
 
Posts: 22
Joined: Mon Jun 13, 2011 8:19 am

Top

Re: Funny layout problem :)

Postby Dockland » Mon Sep 19, 2011 7:29 am

afaik by default the activity is recreated when configuration changes happens.

Works (at least in emulator)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                             final Button btnSetOrientation = (Button) findViewById(R.id.btnChangeOrient);
  2.                 int orient = getResources().getConfiguration().orientation;
  3.                 m_portraitOrientation = orient == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  4.                 btnSetOrientation.setText(m_portraitOrientation ? "Landscape" : "Portrait");
  5.                 btnSetOrientation.setOnClickListener(new View.OnClickListener() {
  6.                        
  7.                         @Override
  8.                         public void onClick(View v) {
  9.                                 if (m_portraitOrientation) {
  10.                                         btnSetOrientation.setText("Portrait");
  11.                                         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  12.                                 } else {
  13.                                         btnSetOrientation.setText("Landscape");
  14.                                         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  15.                                 }
  16.                                 m_portraitOrientation = ! m_portraitOrientation;
  17.                         }
  18.                 });
  19.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Re: Funny layout problem :)

Postby xjaphx » Mon Sep 19, 2011 5:08 pm

@Dockland: nice one :)

Mine is to add attribute: android:configChanges="orientation", to the acitivity in AndroidManifest.xml
based on the documentation on Acitivity#ConfigurationChanges: http://developer.android.com/reference/ ... ionChanges
[Android - Newbie]: contains all the basic stuffs that help you learn/study Android.
Just click to visit: ..:: Android Code Breaker Diary ::..

>>> YOU'RE ALL WELCOME <<<
User avatar
xjaphx
Junior Developer
Junior Developer
 
Posts: 22
Joined: Mon Jun 13, 2011 8:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 18 guests