Monday, 8 August 2016

Custom Navigation Drawer using Slidemenu in Android


Screenshots :


activity_main.xml :


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout 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"
tools:context="com.chantisandroid.exampleapp.MainActivity">

    <include layout="@layout/header" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to ChantisAndroid&apos;s Android Tutorial"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:textColor="@color/black"
        android:textSize="15dp"/>
</RelativeLayout>

    <include layout="@layout/slidemenu" />
</android.support.v4.widget.DrawerLayout>


slidemenu.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/left_drawer"
    android:layout_width="260dp"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:fillViewport="true" >

    <FrameLayout
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/RLSide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true">


        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollbars="none" >



            <!-- The navigation drawer -->
            <RelativeLayout
                android:layout_width="260dp"
                android:layout_height="match_parent"
                android:id="@+id/drawerPane"
                android:layout_gravity="start">

                <!-- Profile Box -->

                <RelativeLayout
                    android:id="@+id/profileBox"
                    android:layout_width="260dp"
                    android:layout_height="130dp"
                    android:layout_marginTop="20dp">

                    <ImageView
                        android:id="@+id/avatar"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginBottom="20dp"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:src="@drawable/kba_personn" />

                    <TextView
                        android:id="@+id/farmername"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/avatar"
                        android:layout_marginTop="26dp"
                        android:layout_toRightOf="@+id/avatar"
                        android:text="ChantisAndroid"
                        android:textColor="@color/black"
                        android:textSize="20dp" />

                </RelativeLayout>


                <TextView
                    android:id="@+id/text1"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#BDBDBD"
                    android:layout_below="@+id/profileBox"/>

                <RelativeLayout
                    android:id="@+id/Home"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_below="@+id/text1"
                    android:layout_marginTop="10dp"
                    android:padding="8dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/hme"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_home"
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:gravity="left|center"
                            android:text="Home"
                            android:textStyle="bold"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/hme"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="1dp"
                        android:src="@drawable/kba_home" />

                </RelativeLayout>


                <TextView
                    android:id="@+id/text2"
                    android:layout_width="187dp"
                    android:layout_height="1dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="10dp"
                    android:background="#BDBDBD"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/Home"/>

                <RelativeLayout
                    android:id="@+id/services"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_below="@+id/text2"
                    android:padding="8dp" >
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/seting"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_service"
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:text="Services"
                            android:textStyle="bold"
                            android:gravity="left|center"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/seting"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"

                        android:src="@drawable/kba_services" />

                </RelativeLayout>


                <TextView
                    android:id="@+id/text3"
                    android:layout_width="187dp"
                    android:layout_height="1dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="10dp"
                    android:background="#BDBDBD"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/services"/>


                <RelativeLayout
                    android:id="@+id/terms"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_below="@+id/text3"
                    android:padding="8dp"
                    android:layout_marginTop="2dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/img_terms"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_terms"
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:text="Privacy &amp; Terms"
                            android:textStyle="bold"
                            android:gravity="left|center"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/img_terms"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="1dp"
                        android:src="@drawable/kba_newprivacy" />

                </RelativeLayout>

                <TextView
                    android:id="@+id/text5"
                    android:layout_width="187dp"
                    android:layout_height="1dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="10dp"
                    android:background="#BDBDBD"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/terms"/>


                <RelativeLayout
                    android:id="@+id/feedback"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_below="@+id/text5"
                    android:padding="8dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/img_feedback"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_feedback"
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:text="Feedback"
                            android:gravity="left|center"
                            android:textStyle="bold"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/img_feedback"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="1dp"
                        android:src="@drawable/kba_newfeedback" />
                </RelativeLayout>



                <TextView
                    android:id="@+id/text6"
                    android:layout_width="187dp"
                    android:layout_height="1dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="10dp"
                    android:background="#BDBDBD"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/feedback"/>


                <RelativeLayout
                    android:id="@+id/aboutus"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_below="@+id/text6"
                    android:padding="8dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:layout_toRightOf="@+id/img_aboutus"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/txt_aboutus"
                            android:layout_width="match_parent"
                            android:layout_height="42dp"
                            android:text="About us"
                            android:textStyle="bold"
                            android:gravity="left|center"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/img_aboutus"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="4dp"
                        android:src="@drawable/kba_aboutus" />
                </RelativeLayout>

            </RelativeLayout>
        </ScrollView>
        <View
            android:layout_width="0.5dip"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

</RelativeLayout>


header.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/top_layout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"

        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="14dp"
            android:src="@drawable/menu_icn" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/imageView"
            android:layout_centerVertical="true"
            android:layout_marginLeft="40dp"
            android:text="Example App"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            android:textStyle="bold" />


    </RelativeLayout>
</RelativeLayout>


MainActivity.java:


package com.chantisandroid.exampleapp;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public RelativeLayout mDrawerList;
    public DrawerLayout mDrawerLayout;

    TextView home,service,terms,feedback,aboutus;

    ImageView drawer;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (RelativeLayout) findViewById(R.id.left_drawer);

        drawer = (ImageView)findViewById(R.id.imageView);
        drawer.setOnClickListener(this);

        home = (TextView) findViewById(R.id.txt_home);
        home.setOnClickListener(this);

        service = (TextView) findViewById(R.id.txt_service);
        service.setOnClickListener(this);

        terms = (TextView) findViewById(R.id.txt_terms);
        terms.setOnClickListener(this);

        feedback = (TextView) findViewById(R.id.txt_feedback);
        feedback.setOnClickListener(this);

        aboutus = (TextView) findViewById(R.id.txt_aboutus);
        aboutus.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.imageView:
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {

            mDrawerLayout.openDrawer(mDrawerList);
        }
         break;

            case R.id.txt_home:
                Toast.makeText(this,"Home",Toast.LENGTH_LONG).show();

                break;

            case R.id.txt_service:
                Toast.makeText(this,"Services",Toast.LENGTH_LONG).show();

                break;

            case R.id.txt_terms:
                Toast.makeText(this,"Terms & Conditions",Toast.LENGTH_LONG).show();

                break;

            case R.id.txt_feedback:
                Toast.makeText(this,"Feedback",Toast.LENGTH_LONG).show();

                break;

            case R.id.txt_aboutus:
                Toast.makeText(this,"About us",Toast.LENGTH_LONG).show();

                break;
      }
    }
}

Swipable Tab Navigation using Fragments in Android


Screenshots :




activity_main.xml :


<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</android.support.v4.view.ViewPager>


activity_frag_a.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.chantisandroid.exampleapp.FragA"
    android:background="#f4e61c">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment A"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="107dp"
        android:textSize="30dp" />
</RelativeLayout>

activity_frab_b.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.chantisandroid.exampleapp.FragB"
    android:background="#0af9f1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment B"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="107dp"
        android:textSize="30dp" />
</RelativeLayout>


activity_frab_c.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.chantisandroid.exampleapp.FragC"
    android:background="#67e563">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment C"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="107dp"
        android:textSize="30dp" />
</RelativeLayout>


activity_frab_d.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.chantisandroid.exampleapp.FragD"
    android:background="#f09580">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment D"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="107dp"
        android:textSize="30dp" />
</RelativeLayout>


FragA:


package com.chantisandroid.exampleapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class FragA extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.activity_frag_a, container, false);

        return view;
    }
}


FragB:


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class FragB extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.activity_frag_b, container, false);

        return view;
    }
}



FragC:


package com.chantisandroid.exampleapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class FragC extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.activity_frag_c, container, false);

        return view;
    }
}


FragD:


package com.chantisandroid.exampleapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class FragD extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View view = inflater.inflate(R.layout.activity_frag_d, container, false);

        return view;
    }
}



MainActivity.java :


package com.chantisandroid.exampleapp;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {

    private ViewPager viewPager;
    private TabAdapter tabAdapter;
    private android.support.v7.app.ActionBar actionBar;

    private String[] tabs = { "FragA", "FragB", "FragC" ,"FragD"};

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

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getSupportActionBar();
        tabAdapter = new TabAdapter(getSupportFragmentManager());

        viewPager.setAdapter(tabAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);

        for (String tab_name : tabs)
        {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int position)
            {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
            }
        });
    }

    public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0, android.support.v4.app.FragmentTransaction arg1) {

    }

    public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction arg1) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0, android.support.v4.app.FragmentTransaction arg1) {

    }

}


TabAdapter.java :


package com.chantisandroid.exampleapp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabAdapter extends FragmentPagerAdapter
{

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

    @Override
    public Fragment getItem(int index)
    {

        switch (index)
        {
            case 0:

                return new FragA();
            case 1:

                return new FragB();
            case 2:

                return new FragC();
            case 3:

                return new FragD();
        }

        return null;
    }

    @Override
    public int getCount()
    {
        return 4;
    }

Alarm Manager Example in Android