Wednesday 15 April 2015

FlipViewPager in List View





This tutorial aims to provide a working page flip implementation for usage in ListView.

Compatibility :  Android 4.0+

Source code from : https://github.com/Yalantis/FlipViewPager.Draco

For a working implementation, Have a look at the Live Demo and download the Sample Project - attached :)  

 To achieve the same grid-looking view you should :

1. I have implemented the library in same project if this is not convenient, you can create a lib and reference it to the project

2. Create your main layout, it will be the view with 2 items merged together

friends_merge_page.xml
<!-- ... -->
   
    <ImageView 
        android:id = "@+id/first"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"
        android:contentDescription = "left image"
        android:scaleType = "fitXY"/>
    <LinearLayout
        android:layout_width = "1dp"
        android:layout_height = "fill_parent"
        android:layout_weight = "0"
        android:background = "#000000" /> 

    <ImageView 
        android:id = "@+id/second"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"

        android:contentDescription = "right image"
        android:scaleType = "fitXY"/>

<!-- ... -->

3. Create layout for displaying an additional info for each merged item

                                friends_info.xml

<!-- ... -->
       <com.androiddelight.flip.sample.views.FontTextView
        style = "@style/TextView.Nickname"
        android:id = "@+id/nickname" />
    <LinearLayout
        android:layout_below = "@+id/nickname"
        android:id = "@+id/interestsPrimary"
        style = "@style/LinearLayout.Interests">
    <com.androiddelight.flip.sample.views.FontTextView 
        style = "@style/TextView.Interest"
        android:id = "@+id/interest_1" /> 
<!-- ... -->
</LinearLayout>

4. Create your adapter and extend it from BaseFlipAdapter<T>
  
 class FriendsAdapter extends BaseFlipAdapter<Friend> {
     @Override
         public View getPage(int position,
                 View convertView,
                 ViewGroup parent,
                 Friend friend1,
                 Friend friend2){
             // ...
     }
class FriendsHolder {
        // ...
    }
}

5. Set your adapter in ListView

final ListView friends = (ListView) findViewById(R.id.friends);
friends.setAdapter(new FriendsAdapter(this, Utils.friends, settings)); 

6. You can handle clicks just like in regular ListView

friends.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Friend friend = (Friend) friends.getAdapter().getItem(position);
    Toast.makeText(FriendsActivity.this, friend.getNickname(), 
    Toast.LENGTH_SHORT).show();
    }
});
 
Live Demo :

Source code : Click here to download.

As always, Thanks a lot for reading...
Don't forget to share this post if you found it interesting!

If you find something to add to this post? or any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback's are most welcome.

Thursday 9 April 2015

Android Pattern Lock Tutorial

Hi Folks, In this post i am going to show you how to create a Lock9View and create your own pattern as we have seen in Android Pattern Lock Screen. By following this tutorial you can use this Pattern Lock in you application as a security test for users before performing any task or passing to an activity.

Source code from : http://android-arsenal.com/details/1/1790

Step 1 : Create a new project by going to File ⇒ New Android Application Project. Fill all the details and name your activity as MainActivity.

⇒ Designing pattern node

The next major step is to implement the Lock9View into our project. Before that we should place the drawables for pattern node.
  • lock_9_view_node_highlighted.png
  • lock_9_view_node_normal.png

Step 2 : Create a new xml file under values. Right Click on values folder ⇒ New ⇒ Android XML File and name it as attrs_lock_9_view.xml and fill it with following code.

attrs_lock_9_view.xml
<?xml version = "1.0" encoding = "utf-8"?>
<resources>
    <declare-styleable name = "Lock9View">   
 
                <attr name = "nodeSrc" format = "color|reference">
        <attr name = "nodeOnSrc" format = "color|reference">
        <attr name = "lineColor" format = "color">
        <attr name = "lineWidth" format = "dimension"> 
       </declare-styleable>
</resources>
 
The declare-styleable is used to set attributes to components. We have declared style attributes for Lock9View in attrs_lock_9_view.xml as shown above. These style attributes can be set in xml where the Lock9View is placed.

Step 3 : Create a package under main package. Right Click on main package ⇒ New ⇒ Package and name after your main package as .utils.
[for eg : My main package is : com.android.androiddelight.lock9view Utils package is : com.android.androiddelight.lock9view.utils]

Step 4 : Create a new class under src/utils package. Right Click on src/utils package (com.android.androiddelight.lock9view.utils) ⇒ New ⇒ Class and name it as Lock9View.java and fill it with following code.
                                           Lock9View.java
package com.android.androiddelight.lock9view.utils;

import java.util.ArrayList;
import java.util.List;

import com.android.androiddelight.lock9view.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class Lock9View extends ViewGroup {
  private Paint paint;
  private Bitmap bitmap;
  private Canvas canvas;

  private List<Pair<NodeView, NodeView>> lineList;
  private NodeView currentNode;
  
  private StringBuilder pwdSb;
  private CallBack callBack;
 
  private Drawable nodeSrc;
  private Drawable nodeOnSrc;
public Lock9View(Context context) {
        this(context, null);
    }
public Lock9View(Context context, AttributeSet attrs) {
        this(context, attrs,
0);
    }
public Lock9View(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,
0);
    }
public Lock9View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr); 
        initFromAttributes(attrs, defStyleAttr);
    }
private void initFromAttributes(AttributeSet attrs, int defStyleAttr) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs,    R.styleable.Lock9View, defStyleAttr,
0);

        nodeSrc = a.getDrawable(R.styleable.Lock9View_nodeSrc);
        nodeOnSrc = a.getDrawable(R.styleable.Lock9View_nodeOnSrc);
        int lineColor = Color.argb(
0, 0, 0, 0);
        lineColor = a.getColor(R.styleable.Lock9View_lineColor, lineColor);
        float lineWidth = 20.0f;
        lineWidth = a.getDimension(R.styleable.Lock9View_lineWidth, lineWidth);

        a.recycle();

        paint = new Paint(Paint.DITHER_FLAG);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(lineWidth);
        paint.setColor(lineColor);
        paint.setAntiAlias(true);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        bitmap = Bitmap.createBitmap(dm.widthPixels, dm.widthPixels,   Bitmap.Config.ARGB_8888);
        canvas = new Canvas();
        canvas.setBitmap(bitmap);

        for (int n = 0; n < 9; n++) {
            NodeView node = new NodeView(getContext(), n + 1);
            addView(node);
        }
        lineList = new ArrayList<Pair<NodeView,NodeView>>();
        pwdSb = new StringBuilder();

        setWillNotDraw(false);
    } 

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
    }
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (!changed) {
            return;
        }
        int width = right - left;
        int nodeWidth = width / 3;
        int nodePadding = nodeWidth / 6;
        for (int n = 0; n < 9; n++) {
            NodeView node = (NodeView) getChildAt(n);
            int row = n / 3;
            int col = n % 3;
            int l = col * nodeWidth + nodePadding;
            int t = row * nodeWidth + nodePadding;
            int r = col * nodeWidth + nodeWidth - nodePadding;
            int b = row * nodeWidth + nodeWidth - nodePadding;
            node.layout(l, t, r, b);
        }
    }
@Override 
protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, 0, 0, null);
    }
@Override 
protected boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                NodeView nodeAt = getNodeAt(event.getX(), event.getY());
                if (nodeAt == null && currentNode == null) {
                    return true;
                } else {
                    clearScreenAndDrawList();
                    if (currentNode == null) {
                        currentNode = nodeAt;
                        currentNode.setHighLighted(true);
                        pwdSb.append(currentNode.getNum());
                    }
                    else if (nodeAt == null || nodeAt.isHighLighted()) {
                        
                        canvas.drawLine(currentNode.getCenterX(), currentNode.getCenterY(), event.getX(), event.getY(), paint);
                    } else {
                        canvas.drawLine(currentNode.getCenterX(), currentNode.getCenterY(), nodeAt.getCenterX(), nodeAt.getCenterY(), paint);                        
            nodeAt.setHighLighted(true);
            Pair<NodeView, NodeView> pair = new Pair<NodeView, NodeView>(currentNode, nodeAt);
            lineList.add(pair);
                       
             currentNode = nodeAt;
             pwdSb.append(currentNode.getNum());
         }
             invalidate();
         }
         return true;
         
         case MotionEvent.ACTION_UP:
              
                if (pwdSb.length() <= 0) {
                    return super.onTouchEvent(event);
                }
             
                if (callBack != null) {
                    callBack.onFinish(pwdSb.toString());
                    pwdSb.setLength(0);
                }
              
                currentNode =
null;
                lineList.clear();
                clearScreenAndDrawList();
               
                for (int n = 0; n < getChildCount(); n++) {
                    NodeView node = (NodeView) getChildAt(n);
                    node.setHighLighted(false);
                }
               
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }

private void clearScreenAndDrawList() {
     canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
     for (Pair<NodeView, NodeView> pair : lineList) {
          canvas.drawLine(pair.first.getCenterX(), pair.first.getCenterY(),  pair.second.getCenterX(), pair.second.getCenterY(), paint);
        }
    }
private NodeView getNodeAt(float x, float y) {
        
          for (int n = 0; n < getChildCount(); n++) {
            NodeView node = (NodeView) getChildAt(n);
            if (!(x >= node.getLeft() && x < node.getRight())) {
                continue;
            }
            if (!(y >= node.getTop() && y < node.getBottom())) {
               
continue;
            }
            return node;
        }
        return null;
    }
public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

public class NodeView extends View {

        private int num;
       
private boolean highLighted;

       
private NodeView(Context context) {
            super(context);
        }

       
public NodeView(Context context, int num) {
            this(context);
            this.num = num;
            highLighted = false;
            if (nodeSrc == null) {
                setBackgroundResource(0);
            } else {
                setBackgroundDrawable(nodeSrc);
            }
        }

       
public boolean isHighLighted() {
            return highLighted;
        }

       
public void setHighLighted(boolean highLighted) {
            this.highLighted = highLighted;
            if (highLighted) {
                if (nodeOnSrc == null) {
                    setBackgroundResource(0);
                } else {
                    setBackgroundDrawable(nodeOnSrc);
                }
            } else {
                if (nodeSrc == null) {
                    setBackgroundResource(0);
                } else {
                    setBackgroundDrawable(nodeSrc);
                }
            }
        }

        public int getCenterX() {
            return (getLeft() + getRight()) / 2;
        }

        public int
getCenterY() {
           
return (getTop() + getBottom()) / 2;
        }

        public int getNum() {
           
return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
public interface CallBack {
        public void onFinish(String password);
    }
}

Now we are ready with the 9 Pattern View. Next i am going to declare this view inside my main layout that is activity_main.xml.

Step 5 : Open the activity_main.xml under res/layout and fill it with following code.

activity_main.xml

<?xml version = "1.0" encoding = "utf-8"?>
<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:background = "#ff222233">
      <!--Include the Lock9View along with the package where it is located -->
      <com.android.androiddelight.lock9view.utils.Lock9View
        android:id = "@+id/lock_9_view"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"
        <!--Note : These  attributes are from declare-styleable -->
        app:nodeSrc = "@drawable/lock_9_view_node_normal"
        app:nodeOnSrc = "@drawable/lock_9_view_node_highlighted"
        app:lineColor = "#ff006699"
        app:lineWidth = "8dp" />

</FrameLayout

Step 6 : Open the MainActivity.java under src/package we need to declare the Lock9View and reference it in MainActivity. Write the code as follows in MainActivity.

MainActivity.java
package com.android.androiddelight.lock9view;

import com.android.androiddelight.lock9view.utils.Lock9View;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private Lock9View lock9View;
private static String MY_PREFS_NAME = "PatternLock";
private static String PATTERN_KEY; 
 
SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
   
   lock9View = (Lock9View) findViewById(R.id.lock_9_view);
   lock9View.setCallBack(new Lock9View.CallBack() {

      @Override
     
public void onFinish(String password) {
        PATTERN_KEY = prefs.getString("Pattern", "invalid");
               
        if(PATTERN_KEY.equals("invalid")){
        Toast.makeText(MainActivity.this, "Options --> Create new Pattern", Toast.LENGTH_LONG).show();   
        }else{
        if(password.equals(PATTERN_KEY)){
           Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
           startActivity(intent);
           finish();
           Toast.makeText(MainActivity.this, "Login success!",   Toast.LENGTH_SHORT).show();
           }else{
           Toast.makeText(MainActivity.this, "Pattern incorrect!", Toast.LENGTH_SHORT).show();
                    }
                } 
            }
        });

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.contact:
                new AlertDialog.Builder(this)
                .setTitle("Android-Lock9View")
                .setMessage(
                "Email : anfersyed@gmail.com\n" +
                "Blog  : http://android-delight.blogspot.in/\n")
                .setPositiveButton("OK", null)
                .show();
                return true;
               
            case R.id.create_new_pattern:
            Intent intent = new Intent(MainActivity.this, ChangeActivity.class);
                startActivity(intent);
                finish();
                return true;
               
            default:
            return super.onOptionsItemSelected(item);
        }
    }
}

Step 7 : We have to create one more activity to change the pattern. So Create a new Activity by going to File ⇒ New Other ⇒ Android Activity. Fill all the details and name your activity as ChangeActivity.

In ChangeActivity we need 2 Lock9View 1- for drawing new pattern 2 - for confirm drawing pattern
 
Step 8 : Open the activity_change.xml under res/layout and fill it with following code.

activity_change.xml
<?xml version = "1.0" encoding = "utf-8"?>
<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:background = "#ff222233">
    <!--TextView for prompting the users  -->
      <TextView
        android:id = "@+id/tvMsg"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginLeft = "5dp"
        android:layout_marginTop = "20dp"
        android:textAppearance = "?android:attr/textAppearanceSmall"
        android:textColor = "#ff006699" />
      <!-- FrameLayout to hold the 1st Lock9View -->
      <FrameLayout
        android:id = "@+id/enterPattern"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center" >
 
      <com.android.androiddelight.lock9view.utils.Lock9View
        android:id = "@+id/lock_9_view"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"
        app:nodeSrc = "@drawable/lock_9_view_node_normal"
        app:nodeOnSrc = "@drawable/lock_9_view_node_highlighted"
        app:lineColor = "#ff006699"
        app:lineWidth = "8dp" />
     
      </FrameLayout>
      <!-- FrameLayout to hold the 2nd Lock9View -->
      <FrameLayout
        android:id = "@+id/confirmPattern"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center" 
        android:visibility = "gone" >
 
      <com.android.androiddelight.lock9view.utils.Lock9View
        android:id = "@+id/lock_viewConfirm"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"

        app:nodeSrc = "@drawable/lock_9_view_node_normal"
        app:nodeOnSrc = "@drawable/lock_9_view_node_highlighted"
        app:lineColor = "#ff006699"
        app:lineWidth = "8dp" />
      </FrameLayout> 
</FrameLayout

The layout is build. Now getting to java code to write business logic.

Step 9 : Open the ChangeActivity.java under src/package and write down the code as follows.

                                                           ChangeActivity.java

package com.android.androiddelight.lock9view;

import com.android.androiddelight.lock9view.utils.Lock9View;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ChangeActivity extends  ActionBarActivity {

private FrameLayout enterPatternContainer, confirmPatternContainer;
private Lock9View lockViewFirstTry, lockViewConfirm;
private static String MY_PREFS_NAME = "PatternLock";
private static String PATTERN_KEY;
private SharedPreferences prefs;private Editor editor;
   
private TextView tvMsg;
@Override
protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_change);
  prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
  editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
  
  tvMsg = (TextView)findViewById(R.id.tvMsg);
  tvMsg.setText(getResources().getString(R.string.draw_pattern_msg)); 
    
  enterPatternContainer = (FrameLayout) findViewById(R.id.enterPattern);
  confirmPatternContainer = (FrameLayout) findViewById(R.id.confirmPattern);

  lockViewFirstTry = (Lock9View) findViewById(R.id.lock_viewFirstTry);
  lockViewConfirm =  (Lock9View) findViewById(R.id.lock_viewConfirm);
//we can get a call back string when ever user interacts 
//with the pattern lock view
lockViewFirstTry.setCallBack(new Lock9View.CallBack() {

 
@Override  
  public void onFinish(String password) {
     PATTERN_KEY = password;
     enterPatternContainer.setVisibility(View.GONE);
     tvMsg.setText(getResources().getString
     (R.string.redraw_confirm_pattern_msg));
     confirmPatternContainer.setVisibility(View.VISIBLE);
  }
}); 

lockViewConfirm.setCallBack(new Lock9View.CallBack() {

 
@Override  
  public void onFinish(String password) {

    if(password.equals(PATTERN_KEY)){
    Toast.makeText(getApplicationContext(), 
    "Pattern created successfully!", 
    Toast.LENGTH_SHORT).show();
    editor.putString("Pattern", password);
    editor.commit();
    Intent intent = new Intent(ChangeActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
    }else{
    Toast.makeText(getApplicationContext(), 
    "You have drawn the wrong Pattern", Toast.LENGTH_SHORT).show();
    }
   }
  });

@Override     
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_change, menu);
    return true;
    }

@Override     
public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.contact:
            new AlertDialog.Builder(this)
            .setTitle("Android-Lock9View")
            .setMessage(
            "Email : anfersyed@gmail.com"
            +"\nBlog  : http://android-delight.blogspot.in/")
            .setPositiveButton("OK", null).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Checkout the live demo :




Click here to get the source code. Share it!

Tuesday 7 April 2015

Restricting user from clearing internal app data like DB (SQLite), Shared Preference

As an android developer we should be aware of  the options provided by android for saving app data.

Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.  

SQLite : Android platform includes the SQLite embedded database and provides out of the box support to use it via Android APIs.

Shared Preference : Android provides some ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

Internal or External Storage : All Android devices have two file storage areas: "internal" and "external" storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not.

Android provides an option for clearing all the internal saved data. If users tap on "Clear data" from app setting all internal data of the app will be lost, shared preference , internal files, database, account settings, persistent preferences, etc.. 
Clearing the data should revert the application back to it's state as it was when you first installed it. So for developers its a bitter task when some users do this.

In this tutorial we are going to learn how to divert users when they go for "Clear data".

Step 1 : Create a new project by going to File ⇒ New Android Application Project. Fill all the details and name your activity as MainActivity.

Step 2 : When an app is having some internal data saved like sqlite, shared preference then we can find the "Clear data" button enabled in app settings. So in this project we are including the local database (SQLite) access.

Step 3 :  Create a new package for placing DataBaseHandler. Right Click on src/package folder ⇒ New ⇒ Package and name it as yourchoice.

Step 4 :  Create a new class under src/yourchoice package. Right Click on src/package folder ⇒ New ⇒ Class and name it as DataBaseHandler.java and fill it with following code.

                                                     DataBaseHandler.java

package com.androiddelight.cleardatademo.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;

public class DataBaseHandler extends  SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "ClearDataDemo";
// Table Name

private static final String TABLE_DEMO = "demo";
// Demo Table Columns names 
private static final String ID = "id";
private static final String NAME = "name";

private static final String PASS = "pass";
public DataBaseHandler (Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

@Override
public void onCreate(SQLiteDatabase db){
// TODO Auto-generated method stub
   String CREATE_DEMO_TABLE = "CREATE TABLE " + TABLE_DEMO + "("
                + ID + " INTEGER PRIMARY KEY,"
                + NAME + " TEXT,"
                + PASS + " TEXT" + ")";
   db.execSQL(CREATE_DEMO_TABLE); 
 }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// Drop older table if existed

   db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEMO);
   onCreate(db);
 }
// Saving to DB
public void saveDemo_data(String name, String pass){
// TODO Auto-generated method stub

   System.out.println(name +" " + pass);
   SQLiteDatabase db = this.getWritableDatabase();
   ContentValues values = new ContentValues();
        values.put(NAME, name);
        values.put(PASS, pass);
               
        db.insert(TABLE_DEMO, null, values);
       
        db.close();
 } 
// Getting row count from table
public int getTable_Count(){

   String selectQuery = null;
   selectQuery = "SELECT  * FROM " + TABLE_DEMO ;
   SQLiteDatabase db = this.getReadableDatabase();
   Cursor cursor = db.rawQuery(selectQuery, null);
   return cursor.getCount();
  }
}

Step 5 :  Open the activity_main.xml file under res/layout. Drag and drop a button and place it in center of the layout. Text the button as "Save data" eg: android:text = "Save data"

Step 6 :  Open the MainActivity.java file under src/package. We need to declare and reference the button in java file like Button btn_save = (Button) findViewById(R.id.button1);

Step 7 :  Set OnClickListener for btn_save. When the users taps on button one row is saved in sqlite by calling the method saveDemo_data and the total row count is retrieved by calling getTable_Count method in DataBaseHandler. It is displayed in Toast for the users.

Now the SQLite implementation and storing data all are finished.

To start, open your phone’s Settings app. Scroll down and tap Apps under the Device heading.
From there, find the app "ClearDataDemo" and tap on it.
Once you do that, you’ll be taken to an "app info" screen that provides some technical details including the app’s version number and how much storage space it eats up. There you can find the button "Clear data" as shown in the image below side


The main objective of this tutorial is to remove the "Clear data" and replace "Manage space" button as shown below which will launch an activity.

Step 8 : Create a new Activity by going to File ⇒ New Other ⇒ Android Activity. Fill all the details and name your activity as ManageData.

Step 9 : Open the activity_manage_data.xml file under res/layout. Drag and drop a TextView and place it in center of the layout. Add text to TextView as "Sorry! you don't have permission to clear app data". This is to prompt users that they don't have access to clear app data.

Step 10 : Open the AndroidManifest.xml file under project folder and add the following code.


AndroidManifest.xml
<application>
    android : allowBackup = "true" 
   android : icon = "@drawable/ic_launcher"  
  android : label = "@string/app_name"
  android : manageSpaceActivity = ".ManageData" >  <!--This activity is  launched on taping the Manage space button from setting  -->
 And declare the ManageData activity in Manifest as below 

<activity
    android : name = ".ManageData"  

  android : label = "@string/title_activity_manage_data" >

Finally when you try to tap on the Manage space button it will launch an Activity as shown below



There you go!!! Click here for source code.  Share this if it is helpful :-)