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);
            }
        });

    }
}

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...