• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 12 comments


SIMPLE EDITTEXT

SOURCE CODE [main.xml] is

<?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">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<EditText android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

    <Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show the Text" />

</LinearLayout>

SOURCE CODE [EditTextExample.java] is

package com.EditTextExample;

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 EditTextExample extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
                               
final EditText et;
final Button b;
       
et = (EditText) findViewById(R.id.edittext);
b = (Button) findViewById(R.id.button);
       
                                b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
                                                                String text=et.getText().toString();
                                                               
                                                Toast msg = Toast.makeText(getBaseContext(),
                                                                                text, Toast.LENGTH_LONG);
msg.show();
                                                }
                                });
}
}

The OUTPUT will be


https://lh5.googleusercontent.com/_xnrF0YSUul8/TblFiiaZ0kI/AAAAAAAAAIg/3ePvaNbV7Eg/simple_edittext1.png


https://lh3.googleusercontent.com/_xnrF0YSUul8/TblFiyo-Y2I/AAAAAAAAAIk/ZNaUCmdj2qg/simple_edittext2.png

Posted by Jack - - 7 comments


SIMPLE BUTTON CLICK

SOURCE CODE [main.xml] is

<?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">
               
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

                <Button android:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 1" />

                <Button android:id="@+id/button2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 2" />

</LinearLayout>

SOURCE CODE [ButtonExample.java] is

package com.ButtonExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ButtonExample extends Activity
{
Button b1,b2;

public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);

b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 1", Toast.LENGTH_LONG);
                                                                msg.show();
}
                                });

b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 2", Toast.LENGTH_LONG);
 msg.show();
}
                                });
 }
}

The OUTPUT will be


https://lh5.googleusercontent.com/_xnrF0YSUul8/TbkRukOp9rI/AAAAAAAAAIM/orjOySOvp0g/simple_button1.png


https://lh3.googleusercontent.com/_xnrF0YSUul8/TbkRuhUTv2I/AAAAAAAAAIQ/CMWs8yNDJ68/simple_button2.png

Posted by Jack - - 9 comments


SIMPLE GRIDVIEW

SOURCE CODE [main.xml] is

<?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">

                <TextView android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="@string/hello" />

                <GridView android:id="@+id/gridview"
android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
android:numColumns="auto_fit"
                                android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
                                android:columnWidth="90dp"
android:stretchMode="columnWidth"
                                android:gravity="center" />

</LinearLayout>

SOURCE CODE [GridViewExample.java] is

package com.GridViewExample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewExample extends Activity
{

public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

GridView gridview = (GridView) findViewById(R.id.gridview);
                                 gridview.setAdapter(new ImageAdapter(this));
}

public class ImageAdapter extends BaseAdapter
{
private Context mContext;

public ImageAdapter(Context c)
{
mContext = c;
                                }

                                public int getCount()
{
                                                return mThumbIds.length;
                                }

public Object getItem(int position)
{
                                                return null;
                                }

public long getItemId(int position)
{
return 0;
                                }

// create a new ImageView for each item referenced by the Adapter
                                public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
                                                if (convertView == null)
{
 imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
                                                }
imageView.setImageResource(R.drawable.icon);
                                                return imageView;
                                }
private Integer[] mThumbIds = {R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
                                                R.drawable.icon, R.drawable.icon,
                                                R.drawable.icon, R.drawable.icon,
                                                R.drawable.icon, R.drawable.icon};
}   

}

The OUTPUT will be


https://lh5.googleusercontent.com/_xnrF0YSUul8/TbaJzeBhTXI/AAAAAAAAAH4/B-u0yNQRLWQ/simple_gridview.png

Posted by Jack - - 10 comments

SIMPLE MENU

SOURCE CODE is

package com.MenuExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MenuExample extends Activity
{
               
private static final int MENU_ITEM_1 = Menu.FIRST + 1;
                private static final int MENU_ITEM_2 = Menu.FIRST + 2;
                private static final int MENU_ITEM_3 = Menu.FIRST + 3;
               
                Toast msg;
               
                @Override
                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);
                }
   
                @Override
                public boolean onCreateOptionsMenu(Menu menu)
                {
                                menu.add(Menu.NONE, MENU_ITEM_1, Menu.NONE,
                                "Menu Item 1").setIcon(R.drawable.icon);

                                menu.add(Menu.NONE, MENU_ITEM_2, Menu.NONE,
                                "Menu Item 2").setIcon(R.drawable.icon);

                                menu.add(Menu.NONE, MENU_ITEM_3, Menu.NONE,
                                "Close").setIcon(R.drawable.icon);

                                return (super.onCreateOptionsMenu(menu));

                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item)
{
                                switch (item.getItemId())
{
case MENU_ITEM_1:
                                                                 msg = Toast.makeText(MenuExample.this,
                                                                                "Menu Item 1 Clicked", Toast.LENGTH_LONG);
                                                                msg.show();
                                                                break;

                                                case MENU_ITEM_2:
                                                                 msg = Toast.makeText(MenuExample.this,
                                                                                "Menu Item 2 Clicked", Toast.LENGTH_LONG);
                                                                msg.show();
                                                                break;


                                                case MENU_ITEM_3:
                                                                finish();               
                                }
                                return (super.onOptionsItemSelected(item));
                }
   
}



The OUTPUT will be


https://lh6.googleusercontent.com/_xnrF0YSUul8/TbU9bmmCPnI/AAAAAAAAAHo/TBiBEFln8tY/simple_menu1.png


https://lh6.googleusercontent.com/_xnrF0YSUul8/TbU9bcLfk4I/AAAAAAAAAHk/aO8NUtKg3Cw/simple_menu2.png

Posted by Jack - - 4 comments


AN ACTIVITY WITH AN EXPANDING LISTVIEW

Creating a Simple ListView

If you have an activity that will only contain a single ListView control, you can derive your activity from the ListActivity instead of Activity. However, I think I might like to show some extra info below my ListView so I chose to have a separate ListView object. My activity layout (main.xml) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <ListView android:id="@+id/ listview "
                                 android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1" />
</LinearLayout>

And I need the layout for items in the ListView (listitem.xml):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="24dip"
                android:padding="8dip"/>

Note a nice trick that I’ve used to get the TextView to “dock” at the bottom with a definied height, and have the ListView automatically fill out the rest of the height. This kind of thinking is important since Android devices can have different resolutions. The trick is to set the layout_height to zero pixels and the layout_weight to one (default is zero). Not sure about the logic behind that, but it works!

In order to have something to put into my ListView, I created a string array and I added to the ListView with the ArrayAdapter object.

public class ListViewExample extends Activity
{
                private ListView lv;
                private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                                "SEVEN", "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle icicle)
{
                                super.onCreate(icicle);
                                setContentView(R.layout.main);
                                lv = (ListView) findViewById(R.id.listview);
                                lv.setAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, listview_array));
                }
}

Creating a ContextMenu in Android

When the user long-clicks, the event onCreateContextMenu is fired for the control that the user is clicking. For me, that is the ListView control. But since I don’t want to write a custom ListView-derived class, I want to catch that event in my activity. There does not seem to be any bubbling going on. Events fired in a child control does not bubble up to the parent if they are unhandled.

But obviously, the api designers have thought of this since there is a special method for this situation. Call the registerForContextMenu in your activity for this! This will actually make sure your overridden methods for both onCreateContextMenu and onContextItemSelected is called for the ListView-events as we’ll see soon.

Next, we’ll provide the implementation of onCreateContextMenu. Here I want to ensure that the event comes from the ListView and if so, I want to determine on which item in the ListView the user long-clicked.

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
                  if (v.getId()==R.id.listview)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("Title " +listview_array[info.position]);

//menu.add(groupId, itemId, order, title)
menu.add(Menu.NONE, 0, 0, "Item 1");
menu.add(Menu.NONE, 1, 1, "Item 2");
                  
                  }
}

As you can see, the argument of type ContextMenuInfo can actually change depending on what type of control is sending the event. For ListViews, the class you need to type cast into is AdapterView.AdapterContextMenuInfo. From there I used the position, which in my case corresponds to the index into the string-array. From the array I retrieve the string for that particular item and use as title for the menu. Then you can of course add all the menu commands you like.

When creating the menu items with the add-call, I specify that I don’t want any grouping of the items (Menu.NONE) and that the order and id of the item is the same. The last argument to add is the text to display for the item.

Responding to selected MenuItem

If the user dismisses the context menu (for instance, by back button) you don’t need to do anything. But for catching the actual selection of one of the items, you need to override onContextItemSelected.

public boolean onContextItemSelected(MenuItem item)
{
                  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                  int menuItemIndex = item.getItemId();
                  String[] menuItems = {"Option 1", "Option 2"};
                  String menuItemName = menuItems[menuItemIndex];
                  String listItemName = listview_array[info.position];

                  Toast t = Toast.makeText(this, String.format("Selected %s for item %s", menuItemName, listItemName), Toast.LENGTH_LONG);
                  t.show();
                  return true;
}

The MenuItem argument holds all information that you need. The ContextMenuInfo object that got sent to onCreateContextMenu is still there and still needs type casting. Or I guess you could have saved that info in the activity between the calls, but I didn’t.

The id of the menu item selected is the same as the index into the string array of menu item texts for me. Instead of just outputting the menu command name and the list item text in a Toast, you would most likely have a big switch statement on menuItemIndex.

https://lh4.googleusercontent.com/_xnrF0YSUul8/TaP1ywY5XZI/AAAAAAAAAHM/N_3xkzfqm3s/listview_tutorial1.png https://lh6.googleusercontent.com/_xnrF0YSUul8/TaP1yjH0mpI/AAAAAAAAAHI/R4SyX4EXmCQ/listview_tutorial2.png https://lh5.googleusercontent.com/_xnrF0YSUul8/TaP1zAVJk5I/AAAAAAAAAHQ/XYAj0kKaR_8/listview_tutorial3.png

Thank you!


Have a great day!

Posted by Jack - - 3 comments


SIMPLE ALERTDIALOG

SOURCE CODE is

package com.AlertDialogExample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertDialogExample extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
               
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                adb.setTitle("Set Title here");
                adb.setMessage("Set the Text Message here");
                adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
// Action for 'Ok' Button
                                }
                });
                adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
                                public void onClick(DialogInterface dialog, int id)
                                                {
                                                // Action for 'Cancel' Button
                                                dialog.cancel();
                                }
                });
                adb.setIcon(R.drawable.icon);
adb.show();
}
}

The OUTPUT will be


https://lh4.googleusercontent.com/_xnrF0YSUul8/TaPkP7vZksI/AAAAAAAAAHA/IM0PESrIjco/simple_alertdialog.png

Posted by Jack - - 20 comments


LISTVIEW ONCLICK
 
SOURCE CODE [main.xml] is 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android">
                <ListView android:id="@+id/listview"
android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />
</LinearLayout>

SOURCE CODE [ListviewOnclickExample.java] is 

package com.ListViewExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListviewOnclickExample extends Activity
{
                private ListView lv;
                private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                                "SEVEN", "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle icicle)

{
super.onCreate(icicle);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
AlertDialog.Builder adb = new AlertDialog.Builder(
ListviewOnclickExample.this);
adb.setTitle("ListView OnClick");
adb.setMessage("Selected Item is = "
+ lv.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();                     
                                                }
                                });
                }
}


The OUTPUT will be

https://lh6.googleusercontent.com/_xnrF0YSUul8/TaPcM8-D-TI/AAAAAAAAAGo/9uyK_oFKYXs/listview_onclick1.png

https://lh5.googleusercontent.com/_xnrF0YSUul8/TaPdt6GSm1I/AAAAAAAAAG4/NNcHKvX1N-M/listview_onclick2.png