The Power of Clean in Eclipse
Sometimes you will come across an error that you can’t figure out how to fix, even after triple checking your code. This can be Eclipse’s fault half of the time. Android builds a resource file called R.java (found in the ‘gen’ folder) that gives static int references to your XML items. These references can get old and need to be refreshed. Eclipse should do this for you automatically, but in the real world that never happens. So, whenever I run into a random error I always make sure to clean my project. To do this go to Project->Clean.

This will essentially rebuild your project (if ‘Build Automatically’ is checked) by generating all of the class files again and rebuilding the Android resource file. It should be harmless to your project, so give it a shot if you can’t figure out your pesky problem.
How to Make a Landscape Layout in Android
Handling screen orientation changes is one of the more frustrating parts of programming for Android to me. Users will often switch to landscape orientation for comfort or more horizontal screen space, but regardless of why you need to make sure that your app can make appropriate use of the screen changes. If you are displaying data in a table style, then it would make sense to have the rows more spaced out or even show more rows when the user switches to landscape. Luckily, this is very easy to do in Android. Here’s how:
HowtoDevelopAndroidapps.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class HowtoDevelopAndroidApps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What API Level Should Your Android App Support?
Choosing which version of Android to support can be tricky. You want to provide the best user experience while targeting the largest number of users possible. Sadly, the two often don’t go hand in hand. A useful tool to help you decide which version to develop for can be found here:
http://developer.android.com/resources/dashboard/platform-versions.html

(Current as of Aug 1st)
From this it is easy to see how version 1.5 and 1.6 are virtually extinct. A large portion of people still have 2.1 and a vast majority have 2.2 or higher. Personally, I try to target 2.1 and higher since that is about 95% of users.
How to Create a Button in Android
A button is the most common method for a user to submit input. Here’s how to do it:
SogacityActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SogacityActivity extends Activity {
EditText text;
Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button Pressed!", Toast.LENGTH_SHORT).show();
}
});
}
}
main.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"> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"/> </LinearLayout>
First, I find the button view and assign it to a variable using findViewById(). Next, I create an OnClickListener for the button to detect when the user presses it.
Continue reading »
How to Get Text from Text Box in Android
A text box is a fundamental way of retrieving input from a user. Luckily, it is very easy to fetch the text that the user types into the box. Here’s an example with an EditText:
SogacityActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SogacityActivity extends Activity {
EditText text;
Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.text);
button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), text.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
main.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"> <EditText android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save"/> </LinearLayout>
First, I find the view with findViewById() and assign it to a variable. After that it is trivial to get the text using the getText() method. Don’t forget to call toString() on it to get it as a string.
Continue reading »



