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;

}