This summer my brother and I decided to give my parents a break and get our own cell phone plan. I know, how grown up of us. Anyway, we decided on Verizon and the HTC Incredible. Me being a developer I immediately had visions of writing all kinds of sweet apps for my phone. Unfortunately the process of learning how to develop for Android took a little longer than i had hoped. I went to Android’s website and followed the instructions to set up the IDE and a few tutorials and then bought a book to get an overview of the capabilities of Android. After a few months I now feel that I have a good grasp of the OS and feel pretty confident enough to start writing apps that demonstrate certain functionality.

Being new to Java development in general I have struggled with things that I assumed would be trivial. I am not sure if it the lack of good documentation or the fact that I am new to the platform. Either way I hope to document my struggles, as well as my accomplishments, in hopes that it will help people like me who are just looking to get started and don’t want to have to look all over for relatively simple things.

With that out of the way lets get started. For my first topic on android I want to talk about how to handle the screen orientation changing. I will assume you already can lay out a simple UI with TextViews and Buttons. In android when the screen changes all of the buttons, labels and other controls are destroyed and redrawn when the orientation changes. That means if you assign text to a TextView from a button click and then turn the screen, the text you entered will no longer be shown in the TextView. To handle this your Activity needs to override the onConfigurationChanged method. For your activity to catch this though you need to add an attribute to the activity node in the manifest file to handle the change.

<activity android:name=".HelloAndroid"
                  android:label="@string/app_name"
                  android:configChanges="orientation">

 

With that in place you can now add your code to handle the change. The thing that threw me on this was that I thought the whole activity was restarted. Turns out that isn’t the case. The only thing I needed to do was put the code from my onCreate method into the onConfigurationChanged method. In my code I have a custom view with points on it which was what I was trying to redraw when the screen changed. I stored the list of points in the activity and set a property for them on the view if the list wasn’t null. If it were just a normal string I would have stored it the same way and then called setText() on a TextView on the onConfigurationChanged method. Don’t pay too much attention the the Custom View, I will cover how it works in another post.

Noticed the called to itemView.setPoints(points). This is where I take the list I have in memory and set it back the custom view so that it is able to display the items. The thing to take away is that you need to reinitialize all of your View items and their events just like you do when the Activity is first started. Please feel free to let me know if there is something I missed or anything that would make this example clearer. Here is the code for my Activity:

ToDoItemView itemView;
    EditText text;
    EditText txtX;
    EditText txtY;
    ArrayList<String> items;
    private GridPointList points;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        if (savedInstanceState != null && savedInstanceState.containsKey("Points"))
        	points = savedInstanceState.getParcelable("Points");
        
        SetupActivity();
    }   
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	super.onConfigurationChanged(newConfig);
    	setContentView(R.layout.main);
    	
    	SetupActivity();
    }
    
    private void SetupActivity() {
    	itemView = (ToDoItemView)findViewById(R.id.cstNote);
        itemView.setOnTouchListener(myTouch);
        
        if (points != null)
        	itemView.setPoints(points);
        
        text = (EditText)findViewById(R.id.txtText);
        txtX = (EditText)findViewById(R.id.txtX);
        txtY = (EditText)findViewById(R.id.txtY);
        
        text.setOnKeyListener(myListener);        
        txtX.setOnKeyListener(myListener);        
        txtY.setOnKeyListener(myListener);
    }