Monday, November 9, 2015

Adding a custom toolbar


Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:background="#E91E63"
app:theme="@style/MyActionBarTheme">
</android.support.v7.widget.Toolbar>


How to set the toolbar

Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_add);
setSupportActionBar(toolbar);

//Enables the home feature in the toolbar
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


Menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.imare.weddingapp_v1.SingleTaskActivity">
<!--<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />-->

<item
android:id="@+id/callMember"
android:orderInCategory="200"
android:title="@string/callMember"
android:icon="@drawable/call"
app:showAsAction="always"></item>

<item
android:id="@+id/emailMember"
android:orderInCategory="200"
android:title="@string/mailMember"
android:icon="@drawable/email"
app:showAsAction="always"></item>

<item
android:id="@+id/editMember"
android:orderInCategory="200"
android:title="@string/editTask"
android:icon="@drawable/edit"
app:showAsAction="always"></item>

<item
android:id="@+id/deleteMember"
android:orderInCategory="200"
android:title="@string/deleteTask"
android:icon="@drawable/delete"
app:showAsAction="always"></item>
</menu>


Sunday, November 1, 2015

Custom alert dialogs with different widgets

public AlertDialog createDialog(Context context, final View view, final EditText editText) {

final String[] values = {"Get Started", "Venue_CA", "Guests_CA", "Decorations", "Beauty_CA", "Catering_CA & Entertainment", "Photography_CA", "FinishUp_CA"};


final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setView(view);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Select", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {


if (view instanceof EditText) {
String data = ((EditText) view).getText().toString();
editText.setText(data);

} else if (view instanceof DatePicker) {

Calendar cal = Calendar.getInstance();
((DatePicker) view).setMinDate(cal.getTimeInMillis());
((DatePicker) view).setCalendarViewShown(false);
int day = ((DatePicker) view).getDayOfMonth();
String month = MONTHS[((DatePicker) view).getMonth()];
int year = ((DatePicker) view).getYear();
editText.setText(day + " " + month + " " + year);

} else if (view instanceof NumberPicker) {

int selection = ((NumberPicker) view).getValue();
editText.setText(values[selection]);

} else if (view instanceof TimePicker) {

int hour = ((TimePicker)view).getCurrentHour();
int minute = ((TimePicker)view).getCurrentMinute();
int second = 00;
String ampm = (hour < 12) ? "AM" : "PM";

editText.setText(hour+":"+minute+" "+ampm);
}
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

alertDialog.hide();
}
});

return alertDialog;

}

Sunday, October 25, 2015

Cursor adapter for a custom list view with checkboxes

public class TasksCursorAdapter_CA extends android.widget.CursorAdapter {


SQLiteDatabase dh = DatabaseHelper_CA.getInstance().getDb();
private LayoutInflater mInflater;
private Context mContext;
Cursor cursor;

public TasksCursorAdapter_CA(Context context, Cursor c) {
super(context, c);

mInflater = LayoutInflater.from(context);
mContext = context;
cursor = c;
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {

ViewHolder_CA holder = (ViewHolder_CA) view.getTag();

holder.setTextView((TextView) view.findViewById(R.id.textviewx));
holder.setCheckBox((CheckBox) view.findViewById(R.id.checkbox));
CheckBox cb = holder.getCheckBox();

holder.getTextView().setText(cursor.getString(cursor.getColumnIndex("name")));

cb.setTag(new Integer(cursor.getPosition()));

CompoundButton.OnCheckedChangeListener checkedChange = new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


ContentValues contentValues = new ContentValues();

Integer currentPosition = (Integer) buttonView.getTag();

String currentPositionString = Double.toString(currentPosition);
if (cursor.moveToPosition(currentPosition)) {

String rowID = cursor.getString(cursor.getColumnIndex("_id"));
if (isChecked) {
contentValues.put("selected", "1");
dh.update(DatabaseHelper_CA.TABLE_NAME, contentValues, "_id = ?",
new String[]{rowID});

} else if (!isChecked) {
contentValues.put("selected", "0");
dh.update(DatabaseHelper_CA.TABLE_NAME, contentValues, "_id = ?",
new String[]{rowID});
}

}

}
};
cb.setOnCheckedChangeListener(checkedChange);

if (cursor.getString(cursor.getColumnIndex("selected")).compareTo("1") == 0) {
cb.setChecked(true);
} else {
cb.setChecked(false);
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

ViewHolder_CA holder;
View convertView = mInflater.inflate(R.layout.custom_ca, parent, false);
holder = new ViewHolder_CA(convertView);
convertView.setTag(holder);
return convertView;
}

}

Tuesday, October 13, 2015

How to set a notification using a broadcast reciever

Receiver class
public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {

NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);

}
}


Main Class
private void scheduleNotification(Notification notification, Calendar cal) { int newid = NotificationID.getID(); Intent notificationIntent = new Intent(this, NotificationPublisher.class); //making the intent unique notificationIntent.setAction("actionstring" + System.currentTimeMillis()); //Sending variables to the Broadcast Reciever notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, newid); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) //Create and returns a notification object private Notification getNotification(String content) { Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle("Wedding App Reminder"); builder.setContentText(content); builder.setSmallIcon(R.drawable.alarm); return builder.build(); } private void setReminder(){ setReminder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String reminderDate = editTextDate.getText().toString(); String reminderTime = editTextTime.getText().toString(); String notes = editTextNotes.getText().toString(); String[] daySplits = reminderDate.split(" "); String[] timeSplits = reminderTime.split(":"); day = Integer.parseInt(daySplits[0]); for (int i = 0; i < 12; i++) { if (daySplits[1].equals(MONTHS[i])) { month = i + 1; } } year = Integer.parseInt(daySplits[2]); hour = Integer.parseInt(timeSplits[0]); String[] splits = timeSplits[1].split(" "); minute = Integer.parseInt(splits[0]); ampm = splits[1]; second = 00; Calendar c = Calendar.getInstance(); c.set(year, (month - 1), day); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); if (ampm.equals("AM")) c.set(Calendar.AM_PM, Calendar.AM); else if (ampm.equals("PM")) c.set(Calendar.AM_PM, Calendar.PM); // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service scheduleNotification(getNotification(name + " task was scheduled to " + date + " " + mny), c); // Notify the user what they just did showToast("Notification set for: " + day + "/" + month + "/" + year); } }); } }
NotificatioID class
public class NotificationID { private final static AtomicInteger c = new AtomicInteger(0); public static int getID() { return c.incrementAndGet(); } }


AndroidManifest.XML
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name=".NotificationPublisher_CA" android:enabled="true" android:label="NotificationPublisher"> android:exported="true" <!--<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter>--> </receiver>