; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

CalendarView like Google Calendar in Android


I found a great library to add events to our own CalendarView.

Credits : 
github : https://github.com/SundeepK/CompactCalendarView

Step: 1
======
Add Dependency to build.gradle file


dependencies {

compile 'com.github.sundeepk:compact-calendar-view:1.8.3'
}

Step: 2
======

Create an XML Layout like below with CompactCalendarView.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

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


<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:id="@+id/compactcalendar_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:compactCalendarBackgroundColor="#00bcd4"
app:compactCalendarCurrentDayBackgroundColor="#1a8cd7"
app:compactCalendarCurrentSelectedDayBackgroundColor="#E57373"
app:compactCalendarTextColor="#FFF"
app:compactCalendarTextSize="12sp" />

</LinearLayout>

Step: 3
======
Create an Activity


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.pratap.calendarview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.CalendarDayEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
CompactCalendarView compactCalendarView;

private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("MMMM- yyyy", Locale.getDefault());

private Calendar currentCalender = Calendar.getInstance(Locale.getDefault());

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


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

final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24px);
actionBar.setDisplayHomeAsUpEnabled(true);
// Setting default toolbar title to empty
actionBar.setTitle(null);


compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
compactCalendarView.drawSmallIndicatorForEvents(true);
compactCalendarView.setUseThreeLetterAbbreviation(true);

//set initial title
actionBar.setTitle(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth()));

//set title on calendar scroll
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override
public void onDayClick(Date dateClicked) {


Toast.makeText(MainActivity.this, "Date : " + dateClicked.toString(), Toast.LENGTH_SHORT).show();

}

@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
// Changes toolbar title on monthChange
actionBar.setTitle(dateFormatForMonth.format(firstDayOfNewMonth));

}

});


addDummyEvents();

// gotoToday();


}

// Adding dummy events in calendar view for April, may, june 2016
private void addDummyEvents() {

addEvents(compactCalendarView, Calendar.APRIL);
addEvents(compactCalendarView, Calendar.MAY);
addEvents(compactCalendarView, Calendar.JUNE);

// Refresh calendar to update events
compactCalendarView.invalidate();
}


// Adding events from 1 to 6 days

private void addEvents(CompactCalendarView compactCalendarView, int month) {
currentCalender.setTime(new Date());
currentCalender.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = currentCalender.getTime();
for (int i = 0; i < 6; i++) {
currentCalender.setTime(firstDayOfMonth);
if (month > -1) {
currentCalender.set(Calendar.MONTH, month);
}
currentCalender.add(Calendar.DATE, i);
setToMidnight(currentCalender);
compactCalendarView.addEvent(new CalendarDayEvent(currentCalender.getTimeInMillis(), Color.argb(255, 255, 255, 255)), false);
}
}


private void setToMidnight(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}


public void gotoToday() {

// Set any date to navigate to particular date
compactCalendarView.setCurrentDate(Calendar.getInstance(Locale.getDefault()).getTime());


}
}

Screenshot
========




Source Code 
==========

Dropbox Link


Demo
====





Create PieChart using MPAndroidChart Library




Step: 1
======
Add Dependency to your build.gradle file in android studio

Credits to Author : PhilJay
GithubLink : https://github.com/PhilJay/MPAndroidChart


1
2
3
4
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.2.2'

}



Step: 2
======
create an XML layout with PieChart like below

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="5dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center_horizontal"
android:text="February, 2016"/>
</LinearLayout>



Step: 3
======
Create an Activity like below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.pratap.piechart;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


PieChart mChart;
// we're going to display pie chart for school attendance
private int[] yValues = {21, 2, 2};
private String[] xValues = {"Present Days", "Absents", "Leaves"};

// colors for different sections in pieChart
public static final int[] MY_COLORS = {
Color.rgb(84,124,101), Color.rgb(64,64,64), Color.rgb(153,19,0),
Color.rgb(38,40,53), Color.rgb(215,60,55)
};

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

mChart = (PieChart) findViewById(R.id.chart1);

// mChart.setUsePercentValues(true);
mChart.setDescription("");

mChart.setRotationEnabled(true);

mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;

Toast.makeText(MainActivity.this,
xValues[e.getXIndex()] + " is " + e.getVal() + "", Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected() {

}
});

// setting sample Data for Pie Chart
setDataForPieChart();


}


public void setDataForPieChart() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();

for (int i = 0; i < yValues.length; i++)
yVals1.add(new Entry(yValues[i], i));

ArrayList<String> xVals = new ArrayList<String>();

for (int i = 0; i < xValues.length; i++)
xVals.add(xValues[i]);

// create pieDataSet
PieDataSet dataSet = new PieDataSet(yVals1, "");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);

// adding colors
ArrayList<Integer> colors = new ArrayList<Integer>();

// Added My Own colors
for (int c : MY_COLORS)
colors.add(c);


dataSet.setColors(colors);

// create pie data object and set xValues and yValues and set it to the pieChart
PieData data = new PieData(xVals, dataSet);
// data.setValueFormatter(new DefaultValueFormatter());
// data.setValueFormatter(new PercentFormatter());

data.setValueFormatter(new MyValueFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);

mChart.setData(data);

// undo all highlights
mChart.highlightValues(null);

// refresh/update pie chart
mChart.invalidate();

// animate piechart
mChart.animateXY(1400, 1400);


// Legends to show on bottom of the graph
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
}


public class MyValueFormatter implements ValueFormatter {

private DecimalFormat mFormat;

public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0"); // use one decimal if needed
}

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// write your logic here
return mFormat.format(value) + ""; // e.g. append a dollar-sign
}
}





}

Screenshot
=========


























Demo
=====




BottomBar in android


I have created BottomBar example using this beautiful library.
https://github.com/roughike/BottomBar

Credits : Iiro Krankka


Step: 1
======
Add this library to your gradle file


1
2
3
dependencies {
compile 'com.roughike:bottom-bar:1.2.4'
}

Step: 2
======
Now create menu folder under res folder.

res/menu/bottombar_menu.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_white_24dp"
android:title="Home" />

<item
android:id="@+id/nav_fav"
android:icon="@drawable/ic_favorite_white_24dp"
android:title="Favourites" />

<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_dashboard"
android:title="Gallery" />

<item
android:id="@+id/nav_events"
android:icon="@drawable/ic_event_note_black_24dp"
android:title="Events" />
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="Alerts" />


</menu>


Step: 3
======

Now create an xml layout for the activity.

activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<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/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

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

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


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

<TextView
android:id="@+id/tvLabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text=""
android:textSize="20sp" />


</LinearLayout>
</LinearLayout>


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



Step: 4
======
Create an activity and add the bottom bar from menu items


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.pratap.bottombar;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarBadge;
import com.roughike.bottombar.OnMenuTabClickListener;

public class MainActivity extends AppCompatActivity {
private BottomBar mBottomBar;

private TextView tvLabel;

private Toolbar toolbar;

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

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


tvLabel = (TextView) findViewById(R.id.tvLabel);

mBottomBar = BottomBar.attach(this, savedInstanceState);
mBottomBar.noNavBarGoodness();

mBottomBar.setItemsFromMenu(R.menu.bottombar_menu, new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(@IdRes int menuItemId) {

if (menuItemId == R.id.nav_home) {

updateTextView("Home");

} else if (menuItemId == R.id.nav_fav) {
updateTextView("Favourites");

} else if (menuItemId == R.id.nav_gallery) {
updateTextView("Gallery");

} else if (menuItemId == R.id.nav_events) {
updateTextView("Events");

} else if (menuItemId == R.id.nav_notifications) {
updateTextView("Alerts");


}
}

@Override
public void onMenuTabReSelected(@IdRes int menuItemId) {
if (menuItemId == R.id.nav_home) {
// The user reselected item number one, scroll your content to top.

}
}
});


// Setting colors for different tabs when there's more than three of them.
// You can set colors for tabs in three different ways as shown below.
mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorPrimaryDark));
mBottomBar.mapColorForTab(1, ContextCompat.getColor(this, R.color.colorPrimaryDark));
mBottomBar.mapColorForTab(2, ContextCompat.getColor(this, R.color.colorPrimaryDark));
mBottomBar.mapColorForTab(3, ContextCompat.getColor(this, R.color.colorPrimaryDark));
mBottomBar.mapColorForTab(4, ContextCompat.getColor(this, R.color.colorPrimaryDark));


// Set the color for the active tab. Ignored on mobile when there are more than three tabs.
// mBottomBar.setActiveTabColor("#009688");


// mBottomBar.selectTabAtPosition(1, true);

setNotificationBadge();


}


private void setNotificationBadge() {

// Make a Badge for the first tab, with red background color and a value of "13".
BottomBarBadge unreadMessages = mBottomBar.makeBadgeForTabAt(4, "#FF0000", 10);

// Control the badge's visibility
unreadMessages.show();
// unreadMessages.hide();

// Change the displayed count for this badge.
unreadMessages.setCount(4);

// Change the show / hide animation duration.
unreadMessages.setAnimationDuration(200);

// If you want the badge be shown always after unselecting the tab that contains it.
unreadMessages.setAutoShowAfterUnSelection(false);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
mBottomBar.onSaveInstanceState(outState);
}


public void updateTextView(String text) {


tvLabel.setText(text);
}


}


Screenshots
=========
                                          





Source Code
=========
BottomBar

BottomBarWithFragments


Demo
=====







Horizontal RecyclerView in Vertical RecyclerView like Google Play Store



Step: 1
======
Create two Model Classes like below.

SingleItemModel.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.pratap.gplaystore.models;

/**
* Created by pratap.kesaboyina on 01-12-2015.
*/
public class SingleItemModel {


private String name;
private String url;
private String description;


public SingleItemModel() {
}

public SingleItemModel(String name, String url) {
this.name = name;
this.url = url;
}


public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getName() {
return name;
}

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

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}


}

SectionDataModel.java



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.pratap.gplaystore.models;

import java.util.ArrayList;

/**
* Created by pratap.kesaboyina on 30-11-2015.
*/
public class SectionDataModel {



private String headerTitle;
private ArrayList<SingleItemModel> allItemsInSection;


public SectionDataModel() {

}
public SectionDataModel(String headerTitle, ArrayList<SingleItemModel> allItemsInSection) {
this.headerTitle = headerTitle;
this.allItemsInSection = allItemsInSection;
}



public String getHeaderTitle() {
return headerTitle;
}

public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}

public ArrayList<SingleItemModel> getAllItemsInSection() {
return allItemsInSection;
}

public void setAllItemsInSection(ArrayList<SingleItemModel> allItemsInSection) {
this.allItemsInSection = allItemsInSection;
}


}


Step: 2
======
Create an Activity with RecyclerView to show the list in vertical order.


MainActivity.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.pratap.gplaystore;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.pratap.gplaystore.adapters.RecyclerViewDataAdapter;
import com.pratap.gplaystore.models.SectionDataModel;
import com.pratap.gplaystore.models.SingleItemModel;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;


ArrayList<SectionDataModel> allSampleData;


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

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

allSampleData = new ArrayList<SectionDataModel>();

if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setTitle("G PlayStore");

}


createDummyData();


RecyclerView my_recycler_view = (RecyclerView) findViewById(R.id.my_recycler_view);

my_recycler_view.setHasFixedSize(true);

RecyclerViewDataAdapter adapter = new RecyclerViewDataAdapter(this, allSampleData);

my_recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

my_recycler_view.setAdapter(adapter);


}

public void createDummyData() {
for (int i = 1; i <= 5; i++) {

SectionDataModel dm = new SectionDataModel();

dm.setHeaderTitle("Section " + i);

ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 5; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}

dm.setAllItemsInSection(singleItem);

allSampleData.add(dm);

}
}
}

Create an XML Layout for the above activity class

activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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


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


</LinearLayout>

Step: 3
======
Now Create an Adapter Class for the recyclerView in the MainActivity.

RecyclerViewDataAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.pratap.gplaystore.adapters;

/**
* Created by pratap.kesaboyina on 24-12-2014.
*/
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.pratap.gplaystore.R;
import com.pratap.gplaystore.models.SectionDataModel;

import java.util.ArrayList;

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {

private ArrayList<SectionDataModel> dataList;
private Context mContext;

public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
this.dataList = dataList;
this.mContext = context;
}

@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder mh = new ItemRowHolder(v);
return mh;
}

@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {

final String sectionName = dataList.get(i).getHeaderTitle();

ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();

itemRowHolder.itemTitle.setText(sectionName);

SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);

itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);


itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


Toast.makeText(v.getContext(), "click event on more, "+sectionName , Toast.LENGTH_SHORT).show();



}
});


/* Glide.with(mContext)
.load(feedItem.getImageURL())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.error(R.drawable.bg)
.into(feedListRowHolder.thumbView);*/
}

@Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}

public class ItemRowHolder extends RecyclerView.ViewHolder {

protected TextView itemTitle;

protected RecyclerView recycler_view_list;

protected Button btnMore;



public ItemRowHolder(View view) {
super(view);

this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
this.btnMore= (Button) view.findViewById(R.id.btnMore);


}

}

}


Now create an xml Layout file for the above adapter class.

list_item.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:background="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp">


<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="@+id/btnMore"
android:text="Sample title"
android:textColor="@android:color/black"
android:textSize="18sp" />

<Button
android:id="@+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:theme="@style/MyButton"
android:text="more"
android:textColor="#FFF" />


</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />


</LinearLayout>


Step: 4
======

Now , In order to make a horizontal RecyclerView , we need create a layout and Adapter class for each row.

list_single_card.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="?android:selectableItemBackground"
android:orientation="vertical">

<ImageView
android:id="@+id/itemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="@drawable/android" />


<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemImage"
android:gravity="center"
android:padding="5dp"
android:text="Sample title"
android:textColor="@android:color/black"
android:textSize="18sp" />


</LinearLayout>

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

The Adapter Class for Horizontal RecyclerView

SectionListDataAdapter.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.pratap.gplaystore.adapters;

/**
* Created by pratap.kesaboyina on 24-12-2014.
*/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pratap.gplaystore.R;
import com.pratap.gplaystore.models.SingleItemModel;

import java.util.ArrayList;

public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {

private ArrayList<SingleItemModel> itemsList;
private Context mContext;

public SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

SingleItemModel singleItem = itemsList.get(i);

holder.tvTitle.setText(singleItem.getName());


/* Glide.with(mContext)
.load(feedItem.getImageURL())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.error(R.drawable.bg)
.into(feedListRowHolder.thumbView);*/
}

@Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

protected TextView tvTitle;

protected ImageView itemImage;


public SingleItemRowHolder(View view) {
super(view);

this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);


view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();

}
});


}

}

}

ScreenShots
=========

 




Source code
========
DropBox Link




Demo
========








UPTET news