Thursday 14 December 2017

Create,Retrieve,Update and Delete row in Recyclerview in  TabLayout with search  option in Toolbar of Android App.


This is a simple TaskManager android application. There is a TabLayout with two tabs. Each tab contains a recyclerview in viewpager .User can add new task. Delete and Update existing task.

Software Used
1.JDK 7 or 8
2. Android Studio 2.3.3
3. BlueStack (Any other AVD device like genymotion etc can also be used)

To Integrate SQLite database,please follow tutorial at below  url

 http://codestart.info/android-sqlite-tutorial-with-recyclerview-crud/



Required Files for The project are provided at the end for download
There is only one package named com.murach.mytaskmanager which contains  six files
1.MainActivity.java
2.RVAdapter.javaàRecyclerViewAdapter class.
3.ViewPagerAdapter.javaàAdapter class For the ViewPager in Tabs.
Other three files are inside a folder named TabFragments which is inside the above package.
1.BusinessTaskFragment:àFragment Class
2.PersonalTaskFragment:àFragment Class
3.TaskModel(Entity Class)
The application has six layout files.
1.activity-main.xmlàLayout file of MainActivity  class.
2.business_task_fragment.xmlàLayout  file of Business Task Fragment Class
3.personal_task_fragmentàLayout  file of Personal Task Fragment Class
4.input_box.xmlàLayout file of   Dialog to edit task.
5.list_row.xmlàLayout  file of row of recyclerview component
6.menu_main.xmlàLayout file of menu bar with searchview widget

ProjectStructure


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.murach.mytaskmanager" >

    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:theme="@style/AppTheme" >
        <
activity
           
android:name=".MainActivity"
           
android:label="@string/app_name" >
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />

                <
category android:name="android.intent.category.LAUNCHER" />
            </
intent-filter>
        </
activity>
    </
application>

</
manifest>

Module.app(Gradle File)

apply plugin: 'com.android.application'

android {
    compileSdkVersion
26
   
buildToolsVersion "27.0.2"
   
defaultConfig {
        applicationId
"com.rray.mytaskmanager"
       
minSdkVersion 15
       
targetSdkVersion 26
       
versionCode 1
       
versionName "1.0"
       
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   
}
    buildTypes {
        release {
            minifyEnabled
false
           
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    compile fileTree(
dir: 'libs', include: ['*.jar'])
    androidTestCompile(
'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude
group: 'com.android.support', module: 'support-annotations'
   
})
    compile
'com.android.support:appcompat-v7:26.+'
   
compile 'com.android.support:design:26.+'
   
compile 'com.android.support.constraint:constraint-layout:1.0.2'
   
testCompile 'junit:junit:4.12'
}

Below code in grade file is important as it adds material design components to the application .
dependencies {
    compile fileTree(
dir: 'libs', include: ['*.jar'])
    androidTestCompile(
'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude
group: 'com.android.support', module: 'support-annotations'
   
})
    compile
'com.android.support:appcompat-v7:26.+'
   
compile 'com.android.support:design:26.+'
   
compile 'com.android.support.constraint:constraint-layout:1.0.2'
   
testCompile 'junit:junit:4.12'
}
 
 
Floating Action Button at right Button is used to add new Task.
 
In RVAdepter class code using dialog is as below
 
 
 
For using AlertDialog the code is as below(To be used in RVAdapter class)
 
//  dialog box to edit an item
private void editItemFromList(final View v, final int position)
{

    LayoutInflater li = LayoutInflater.from(v.getContext());
    View alertLayout = li.inflate(R.layout.
input_box, null);


   
final EditText inputTask = (EditText) alertLayout.findViewById(R.id.txtinput);


    String  taskVal=
task_list.get(position).getTaskName().toString();
    inputTask.setText(taskVal);

    AlertDialog.Builder builder =
new AlertDialog.Builder(v.getContext());

   
// this is set the view from XML inside AlertDialog
   
builder.setView(alertLayout);
    builder.setTitle(
"Edit Task");
    builder.setMessage(
"What do you want to do?");
    builder.setCancelable(
false);


    builder.setPositiveButton(
"UpdateTask",new DialogInterface.OnClickListener()

            {
               
public void onClick (DialogInterface dialog,int id){

                   
//Code to add new item to RecyclerView
                   
String editedTaskVal = String.valueOf(inputTask.getText());
                    TaskModel taskModel =
new TaskModel(editedTaskVal ,false);
                    UpdateTask(
position, taskModel);
                    Toast.makeText(
v.getContext(), "Updated Task", Toast.LENGTH_SHORT).show();


                }
            }

    );
    builder.setNegativeButton(
"Cancel",null);
    Toast.makeText(v.getContext(),
"Update Cancelled", Toast.LENGTH_SHORT).show();
    builder.create().show();

};

For delete an Item to Recyclerview the code in RV adapter class is as follows

// confirmation dialog box to delete an unit
private void deleteItemFromList(View v, final int position) {

    AlertDialog.Builder builder =
new AlertDialog.Builder(v.getContext());

   
//builder.setTitle("Dlete ");
   
builder.setMessage("Delete Task ?")
            .setCancelable(
false)
            .setPositiveButton(
"CONFIRM",
                    
new DialogInterface.OnClickListener() {
                       
public void onClick(DialogInterface dialog, int id) {

                           
task_list.remove(position);
                            notifyDataSetChanged();


                        }
                    })
            .setNegativeButton(
"CANCEL", new DialogInterface.OnClickListener() {
               
public void onClick(DialogInterface dialog, int id) {


                }
            });

    builder.show();

}



View Task



When user taps a  record, it is displayed in a dialog box with delete option.



Add New Task




Delete Task



Edit Task




Searching Tasks




Project Files can be downloaded at below URL




https://www.dropbox.com/s/5cgqehlznblmcfx/MyTaskManager.rar?dl=0

5 comments:

  1. Hi,
    If there is any problem with this tutorial,Please comment about it.So that it can be fixed soon.
    Thanks
    Raichand

    ReplyDelete
  2. Your code looks good to me. Thanks! :)
    Ronny.
    http://codestart.info/android-sqlite-tutorial-with-recyclerview-crud/

    ReplyDelete
  3. Hello bro kindly tell me how can i open new fragment from RecyclerViewitems thru adapter

    ReplyDelete