• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps change number of grids in gridview

For this you will need to use a onConfigurationChanged listener. Here is a simple usage:
[HIGH]
@Override
public void onConfigurationChanged(Configuration myConfig) {
super.onConfigurationChanged(myConfig);

int orientation = getResources().getConfiguration().orientation;

switch(orientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
....your grid code here...
break;

case Configuration.ORIENTATION_PORTRAIT:
....your grid code here...
break;
}
}
[/HIGH]

Let me know if you need any more help.
 
Thanks, I already tried this code.
But the no. of rows do not change when i change from portrait to landscape and from landscape to portrait.
 
Thanks, I already tried this code.
But the no. of rows do not change when i change from portrait to landscape and from landscape to portrait.

Post your code so we can help. Also the manifest code for this activity.
 
This is my MainActivity.java page

  1. public class MainActivity extends Activity {
  2. GridView gridView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. gridView=(GridView) findViewById(R.id.gridView1);
  8. gridView.setAdapter(new adapclass());
  9. }
  10. public class adapclass extends BaseAdapter
  11. {
  12. @Override
  13. public int getCount() {
  14. // TODO Auto-generated method stub
  15. return 6;
  16. }
  17. @Override
  18. public Object getItem(int arg0) {
  19. // TODO Auto-generated method stub
  20. return null;
  21. }
  22. @Override
  23. public long getItemId(int arg0) {
  24. // TODO Auto-generated method stub
  25. return 0;
  26. }
  27. @Override
  28. public View getView(int arg0, View arg1, ViewGroup arg2) {
  29. // TODO Auto-generated method stub
  30. ViewHolder viewHolder;
  31. if(arg1==null)
  32. {
  33. LayoutInflater inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  34. arg1=inflater.inflate(R.layout.sample_image, null);
  35. viewHolder=new ViewHolder();
  36. viewHolder.imageView=(ImageView) arg1.findViewById(R.id.imageView1);
  37. arg1.setTag(viewHolder);
  38. }
  39. else
  40. {
  41. viewHolder=(ViewHolder) arg1.getTag();
  42. }
  43. viewHolder.imageView.setImageResource(R.drawable.ic_launcher);
  44. return arg1;
  45. }
  46. }
  47. static class ViewHolder
  48. {
  49. ImageView imageView;
  50. }
  51. @Override
  52. public boolean onCreateOptionsMenu(Menu menu) {
  53. // Inflate the menu; this adds items to the action bar if it is present.
  54. getMenuInflater().inflate(R.menu.main, menu);
  55. return true;
  56. }
  57. @Override
  58. public void onConfigurationChanged(Configuration newConfig) {
  59. // TODO Auto-generated method stub
  60. super.onConfigurationChanged(newConfig);
  61. int orientation=getResources().getConfiguration().orientation;
  62. switch (orientation) {
  63. case Configuration.ORIENTATION_LANDSCAPE:
  64. gridView.setNumColumns(4);
  65. break;
  66. case Configuration.ORIENTATION_PORTRAIT:
  67. gridView.setNumColumns(3);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. }


Below is my androidmanifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sample.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>


I don't know where I made a mistake?

Please check my code.
 
Back
Top Bottom