Hi everyone there, this is my first attempt to write a blog, bare with me for any mistakes. A few days back I had this requirement of creating a Grid view which will be able to slide and also indicate how many screens the grid view has in total, just like we do in the Menu option of any Android phone. So after lot of research I made a solution with the help of an library which will be helpful to show the number of pages just like we get in menu option of android phone and so thought of putting it up on my blog so that others would be benefited from it.
We need to download and import it in our project work space. I hope you guys wont be having much of trouble doing that. Firstly i would like to show how the output will look like. Please see it in full screen where you will see the total number of dots equal to total number of pages at the bottom.
The next step is start writing our code for the actual project. So lets first start with the XML files required. Total we will need 3 layout files.
1) activity_main.xml : - which holds the viewpager and also the PagerIndicator.
2) grid.xml: - which holds the gridview
3) custom.xml - which holds the gridview custom layout (A gridview with a Image and text in our case).
The activity_main.xml looks like this:
<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:background="@android:color/black"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="400dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/pagerIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:padding="3dp" />
</LinearLayout>
It holds a LinearLayout which will hold our GridView a ViewPager and a PagerIndicator. The grid.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginTop="2dp"
android:horizontalSpacing="2dp"
android:listSelector="@android:color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
</LinearLayout>
It has a GridView with 3 columns. You can specify as much as you want. The last xml file is the custom.xml which holds a ImageView and TextView for every grid item. The custom.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="@android:color/white"
android:textSize="15sp" >
</TextView>
</LinearLayout>
The following are the class files we require. The first file is the MainActivity.class file which looks like below:
As you can see we have to create objects of PagerIndicator, ViewPager & PagerAdapter.
The PagerIndicator to indicate the current page
The ViewPager is just another view like any other view. We will set the PagerAdapter to it.
Another important class is GridFragment class which will add our gridview to our layout. It looks as shown below:
Here we have inflated our grid.xml and set adapter to the gridview. Also to check which item in the gridview was clicked we have used the setOnItemClickListener method. Next is the GridAdapter class to set the adapter to GridView. It looks as shown below:
The next is the Category.class
Firstly I would like you to thank Jake Wharton for his library "Android-ViewPagerIndicator" which can be downloaded from here https://github.com/JakeWharton/Android-ViewPagerIndicator
The next step is start writing our code for the actual project. So lets first start with the XML files required. Total we will need 3 layout files.
1) activity_main.xml : - which holds the viewpager and also the PagerIndicator.
2) grid.xml: - which holds the gridview
3) custom.xml - which holds the gridview custom layout (A gridview with a Image and text in our case).
The activity_main.xml looks like this:
<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:background="@android:color/black"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="400dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/pagerIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:padding="3dp" />
</LinearLayout>
It holds a LinearLayout which will hold our GridView a ViewPager and a PagerIndicator. The grid.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginTop="2dp"
android:horizontalSpacing="2dp"
android:listSelector="@android:color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
</LinearLayout>
It has a GridView with 3 columns. You can specify as much as you want. The last xml file is the custom.xml which holds a ImageView and TextView for every grid item. The custom.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="@android:color/white"
android:textSize="15sp" >
</TextView>
</LinearLayout>
The following are the class files we require. The first file is the MainActivity.class file which looks like below:
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import com.viewpagerindicator.PagerIndicator; public class MainActivity extends FragmentActivity { public PagerIndicator mIndicator; private ViewPager awesomePager; private PagerAdapter pm; ArrayListcodeCategory; String deviceNames[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); awesomePager = (ViewPager) findViewById(R.id.pager); mIndicator = (PagerIndicator) findViewById(R.id.pagerIndicator); ArrayList a = new ArrayList (); Category m = new Category(); for (int i = 0; i < deviceNames.length; i++) { a.add(i, deviceNames[i]); m.name = a.get(i); } codeCategory = new ArrayList (); codeCategory.add(m); Iterator it = a.iterator(); List gridFragments = new ArrayList (); it = a.iterator(); int i = 0; while (it.hasNext()) { ArrayList itmLst = new ArrayList (); GridItems itm = new GridItems(0, it.next()); itmLst.add(itm); i = i + 1; if (it.hasNext()) { GridItems itm1 = new GridItems(1, it.next()); itmLst.add(itm1); i = i + 1; } if (it.hasNext()) { GridItems itm2 = new GridItems(2, it.next()); itmLst.add(itm2); i = i + 1; } if (it.hasNext()) { GridItems itm3 = new GridItems(3, it.next()); itmLst.add(itm3); i = i + 1; } if (it.hasNext()) { GridItems itm4 = new GridItems(4, it.next()); itmLst.add(itm4); i = i + 1; } if (it.hasNext()) { GridItems itm5 = new GridItems(5, it.next()); itmLst.add(itm5); i = i + 1; } if (it.hasNext()) { GridItems itm6 = new GridItems(6, it.next()); itmLst.add(itm6); i = i + 1; } if (it.hasNext()) { GridItems itm7 = new GridItems(7, it.next()); itmLst.add(itm7); i = i + 1; } if (it.hasNext()) { GridItems itm8 = new GridItems(8, it.next()); itmLst.add(itm8); i = i + 1; } GridItems[] gp = {}; GridItems[] gridPage = itmLst.toArray(gp); gridFragments.add(new GridFragment(gridPage, MainActivity.this)); } pm = new PagerAdapter(getSupportFragmentManager(), gridFragments); awesomePager.setAdapter(pm); mIndicator.setViewPager(awesomePager); } private class PagerAdapter extends FragmentStatePagerAdapter { private List fragments; public PagerAdapter(FragmentManager fm, List fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } }
As you can see we have to create objects of PagerIndicator, ViewPager & PagerAdapter.
The PagerIndicator to indicate the current page
The ViewPager is just another view like any other view. We will set the PagerAdapter to it.
Another important class is GridFragment class which will add our gridview to our layout. It looks as shown below:
import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; @SuppressLint("ValidFragment") public class GridFragment extends Fragment { private GridView mGridView; private GridAdapter mGridAdapter; GridItems[] gridItems = {}; private Activity activity; public GridFragment(GridItems[] gridItems, Activity activity) { this.gridItems = gridItems; this.activity = activity; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view; view = inflater.inflate(R.layout.grid, container, false); mGridView = (GridView) view.findViewById(R.id.gridView); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (activity != null) { mGridAdapter = new GridAdapter(activity, gridItems); if (mGridView != null) { mGridView.setAdapter(mGridAdapter); } mGridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { onGridItemClick((GridView) parent, view, position, id); } }); } } public void onGridItemClick(GridView g, View v, int position, long id) { Toast.makeText( activity, "Position Clicked: - " + position + " & " + "Text is: - " + gridItems[position].title, Toast.LENGTH_LONG).show(); Log.e("TAG", "POSITION CLICKED " + position); } }
Here we have inflated our grid.xml and set adapter to the gridview. Also to check which item in the gridview was clicked we have used the setOnItemClickListener method. Next is the GridAdapter class to set the adapter to GridView. It looks as shown below:
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class GridAdapter extends BaseAdapter { Context context; int images[] = { R.drawable.ic_launcher, R.drawable.ios, R.drawable.windows, R.drawable.ic_launcher, R.drawable.ios, R.drawable.windows, R.drawable.ic_launcher, R.drawable.ios, R.drawable.windows }; public class ViewHolder { public ImageView imageView; public TextView textTitle; } private GridItems[] items; private LayoutInflater mInflater; public GridAdapter(Context context, GridItems[] locations) { mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.context = context; items = locations; } public GridItems[] getItems() { return items; } public void setItems(GridItems[] items) { this.items = items; } @Override public int getCount() { if (items != null) { return items.length; } return 0; } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } @Override public Object getItem(int position) { if (items != null && position >= 0 && position < getCount()) { return items[position]; } return null; } @Override public long getItemId(int position) { if (items != null && position >= 0 && position < getCount()) { return items[position].id; } return 0; } public void setItemsList(GridItems[] locations) { this.items = locations; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; ViewHolder viewHolder; if (view == null) { view = mInflater.inflate(R.layout.custom, parent, false); viewHolder = new ViewHolder(); viewHolder.imageView = (ImageView) view .findViewById(R.id.grid_item_image); viewHolder.textTitle = (TextView) view .findViewById(R.id.grid_item_label); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } GridItems gridItems = items[position]; setCatImage(position, viewHolder, gridItems.title); return view; } private void setCatImage(int pos, ViewHolder viewHolder, String catTitle) { viewHolder.imageView.setImageResource(images[pos]); viewHolder.textTitle.setText(catTitle); } }Below are the other GetterSetter classes. First is the GridItems.class
public class GridItems { public int id; public String title; public GridItems(int id, String address) { this.id = id; this.title = address; } }
The next is the Category.class
public class Category { int id; String name; public Category(int id, String name) { this.id = id; this.name = name; } public Category() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Thanks for your blog, I too working on this type page indicator, I have used your codes but I have getting this exception
ReplyDelete11-07 12:29:44.102: E/AndroidRuntime(17301): FATAL EXCEPTION: main
11-07 12:29:44.102: E/AndroidRuntime(17301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exam.gridview/com.exam.gridview.MainActivity}: android.view.InflateException: Binary XML file line #14: Class is not a View com.exam.gridview.PageIndicator
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread.access$600(ActivityThread.java:149)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.os.Looper.loop(Looper.java:153)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-07 12:29:44.102: E/AndroidRuntime(17301): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 12:29:44.102: E/AndroidRuntime(17301): at java.lang.reflect.Method.invoke(Method.java:511)
11-07 12:29:44.102: E/AndroidRuntime(17301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-07 12:29:44.102: E/AndroidRuntime(17301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-07 12:29:44.102: E/AndroidRuntime(17301): at dalvik.system.NativeStart.main(Native Method)
11-07 12:29:44.102: E/AndroidRuntime(17301): Caused by: android.view.InflateException: Binary XML file line #14: Class is not a View com.exam.gridview.PageIndicator
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.createView(LayoutInflater.java:604)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-07 12:29:44.102: E/AndroidRuntime(17301): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.Activity.setContentView(Activity.java:1867)
11-07 12:29:44.102: E/AndroidRuntime(17301): at com.exam.gridview.MainActivity.onCreate(MainActivity.java:29)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.Activity.performCreate(Activity.java:5020)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-07 12:29:44.102: E/AndroidRuntime(17301): ... 11 more
11-07 12:29:44.102: E/AndroidRuntime(17301): Caused by: java.lang.ClassCastException: com.exam.gridview.PageIndicator cannot be cast to android.view.View
11-07 12:29:44.102: E/AndroidRuntime(17301): at java.lang.Class.asSubclass(Class.java:1380)
11-07 12:29:44.102: E/AndroidRuntime(17301): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-07 12:29:44.102: E/AndroidRuntime(17301): ... 22 more
please tell me the solution.
Nagesh can you tell me when do you get this exception???
ReplyDeleteor how can i replicate it because it is running fine with me..
when I am going to launch the application its always crash. I think rather using PageIndicator, I have to use CirclePageIndicator. here log cat saying that "Caused by: java.lang.ClassCastException: com.exam.gridview.PageIndicator cannot be cast to android.view.View".. so I am thinking to use CirclePageIndicator.
DeleteHey Shrikant Sonar if you have overall project bundle please send me....
DeleteHi Nagesh, in order to make the above code executable you need to make a change in activity_main.xml, as from the error log says it cannot find PagerIndicator Class from the library so instead of that class try to use CirclePageIndicator class
ReplyDeleteBasically change your line no 14 of the xml
from com.viewpagerindicator.PagerIndicator to
com.viewpagerindicator.CirclePageIndicator.
Hope this solves your issue.
hello sir,
ReplyDeletei refer ur code its very usable..i am trying to develop a magazine application for that i had use a view pager for sliding the pages..now i wanted to add a sliding pages at the bottom of layout like we were seen in digital magazines...but i couldnt understand how it will be done..can you suggest me a code..please give me a guidance..i am awating..my email id is[rajashri.thorat73@gmail.com]
Thank you
Hi,
ReplyDeleteI am a beginner, and working for my app. This function(Gridview with indicator) is what I want to add into my app, but I'm not be able to reuse your code, coz I don't understand some of the code, I'd greatly appreciate for your help!!
please contact me via caseofr@gmail.com
please.
ReplyDeletehow to add images dynamically as deviceNames
Your code is working fine but, when orientation changes it crashes. As Fragment doesnot contain empty constructor. How to handle it, without placing android:configChanges="orientation|screenSize|keyboardHidden" in Manifest.xml
ReplyDeleteI solved this issue by keeping fragment.setRetainInstance(true);
ReplyDeleteBut I think it will cause memory leak.
DeleteThank you for blog, i am facing this error, please help me out.
ReplyDeletejava.lang.NullPointerException
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.View.measure(View.java:15524)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.View.measure(View.java:15524)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.View.measure(View.java:15524)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.View.measure(View.java:15524)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-10 11:41:09.041: E/AndroidRuntime(30139): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.View.measure(View.java:15524)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
02-10 11:41:09.041: E/AndroidRuntime(30139): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
I am facing problem here, its asking for gridfragment return type.
Deletepublic Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
hey archana,
Deletecan you please check your imports.
They should be android.support.v4 throughout all the classes.
I guess that is the issue.
Please let me know if this solves it.
It's Run Well
ReplyDeleteThank You.
I am facing problem here, its asking for gridfragment return type.
Deletepublic Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
how to solve
have you any idea ?
This is the perfect which I am looking for. thanks for this best example..
ReplyDeleteI got some error when app is on resume mode.. Some time not every time.. My log cat is here,
05-05 10:29:20.744: W/dalvikvm(8427): threadid=1: thread exiting with uncaught exception (group=0x41020258)
05-05 10:29:20.787: E/AndroidRuntime(8427): FATAL EXCEPTION: main
05-05 10:29:20.787: E/AndroidRuntime(8427): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grid_viewpager/com.example.grid_viewpager.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.grid_viewpager.GridFragment: make sure class name exists, is public, and has an empty constructor that is public
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3510)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.os.Looper.loop(Looper.java:154)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.main(ActivityThread.java:4624)
05-05 10:29:20.787: E/AndroidRuntime(8427): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 10:29:20.787: E/AndroidRuntime(8427): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 10:29:20.787: E/AndroidRuntime(8427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-05 10:29:20.787: E/AndroidRuntime(8427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-05 10:29:20.787: E/AndroidRuntime(8427): at dalvik.system.NativeStart.main(Native Method)
05-05 10:29:20.787: E/AndroidRuntime(8427): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.grid_viewpager.GridFragment: make sure class name exists, is public, and has an empty constructor that is public
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.support.v4.app.FragmentState.instantiate(Fragment.java:96)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1726)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198)
05-05 10:29:20.787: E/AndroidRuntime(8427): at com.example.grid_viewpager.MainActivity.onCreate(MainActivity.java:30)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.Activity.performCreate(Activity.java:4479)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
05-05 10:29:20.787: E/AndroidRuntime(8427): ... 12 more
05-05 10:29:20.787: E/AndroidRuntime(8427): Caused by: java.lang.InstantiationException: can't instantiate class com.example.grid_viewpager.GridFragment; no empty constructor
05-05 10:29:20.787: E/AndroidRuntime(8427): at java.lang.Class.newInstanceImpl(Native Method)
05-05 10:29:20.787: E/AndroidRuntime(8427): at java.lang.Class.newInstance(Class.java:1319)
05-05 10:29:20.787: E/AndroidRuntime(8427): at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
05-05 10:29:20.787: E/AndroidRuntime(8427): ... 19 more
add a blank constructor to GridFragment class as below.
Deletepublic GridFragment () {
}
Hope this resolves your issue.
I am facing problem here, its asking for gridfragment return type.
ReplyDeletepublic Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
how to solve
have you any idea ?
Hi mayank,
DeleteAs posted in one of the above replies,
can you please check your imports.
They should be android.support.v4 throughout all the classes.
I guess that is the issue.
Please let me know if this solves it. Or the steps to reproduce the error.
my issue is not resolve ..
Deleten i import android.support.v4.*; in all the classes
same issues.
plz check if you are passing List to PagerAdapter class. Maybe it is null so you are getting the exception. Also let me know the steps to reproduce the crash.
DeleteCan we load images from url using this code?
ReplyDeletetype mis match: cannot convert from GridFragment to Fragment
ReplyDelete@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
I found this exception
ReplyDelete@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);}
can' t create new class of gridfragments. Create static class gridfragments inside the mainactivity.
Hi Sir,
ReplyDeletePls can u mail me the full project saravananchandru7@gmail.com
Thanks in advance...!!!
For those have some problems on importing Library (including me)
ReplyDeleteMay try just copy the required classes & xmls to your project
Its work to me
Really an awesome example!!
thanks a lot!!
How we can use "Android-ViewPagerIndicator" to my project and which folder i must import to , please answer its critical for me
ReplyDeleteHi
ReplyDeletewhen i change orientation it crashes. How we can fixed it?
How to do this as dynamic.I'm gettting all the gridview datas from backend but iam geting first nine items in a gridview instead iam getting all the datas in a horizontall view.How to overcome this.Any idea
ReplyDeleteThank you for taking the time to make the post.
ReplyDeleteI'm finding a lot of typos in the code as posted, but no mention in any of the comments. I wonder if you've somehow posted an incomplete version of the code. I can probably work through the typos, but you may want to take a look. If there is a up-to-date version, I'd love to see it.
Some examples: using string instead of String. Using category instead of Category. Using PagerIndicator instead of PageIndicator. (The last may reflect a change in the ViewPagerIndicator library).
In addition, none of the graphics are available. This my not be resolvable, but perhaps you meant to include links for them.
This comment has been removed by the author.
ReplyDeleteGreat post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleterpa Training in Chennai
rpa Training in bangalore
rpa Training in pune
blueprism Training in Chennai
blueprism Training in bangalore
blueprism Training in pune
rpa online training
This comment has been removed by the author.
ReplyDeleteYour very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeleterpa Training in tambaram
blueprism Training in tambaram
automation anywhere training in tambaram
iot Training in tambaram
rpa training in sholinganallur
blue prism training in sholinganallur
automation anywhere training in sholinganallur
iot training in sholinganallur
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteData Science training in marathahalli
Data Science training in btm
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in kalyan nagar
Data Science training in electronic city
Data Science training in USA
Data science training in pune
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai
I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!
ReplyDeletejava training in tambaram | java training in velachery
java training in omr | oracle training in chennai
java training in annanagar | java training in chennai
I prefer to study this kind of material. Nicely written information in this post, the quality of content is fine and the conclusion is lovely. Things are very open and intensely clear explanation of issues
ReplyDeletepython training in tambaram
python training in annanagar
python training in OMR
python training in chennai
Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.
ReplyDeletepython training in tambaram
python training in annanagar
python training in OMR
python training in chennai
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
ReplyDeleterpa training in Chennai | best rpa training in chennai
rpa training in pune
rpa online training | rpa training in bangalore
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteBlueprism online training
Blue Prism Training in Pune
Blueprism training in tambaram
Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteBlueprism training in annanagar
Blueprism training in velachery
Blueprism training in marathahalli
Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteangularjs
Training in chennai
angularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
angularjs-Training in sholinganallur
All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
ReplyDeleteData Science Training in Chennai | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm
Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.
ReplyDeletepython training in rajajinagar
Python training in btm
Python training in usa
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteBest Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies
Selenium Training in Bangalore | Best Selenium Training in Bangalore
Best AWS Training in Marathahalli | AWS Training in Marathahalli
Amazon Web Services Training in Anna Nagar, Chennai |Best AWS Training in Anna Nagar, Chennai
AWS Training in Velachery | Best AWS Course in Velachery,Chennai
Best AWS Training in Chennai | AWS Training Institutes |Chennai,Velachery
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteBest Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies
Selenium Training in Bangalore | Best Selenium Training in Bangalore
Best AWS Training in Marathahalli | AWS Training in Marathahalli
Amazon Web Services Training in Anna Nagar, Chennai |Best AWS Training in Anna Nagar, Chennai
AWS Training in Velachery | Best AWS Course in Velachery,Chennai
Best AWS Training in Chennai | AWS Training Institutes |Chennai,Velachery
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
https://sapnagiyanwani.blogspot.com/2013/09/adrotator-control-in-aspnet.html
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
http://simpleandroidtutorials.blogspot.com/2012/06/fetching-data-from-server-and-setting.html
Really I enjoy your blog with an effective and useful information. Very nice post with loads of information. Thanks for sharing with us.
ReplyDeleteSelenium training in Chennai
Selenium Courses in Chennai
best ios training in chennai
Digital Marketing Training in Chennai
JAVA J2EE Training Institutes in Chennai
SEO Training Chennai
Best seo training in chennai
the blog is nicely maintained.its good for study.good job.
ReplyDeleteBig Data Analytics Courses in Chennai
Big Data Analytics Training in Chennai
Data Analyst Course in Chennai
Machine Learning Course in Chennai
Machine Learning institute in Chennai
Data Science Training in Anna Nagar
Data Science Training in T Nagar
It is a great post. Keep sharing such kind of useful information.
ReplyDeleteEducation
Technology
Excellent Article. Thanks Admin
ReplyDeleteDevOps Certification in Chennai
indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteSOFTWARE TRAINING IN CHENNAI
POWERBI TRAINING IN CHENNAI
CCNA TRAINING IN CHENNAI
ANDROID TRAINING IN CHENNAI
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteMATLAB TRAINING IN CHENNAI | BEST MATLAB TRAINING IN CHENNAI
EMBEDDED SYSTEMS TRAINING IN CHENNAI | BEST EMBEDDED TRAINING IN CHENNAI | EMBEDDED SYSTEMS COURSE IN CHENNAI
MCSA / MCSE TRAINING IN CHENNAI | BEST MCSE TRAINING IN CHENNAI | MCSE COURSE IN CHENNAI
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeletePython Training in Chennai| Python Training institute in Chennai
Datascience Training in Chennai |Datascience Training institute in Chennai
RPA Training in Chennai | RPA Training institute in Chennai
DevOps Training in Chennai |DevOps Training institute in Chennai
<
Very nice blog. A great and very informative post, Keep up the good work!
ReplyDeleteData Science Bangalore
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
ReplyDeleteDATA SCIENCE COURSE MALAYSIA
ReplyDeleteI have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. I appreciate your post and look forward tomorrow.data science course in singapore
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page digital marketing course in singapore
ReplyDelete
ReplyDeleteI am looking for and I love to post a comment that "The content of your post is awesome" Great work!
love it
Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
ReplyDeletebig data course malaysia
Really awesome blog!!! Thanks for sharing valuable information.
ReplyDeleteData Science Course Training in Bangalore
The Blog is Really very Impressive.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, thanks guys
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
this post most user friendly and super navigation to all posts... Thank you
ReplyDeletePython Hyderabad
Python Training in Coimbatore
Python Training in Chennai
Python Training in Training
Python Training in Bangalore
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteYour Site Is Very Good and The Post is Well On Topic about Android GridView with ViewPager, Thanks for Sharing it with us.
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteoracle training in chennai
oracle training in omr
oracle dba training in chennai
oracle dba training in omr
ccna training in chennai
ccna training in omr
seo training in chennai
seo training in omr
This Was An Amazing! I Haven't Seen This Type of Blog Ever! Thank you for Sharing, data scientist course in Hyderabad with placement
ReplyDeleteExtremely good content, i enjoyed alot and gain some knowledge as well.Thanks for this.
ReplyDeleteData Science Training in Pune
Amazing Article! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.If you are Searching for info click on given link
ReplyDeleteData science course in pune
Thank you for sharing such detailed Blog. I am learning a lot from you. Visit my website to get best Information About Best IAS coaching in Mumbai
ReplyDeleteBest IAS coaching in Mumbai
Top IAS coaching in Mumbai
The King Casino | Ventureberg
ReplyDeleteDiscover the rise and fall of www.jtmhub.com the worrione.com king casino, ventureberg.com/ one of the world's largest https://septcasino.com/review/merit-casino/ The Casino is operated by the King Casino Group. You can
TÜL PERDE MODELLERİ
ReplyDeletesms onay
mobil ödeme bozdurma
Nft nasıl alınır
Ankara Evden Eve Nakliyat
TRAFİK SİGORTASİ
DEDEKTOR
web sitesi kurma
aşk kitapları
çekmeköy mitsubishi klima servisi
ReplyDeleteataşehir mitsubishi klima servisi
maltepe vestel klima servisi
kadıköy vestel klima servisi
maltepe bosch klima servisi
kadıköy bosch klima servisi
maltepe arçelik klima servisi
tuzla arçelik klima servisi
çekmeköy samsung klima servisi
This is a fantastic read. I appreciate the thorough research and attention to detail. It’s always refreshing to find well-written content that’s both informative and engaging
ReplyDeleteMysore Ooty Coorg Tour Package
Mysore Ooty Kodaikanal Tour Package
Manali Rohtang Tour Package
This is a really interesting post. I appreciate the depth of your research and the clarity of your writing. Looking forward to reading more
ReplyDeleteKhajuraho Tour Packages
Western Group of Temples in Khajuraho
This post was exactly what I needed to read today. It’s very well-written and provides practical advice. Keep up the great work
ReplyDeleteDestination Wedding Planner Packages
Mice Tour Operators
Corporate Event Planner
This setup gives you a swipeable menu with grid items on each page, similar to a carousel or multiple menus inside an Android application.
ReplyDeleteData science courses in Pune
This is a great read! Data science courses in Kochi offer amazing hands-on learning experiences. If you're looking to dive into this field, don't miss out on the Data science courses in Kochi!"
ReplyDeleteThanks for this detailed guide! Your blog makes understanding complex Android UI implementations so much easier. Looking forward to more content like this.
ReplyDeleteData science Courses in Sydney
I thoroughly enjoyed reading this. It’s insightful, engaging, and well-written from start to finish
ReplyDeleteData science Courses in London
Fantastic tutorial! The GridView combined with ViewPager concept is something I’ve been looking for in my Android projects. Your clear explanation and code snippets make it easy to understand and implement. I’ll definitely be trying this out in my app development!
ReplyDeleteData science courses in Glasgow
Combining Android GridView with ViewPager creates a dynamic and user-friendly interface for displaying grid-based content in swipeable pages. This approach enhances navigation, supports better content organization, and offers a seamless, visually appealing experience for users in Android applications.
ReplyDeleteThank you for the article.
Data science Courses in Berlin
Great insights on the topic! I appreciate the detailed explanation and valuable points shared. This post truly added to my understanding of the subject!
ReplyDeleteData science courses in France
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeleteData Analytics Courses In Chennai