Friday, February 8, 2013

Android 101- Part 2

Lets begin coding !

This is a very simple tutorial. In this tutorial we will learn how to add a simple listview (tableview) to our screen.
This is a one screen app and all it does it map strings of an array to each row in the listview.

Lets create our very first project. Android101. You can see the screenshots below to see what settings I used.











































































Now click on Finish. You will see Android 101 on left panel of Eclipse as shown in the screenshot below.





























Select activity_main.xml. On the right panel you will see Outline Tab. By default you will see RelativeLayout and TextView as its child. 


























Delete TextView. Your Outline tab will have only RelativeLayout now.























In the palette, Under Composite you can drag and drop the ListView on your activity_main.xml





























Finally  activity_main.xml will look like following






























Now go to your MainActivity.java file. You will see following code by default. Now we need to write more code  in it.































Note: If you remember when I dropped listview in xml file its name can be seen in Properties of activity_main.xml file, id is @+id/listView1 .We need to know this name to create the reference in .java file.

Don't know where the Outline Window is ? Check out this article.




























We need to create a reference in our code to listview and then create an array and connect array with listview. As you can see that while I am creating a reference to listview, I can use code completion technique to help me code faster.






















Here is the code for you
________________________________________________________________________________


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView=(ListView)findViewById(R.id.listView1);
//Create the array to fill the list view with
final String []listArray= new String[]{"Apple","Banana","Cherry","Lemon"};
ArrayAdapter<String> madapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,listArray);
myListView.setAdapter(madapter);
//When you tap on any row. it prints the string of that row.
myListView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   
  String itemValue=listArray[position];
  Log.d("item clicked at", itemValue);
  }


}); 
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}








________________________________________________________________________________



After you have added all the code in MainActivity.java .Hit the RUN green button.
You will see following pop up, Select Android Application.









































When you run the app for the first time on AVD. It will take long time to start  like 5 mins may be.
So you will see following screen for long time.
























After 5 mins or so, you will see home screen on your AVD.






















You can unlock your home screen. If you are able to run the app successfully; you will see following in console of Eclipse.

[2013-02-08 17:22:19 - Android101] ------------------------------
[2013-02-08 17:22:19 - Android101] Android Launch!
[2013-02-08 17:22:19 - Android101] adb is running normally.
[2013-02-08 17:22:19 - Android101] Performing com.reebok.android101.MainActivity activity launch
[2013-02-08 17:22:25 - Android101] Automatic Target Mode: Preferred AVD 'ReetuSim2' is available on emulator 'emulator-5554'
[2013-02-08 17:22:25 - Android101] Uploading Android101.apk onto device 'emulator-5554'
[2013-02-08 17:22:25 - Android101] Installing Android101.apk...
[2013-02-08 17:22:46 - Android101] Success!
[2013-02-08 17:22:46 - Android101] Starting activity com.reebok.android101.MainActivity on device emulator-5554
[2013-02-08 17:22:50 - Android101] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.reebok.android101/.MainActivity }






And you will be able to see listview in AVD screen finally !! Download the code.























If you have multiple AVDs and you want to switch it to see what your app will run like on another AVD with different dimension. Here is a little tutorial to help. LINK




No comments:

Post a Comment