Introduction
- <?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="horizontal">
- <EditText android:id="@+id/edit_message"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:hint="@string/edit_message" />
- <Button android:id="@+id/button_send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_send"
- android:onClick="sendMessage" />
- </LinearLayout>
Button class

- Button with text
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_text"
- ... />
- Button with an icon using ImageButton
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/button_icon"
- ... />
- Button with text and icon using android.drawableLeft
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_text"
- android:drawableLeft="@drawable/button_icon"
- ... />
Responding to Click Events
- <?xml version="1.0" encoding="utf-8"?>
- <Button xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/button_send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_send"
- android:onClick="sendMessage" />
- /** Called when the user touches the button */
- public void sendMessage(View view) {
- // Do something in response to button click
- }
- Button button = (Button) findViewById(R.id.button_send);
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // Do something in response to button click
- }
- });
Pickers
Creating a Time Picker
Extending DialogFragment for a time picker
- Define the onCreateDialog() method to return an instance of TimePickerDialog
- Implement the TimePickerDialog.OnTimeSetListener interface to receive a callback when the user sets the time
Showing the time picker
- public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- // Use the current time as the default values for the picker
- final Calendar c = Calendar.getInstance();
- int hour = c.get(Calendar.HOUR_OF_DAY);
- int minute = c.get(Calendar.MINUTE);
- // Create a new instance of TimePickerDialog and return it
- return new TimePickerDialog(getActivity(), this, hour, minute,
- DateFormat.is24HourFormat(getActivity()));
- }
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- // Do something with the time chosen by the user
- }
- }
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pick_time"
- android:onClick="showTimePickerDialog" />
- public void showTimePickerDialog(View v) {
- DialogFragment newFragment = new TimePickerFragment();
- newFragment.show(getSupportFragmentManager(), "timePicker");
- }
The preceding method calls the instance created of the DialogFragment defined above.
Creating a date picker
- public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- // Use the current date as the default date in the picker
- final Calendar c = Calendar.getInstance();
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- // Create a new instance of DatePickerDialog and return it
- return new DatePickerDialog(getActivity(), this, year, month, day);
- }
- public void onDateSet(DatePicker view, int year, int month, int day) {
- // Do something with the date chosen by the user
- }
- }
Showing the Date Picker
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pick_date"
- android:onClick="showDatePickerDialog" />
This is the same as the previously written procedure. Look at the following of when the user clicks the button, a system call invokes the method.
- public void showDatePickerDialog(View v) {
- DialogFragment newFragment = new DatePickerFragment();
- newFragment.show(getSupportFragmentManager(), "datePicker");
- }
TextFields


Gowtham RajamanickamPosted Apr 21, 2015, 2:48 AM
great