This is my MainActivity.java page
- public class MainActivity extends Activity {
- GridView gridView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- gridView=(GridView) findViewById(R.id.gridView1);
- gridView.setAdapter(new adapclass());
- }
- public class adapclass extends BaseAdapter
- {
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return 6;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- // TODO Auto-generated method stub
- ViewHolder viewHolder;
- if(arg1==null)
- {
- LayoutInflater inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
- arg1=inflater.inflate(R.layout.sample_image, null);
- viewHolder=new ViewHolder();
- viewHolder.imageView=(ImageView) arg1.findViewById(R.id.imageView1);
- arg1.setTag(viewHolder);
- }
- else
- {
- viewHolder=(ViewHolder) arg1.getTag();
- }
- viewHolder.imageView.setImageResource(R.drawable.ic_launcher);
- return arg1;
- }
- }
- static class ViewHolder
- {
- ImageView imageView;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- // TODO Auto-generated method stub
- super.onConfigurationChanged(newConfig);
- int orientation=getResources().getConfiguration().orientation;
- switch (orientation) {
- case Configuration.ORIENTATION_LANDSCAPE:
- gridView.setNumColumns(4);
- break;
- case Configuration.ORIENTATION_PORTRAIT:
- gridView.setNumColumns(3);
- break;
- default:
- break;
- }
- }
- }
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.