Thursday, 31 January 2019

Android Scroll To Hide Toolbar



1) build.gradle

    implementation 'com.android.support:design:28.0.0'

2) Style.xml
res/values/style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

3) MainActivity.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

add a  layout_scrollFlags in Toolbar and layout_behavior in ViewPager 

4) MAinActivity.java

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        setSupportActionBar(toolbar);
        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new One(), "TAB ONE");
        adapter.addFragment(new Two(), "TAB TWO");
        adapter.addFragment(new Three(), "TAB THREE");
        viewPager.setAdapter(adapter);
    }

    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

}

3) In Fragment which i add on above code is just have a one textview don't need to add in here  

Monday, 21 January 2019

How to Make Blinking Animation in Android




1) anim/blinkinh_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="400"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toAlpha="1.0" />


</set>

2) MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Blinking Animation"
        android:textSize="22sp"
        android:textStyle="bold"
        android:textAlignment="center"/>

    <Button
        android:id="@+id/blinking_animation"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:text="Blinking Animation"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/start"
        android:text="Start" />

</LinearLayout>

3) MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button start, button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button) findViewById(R.id.start);
        button = (Button) findViewById(R.id.blinking_animation);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(start.getText().toString().matches("Start")){
                    startBlinkingAnimation();
                    start.setText("Stop");
                } else {
                    button.setAnimation(null);
                    start.setText("Start");
                }
            }
        });
    }
    public void startBlinkingAnimation() {
        Animation startAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blinking_animation);
        button.startAnimation(startAnimation);
    }

}

Wednesday, 9 January 2019

Android Pop up window with animation





Create a directory anim on your res folder and add this two xml file on anim folder

1) fromnothing.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1"
        android:duration="800"/>
</set>


2) fromsmall.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--Todo: Pop-up Animation -->

    <translate
        android:fromYDelta="-100%p"
        android:toYDelta="0"
        android:duration="1600"/>

    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:toXScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1600"/>

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1"
        android:duration="800"/>
</set>

3) MainActivity.XML

<?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=".MainActivity">


    <!--Todo: Pop-Up Start-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/popup_bg"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:elevation="4dp"
            android:id="@+id/popup"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:text="Pop-Up"
                android:layout_marginTop="5dp"
                android:textStyle="italic"
                android:textSize="20sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Pop-Up Message Here"
                android:textSize="17sp"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textAlignment="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Close"
                android:id="@+id/btn_close"
                android:layout_marginTop="10dp"
                android:layout_gravity="center"
                android:backgroundTint="@android:color/holo_green_light"/>

        </LinearLayout>

    <!--Todo: Background End-->


    <!--Todo: Background Start-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#CF000000"
        android:elevation="2dp"
        android:id="@+id/bg_popup"/>

    <!--Todo: Background End-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="33dp"
        android:text="Open Pop-Up"
        android:id="@+id/popup_open"/>.

</RelativeLayout>

4) MainActivity.java


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {


    LinearLayout bg_popup, popup;
    Animation fromsmall, fromnothing;
    Button popup_open, btn_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bg_popup = (LinearLayout) findViewById(R.id.bg_popup);
        popup = (LinearLayout) findViewById(R.id.popup);

        popup_open = (Button) findViewById(R.id.popup_open);
        btn_close = (Button) findViewById(R.id.btn_close);

        fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
        fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);

        bg_popup.setAlpha(0);
        popup.setAlpha(0);

        popup_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bg_popup.setAlpha(1);
                bg_popup.startAnimation(fromnothing);
                popup.setAlpha(1);
                popup.startAnimation(fromsmall);
            }
        });

        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bg_popup.setAlpha(0);
                popup.setAlpha(0);
            }
        });

    }
}

Wednesday, 26 December 2018

Android Tablayout Example With Viewpager






1) MainActivity.XML

<?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=".MainActivity">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/tablayout_id"
        android:background="@color/colorPrimary"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpager_id"
        android:layout_below="@+id/tablayout_id"/>

</RelativeLayout>

2) MainActivity.java

public class MainActivity extends AppCompatActivity {

    TabLayout tableLayout;
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewpager_id);
        tableLayout = (TabLayout) findViewById(R.id.tablayout_id);

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new OneFragment(), "One"); // Add Fragments as per your Need
        adapter.addFragment(new TwoFragment(), "Two");
        adapter.addFragment(new ThreeFragment(), "Three");

        viewPager.setAdapter(adapter);

        tableLayout.setupWithViewPager(viewPager);
    }
}


3) ViewPagerAdapter.java

On Above Video I Used FragmentPagerAdapter for ViewPagerAdapter  
but i strongly recommended you to used FragmentStatePagerAdapter it will help to load your fragment batter tahn FragmentPagerAdapter 

public class ViewPagerAdapter  extends FragmentStatePagerAdapter{

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

    @Override
    public int getCount() {
        return fragmentTitleList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTitleList.get(position);
    }

    public void addFragment(Fragment fragment, String title){
        fragmentList.add(fragment);
        fragmentTitleList.add(title);
    }
}

4) Now Add Your Fragment As Per Your Need


i) OneFragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/btn_bg1"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    tools:context=".OneFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One"
        android:layout_gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="50sp"/>

</FrameLayout>

i) TwoFragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/btn_bg1"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    tools:context=".TwoFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One"
        android:layout_gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="50sp"/>

</FrameLayout>

iii) ThreeFragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/btn_bg1"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    tools:context=".ThreeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One"
        android:layout_gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="50sp"/>

</FrameLayout>

Monday, 3 December 2018

Android Floating Action Button With Text Material Design Without library





1) Make Sure This Library is Added In Your gradle.build file

    implementation 'com.android.support:design:28.0.0'

2) Activity.XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:gravity="center_horizontal"
        android:layout_marginRight="16dp">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:fabSize="mini"
            android:tint="#ffffff"
            android:layout_marginTop="5dp"
            app:srcCompat="@drawable/add_photo"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/photos"
            android:text="Photos"
            android:textAlignment="center"
            android:textSize="15sp"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:background="@drawable/shape_lable"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:fabSize="mini"
            android:tint="#ffffff"
            android:layout_marginTop="5dp"
            app:srcCompat="@drawable/music"
            tools:ignore="VectorDrawableCompat" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/music"
            android:text="Music"
            android:textAlignment="center"
            android:textSize="15sp"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:background="@drawable/shape_lable"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:fabSize="mini"
            android:tint="#ffffff"
            android:layout_marginTop="5dp"
            app:srcCompat="@drawable/done"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/done"
            android:text="Done"
            android:textAlignment="center"
            android:textSize="15sp"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:background="@drawable/shape_lable"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:fabSize="normal"
            android:tint="#ffffff"
            app:srcCompat="@drawable/add"
            tools:ignore="VectorDrawableCompat"
            android:layout_marginTop="10dp"/>

    </LinearLayout>



</android.support.constraint.ConstraintLayout>


3) Activity.Java

import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    FloatingActionButton fab, fab_one, fab_two, fab_three;
    TextView photo, music, done;
    Float translationY = 100f;

    OvershootInterpolator interpolator = new OvershootInterpolator();


    Boolean isMenuOpen = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fab = (FloatingActionButton) findViewById(R.id.fab);

        fab_one = (FloatingActionButton) findViewById(R.id.fab_one);
        fab_two = (FloatingActionButton) findViewById(R.id.fab_two);
        fab_three = (FloatingActionButton) findViewById(R.id.fab_three);

        photo = (TextView) findViewById(R.id.photos);
        music = (TextView) findViewById(R.id.music);
        done = (TextView) findViewById(R.id.done);

        fab_one.setAlpha(0f);
        photo.setAlpha(0f);
        fab_two.setAlpha(0f);
        music.setAlpha(0f);
        fab_three.setAlpha(0f);
        done.setAlpha(0f);

        fab_one.setTranslationY(translationY);
        photo.setTranslationY(translationY);
        fab_two.setTranslationY(translationY);
        music.setTranslationY(translationY);
        fab_three.setTranslationY(translationY);
        done.setTranslationY(translationY);

        fab.setOnClickListener(this);

        fab_one.setOnClickListener(this);
        fab_two.setOnClickListener(this);
        fab_three.setOnClickListener(this);
    }

    private void openMenu() {
    isMenuOpen = !isMenuOpen;

 fab.animate().setInterpolator(interpolator).rotation(45f).setDuration(300).start();

        fab_one.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
photo.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();

        fab_two.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
music.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();

        fab_three.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
done.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();


    }

    private void closeMenu() {
        isMenuOpen = !isMenuOpen;

        fab.animate().setInterpolator(interpolator).rotation(0f).setDuration(300).start();

        fab_one.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
        photo.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();

        fab_two.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
        music.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();

        fab_three.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
        done.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fab:
                if (isMenuOpen) {
                    closeMenu();
                } else {
                    openMenu();
                }
                break;
            case R.id.fab_one:
                if (isMenuOpen) {
                    closeMenu();
                } else {
                    openMenu();
                }
                break;
            case R.id.fab_two:
                if (isMenuOpen) {
                    closeMenu();
                } else {
                    openMenu();
                }
                break;
            case R.id.fab_three:
                if (isMenuOpen) {
                    closeMenu();
                } else {
                    openMenu();
                }
                break;
        }
    }
}

Tuesday, 30 October 2018

Android SQLlite Database Example

1) DatabaseHelper.java

package com.example.admin.myapplication;

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


public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String name,String surname) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        long result = db.insert(TABLE_NAME,null ,contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

    public boolean updateData(String id,String name,String surname) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,id);
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
        return true;
    }

    public Integer deleteData (String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
    }

}


2) MainActivity.xml

<?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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add_data"
            android:orientation="vertical"
            android:layout_marginTop="20dp"
            android:padding="15dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name"
                android:id="@+id/et_name"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Surname"
                android:id="@+id/et_surname"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add Data"
                android:id="@+id/btn_add"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/edit_data"
            android:layout_marginTop="20dp"
            android:padding="15dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Id"
                android:id="@+id/et_id_edit"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name"
                android:id="@+id/et_name_edit"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Surname"
                android:id="@+id/et_surname_edit"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Edit Data"
                android:id="@+id/btn_edit"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/delete_data"
            android:layout_marginTop="20dp"
            android:padding="15dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Id"
                android:id="@+id/et_id_delete"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete Data"
                android:id="@+id/btn_delete"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:padding="10dp">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Get Data"
                android:id="@+id/btn_get"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>


3) MainActivity.java

package com.example.admin.myapplication;

import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    Button btn_get, btn_add, btn_edit, btn_delete;
    EditText et_name, et_surname, et_id_edit, et_name_edit, et_surname_edit, et_id_delete;
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseHelper(this);


        btn_get = (Button)findViewById(R.id.btn_get);
        btn_add = (Button)findViewById(R.id.btn_add);
        btn_edit = (Button)findViewById(R.id.btn_edit);
        btn_delete = (Button)findViewById(R.id.btn_delete);

        //Add Data
        et_name = (EditText)findViewById(R.id.et_name);
        et_surname = (EditText)findViewById(R.id.et_surname);

        // Update Data
        et_id_edit = (EditText)findViewById(R.id.et_name_edit);
        et_name_edit = (EditText)findViewById(R.id.et_name_edit);
        et_surname_edit = (EditText)findViewById(R.id.et_surname_edit);

        // Delete Data
        et_id_delete = (EditText)findViewById(R.id.et_id_delete);


        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddData();
            }
        });

        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateData();
            }
        });

 btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeleteData();
            }
        });

    }

 public void viewAll() {
        Cursor res = db.getAllData();
        if(res.getCount() == 0) {
            // show message
            showMessage("Error","Nothing found");
            return;
        }

        StringBuffer buffer = new StringBuffer();
        while (res.moveToNext()) {
            buffer.append("Id :"+ res.getString(0)+"\n");
            buffer.append("Name :"+ res.getString(1)+"\n");
            buffer.append("Surname :"+ res.getString(2)+"\n");
            buffer.append("Marks :"+ res.getString(3)+"\n\n");
        }

        // Show all data
        showMessage("Data",buffer.toString());

    }

    public void DeleteData() {
        Integer deletedRows = db.deleteData(et_id_delete.getText().toString());
        if(deletedRows > 0) {
            Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
            et_id_delete.setText("");
        } else{
            Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show();
        }
    }
    public void UpdateData() {
        boolean isUpdate = db.updateData(et_id_edit.getText().toString(),
                et_name_edit.getText().toString(),
                et_surname_edit.getText().toString());
        if(isUpdate == true) {
            Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
            et_id_edit.setText("");
            et_name_edit.setText("");
            et_surname_edit.setText("");
        } else{
            Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
        }

    }
    public  void AddData() {
        boolean isInserted = db.insertData(et_name.getText().toString(),
                et_surname.getText().toString());
        if(isInserted == true)
        {
            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
            et_name.setText("");
            et_surname.setText("");
        } else{
            Toast.makeText(MainActivity.this,"Data not Inserted", Toast.LENGTH_LONG).show();
        }
    }


    public void showMessage(String title,String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...