Membuat Aplikasi CRUD SQLite dengan Content Provider dan Circular Reveal Animation di Android Part4

November 12, 2017 , , , 0 Comments



di part ke3 kita sudah membuat beberapa class untuk menambahkan data, menampilkan data menampilkan details data dan mengedit data. sekarang kita akan menambahkan class Searchable activity untuk fungsi pencarian data CRUD kita

pertama buat class SearchableActivity dan masukan kode berikut:
package com.giviews.employee;

import
android.app.ListActivity;
import
android.app.SearchManager;
import
android.content.Intent;
import
android.database.Cursor;
import
android.net.Uri;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.support.v7.widget.SearchView;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.widget.ListAdapter;
import
android.widget.ListView;
import
android.widget.SimpleCursorAdapter;
import
android.widget.Toast;

import
com.giviews.employee.data.EmployeeContract;

import
java.util.Timer;
import
java.util.TimerTask;

/**
 * Created by asus on 29/10/2017.
 */

public class SearchableActivity extends ListActivity implements SearchView.OnQueryTextListener{

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);

       
Intent intent = getIntent();
       
checkIntent(intent);
   
}

   
@Override
   
protected void onNewIntent(Intent newIntent) {
        setIntent(newIntent)
;

       
checkIntent(newIntent);
   
}

   
private void checkIntent(Intent intent) {
        String query =
"";
       
String intentAction = intent.getAction();
        if
(Intent.ACTION_SEARCH.equals(intentAction)) {
            query = intent.getStringExtra(SearchManager.
QUERY);
       
} else if (Intent.ACTION_VIEW.equals(intentAction)) {
            Uri details = intent.getData()
;
           
Intent detailsIntent = new Intent(Intent.ACTION_VIEW, details);
           
startActivity(detailsIntent);
       
}
        fillList(query)
;
   
}

   
private void fillList(String query) {
        String wildcardQuery =
"%" + query + "%";

       
Cursor cursor = getContentResolver().query(
                EmployeeContract.EmployeeEntry.
CONTENT_URI,
                null,
               
EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME + " LIKE ? OR " + EmployeeContract.EmployeeEntry.COLUMN_LASTNAME + " LIKE ? OR ",
                new
String[]{ wildcardQuery, wildcardQuery },
                null
);

        if
(cursor.getCount() == 0) {
            Toast.makeText(
this, " NO SEARCH RESULT ", Toast.LENGTH_SHORT).show();
            int
timeout = 3000;

           
Timer timer = new Timer();
           
timer.schedule(new TimerTask() {
                
@Override
               
public void run() {
                    finish()
;
                   
Intent intent = new Intent(SearchableActivity.this, EmployeeActivity.class);
                   
startActivity(intent);
               
}
            }
, timeout);

       
}

        ListAdapter adapter =
new SimpleCursorAdapter(
               
this,
               
android.R.layout.simple_list_item_2,
               
cursor,
                new
String[]{ EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME, EmployeeContract.EmployeeEntry.COLUMN_LASTNAME },
                new int
[]{android.R.id.text1, android.R.id.text2},
               
0);
       
setListAdapter(adapter);
   
}

   
@Override
   
protected void onListItemClick(ListView l, View view, int position, long id) {
        Intent intent =
new Intent(SearchableActivity.this, EmployeeActivity.class);

       
Uri details = Uri.withAppendedPath(EmployeeContract.EmployeeEntry.CONTENT_URI, "" + id);
       
intent.setData(details);
       
startActivity(intent);
   
}

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
if (item.getItemId() == android.R.id.home) {

            Intent parentActivityIntent =
new Intent(this, EmployeeActivity.class);
           
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
           
startActivity(parentActivityIntent);
           
finish();
            return true;
       
}
       
return super.onOptionsItemSelected(item);
   
}

   
@Override
    
public boolean onQueryTextSubmit(String query) {
       
return false;
   
}

   
@Override
   
public boolean onQueryTextChange(String newText) {
       
return false;
   
}
}

kemudian buat class SettingsActivity untuk menggati warna thema aplikasi kita, dengan kode seperti berikut:
package com.giviews.employee;



import android.annotation.TargetApi;

import android.content.Intent;

import android.content.SharedPreferences;

import android.graphics.Paint;

import android.graphics.drawable.ShapeDrawable;

import android.graphics.drawable.shapes.OvalShape;

import android.os.Build;

import android.preference.PreferenceManager;

import android.support.annotation.RequiresApi;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.Button;





import com.turkialkhateeb.materialcolorpicker.ColorChooserDialog;

import com.turkialkhateeb.materialcolorpicker.ColorListener;



public class SettingsActivity extends AppCompatActivity {



    SharedPreferences sharedPreferences, app_preperences;

    SharedPreferences.Editor editor;

    Button button;

    Methods methods;



    int appTheme;

    int themeColor;

    int appColor;

    Constant constant;



    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        app_preperences = PreferenceManager.getDefaultSharedPreferences(this);

        appColor = app_preperences.getInt("color", 0);

        appTheme = app_preperences.getInt("theme", 0);

        themeColor = appColor;

        constant.color = appColor;



        if (themeColor == 0) {

            setTheme(Constant.theme);

        } else if (appTheme == 0) {

            setTheme(Constant.theme);

        } else {

            setTheme(appTheme);

        }

        setContentView(R.layout.activity_settings);



        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);

        toolbar.setTitle("Settings");

        toolbar.setBackgroundColor(Constant.color);



        methods = new Methods();



        button = (Button) findViewById(R.id.button_color);



        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);



        editor = sharedPreferences.edit();



        colorize();



        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                ColorChooserDialog dialog = new ColorChooserDialog(SettingsActivity.this);

                dialog.setTitle("Select");

                dialog.setColorListener(new ColorListener() {

                    @Override

                    public void OnColorClick(View v, int color) {

                        colorize();

                        Constant.color = color;



                        methods.setColorTheme();

                        editor.putInt("color", color);

                        editor.putInt("theme", Constant.theme);

                        editor.commit();



                        Intent intent = new Intent(getApplicationContext(), EmployeeActivity.class);

                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        startActivity(intent);

                    }

                });



                dialog.show();

            }



        });

    }



    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

    private void colorize(){

        ShapeDrawable d = new ShapeDrawable(new OvalShape());

        d.setBounds(58, 58, 58, 58);



        d.getPaint().setStyle(Paint.Style.FILL);

        d.getPaint().setColor(Constant.color);



        button.setBackground(d);

    }



}

kemudian kita buat class Constant untuk mengganti warna thema aplikasi kita:
package com.giviews.employee;



import java.io.Serializable;



/**

 * Created by asus on 05/11/2017.

 */



public class Constant implements Serializable {



    public static int nav_clicked = 0;

    public static Boolean isNavClicked = false;



    public static Boolean isToggle = true;

    public static  int color = 0xfff67f21;

    public static  int theme = R.style.AppTheme;

}

selanjutnya buat class Methods untuk mengubah warna theme, dengan kode seperti berikut:
package com.giviews.employee;



/**

 * Created by asus on 05/11/2017.

 */



public class Methods {



    public void setColorTheme() {



        switch (Constant.color){

            case 0xffF44336:

                Constant.theme = R.style.AppTheme_red;

                break;

            case 0xffE91E63:

                Constant.theme = R.style.AppTheme_pink;

                break;

            case 0xff9C27B0:

                Constant.theme = R.style.AppTheme_darpink;

                break;

            case 0xff673AB7:

                Constant.theme = R.style.AppTheme_violet;

                break;

            case 0xff3F51B5:

                Constant.theme = R.style.AppTheme_blue;

                break;

            case 0xff03A9F4:

                Constant.theme = R.style.AppTheme_skyblue;

                break;

            case 0xff4CAF50:

                Constant.theme = R.style.AppTheme_green;

                break;

            case 0xffFF9800:

                Constant.theme = R.style.AppTheme;

                break;

            case 0xff9E9E9E:

                Constant.theme = R.style.AppTheme_grey;

                break;

            case 0xff795548:

                Constant.theme = R.style.AppTheme_brown;

                break;

            default:

                    Constant.theme = R.style.AppTheme;

        }

    }

}

Terakhir sesuaikan AndroidManifest anda seperti berikut:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.giviews.employee">



    <uses-feature android:name="android.hardware.camera2"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>



    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".EmployeeActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:name=".EmployeeEditor"

            android:parentActivityName=".EmployeeActivity">

            <meta-data

                android:name="android.support.PARENT_ACTIVITY"

                android:value=".EmployeeActivity" />

        </activity>



        <!-- Employee Details -->

        <activity

            android:name=".EmployeeDetailsActivity"

            android:parentActivityName=".EmployeeActivity">

            <meta-data

                android:name="android.support.PARENT_ACTIVITY"

                android:value=".EmployeeActivity" />

        </activity>



        <provider

            android:name=".data.EmployeeProvider"

            android:authorities="com.giviews.employee"

            android:exported="false" />



        <!-- searchable -->

        <activity

            android:name=".SearchableActivity"

            android:launchMode="singleTop">

            <intent-filter>

                <action android:name="android.intent.action.SEARCH" />

            </intent-filter>

            <intent-filter>

                <action android:name="android.intent.action.VIEW" />

            </intent-filter>



            <meta-data

                android:name="android.app.searchable"

                android:resource="@xml/searchable" />

        </activity>



        <meta-data

            android:name="android.app.default_searchable"

            android:value="com.giview.employee.SearchableActivity" />



        <activity android:name=".SettingsActivity"></activity>

    </application>



</manifest>


Sekarang jalankan aplikasi anda, jika ada yang kurang jelas silakan ditanyakan pada komentar dibawah. jika anda ingin melihat sourcode versi full silakan download disini. jika artikel ini bermanfaat silakan di share, selamat mencoba semoga bermanfaat kdepanya kita akan membuat CRUD SQLite dengan model dan adapter, CRUD dengan database firebase, dan CRUD dengan MySQL database

Unknown

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 komentar :