Now the layout for the list item:
<?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="wrap_content">
<LinearLayout
android:id="@+id/alarmSelectionBox"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_weight="1"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="vertical"
android:clickable="true"
android:background="@android:drawable/menuitem_background">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="28sp"
android:paddingTop="-4dp"
android:paddingBottom="-4dp"
android:text="1:55"
android:textColor="#ffffffff"
android:id="@+id/itemTimeTextView"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="4dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="3dp"
android:textSize="12sp"
android:text="@string/am_string"
android:id="@+id/itemAMTextView"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="-4dp"
android:textSize="12sp"
android:text="@string/pm_string"
android:id="@+id/itemPMTextView"
android:includeFontPadding="false"/>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingTop="-4dp"
android:paddingBottom="-4dp"
android:text="Alarm1"
android:singleLine="true"
android:textSize="28sp"
android:layout_marginLeft="20dp"
android:textColor="#ffffffff"
android:id="@+id/itemTitleTextView"/>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Mon, Tues, Wed, Thu, Fri"
android:textSize="10sp"
android:layout_marginLeft="2dp"
android:textColor="#ffffffff"
android:id="@+id/itemMessageTextView"/>
</LinearLayout>
<CheckBox
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:id="@+id/itemEnabledCheckBox">
</CheckBox>
</LinearLayout>
On Jul 23, 6:22 pm, eags <[email protected]> wrote:
> Next is the layout for the main app:
>
> <?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:id="@+id/timeTextView"
> android:layout_height="wrap_content"
> android:layout_width="fill_parent"
> android:text="Alarmsutta!"
> android:textSize="40px"
> android:gravity="center"
> android:layout_marginTop="20px"
> android:layout_marginBottom="20px"
> />
> <ListView
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:layout_weight="1"
> android:id="@+id/alarmsListView"
> />
> </LinearLayout>
>
> On Jul 23, 6:20 pm, eags <[email protected]> wrote:
>
>
>
> > Ok, Just for the practice, I'm implementing something very similar to
> > the Alarm Clock app included in Android by default. I'm going to put
> > each file in their own post.
>
> > First in this post, Alarmsutta.java which includes the main setup, and
> > the CursorAdapter:
>
> > package net.esalazar.alarmsutta;
>
> > import java.text.SimpleDateFormat;
> > import java.util.Calendar;
> > import java.util.Date;
>
> > import android.app.Activity;
> > import android.content.Context;
> > import android.content.Intent;
> > import android.content.res.Resources;
> > import android.database.Cursor;
> > import android.os.Bundle;
> > import android.view.LayoutInflater;
> > import android.view.Menu;
> > import android.view.MenuItem;
> > import android.view.View;
> > import android.view.ViewGroup;
> > import android.view.View.OnClickListener;
> > import android.widget.AdapterView;
> > import android.widget.CheckBox;
> > import android.widget.CompoundButton;
> > import android.widget.CursorAdapter;
> > import android.widget.LinearLayout;
> > import android.widget.ListView;
> > import android.widget.TextView;
> > import android.widget.AdapterView.OnItemSelectedListener;
> > import android.widget.CompoundButton.OnCheckedChangeListener;
>
> > public class Alarmsutta extends Activity {
>
> > private Alarm selectedAlarm;
> > private AlarmDBAdapter dbAdapter;
>
> > // Options menu codes
> > public final int MENU_NEW = Menu.FIRST;
> > public final int MENU_EDIT = Menu.FIRST + 1;
> > public final int MENU_DELETE = Menu.FIRST + 2;
>
> > // Intent codes for starting activities
> > public final int NEW_ALARM = 1;
> > public final int EDIT_ALARM = 2;
>
> > ListView alarmsListView;
> > AlarmCursorAdapter cursorAdapter;
>
> > Cursor allAlarmsCursor;
>
> > private LayoutInflater layoutFactory;
>
> > private class AlarmCursorAdapter extends CursorAdapter {
>
> > public AlarmCursorAdapter(Context context, Cursor c) {
> > super(context, c, true);
> > }
>
> > @Override
> > public View newView(Context context, Cursor cursor,
> > ViewGroup
> > parent) {
> > return
> > layoutFactory.inflate(R.layout.alarmlist_item, parent,
> > false);
> > }
>
> > @Override
> > public void bindView(View view, Context context, Cursor
> > cursor) {
> > final Alarm item =
> > dbAdapter.getAlarmFromCursor(cursor);
> > LinearLayout alarmView = (LinearLayout)view;
>
> > Date expiry = item.getExpiry();
> > String title = item.getTitle();
>
> > SimpleDateFormat sdf;
> > sdf = new SimpleDateFormat("MM/dd/yyyy");
> > String expiryDateString = sdf.format(expiry);
> > sdf = new SimpleDateFormat("hh:mm");
> > String expiryTimeString = sdf.format(expiry);
>
> > String message = item.getMessage();
> > final boolean enabled = item.isEnabled();
>
> > Calendar calendar = Calendar.getInstance();
> > calendar.setTime(expiry);
>
> > // Get references to all the elements we wish to
> > muck with
> > TextView titleTextView =
> > (TextView)alarmView.findViewById
> > (R.id.itemTitleTextView);
> > TextView messageTextView =
> > (TextView)alarmView.findViewById
> > (R.id.itemMessageTextView);
> > TextView timeTextView =
> > (TextView)alarmView.findViewById
> > (R.id.itemTimeTextView);
>
> > // Dim the AM/PM label accordingly leaving the
> > correct one bright
> > TextView amTextView =
> > (TextView)alarmView.findViewById
> > (R.id.itemAMTextView);
> > TextView pmTextView =
> > (TextView)alarmView.findViewById
> > (R.id.itemPMTextView);
> > Resources r = getResources();
> > int amPmColorOn = r.getColor(R.color.ampm_on);
> > int amPmColorOff = r.getColor(R.color.ampm_off);
> > if( calendar.get(Calendar.HOUR_OF_DAY) >= 12 ) {
> > amTextView.setTextColor(amPmColorOff);
> > pmTextView.setTextColor(amPmColorOn);
> > } else {
> > amTextView.setTextColor(amPmColorOn);
> > pmTextView.setTextColor(amPmColorOff);
> > }
> > CheckBox enabledCheckBox =
> > (CheckBox)alarmView.findViewById
> > (R.id.itemEnabledCheckBox);
>
> > // Set the title and message accordingly. We
> > prefix the message
> > with
> > // the date just to be coy.
> > titleTextView.setText(title);
> > messageTextView.setText( expiryDateString + " " +
> > message );
> > timeTextView.setText(expiryTimeString);
> > enabledCheckBox.setChecked(enabled);
>
> > enabledCheckBox.setOnCheckedChangeListener(new
> > OnCheckedChangeListener() {
> > public void onCheckedChanged(CompoundButton
> > buttonView, boolean
> > isChecked) {
> > item.setEnabled(isChecked);
> > dbAdapter.updateAlarm(item.getId(),
> > item);
> > notifyDataSetChanged();
> > }
> > });
> > LinearLayout alarmSelectionBox = (LinearLayout)
> > alarmView.findViewById(R.id.alarmSelectionBox);
> > alarmSelectionBox.setOnClickListener(new
> > OnClickListener() {
> > public void onClick(View v) {
> >
> > startEditAlarm(dbAdapter.getAlarm(item.getId()));
> > }
> > });
>
> > }
>
> > }
>
> > @Override
> > public void onCreate(Bundle savedInstanceState) {
> > super.onCreate(savedInstanceState);
> > setContentView(R.layout.main);
> > layoutFactory = LayoutInflater.from(this);
>
> > // Set up array adapter and viewer for alarms list
> > dbAdapter = new AlarmDBAdapter(this);
> > dbAdapter.open();
> > allAlarmsCursor = dbAdapter.getAllAlarmItemsCursor();
> > cursorAdapter = new AlarmCursorAdapter(this, allAlarmsCursor);
>
> > alarmsListView = (ListView)findViewById(R.id.alarmsListView);
>
> > alarmsListView.setAdapter(cursorAdapter);
> > alarmsListView.setVerticalScrollBarEnabled(true);
> > alarmsListView.setItemsCanFocus(true);
> > alarmsListView.setOnItemSelectedListener(new
> > OnItemSelectedListener() {
> > public void onItemSelected(AdapterView<?> parent,
> > View view, int
> > position, long id) {
> > selectedAlarm = dbAdapter.getAlarm(id);
> > }
> > public void onNothingSelected(AdapterView<?> arg0) {
> > selectedAlarm = null;
> > }
> > });
>
> > }
>
> > @Override
> > protected void onActivityResult(int requestCode, int resultCode,
> > Intent data) {
>
> > super.onActivityResult(requestCode, resultCode, data);
>
> > if( resultCode != Activity.RESULT_OK )
> > return;
>
> > Date expiry = new Date(data.getLongExtra("expiryTime", 0));
> > String title = data.getStringExtra("title");
> > String message = data.getStringExtra("message");
> > boolean enabled = data.getBooleanExtra("enabled", true);
> > int id =
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---