Android AsyncTask

MyTask.java

public class MyTask extends AsyncTask<Void,Integer,String> {

    private Context context;
    private Button buttonDownload;
    private TextView textViewDownload;

    ProgressDialog progressDialog;

    public void setButtonDownload(Button button)
    {
        this.buttonDownload=button;
    }

    public void setTextViewDownload(TextView textView)
    {
        this.textViewDownload=textView;
    }

    public MyTask(Context context)
    {
        this.context=context;
    }

    @Override
    protected String doInBackground(Void... params) {

        int i=0;

        synchronized (this)
        {
            while (i<101)
            {
                try {
                    wait(100);
                    i++;
                    this.publishProgress(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


        return "Download Completed.";
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        buttonDownload.setEnabled(false);
        textViewDownload.setText("Downloading...");

        progressDialog=new ProgressDialog(context);
        progressDialog.setTitle("Downloading ....");
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        textViewDownload.setText(s);
        buttonDownload.setEnabled(true);
        progressDialog.hide();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        Integer progress=values[0];
        progressDialog.setProgress(progress);
    }

    @Override
    protected void onCancelled(String s) {
        super.onCancelled(s);

        if (s.isEmpty())
        {
            textViewDownload.setText("Download Canceled.");
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textViewDownload;
    Button buttonDownload;

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

        textViewDownload= (TextView) findViewById(R.id.textViewDownload);
        buttonDownload= (Button) findViewById(R.id.buttonDownload);

        buttonDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyTask myTask=new MyTask(MainActivity.this);
                myTask.setButtonDownload(buttonDownload);
                myTask.setTextViewDownload(textViewDownload);
                myTask.execute();
            }
        });
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/050

Android Working with ScrollView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="iterator.ir.a049.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Text 1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Text 2"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Text 3"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"/>

    </LinearLayout>

</ScrollView>

References
https://github.com/mhdr/AndroidSamples/tree/master/049

Android Navigating between Menu Items of Navigation Drawer

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="iterator.ir.a048.MainActivity"
    android:id="@+id/drawerLayout">

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

        <include
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


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

        </FrameLayout>
    </LinearLayout>

    <!--Drawer-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu">

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

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;

    FragmentTransaction fragmentTransaction;
    NavigationView navigationView;
    DrawerLayout drawerLayout;

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

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // load at application start up
        fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container,new HomeFragment());
        fragmentTransaction.commit();

        getSupportActionBar().setTitle("Home");

        navigationView= (NavigationView) findViewById(R.id.navigationView);
        drawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout);

        // handle navigation menu item click event
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId())
                {
                    case R.id.itemHome:
                        fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,new HomeFragment());
                        fragmentTransaction.commit();

                        item.setChecked(true);
                        drawerLayout.closeDrawers();

                        return true;

                    case R.id.itemAdd:
                        fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,new AddFragment());
                        fragmentTransaction.commit();

                        item.setChecked(true);
                        drawerLayout.closeDrawers();

                        return true;
                }

                return false;
            }
        });
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/048
https://www.youtube.com/watch?v=o4gF75f4BoI&index=78&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk

Android Add Hamburger icon for Navigation Drawer

MainActivity.java

public class MainActivity extends AppCompatActivity {


    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ActionBarDrawerToggle actionBarDrawerToggle;

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

        drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout, R.string.drawer_open, R.string.drawer_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/047
https://www.youtube.com/watch?v=dvYGgSxiR1c&index=77&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk
https://developer.android.com/training/implementing-navigation/nav-drawer.html
http://codetheory.in/android-navigation-drawer/

Android Add Header for Navigation Drawer

/res/layout/navigation_drawer_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:background="@android:color/holo_green_light">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="70dp"
        android:paddingBottom="70dp"
        android:text="Header"
        android:gravity="center"/>

</RelativeLayout>

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="iterator.ir.a046.MainActivity">

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

        <include
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!--Drawer-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/drawer_menu">

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

</android.support.v4.widget.DrawerLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/046
https://www.youtube.com/watch?v=dvYGgSxiR1c&t=58s&index=77&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk

Android Create Navigation Drawer using NavigationView

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'
}

/res/values/styles.xml

<resources>

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

</resources>

/res/layout/toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?android:attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

</android.support.v7.widget.Toolbar>

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="iterator.ir.a045.MainActivity">

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

        <include
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!--Drawer-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu">

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

</android.support.v4.widget.DrawerLayout>

/res/menu/drawer_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">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/itemHome"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />

        <item
            android:id="@+id/itemAdd"
            android:icon="@drawable/ic_alarm_add_black_24dp"
            android:title="Add" />
    </group>


    <item android:title="Other">
        <menu>
            <item
                android:id="@+id/itemSettings"
                android:icon="@drawable/ic_settings_black_24dp"
                android:title="Settings" />

            <item
                android:id="@+id/itemShare"
                android:icon="@drawable/ic_share_black_24dp"
                android:title="Share" />
        </menu>
    </item>
</menu>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;

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

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/045
https://www.youtube.com/watch?v=mtOztjHmM0c&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=76
https://developer.android.com/training/implementing-navigation/nav-drawer.html

Android Send data to a Fragment using Bundle

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        Fragment fragment=new PageFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("pageNumber",10);
        fragment.setArguments(bundle);

        FragmentManager fragmentManager=getSupportFragmentManager();

        FragmentTransaction transaction= fragmentManager.beginTransaction();

        transaction.add(R.id.relativeLayoutFragmentContainer,fragment);

        transaction.commit();
    }

}

PageFragment.java

public class PageFragment extends Fragment {


    public PageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

        TextView textViewPage= (TextView) view.findViewById(R.id.textViewPage);
        Bundle bundle=getArguments();
        int pageNumber= bundle.getInt("pageNumber");

        String output=String.format("Page %d",pageNumber);
        textViewPage.setText(output);

        return view;
    }

}

References
https://github.com/mhdr/AndroidSamples/tree/master/044

Android Implement Swipe Views

To insert child views that represent each page, you need to hook this layout to a PagerAdapter. There are two kinds of adapter you can use:

FragmentPagerAdapter
This is best when navigating between sibling screens representing a fixed, small number of pages.
FragmentStatePagerAdapter
This is best for paging across a collection of objects for which the number of pages is undetermined. It destroys fragments as the user navigates to other pages, minimizing memory usage.

activity_main.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="iterator.ir.a043.MainActivity">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/viewPager">

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

</android.support.constraint.ConstraintLayout>

fragment_page.xml

<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"
    tools:context="iterator.ir.a043.PageFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textViewCounter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Fragment"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />
    </RelativeLayout>
</FrameLayout>

PageFragment.java

public class PageFragment extends Fragment {


    public PageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

        TextView textViewCounter= (TextView) view.findViewById(R.id.textViewCounter);

        Bundle bundle=getArguments();

        int counter=bundle.getInt("counter");
        String output=String.format("Page %d",counter);

        textViewCounter.setText(output);

        return view;
    }

}

SwipeAdapter.java

public class SwipeAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=new PageFragment();

        Bundle bundle=new Bundle();
        bundle.putInt("counter",position+1);

        fragment.setArguments(bundle);

        return fragment;
    }

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;

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

        viewPager= (ViewPager) findViewById(R.id.viewPager);

        SwipeAdapter adapter=new SwipeAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/043
https://developer.android.com/training/implementing-navigation/lateral.html

Android Working with Expandable List View

activity_main.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="iterator.ir.a042.MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListViewProvinces"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

HeaderInfo.java

public class HeaderInfo {

    public String getProvoniceName() {
        return provoniceName;
    }

    public void setProvoniceName(String provoniceName) {
        this.provoniceName = provoniceName;
    }

    private String provoniceName;

    public int getPopulation() {
        return population;
    }

    private int population;

    public ArrayList<DetailInfo> getCities() {
        return cities;
    }

    public void setCities(ArrayList<DetailInfo> cities) {
        this.cities = cities;

        int pop=0;

        for (DetailInfo info:cities)
        {
            pop+=info.getPopulation();
        }

        this.population=pop;
    }

    private ArrayList<DetailInfo> cities=new ArrayList<>();

}

DetailInfo.java

public class DetailInfo {

    private String cityName;

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    private int population;
}

CustomAdapter.java

public class CustomAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<HeaderInfo> cInfo;

    public CustomAdapter(Context context,ArrayList<HeaderInfo> cInfo)
    {
        this.context=context;
        this.cInfo=cInfo;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return cInfo.get(groupPosition).getCities().size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return cInfo.get(groupPosition).getCities().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 true;
    }

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

        View view=convertView;

        if (view==null)
        {
            LayoutInflater layoutInflater=LayoutInflater.from(context);
            view=layoutInflater.inflate(R.layout.header_layout,parent,false);
        }

        HeaderInfo current=this.cInfo.get(groupPosition);

        if (current!=null)
        {
            TextView textViewProvinceName= (TextView) view.findViewById(R.id.textViewProvinceName);
            TextView textViewProvincePopulation= (TextView) view.findViewById(R.id.textViewProvincePopulation);

            textViewProvinceName.setText(current.getProvoniceName());
            textViewProvincePopulation.setText(String.valueOf(current.getPopulation()));
        }

        return view;
    }

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

        if (view==null)
        {
            LayoutInflater layoutInflater=LayoutInflater.from(context);
            view=layoutInflater.inflate(R.layout.detail_layout,parent,false);
        }

        DetailInfo current=this.cInfo.get(groupPosition).getCities().get(childPosition);

        if (current!=null)
        {
            TextView textViewCityName= (TextView) view.findViewById(R.id.textViewCityName);
            TextView textViewCityPopulation= (TextView) view.findViewById(R.id.textViewCityPopulation);

            textViewCityName.setText(current.getCityName());
            textViewCityPopulation.setText(String.valueOf(current.getPopulation()));
        }

        return view;
    }

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListViewProvinces;
    ArrayList<HeaderInfo> provonices;

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

        GenerateData();

        expandableListViewProvinces= (ExpandableListView) findViewById(R.id.expandableListViewProvinces);
        CustomAdapter adapter=new CustomAdapter(this,provonices);
        expandableListViewProvinces.setAdapter(adapter);
    }

    private void GenerateData()
    {
        provonices=new ArrayList<>();


        DetailInfo detailInfo1=new DetailInfo();
        detailInfo1.setCityName("Rasht");
        detailInfo1.setPopulation(1000000);


        DetailInfo detailInfo2=new DetailInfo();
        detailInfo2.setCityName("Lahijan");
        detailInfo2.setPopulation(700000);

        DetailInfo detailInfo3=new DetailInfo();
        detailInfo3.setCityName("Anzali");
        detailInfo3.setPopulation(800000);

        DetailInfo detailInfo4=new DetailInfo();
        detailInfo4.setCityName("Fouman");
        detailInfo4.setPopulation(200000);

        ArrayList<DetailInfo> cities1=new ArrayList<>();
        cities1.add(detailInfo1);
        cities1.add(detailInfo2);
        cities1.add(detailInfo3);
        cities1.add(detailInfo4);

        HeaderInfo headerInfo1=new HeaderInfo();
        headerInfo1.setProvoniceName("Guilan");
        headerInfo1.setCities(cities1);

        DetailInfo detailInfo5=new DetailInfo();
        detailInfo5.setCityName("Tehran");
        detailInfo5.setPopulation(10000000);


        DetailInfo detailInfo6=new DetailInfo();
        detailInfo6.setCityName("Hashtgerd");
        detailInfo6.setPopulation(700000);


        ArrayList<DetailInfo> cities2=new ArrayList<>();
        cities2.add(detailInfo5);
        cities2.add(detailInfo6);


        HeaderInfo headerInfo2=new HeaderInfo();
        headerInfo2.setProvoniceName("Tehran");
        headerInfo2.setCities(cities2);


        DetailInfo detailInfo7=new DetailInfo();
        detailInfo7.setCityName("Qazvin");
        detailInfo7.setPopulation(1000000);


        ArrayList<DetailInfo> cities3=new ArrayList<>();
        cities3.add(detailInfo7);

        HeaderInfo headerInfo3=new HeaderInfo();
        headerInfo3.setProvoniceName("Qazvin");
        headerInfo3.setCities(cities3);

        provonices.add(headerInfo1);
        provonices.add(headerInfo2);
        provonices.add(headerInfo3);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/042

Android Custom Background for Action Bar

/res/drawable/gradient_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:startColor="#000000"
        android:endColor="#FFFFFF"/>
</shape>

/res/values/styles.xml

<resources>
    <style name="custom_theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/custom_color</item>
        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/custom_color</item>
    </style>

    <style name="custom_color" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/gradient_color</item>
        <!-- Support library compatibility -->
        <item name="background">@drawable/gradient_color</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="iterator.ir.a041">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/custom_theme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

References
https://github.com/mhdr/AndroidSamples/tree/master/041