Android App Development With AndroidCource: July 2018

Thursday, 12 July 2018

Gridview in Android Example

What is GridView ?


GridView layout is a use to show data in grid layout.GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.In this layout is show row and column item in gridview.Gridview is use to BaseAdapter.GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. GridView is mainly useful when we want show data in grid layout like displaying images or icons.This layout can be used to build applications like image viewer, audio or video players in order to show elements in grid view.


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

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" >

    </GridView>

</RelativeLayout>

What is BaseAdapter ?


Base Adapter. Adapter is a bridge between UI and data source, It helps us to fill data in the UI components.Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface) and Spinner (by implementing the specialized SpinnerAdapter interface).


MyAdapter.java


package com.example.bhaumik.gridviewdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MyAdapter extends BaseAdapter{

    Context mContext;

    int img[] = {R.drawable.img_1,R.drawable.img_2,R.drawable.img_3,R.drawable.img_4,R.drawable.img_5,R.drawable.img_6,R.drawable.img_7};

    public MyAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return img.length;
    }

    @Override
    public Object getItem(int i) {
        return img[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(img[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        return imageView;
    }
}

MainActivity.java


package com.example.bhaumik.gridviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {

    GridView gridView;

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

        gridView = findViewById(R.id.grid_view);

        gridView.setAdapter(new MyAdapter(MainActivity.this));

    }
}


Saturday, 7 July 2018

Expandable Listview in Android Example

Android, ExpandableListView is a View that shows items in a vertically scrolling two level list. Different from the listview by allowing two level groups which can individually be expanded to show.Expandable listview is a parent and childview to expand to click parent view to display childview in android.

MainActivity.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bhaumik.expandablelistwithactivity.MainActivity"
    android:background="@color/MainActivityBackground">



    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expand_view" />

</RelativeLayout>
Create XML File to Parent Item Display in Layout

PrentItem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/parent_item_bg">

    <TextView
        android:id="@+id/tv_parent_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/WhiteColor"
        android:text="Parent Item"
        android:textSize="20dp"
        android:padding="5dp"
        android:layout_marginLeft="20dp"/>
</LinearLayout>
Create XML Layout file to Child Item in Listview

ChildItem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/child_item_bg">
    <TextView
        android:id="@+id/tv_child_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/BlackColor"
        android:text="Child Item"
        android:textSize="20dp"
        android:padding="8dp"
        android:layout_marginLeft="80dp"/>
</LinearLayout>

MainActivity.java

package com.bhaumik.expandablelistwithactivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.transition.Explode;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.jar.Manifest;

public class MainActivity extends FragmentActivity {

    ExpandableListView expandableListView;

    List<String> main_title = new ArrayList<>();
    List<String> android_list = new ArrayList<>();
    List<String> java_list = new ArrayList<>();
    List<String> php_list = new ArrayList<>();
    HashMap<String,List<String>> child_list = new HashMap<String,List<String>>();

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

        expandableListView = (ExpandableListView) findViewById(R.id.expand_view);

        String[] cource_title = getResources().getStringArray(R.array.cource_title);
        String[] android_items = getResources().getStringArray(R.array.android_item);
        String[] java_items = getResources().getStringArray(R.array.java_item);
        String[] php_items = getResources().getStringArray(R.array.php_item);

        for(String heading : cource_title)
        {
            main_title.add(heading);
        }
        for (String android : android_items)
        {
            android_list.add(android);
        }
        for (String java : java_items)
        {
            java_list.add(java);
        }
        for (String php : php_items)
        {
            php_list.add(php);
        }

        child_list.put(main_title.get(0),android_list);
        child_list.put(main_title.get(1),java_list);
        child_list.put(main_title.get(2),php_list);

        final MyAdapter myAdapter = new MyAdapter(MainActivity.this,main_title,child_list);
        expandableListView.setAdapter(myAdapter);

    }
}
Create Adapter to set Parent And Child item Data in listview.

MyAdapter.java

package com.bhaumik.expandablelistwithactivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Bhaumik on 3/23/2017.
 */

public class MyAdapter extends BaseExpandableListAdapter{

    Context context;
    List<String> cource_title;
    HashMap<String,List<String>> child_title;

    public MyAdapter(Context context, List<String> cource_title, HashMap<String, List<String>> child_title) {
        this.context = context;
        this.cource_title = cource_title;
        this.child_title = child_title;
    }

    @Override
    public int getGroupCount() {
        return cource_title.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child_title.get(cource_title.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return cource_title.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child_title.get(cource_title.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        String titles = (String) this.getGroup(groupPosition);

        if(convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.parent_item,null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.tv_parent_item);
        textView.setText(titles);

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        String titles = (String) this.getChild(groupPosition,childPosition);

        if(convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.child_item,null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.tv_child_item);
        textView.setText(titles);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,DetailActivity.class);
                intent.putExtra("text",child_title.get(cource_title.get(groupPosition)).get(childPosition));
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
expandable listview in android expandable listview in android

Recyclerview with Searchview Actionbar in Android Example

Recyclerview with searchview in actionbar to add searchview to you can search in item from recyclerview list.it is use to two methods are override to onQueryTextSubmit And onQueryTextChange

onQueryTextSubmit

In this method to use of you can input searchview and click to search item when you can searchable word.but it can first input type to search item and then click button after searchable item show.

onQueryTextChange

In this method to use of at a time to input word to search result show on at a time. ane refresh adapter to at a time and display search item in this method.

Add Depedancy AndroidManifest.xml File

    implementation 'com.android.support:recyclerview-v7:28.0.0-alpha3'

Add Menu Resource File Use to Searchview main_menu.xml

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


    <item
        android:id="@+id/app_bar_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="Search"
        app:showAsAction="always |collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

Add Custom Layout File to Use Set Adapter in Recyclerview item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/img_view"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:textSize="20dp"
            android:id="@+id/text_view"/>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

</LinearLayout>

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"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view"/>

</LinearLayout>

Create Getter Setter Method Class to use this set and get data in list item. FruitsData.java

package com.example.bhaumik.recyclerviewdemo;

public class FruitsData {

    public String name;
    public int image;

    public FruitsData(String name, int image) {
        this.name = name;
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}

MainActivity.java

package com.example.bhaumik.recyclerviewdemo;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    MyAdapter myAdapter;
    List<FruitsData> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list.add(new FruitsData("Apple",R.drawable.apple));
        list.add(new FruitsData("Banana",R.drawable.banana));
        list.add(new FruitsData("Grapes",R.drawable.grapes));
        list.add(new FruitsData("JackFruit",R.drawable.jack_fruit));
        list.add(new FruitsData("Lemon",R.drawable.lemon));
        list.add(new FruitsData("Mango",R.drawable.mango));
        list.add(new FruitsData("Orange",R.drawable.orange));
        list.add(new FruitsData("Papaya",R.drawable.papaya));
        list.add(new FruitsData("pear",R.drawable.pear));
        list.add(new FruitsData("Pineapple",R.drawable.pine_apple));

        recyclerView = findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        myAdapter = new MyAdapter(MainActivity.this,list);
        recyclerView.setAdapter(myAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_menu,menu);
        MenuItem menuItem = menu.findItem(R.id.app_bar_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {


                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                if (myAdapter != null){
                    myAdapter.getFilter().filter(newText);
                }

                return true;
            }
        });
        return true;
    }

}

Create Adapter to Set Data in recyclerview MyAdapter.java

package com.example.bhaumik.recyclerviewdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>  implements Filterable {

    Context context;
    List<FruitsData> list = new ArrayList<>();
    List<FruitsData> list1 = new ArrayList<>();

    public MyAdapter(Context context, List<FruitsData> list) {
        this.context = context;
        this.list = list;
        this.list1 = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_list,viewGroup,false);

        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {

        myHolder.imageView.setImageResource(list.get(i).getImage());
        myHolder.textView.setText(list.get(i).getName());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.img_view);
            textView = itemView.findViewById(R.id.text_view);

        }
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                String charString = constraint.toString();

                if (charString.isEmpty()){
                    list = list1;
                }else{

                    List<FruitsData> filterList = new ArrayList<>();

                    for (FruitsData data : list1){

                        if (data.getName().toLowerCase().contains(charString)){
                            filterList.add(data);
                        }
                    }

                    list = filterList;

                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = list;

                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                list = (List<FruitsData>) results.values;
                notifyDataSetChanged();
            }
        };

    }
}
recyclerview in androidrecyclerview searchview in android