Android Application Development Training Tutorial
For more info visit [Link]
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Applications
Color Picker
[Link]
import import import import import import import [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];
public class MainActivity extends Activity implements [Link] { /** Called when the activity is first created. */ private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness"; private static final String COLOR_PREFERENCE_KEY = "color"; TextView tv; public void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); setContentView([Link]); tv = (TextView) findViewById([Link].TextView01); Button btn = (Button) findViewById([Link].Button01); [Link](new [Link]() { @Override public void onClick(View v) { int color = [Link]( [Link]).getInt(COLOR_PREFERENCE_KEY, [Link]); new ColorPickerDialog([Link], [Link], color).show(); } }); } @Override public void colorChanged(int color) { [Link](this).edit().putInt( COLOR_PREFERENCE_KEY, color).commit(); [Link](color);
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
} }
[Link]
import import import import import import import import import import import import import import import [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];
public class ColorPickerDialog extends Dialog { public interface OnColorChangedListener { void colorChanged(int color); } private final OnColorChangedListener mListener; private final int mInitialColor; private static class ColorPickerView extends View { private final Paint mPaint; private final Paint mCenterPaint; private final int[] mColors; private final OnColorChangedListener mListener; ColorPickerView(Context c, OnColorChangedListener l, int color) { super(c); mListener = l; mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 }; Shader s = new SweepGradient(0, 0, mColors, null); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); [Link](s); [Link]([Link]);
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
[Link](32); mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG); [Link](color); [Link](5); } private boolean mTrackingCenter; private boolean mHighlightCenter; protected void onDraw(Canvas canvas) { float r = CENTER_X - [Link]() * 0.5f; [Link](CENTER_X, CENTER_X); [Link](new RectF(-r, -r, r, r), mPaint); [Link](0, 0, CENTER_RADIUS, mCenterPaint); if (mTrackingCenter) { int c = [Link](); [Link]([Link]); if (mHighlightCenter) { [Link](0xFF); } else { [Link](0x80); } [Link](0, 0, CENTER_RADIUS + [Link](), mCenterPaint); [Link]([Link]); [Link](c); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(CENTER_X * 2, CENTER_Y * 2); } private static final int CENTER_X = 100; private static final int CENTER_Y = 100; private static final int CENTER_RADIUS = 32; private int floatToByte(float x) { int n = [Link](x); return n; }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
private int pinToByte(int n) { if (n < 0) { n = 0; } else if (n > 255) { n = 255; } return n; } private int ave(int s, int d, float p) { return s + [Link](p * (d - s)); } private int interpColor(int colors[], float unit) { if (unit <= 0) return colors[0]; if (unit >= 1) return colors[[Link] - 1]; float p = unit * ([Link] - 1); int i = (int) p; p -= i; // now p is just the fractional part [0...1) and i is the index int c0 = colors[i]; int c1 = colors[i + 1]; int a = ave([Link](c0), [Link](c1), p); int r = ave([Link](c0), [Link](c1), p); int g = ave([Link](c0), [Link](c1), p); int b = ave([Link](c0), [Link](c1), p); return [Link](a, r, g, b); } private int rotateColor(int color, float rad) { float deg = rad * 180 / 3.1415927f; int r = [Link](color); int g = [Link](color); int b = [Link](color); ColorMatrix cm = new ColorMatrix(); ColorMatrix tmp = new ColorMatrix(); cm.setRGB2YUV(); [Link](0, deg); [Link](tmp); tmp.setYUV2RGB(); [Link](tmp);
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
final float[] a = [Link](); int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b); int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b); int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b); return [Link]([Link](color), pinToByte(ir), pinToByte(ig), pinToByte(ib)); } private static final float PI = 3.1415926f; public boolean onTouchEvent(MotionEvent event) { float x = [Link]() - CENTER_X; float y = [Link]() - CENTER_Y; boolean inCenter = [Link](x * x + y * y) <= CENTER_RADIUS; switch ([Link]()) { case MotionEvent.ACTION_DOWN: mTrackingCenter = inCenter; if (inCenter) { mHighlightCenter = true; invalidate(); break; } case MotionEvent.ACTION_MOVE: if (mTrackingCenter) { if (mHighlightCenter != inCenter) { mHighlightCenter = inCenter; invalidate(); } } else { float angle = (float) [Link].atan2(y, x); // need to turn angle [-PI ... PI] into unit [0....1] float unit = angle / (2 * PI); if (unit < 0) { unit += 1; } [Link](interpColor(mColors, unit)); invalidate(); } break; case MotionEvent.ACTION_UP: if (mTrackingCenter) { if (inCenter) { [Link]([Link]()); } mTrackingCenter = false; // so we draw w/o halo invalidate();
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
} break; } return true; } } public ColorPickerDialog(Context context, OnColorChangedListener listener, int initialColor) { super(context); mListener = listener; mInitialColor = initialColor; } @Override protected void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); OnColorChangedListener l = new OnColorChangedListener() { public void colorChanged(int color) { [Link](color); dismiss(); } }; LinearLayout layout = new LinearLayout(getContext()); [Link]([Link]); [Link]([Link]); [Link](10, 10, 10, 10); [Link](new ColorPickerView(getContext(), l, mInitialColor), new [Link]( [Link].WRAP_CONTENT, [Link].WRAP_CONTENT)); setContentView(layout); setTitle("Pick a Color"); } }
CustomImageButton
[Link]
import [Link]; import [Link]; import [Link]; import [Link]; import [Link];
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; /* * Sample CustomImageButton * Programatic draw a own Button, whit different States * default, focused and pressed * * 14.02.2008 by Mauri for [Link] */ public class CustomImageButton extends Activity { private TextView mDebug; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { [Link](icicle); // prepare the Layout // two Buttons // Debug-TextView Below LinearLayout linLayoutMain = new LinearLayout(this); [Link]([Link]); LinearLayout linLayoutButtons = new LinearLayout(this); [Link]([Link]); // buttons MyCustomButton btn1 = new MyCustomButton(this, "btn1"); MyCustomButton btn2 = new MyCustomButton(this, "btn2"); // a TextView for debugging output mDebug = new TextView(this); // add button to the layout [Link](btn1, new [Link](100, 100)); [Link](btn2, new [Link](100, 100));
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
// add buttons layout and Text view to the Main Layout [Link](linLayoutButtons, new [Link](LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); [Link](mDebug, new [Link](LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); setContentView(linLayoutMain); } // Custom Button Class must extends Button, // because drawableStateChanged() is needed public class MyCustomButton extends Button { static final int StateDefault = 0; static final int StateFocused = 1; static final int StatePressed = 2; private int mState = StateDefault; private Bitmap mBitmapDefault; private Bitmap mBitmapFocused; private Bitmap mBitmapPressed; private String mCaption;
public MyCustomButton(Context context, String caption) { super(context); mCaption = caption; setClickable(true); // black Background on the View setBackgroundColor(0xff000000); // create for each State a Bitmap // white Image mBitmapDefault = [Link](100, 100, Config.RGB_565); // Blue Image mBitmapFocused = [Link](100, 100, Config.RGB_565); // Green Image mBitmapPressed = [Link](100, 100, Config.RGB_565); // create the Canvas Canvas canvas = new Canvas(); // define on witch Bitmap should the Canvas draw // default Bitmap [Link](mBitmapDefault);
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
// create the Drawing Tool (Brush) Paint paint = new Paint(); [Link](true); // for a nicer paint // draw a rectangle with rounded edges // white Line [Link](0xffffffff); // 3px line width [Link](3); // just the line, not filled [Link]([Link]); // create the Path Path path = new Path(); // rectangle with 10 px Radius [Link](new RectF(10, 10, 90, 90), 10, 10, [Link]); // draw path on Canvas with the defined "brush" [Link](path, paint); // prepare the "brush" for the Text Paint paintText = new Paint(); [Link](true); [Link](20); [Link](0xffffffff); // white // draw Text [Link](caption, 30, 55, paintText); // do some more drawing stuff here... // for the Pressed Image [Link](mBitmapPressed); // Greed Color [Link](0xff00ff00); [Link](0xff00ff00); // white Line [Link](path, paint); [Link](caption, 30, 55, paintText); // do some more drawing stuff here... // for the Pressed Image [Link](mBitmapFocused); // Blue Color [Link](0xff0000ff); [Link](0xff0000ff); // white Line
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
[Link](path, paint); [Link](caption, 30, 55, paintText); // do some more drawing stuff here... // define OnClickListener for the Button setOnClickListener(onClickListener); } @Override protected void onDraw(Canvas canvas) { switch (mState) { case StateDefault: [Link](mBitmapDefault, 0, 0, null); [Link](mCaption + ":default\n"); break; case StateFocused: [Link](mBitmapFocused, 0, 0, null); [Link](mCaption + ":focused\n"); break; case StatePressed: [Link](mBitmapPressed, 0, 0, null); [Link](mCaption + ":pressed\n"); break; } } @Override protected void drawableStateChanged() { if (isPressed()) { mState = StatePressed; } else if (hasFocus()) { mState = StateFocused; } else { mState = StateDefault; } // force the redraw of the Image // onDraw will be called! invalidate(); } private OnClickListener onClickListener = new OnClickListener() { public void onClick(View arg0) { [Link](mCaption + ":click\n"); } }; }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
DialogBox
[Link]
import import import import import import import import [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];
public class DialogBox extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); setContentView([Link]); /** We need to set up a click listener on the alert button */ Button alert = (Button) findViewById([Link]); [Link](this); /** We need to set up a click listener on the yesno button */ Button yesno = (Button) findViewById([Link]); [Link](this); /** We need to set up a click listener on the selectlist button */ Button selectlist = (Button) findViewById([Link]); [Link](this); /** We need to set up a click listener on the selectlistwithcheckbox button */ Button selectlistwithcheckbox = (Button) findViewById([Link]. listwithcheck); [Link](this); } public void onClick(View view) { /** check whether the alert button has been clicked */ if (view == findViewById([Link])) { // Create the alert box [Link] alertbox = new [Link](this); // Set the message to display [Link]("This is an alert box."); // Add a neutral button to the alert box and assign a click listener
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
[Link]("Ok", new [Link]() { // Click listener on the neutral button of alert box public void onClick(DialogInterface arg0, int arg1) { // The neutral button was clicked [Link](getApplicationContext(), "'OK' button clicked", Toast.LENGTH_LONG).show(); } }); // show the alert box [Link](); } /** check whether the yesno button has been clicked */ if (view == findViewById([Link])) { // Create the dialog box [Link] alertbox = new [Link](this); // Set the message to display [Link]("This is a dialog box with two buttons"); // Set a positive/yes button and create a listener [Link]("Yes", new [Link]() { // Click listener public void onClick(DialogInterface arg0, int arg1) { [Link](getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); } }); // Set a negative/no button and create a listener [Link]("No", new [Link]() { // Click listener public void onClick(DialogInterface arg0, int arg1) { [Link](getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); } }); // display box [Link](); } /** check whether the selectlist button has been clicked */ if (view == findViewById([Link])) { //List items final CharSequence[] items = {"Milk", "Butter", "Cheese"}; //Prepare the list dialog box [Link] builder = new [Link](this); //Set its title [Link]("Pick an item"); //Set the list items and assign with the click listener [Link](items, new [Link]() { // Click listener public void onClick(DialogInterface dialog, int item) {
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
// [Link](getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); [Link]("Yes", new [Link]() { // Click listener public void onClick(DialogInterface arg0, int item) { [Link](getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); // Set a negative/no button and create a listener [Link]("No", new [Link]() { // Click listener public void onClick(DialogInterface arg0, int arg1) { [Link](getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); } }); AlertDialog alert = [Link](); //display dialog box [Link](); } /** check whether the selectlistwithcheckbox button has been clicked */ if (view == findViewById([Link])) { //List items final CharSequence[] items = {"Milk", "Butter", "Cheese"}; //Prepare the list dialog box [Link] builder = new [Link](this); //Set its title [Link]("Pick an item"); //Set the list items along with checkbox and assign with the click listener [Link](items, -1, new [Link]() { // Click listener public void onClick(DialogInterface dialog, int item) { [Link](getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); //If the Cheese item is chosen close the dialog box if(items[item]=="Cheese") [Link](); } }); AlertDialog alert = [Link](); //display dialog box [Link](); } } }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
[Link]
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="[Link] 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="Dialogs" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show Alert" android:id="@+id/showalert" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show Yes/No" android:id="@+id/showyesorno" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show List" android:id="@+id/list" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show List with CheckBoxes" android:id="@+id/listwithcheck" /> </LinearLayout>
DynamicListView
[Link]
import [Link];
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
import import import import import import import import import import import import import import import import
[Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];
public class dynamicListView extends ListActivity implements OnClickListener { EditText textContent; Button submit; ListView mainListview; private static class ListViewAdapter extends BaseAdapter { private LayoutInflater mInflater;
public ListViewAdapter(Context context) { mInflater = [Link](context);
} public int getCount() { return [Link](); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ListContent holder;
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
if (convertView == null) { convertView = [Link]([Link], null); holder = new ListContent(); [Link] = (TextView) [Link]([Link].TextView01); [Link]([Link]().getDrawable([Link]. icon), null, null, null); [Link](holder); } else { holder = (ListContent) [Link](); }
[Link]([Link](position)); return convertView; } static class ListContent { TextView text; } } @Override public void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); setContentView([Link]); [Link]("[Link]");[Link]("Android FROYO"); textContent=(EditText)findViewById([Link].EditText01); submit=(Button)findViewById([Link].Button01); [Link](this); setListAdapter(new ListViewAdapter(this)); } private static final ArrayList<String> ListviewContent = new ArrayList<String>(); @Override public void onClick(View v) { if(v==submit) { [Link]([Link]().toString()); setListAdapter(new ListViewAdapter(this)); }
} }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
[Link]
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="[Link] android:orientation="vertical"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent"> <EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></EditText> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add to Listview"></Button> </LinearLayout> <ListView android:layout_height="wrap_content" android:id="@android:id/list" android:layout_width="fill_parent"></ListView> </LinearLayout>
[Link]
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" xmlns:android="[Link] android:orientation="vertical" android:layout_height="wrap_content" android:background="#FFFFFF" android:paddingLeft="5px"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_height="wrap_content" android:textColor="#000" android:layout_width="fill_parent" android:layout_weight="1"> </TextView> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
android:background="@drawable/arrow"> </TextView> </LinearLayout> </LinearLayout>
AlertDialog with RadioButton
import import import import import [Link]; [Link]; [Link]; [Link]; [Link];
public class Example1 extends Activity { /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); setContentView([Link]); final CharSequence[] PhoneModels = {"iPhone", "Nokia", "Android"}; [Link] alt_bld = new [Link](this); alt_bld.setIcon([Link]); alt_bld.setTitle("Select a Phone Model"); alt_bld.setSingleChoiceItems(PhoneModels, -1, new [Link]() { public void onClick(DialogInterface dialog, int item) { [Link](getApplicationContext(), "Phone Model = "+PhoneModels[item], Toast.LENGTH_SHORT).show(); } }); AlertDialog alert = alt_bld.create(); [Link](); } }
[Link]
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="[Link] 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" />
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
<Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image1" android:layout_width="320px" android:layout_height="250px" android:scaleType="fitXY" /> </LinearLayout>
With Ok and Cancel Button
import import import import [Link]; [Link]; [Link]; [Link];
public class ExampleApp extends Activity { /** Called when the activity is first created. */ protected void onCreate(Bundle savedInstanceState) { [Link](savedInstanceState); setContentView([Link]); final String items[] = {"item1","item2","item3"}; [Link] ab=new [Link]([Link]); [Link]("Title"); [Link](items, 0,new [Link]() { public void onClick(DialogInterface dialog, int whichButton) { // onClick Action } }) .setPositiveButton("Ok", new [Link]() { public void onClick(DialogInterface dialog, int whichButton) { // on Ok button action } }) .setNegativeButton("Cancel", new [Link]() { public void onClick(DialogInterface dialog, int whichButton) { // on cancel button action } }); [Link](); } }
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi