RSS

Android Tutorial – Creating a List Menu

25 Jun

Ok, on the menu today is a look into List Menu in Android, kinda looks like this: Image

If you’ve ever used this before then you must have realized that when you click on an option another activity( thats whats is called in Android Language) is started. Here is the code:

package com.learning.gilo;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuList extends ListActivity{ //The ListActivity contains all the methods that Activity had.

String classes[] = {"FirstActivity","AnathaClass", "AnathaClass2","AnathaClass3"}; //This will appear on the screen
protected void onCreate(Bundle b){
super.onCreate(b);
setListAdapter(new ArrayAdapter<String>(MenuList.this, android.R.layout.simple_list_item_1, classes));/*
The list is put on the screen
*/

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {//This method gets called when you press an option
super.onListItemClick(l, v, position, id);
String usersChoice = classes[position]; //position is the index of the classes array indicating the item you pressed
try {

//the String in the array are the class names of the activities to be launched
Class ourClass = Class.forName("com.learning.gilo." + usersChoice);

Intent myIntent = new Intent(MenuList.this, ourClass);

startActivity(myIntent); //this will start the next activity

} catch (ClassNotFoundException e){
e.printStackTrace();
}


}


}
Advertisements
 
6 Comments

Posted by on June 25, 2012 in Android, Android tutorials

 

6 responses to “Android Tutorial – Creating a List Menu

  1. buy auto insurance

    May 10, 2013 at 1:57 pm

    Hey there! I’ve been following your weblog for some time now and finally got the courage to go ahead and give you a shout out from Dallas Texas! Just wanted to mention keep up the good job!

     
  2. Suggested Site

    May 23, 2013 at 10:44 am

    Gday! I hope you don’t mind but I decided to post your web site: https://mycodeandlife.wordpress.com/2012/06/25/android-tutorial-creating-a-list-menu/comment-page-1/ to my internet directory website. I used, “Android Tutorial” as your weblog title. I hope this is acceptable with you. In the event that you’d like me to change the title or perhaps remove
    it entirely, e-mail me at valentin_robins@zoho.com. Thank you so much.

     
  3. free online auto insurance quotes

    June 5, 2013 at 10:27 pm

    Hey! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My website looks weird when browsing from my iphone 4. I’m
    trying to find a theme or plugin that might be
    able to resolve this problem. If you have any suggestions, please
    share. Thank you!

     
  4. Josie

    June 6, 2013 at 7:32 am

    Hi would you mind stating which blog platform you’re using? I’m planning to start my own blog soon but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.
    P.S My apologies for being off-topic but I had to ask!

     
    • Gilbert Kimutai

      June 8, 2013 at 4:43 am

      i use wordpress.

       
  5. androidbabie

    January 16, 2014 at 6:51 pm

    thanks for your post. I would like to show title not activity name on list at screen . How can I do that?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: