Saturday 4 February 2012

TIME PICKER

XML CODE:
<?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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DEMO" />
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:text="Demo Button" android:textSize="20dp"/>
</LinearLayout>

JAVA CODE:

package com.demo.ui;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class DemoButtonActivity extends Activity {
 private int hour, minute;
 static final int ID_TIMEPICKER = 0;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.main);
 Button imageButton = (Button)findViewById(R.id.button1);
 imageButton.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 final Calendar c = Calendar.getInstance();
 hour = c.get(Calendar.HOUR_OF_DAY);
 minute = c.get(Calendar.MINUTE);
 showDialog(ID_TIMEPICKER);
 }
 }); 
    }
    @Override
    protected Dialog onCreateDialog(int id) {
      switch(id){
      case ID_TIMEPICKER:
       return new TimePickerDialog(this, timeSetListener, hour, minute, false);
      default:
       return null;
      }
    }
    private TimePickerDialog.OnTimeSetListener timeSetListener= new  
TimePickerDialog.OnTimeSetListener(){
     public void onTimeSet(android.widget.TimePicker arg0, int arg1, int arg2) {
      Toast.makeText(getBaseContext(),
              String.valueOf(arg1) + ":" + String.valueOf(arg2),
              Toast.LENGTH_LONG).show();
     }};
  

Date PickerDialog

Xml:
 
<?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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DEMO" />
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:text="Demo Button" android:textSize="20dp"/>
</LinearLayout>
 
 
 
 
 
 
JAVA CODE:
 
package com.demo.ui;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class DemoButtonActivity extends Activity {
private int year, month, day;
static final int ID_DATEPICKER = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button imageButton = (Button)findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case ID_DATEPICKER:
return new DatePickerDialog(this, dateSetListener, year, month, day);
default:
return null;
}
}  
private DatePickerDialog.OnDateSetListener dateSetListener
= new DatePickerDialog.OnDateSetListener(){ 
public void onDateSet(android.widget.DatePicker arg0, int arg1,
int arg2, int arg3) {  
Toast.makeText(getBaseContext(),
String.valueOf(arg1) + "/"
+ String.valueOf(arg2+1) + "/"
+ String.valueOf(arg3),
Toast.LENGTH_LONG).show();
}
};
}
 
 
   

Option Menu Example

/res/menu/menu.xml

Without Menu icon:
xml version="1.0" encoding="utf-8"?>menu xmlns:android="http://schemas.android.com/apk/res/android" >item android:id="@+id/opt1"
android:title
<
="Option 1" />item android:id="@+id/opt2"
android:title
<
="Option 2" />item android:id="@+id/opt3"
android:title
<
="Option 3" />item android:id="@+id/opt4"
android:title
<
="Option 4" />item android:id="@+id/opt5"
android:title
</

With Menu Icon:
<?
<
<
xml version="1.0" encoding="utf-8"?>menu xmlns:android="http://schemas.android.com/apk/res/android" >item android:id="@+id/opt1" android:icon="@drawable/ic_launcher"
android:title
<
="Option 1" />item android:id="@+id/opt2" android:icon="@drawable/ic_launcher"
android:title
<
="Option 2" />item android:id="@+id/opt3" android:icon="@drawable/ic_launcher"
android:title
<
="Option 3" />item android:id="@+id/opt4" android:icon="@drawable/ic_launcher"
android:title
<
="Option 4" />item android:id="@+id/opt5" android:icon="@drawable/ic_launcher"
android:title
</

JAVA CODE:
package
com.demo.ui;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuInflater;
import
android.view.MenuItem;
import
android.view.View.OnClickListener;
import
android.view.View;
import
android.view.Window;
import
android.widget.Button;
public
class DemoButtonActivity extends Activity {/** Called when the activity is first created. */
@Override
requestWindowFeature(Window.
setContentView(R.layout.
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);FEATURE_NO_TITLE);main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.
}
menu, menu);return true;@Override
public boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stub
}
}
}


OPtion Menu without Xml file:

package
com.demo.ui;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuInflater;
import
android.view.MenuItem;
import
android.view.View.OnClickListener;
import
android.view.View;
import
android.view.Window;
import
android.widget.Button;
public
class DemoButtonActivity extends Activity {/** Called when the activity is first created. */
@Override
requestWindowFeature(Window.
setContentView(R.layout.
}
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);FEATURE_NO_TITLE);main); @Override
menu.add(0, 0, 0,
menu.add(0, 1, 1,
menu.add(0, 2, 2,
menu.add(0, 3, 3,
menu.add(0, 4, 4,
}
public boolean onCreateOptionsMenu(Menu menu) {"Option 0");"Option 1");"Option 2");"Option 3");"Option 4"); return true;@Override
}
}
}
public boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case (0):break;case (1):break;case (2):break;case (3):break;case (4):break;return true;

switch(item.getItemId()){case (R.id.opt1):break;case (R.id.opt2):break;case (R.id.opt3):break;case (R.id.opt4):break;case (R.id.opt5):break;return true;
="Option 5" />menu>
="Option 5" />menu>
<?
<
<

AlertDialog.Builder Example

Button imageButton = (Button)findViewById(R.id.
imageButton.setOnClickListener(

AlertDialog.Builder dig=
dig.setTitle(
dig.setMessage(
dig.setPositiveButton(

dialog.cancel();
}
});
AlertDialog alert = dig.create();
alert.show();
 

}
});


button1);new OnClickListener() { public void onClick(View v) {new AlertDialog.Builder(DemoButtonActivity.this);"AlertDialog");"AlertDialog Demo");"OK", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {

Friday 3 February 2012

Display a icon/image on button using Java code and Xml code

The Method
Button.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

Example:
Button imageButton = (Button)findViewById(R.id.imagebutton);
imageButton.setCompoundDrawablesWithIntrinsicBounds(
0, //left
0, //top
R.drawable.icon, //right
0); //bottom

XML Code:
Example:
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- I'm a Button -"
android:drawableRight="@drawable/icon"
/>