Ever thought about WoWing your friends ….or simply just doing cheap tricks with your Android or more ridiculous like plotting your daily Starbucks allowance? Here’ s a neat way of doing exactly that – Screen Orientations & Rotations.
Some basics: Just to clear your frame of mind (..and mine) that is …mmmh
Portrait ..yep UpRight …and
Landscape yep lazy laying. Ok..now lets get to it!
An Example:
Plotting a list of numeric values which are contained in a list view and upon device rotation switches to a graphical plot view of the data. How neat is that!
Portrait List View of your data:

Landscape Graphical Chart View of your data:

Download:
AndroidOrientationChanges.zip
Steps involved:
Modify AndroidManifest.xml to allow for orientation changes and prevent keyboard from popping up.
Write layout XML files – display list
Write Java Activity – charts with DroidCharts
NOTE:
Screen orientation changes are per Activity :
android:configChanges=”orientation|keyboardHidden”
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.examples" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".DemoList" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DemoCharts" android:configChanges="orientation|keyboardHidden"></activity> </application> </manifest>
File: data_listview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ListView> </LinearLayout>
File: DemoList.java
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class DemoList extends ListActivity implements OnItemClickListener
{
private static final String tag = "DemoList";
private ListView listview;
private ArrayAdapter<String> listAdapter;
// Want to pass data values as parameters to next Activity/View/Page
private String params;
// Our data for plotting
private final double[][] data = { { 1, 1.0 }, { 2.0, 4.0 }, { 3.0, 10.0 }, { 4, 2.0 }, { 5.0, 20 }, { 6.0, 4.0 }, { 7.0, 1.0 }, };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set the View Layer
setContentView(R.layout.data_listview);
// Get the Default declared ListView @android:list
listview = getListView();
// List for click events to the ListView items
listview.setOnItemClickListener(this);
// Get the data to
ArrayList<String> dataList = getDataStringList(data);
// Create an Adapter to for viewing the ListView
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
// Bind the adapter to the ListView
listview.setAdapter(listAdapter);
// Set the parameters to pass to the next view/ page
setParameters(data);
}
private String doubleArrayToString(double[][] dataVals)
{
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < dataVals.length; i++)
{
String datum = "[" + String.valueOf(dataVals[i][0]) + "," + String.valueOf(dataVals[i][1]) + "]";
if (i < dataVals.length - 1)
{
strBuilder.append(datum + " , ");
}
else
{
strBuilder.append(datum);
}
}
return strBuilder.toString();
}
/**
* Sets parameters for the Bundle
*
* @param dataList
*/
private void setParameters(double[][] dataVals)
{
params = toJSON(dataVals);
}
public String getParameters()
{
return this.params;
}
/**
* Need todo JSONArray
*
* @param dataVals
* @return
*/
private String toJSON(double[][] dataVals)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("[");
strBuilder.append(doubleArrayToString(dataVals));
strBuilder.append("]");
return strBuilder.toString();
}
/**
*
* @param dataVals
* @return
*/
private ArrayList<String> getDataStringList(double[][] dataVals)
{
ArrayList<String> list = new ArrayList<String>();
// TODO: CONVERT INTO JSON FORMAT
for (int i = 0; i < dataVals.length; i++)
{
String datum = "[" + String.valueOf(dataVals[i][0]) + "," + String.valueOf(dataVals[i][1]) + "]";
list.add(datum);
}
return list;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Create an Intent to switch view to the next page view
Intent intent = new Intent(this, DemoCharts.class);
// Pass parameters along to the next page
intent.putExtra("param", getParameters());
// Start the activity
startActivity(intent);
Log.d(tag, "Orientation Change...");
Log.d(tag, "Params: " + getParameters());
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
{
// Upon clicking item in list pop a toast
String msg = "#Item: " + String.valueOf(position) + " - " + listAdapter.getItem(position);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
File: DemoCharts.java
import java.util.ArrayList;
import net.droidsolutions.droidcharts.core.data.XYDataset;
import net.droidsolutions.droidcharts.core.data.xy.XYSeries;
import net.droidsolutions.droidcharts.core.data.xy.XYSeriesCollection;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class DemoCharts extends Activity
{
private static final String tag = "DemoCharts";
private final String chartTitle = "My Daily Starbucks Allowance";
private final String xLabel = "Week Day";
private final String yLabel = "Allowance";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Access the Extras from the Bundle
Bundle params = getIntent().getExtras();
// If we get no parameters, we do nothing
if (params == null) { return; }
// Get the passed parameter values
String paramVals = params.getString("param");
Log.d(tag, "Data Param:= " + paramVals);
Toast.makeText(getApplicationContext(), "Data Param:= " + paramVals, Toast.LENGTH_LONG).show();
ArrayList<ArrayList<Double>> dataVals = stringArrayToDouble(paramVals);
XYDataset dataset = createDataset("My Daily Starbucks Allowance", dataVals);
XYLineChartView graphView = new XYLineChartView(this, chartTitle, xLabel, yLabel, dataset);
setContentView(graphView);
}
private String arrayToString(String[] data)
{
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < data.length; i++)
{
strBuilder.append(data[i]);
}
return strBuilder.toString();
}
private String cleanNumericString(String val)
{
return val.replaceAll("\\[", "").replaceAll("\\]", "").trim();
}
private ArrayList<ArrayList<Double>> stringArrayToDouble(String paramVals)
{
ArrayList<ArrayList<Double>> plotVals = new ArrayList<ArrayList<Double>>();
if (paramVals.startsWith("[") && paramVals.endsWith("]"))
{
String[] vals = paramVals.substring(1, paramVals.length() - 1).split(" , ");
for (String v : vals)
{
if (v.startsWith("[") && v.endsWith("]"))
{
String[] dataVals = v.split(",");
String xvalStr = cleanNumericString(dataVals[0]);
String yvalStr = cleanNumericString(dataVals[1]);
Log.d(paramVals, xvalStr + " - " + yvalStr);
// Convert to Numeric Values
Double x = Double.parseDouble(xvalStr);
Double y = Double.parseDouble(yvalStr);
// Create (x,y) tuple for data point
ArrayList<Double> list1 = new ArrayList<Double>();
list1.add(x);
list1.add(y);
// Add to our final list
plotVals.add(list1);
}
Log.d(tag, "Values to plot: " + plotVals.toString());
}
}
return plotVals;
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private XYDataset createDataset(String title, ArrayList<ArrayList<Double>> dataVals)
{
final XYSeries series1 = new XYSeries(title);
for (ArrayList<Double> tuple : dataVals)
{
double x = tuple.get(0).doubleValue();
double y = tuple.get(1).doubleValue();
series1.add(x, y);
}
// Create a collection to hold various data sets
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
return dataset;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "Orientation Change", Toast.LENGTH_SHORT);
// Lets get back to our DemoList view
Intent intent = new Intent(this, DemoList.class);
startActivity(intent);
// Finish current Activity
this.finish();
}
}
thanks for tuttorials
You should tell the difference between orientation and rotation, because they are not the same… I’m sure it’s still a good article about orientation issues anyways.