di part ke2 kita sudah membuat beberapa layout antarmuka aplikasi kita sekarang kita
tinggal memberikan fungsi pada layout-layout kita tapi sebelum itu kita
tambahkan lagi dua buah layout untuk activity_details dan settings_activity
1. buat layout dengan nama activity_employee_details
untuk menampilkan details employee kita, sebagai berikut:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.giviews.employee.EmployeeDetailsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_setting"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/AppTheme.AppBarOverlay"
/>
<ScrollView
android:layout_marginTop="60dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/profileImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#fff"
android:src="@drawable/ic_account_circle_black"/>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="15dp"
android:text="First Name" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_firstname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:layout_weight="1"
android:text="Last Name" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_lastname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Title" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Department" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_department"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="City" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Phone" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Email" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_email"
android:layout_weight="1"
android:text="Unknown"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:layout_weight="1"
android:text="Gender" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_gender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
2. buat layout dengan nama
activity_settings.xml dengan kode sebagai berikut, activity ini akan digunakan
untuk memilih warna thema pada aplikasi CRUD kita.
3. <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.giviews.employee.EmployeeDetailsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_setting"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/AppTheme.AppBarOverlay" />
<ScrollView
android:layout_marginTop="60dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/profileImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#fff"
android:src="@drawable/ic_account_circle_black"/>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="15dp"
android:text="First Name" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_firstname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:layout_weight="1"
android:text="Last Name" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_lastname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Title" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Department" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_department"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="City" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Phone" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textStyle="bold"
android:text="Email" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_email"
android:layout_weight="1"
android:text="Unknown"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:layout_weight="1"
android:text="Gender" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/employee_gender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unknown" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Sekarang tinggal java nya, buat class dengan nama EmployeeActivity dan
masukan kode berikut:
package com.giviews.employee;
import android.annotation.TargetApi;
import android.app.LoaderManager;
import android.app.SearchManager;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.giviews.employee.data.EmployeeContract;
import com.giviews.employee.data.EmployeeDbHelper;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class EmployeeActivity extends AppCompatActivity implements SearchView.OnQueryTextListener,
LoaderManager.LoaderCallbacks<Cursor>{
// EmployeeDbHelper employeeDbHelper;
private static final int EMPLOYEE_LOADER = 0;
EmployeeCursorAdapter mCursorAdapter;
@BindView(R.id.fab) FloatingActionButton button;
public EmployeeActivity(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Constant.color);
//Setup FAB
ButterKnife.bind(this);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), EmployeeEditor.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra(EmployeeEditor.EXTRA_RECT, createRect(button));
startActivity(intent);
}
});
ListView employeeListView = (ListView) findViewById(R.id.list);
View emptyView = findViewById(R.id.empty_view);
employeeListView.setEmptyView(emptyView);
mCursorAdapter = new EmployeeCursorAdapter(this, null);
employeeListView.setAdapter(mCursorAdapter);
//TODO
employeeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), EmployeeDetailsActivity.class);
Uri currentEmployeeUri = ContentUris.withAppendedId(EmployeeContract.EmployeeEntry.CONTENT_URI, id);
intent.setData(currentEmployeeUri);
intent.putExtra(EmployeeEditor.EXTRA_RECT, createRect(button));
startActivity(intent);
}
});
employeeListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(EmployeeActivity.this, EmployeeEditor.class);
Uri currentEmployeeUri = ContentUris.withAppendedId(EmployeeContract.EmployeeEntry.CONTENT_URI, id);
intent.setData(currentEmployeeUri);
intent.putExtra(EmployeeEditor.EXTRA_RECT, createRect(button));
startActivity(intent);
return true;
}
});
getLoaderManager().initLoader(EMPLOYEE_LOADER, null, this);
// employeeDbHelper = new EmployeeDbHelper(this);
}
private Rect createRect(View view) {
Rect rect = new Rect();
view.getDrawingRect(rect);
((ViewGroup) view.getParent()).offsetDescendantRectToMyCoords(view, rect);
return rect;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_catalog, menu);
MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_search:
onSearchRequested();
return true;
case R.id.setting:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
default:
return false;
}
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
EmployeeContract.EmployeeEntry._ID,
EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME,
EmployeeContract.EmployeeEntry.COLUMN_LASTNAME,
EmployeeContract.EmployeeEntry.COLUMN_TITLE,
EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT,
EmployeeContract.EmployeeEntry.COLUMN_CITY,
EmployeeContract.EmployeeEntry.COLUMN_PHONE,
EmployeeContract.EmployeeEntry.COLUMN_IMAGE,
EmployeeContract.EmployeeEntry.COLUMN_EMAIL,
};
return new CursorLoader(this,
EmployeeContract.EmployeeEntry.CONTENT_URI,
projection,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<Cursor> newList = new ArrayList<>();
return true;
}
}
selanjutnya buat lagi class dengan nama EmployeeCursorAdapter dengan
kode seperti berikut:
package com.giviews.employee;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
import com.giviews.employee.data.EmployeeContract;
/**
* Created by asus on 27/10/2017.
*/
class EmployeeCursorAdapter extends CursorAdapter{
public EmployeeCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView firstnameTextView = (TextView) view.findViewById(R.id.firstname);
TextView lastnameTextView = (TextView) view.findViewById(R.id.lastname);
TextView titleTextView = (TextView) view.findViewById(R.id.titleview);
int firstnameColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME);
int lastnameColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME);
int titleColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_TITLE);
String employeeFirstName = cursor.getString(firstnameColumnIndex);
String employeeLastName = cursor.getString(lastnameColumnIndex);
String employeeTitle = cursor.getString(titleColumnIndex);
if (TextUtils.isEmpty(employeeFirstName)) {
employeeFirstName = "Unknown";
}
if (TextUtils.isEmpty(employeeLastName)) {
employeeLastName = "Unknown";
}
if (TextUtils.isEmpty(employeeTitle)) {
employeeTitle = "Unknown";
}
firstnameTextView.setText(employeeFirstName);
lastnameTextView.setText(employeeLastName);
titleTextView.setText(employeeTitle);
}
}
Selanjutnya buat lagi class dengan nama EmployeeEditor dan masukan kode
berikut:
package com.giviews.employee;
import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.AlertDialog;
import android.app.LoaderManager;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.giviews.employee.data.EmployeeContract;
import com.hendraanggrian.bundler.BindExtra;
import com.hendraanggrian.bundler.Bundler;
import com.hendraanggrian.kota.content.Themes;
import com.hendraanggrian.reveallayout.Radius;
import com.hendraanggrian.reveallayout.RevealableLayout;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public class EmployeeEditor extends AppCompatActivity implements View.OnClickListener, LoaderManager.LoaderCallbacks<Cursor>{
private static final int EXISTING_EMPLOYEE_LOADER = 0;
private Uri mCurrentEmployeeUri;
private EditText mFirstNameEditText;
private EditText mLastNameEditText;
private EditText mTitleEditText;
private EditText mDepartment;
private EditText mCity;
private EditText mPhone;
private EditText mEmail;
private Spinner mGenderSpinner;
private int mGender = EmployeeContract.EmployeeEntry.GENDER_UNKNOWN;
private ImageView profileImageView;
private Button pickImage;
private static final int SELECT_PHOTO = 1;
private static final int CAPTURE_PHOTO = 2;
//TODO
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String IMAGE_DIRECTORY_NAME = "KAMERA";
private Uri fileUri;
private ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private boolean hasImageChanges = false;
Bitmap thumbnail;
private boolean mEmployeeHasChanged = false;
View rootLayout;
public static final String EXTRA_RECT = "com.giviews.employee";
@BindExtra(EXTRA_RECT)
Rect rect;
@BindView(R.id.revealLayout)
RevealableLayout revealLayout;
@BindView(R.id.root_layout)
ViewGroup layout;
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mEmployeeHasChanged = true;
return false;
}
};
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Toolbar toolbar = (Toolbar)
findViewById(R.id.toolbar_setting);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Constant.color);
Bundler.bindExtras(this);
ButterKnife.bind(this);
//TODO
Intent intent = getIntent();
mCurrentEmployeeUri = intent.getData();
if (mCurrentEmployeeUri == null) {
setTitle("Add an Employee");
invalidateOptionsMenu();
}else {
setTitle("Edit Employee");
getLoaderManager().initLoader(EXISTING_EMPLOYEE_LOADER, null, this);
}
mFirstNameEditText = (EditText) findViewById(R.id.firstName);
mLastNameEditText = (EditText) findViewById(R.id.lastName);
mTitleEditText = (EditText) findViewById(R.id.title);
mDepartment = (EditText) findViewById(R.id.department);
mCity = (EditText) findViewById(R.id.city);
mPhone = (EditText) findViewById(R.id.phone);
mEmail = (EditText) findViewById(R.id.email);
mGenderSpinner = (Spinner) findViewById(R.id.spinner_gender);
mFirstNameEditText.setOnTouchListener(mTouchListener);
mLastNameEditText.setOnTouchListener(mTouchListener);
mTitleEditText.setOnTouchListener(mTouchListener);
mDepartment.setOnTouchListener(mTouchListener);
mCity.setOnTouchListener(mTouchListener);
mPhone.setOnTouchListener(mTouchListener);
mEmail.setOnTouchListener(mTouchListener);
mGenderSpinner.setOnTouchListener(mTouchListener);
setupSpinner();
rootLayout = findViewById(R.id.root_layout);
layout.post(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void run() {
Animator animator = revealLayout.reveal(layout, rect.centerX(), rect.centerY(), Radius.GONE_ACTIVITY);
animator.setDuration(1000);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Themes.getColor(getTheme(), R.attr.colorAccent, true));
}
}
});
animator.start();
}
});
//TODO
profileImageView = (ImageView) findViewById(R.id.profileImageView);
pickImage = (Button) findViewById(R.id.pickImage);
pickImage.setOnClickListener(this);
if (ContextCompat.checkSelfPermission(EmployeeEditor.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
profileImageView.setEnabled(false);
ActivityCompat.requestPermissions(EmployeeEditor.this, new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}else {
profileImageView.setEnabled(true);
}
}
private void setupSpinner() {
ArrayAdapter genderSpinnerAdapter
= ArrayAdapter.createFromResource(this,R.array.array_gender_options, android.R.layout.simple_spinner_item);
genderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mGenderSpinner.setAdapter(genderSpinnerAdapter);
mGenderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection =
(String) parent.getItemAtPosition(position);
if (!TextUtils.isEmpty(selection)){
if (selection.equals(getString(R.string.gender_male))){
mGender = EmployeeContract.EmployeeEntry.GENDER_MALE;
}else if (selection.equals(getString(R.string.gender_female))) {
mGender = EmployeeContract.EmployeeEntry.GENDER_FEMALE;
}else {
mGender = EmployeeContract.EmployeeEntry.GENDER_UNKNOWN;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
mGender = EmployeeContract.EmployeeEntry.GENDER_UNKNOWN;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_editor, menu);
if (mCurrentEmployeeUri == null) {
MenuItem menuItem =
menu.findItem(R.id.action_delete);
menuItem.setVisible(false);
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
saveEmployee();
finish();
return true;
case R.id.action_delete:
showDeleteConfirmationDialog();
return true;
case android.R.id.home:
if (!mEmployeeHasChanged) {
NavUtils.navigateUpFromSameTask(EmployeeEditor.this);
return true;
}
DialogInterface.OnClickListener discardButtonClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
NavUtils.navigateUpFromSameTask(EmployeeEditor.this);
}
};
showUnSavedChangesDialog(discardButtonClickListener);
return true;
}
return super.onOptionsItemSelected(item);
}
private void saveEmployee() {
String firstnameString = mFirstNameEditText.getText().toString().trim();
String lastnameString = mLastNameEditText.getText().toString().trim();
String titleString = mTitleEditText.getText().toString().trim();
String departmentString = mDepartment.getText().toString().trim();
String cityString = mCity.getText().toString().trim();
String phoneString = mPhone.getText().toString().trim();
String emailString = mEmail.getText().toString().trim();
profileImageView.setDrawingCacheEnabled(true);
profileImageView.buildDrawingCache();
Bitmap bitmap = profileImageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
if (mCurrentEmployeeUri == null &&
TextUtils.isEmpty(firstnameString)
&& TextUtils.isEmpty(lastnameString) &&
TextUtils.isEmpty(titleString)
&& TextUtils.isEmpty(departmentString) &&
TextUtils.isEmpty(cityString)
&& TextUtils.isEmpty(phoneString) &&
TextUtils.isEmpty(emailString)
&& mGender == EmployeeContract.EmployeeEntry.GENDER_UNKNOWN) {
return;
}
ContentValues values = new ContentValues();
values.put(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME, firstnameString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME, lastnameString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_TITLE, titleString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT, departmentString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_CITY, cityString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_PHONE, phoneString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_IMAGE, data);
values.put(EmployeeContract.EmployeeEntry.COLUMN_EMAIL, emailString);
values.put(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER, mGender);
if (mCurrentEmployeeUri == null) {
Uri newUri =
getContentResolver().insert(EmployeeContract.EmployeeEntry.CONTENT_URI, values);
if (newUri == null) {
Toast.makeText(this, "Error with saving Employee", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Employee saved", Toast.LENGTH_SHORT).show();
}
}else {
int rowAffected = getContentResolver().update(mCurrentEmployeeUri, values, null, null);
if (rowAffected == 0) {
Toast.makeText(getApplicationContext(), "Error with updating employee", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "Employee updated", Toast.LENGTH_SHORT).show();
}
}
}
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
EmployeeContract.EmployeeEntry._ID,
EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME,
EmployeeContract.EmployeeEntry.COLUMN_LASTNAME,
EmployeeContract.EmployeeEntry.COLUMN_TITLE,
EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT,
EmployeeContract.EmployeeEntry.COLUMN_CITY,
EmployeeContract.EmployeeEntry.COLUMN_PHONE,
EmployeeContract.EmployeeEntry.COLUMN_IMAGE,
EmployeeContract.EmployeeEntry.COLUMN_EMAIL,
EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER,
};
return new CursorLoader(this,
mCurrentEmployeeUri,
projection,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor == null || cursor.getCount() < 1) {
return;
}
if (cursor.moveToFirst()) {
int firstnameColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME);
int lastnameColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME);
int titleColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_TITLE);
int departmentColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT);
int cityColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_CITY);
int phoneColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_PHONE);
int imageColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_IMAGE);
int emailColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_EMAIL);
int genderColumnIndex =
cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER);
String firstName =
cursor.getString(firstnameColumnIndex);
String lastName =
cursor.getString(lastnameColumnIndex);
String title =
cursor.getString(titleColumnIndex);
String department =
cursor.getString(departmentColumnIndex);
String city =
cursor.getString(cityColumnIndex);
String phone = cursor.getString(phoneColumnIndex);
byte[] image =
cursor.getBlob(imageColumnIndex);
String email =
cursor.getString(emailColumnIndex);
int gender =
cursor.getInt(genderColumnIndex);
mFirstNameEditText.setText(firstName);
mLastNameEditText.setText(lastName);
mTitleEditText.setText(title);
mDepartment.setText(department);
mCity.setText(city);
mPhone.setText(phone);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
profileImageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, 200, 200, false));
mEmail.setText(email);
switch (gender) {
case EmployeeContract.EmployeeEntry.GENDER_MALE:
mGenderSpinner.setSelection(1);
break;
case EmployeeContract.EmployeeEntry.GENDER_FEMALE:
mGenderSpinner.setSelection(2);
break;
default:
mGenderSpinner.setSelection(0);
break;
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mFirstNameEditText.setText("");
mLastNameEditText.setText("");
mTitleEditText.setText("");
mDepartment.setText("");
mCity.setText("");
mPhone.setText("");
mEmail.setText("");
mGenderSpinner.setSelection(0);
}
private void showUnSavedChangesDialog(DialogInterface.OnClickListener
discardButtonClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Discard your changes and quit
editing?");
builder.setPositiveButton("Discard", discardButtonClickListener);
builder.setNegativeButton("Keep Editing", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog =
builder.create();
alertDialog.show();
}
private void showDeleteConfirmationDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete this Employee?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteEmployee();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (dialog != null){
dialog.dismiss();
}
}
});
AlertDialog alertDialog =
builder.create();
alertDialog.show();
}
private void deleteEmployee() {
if (mCurrentEmployeeUri != null){
int rowsDeleted = getContentResolver().delete(mCurrentEmployeeUri,null, null);
if (rowsDeleted == 0) {
Toast.makeText(this, "Error with deleteing
employee", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Employee deleted", Toast.LENGTH_SHORT).show();
}
}
finish();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.pickImage:
new MaterialDialog.Builder(this)
.title("set your image")
.items(R.array.upload_images)
.itemsIds(R.array.itemIds)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
switch (which) {
case 0:
Intent
photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
break;
case 1:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_PHOTO);
break;
case 2:
profileImageView.setImageResource(R.drawable.ic_account_circle_black);
break;
}
}
})
.show();
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
profileImageView.setEnabled(true);
}
}
}
public void setProgressBar(){
progressBar = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Please wait....");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
new Thread(new Runnable() {
@Override
public void run() {
while (progressBarStatus < 100) {
progressBarStatus += 30;
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100){
try {
Thread.sleep(2000);
}catch (InterruptedException e){
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO) {
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
setProgressBar();
profileImageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else if (requestCode == CAPTURE_PHOTO){
if (resultCode == RESULT_OK) {
onCaptureImageResult();
}
}
}
}
private void onCaptureImageResult() {
try {
// thumbnail = (Bitmap)
data.getExtras().get("data");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
thumbnail = BitmapFactory.decodeFile(fileUri.getPath(), options);
setProgressBar();
// profileImageView.setMaxWidth(200);
profileImageView.setImageBitmap(thumbnail);
}catch (NullPointerException e) {
e.printStackTrace();
}
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Ooops! Failed create"
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
String timeStamp;
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmsss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
}else {
return null;
}
return mediaFile;
}
}
kemudian buat lagi class dengan nama EmployeeDetailsActivity untuk
menampilkan details data employee nya, dengan kode seperti berikut:
package com.giviews.employee;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.giviews.employee.data.EmployeeContract;
/**
* Created by asus on 28/10/2017.
*/
public class EmployeeDetailsActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private Uri mCurrentEmployeeUri;
private TextView mFirstname;
private TextView mLastname;
private TextView mDepartment;
private TextView mTitle;
private TextView mCity;
private TextView mPhone;
private TextView mEmail;
private TextView mGender;
private ImageView profileImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Constant.color);
Intent intent = getIntent();
mCurrentEmployeeUri = intent.getData();
if (mCurrentEmployeeUri == null) {
Toast.makeText(this, "No Employee Data ", Toast.LENGTH_SHORT).show();
}else {
getLoaderManager().initLoader(0, null, this);
}
mFirstname = (TextView) findViewById(R.id.employee_firstname);
mLastname = (TextView) findViewById(R.id.employee_lastname);
mTitle = (TextView) findViewById(R.id.employee_title);
mDepartment = (TextView) findViewById(R.id.employee_department);
mCity = (TextView) findViewById(R.id.employee_city);
mPhone = (TextView) findViewById(R.id.employee_phone);
mEmail = (TextView) findViewById(R.id.employee_email);
mGender = (TextView) findViewById(R.id.employee_gender);
profileImageView = (ImageView) findViewById(R.id.profileImageView);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
EmployeeContract.EmployeeEntry._ID,
EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME,
EmployeeContract.EmployeeEntry.COLUMN_LASTNAME,
EmployeeContract.EmployeeEntry.COLUMN_TITLE,
EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT,
EmployeeContract.EmployeeEntry.COLUMN_CITY,
EmployeeContract.EmployeeEntry.COLUMN_PHONE,
EmployeeContract.EmployeeEntry.COLUMN_IMAGE,
EmployeeContract.EmployeeEntry.COLUMN_EMAIL,
EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER,
};
return new CursorLoader(this,
mCurrentEmployeeUri,
projection,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor == null || cursor.getCount() < 1) {
return;
}
if (cursor.moveToFirst()) {
int firstnameColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME);
int lastnameColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME);
int titleColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_TITLE);
int departmentColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT);
int cityColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_CITY);
int phoneColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_PHONE);
int imageCoumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_IMAGE);
int emailColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_EMAIL);
int genderColumnIndex = cursor.getColumnIndex(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER);
String firstName = cursor.getString(firstnameColumnIndex);
String lastName = cursor.getString(lastnameColumnIndex);
String title = cursor.getString(titleColumnIndex);
String department = cursor.getString(departmentColumnIndex);
String city = cursor.getString(cityColumnIndex);
String phone = cursor.getString(phoneColumnIndex);
byte[] image = cursor.getBlob(imageCoumnIndex);
String email = cursor.getString(emailColumnIndex);
int gender = cursor.getInt(genderColumnIndex);
setTitle(firstName);
mFirstname.setText(firstName);
mLastname.setText(lastName);
mTitle.setText(title);
mDepartment.setText(department);
mCity.setText(city);
mPhone.setText(phone);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
profileImageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, 200,
200, false));
mEmail.setText(email);
switch (gender) {
case EmployeeContract.EmployeeEntry.GENDER_MALE:
mGender.setText("Male");
break;
case EmployeeContract.EmployeeEntry.GENDER_FEMALE:
mGender.setText("Female");
break;
default:
mGender.setText("Unknown");
break;
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
karena artikel ini sudah terlalu panjang maka part ke3 dicukupkan dulu
sampai sini akan kita lanjutkan ke part4 finishing