Image should adjusts acording to screen size



I am not sure if I can ask this kind of question ...but I have downloaded the source code by using this link...http://ift.tt/1vx2jcn to the programmer .Programmer used viewpager in gridview..However, when I try to change the code and tried to implement my own pictures.. The pictures are showing in full screen . However, the pictures are just displaying in half. I want to show image to the user that fits the screen ..In other words, it should display each and every details of the image.. Following are the codes...


MainActivity.java



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);

// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);

// Show a simple toast message for the item position
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);

// Start ImageViewPager
startActivity(i);
}
});
}

// Not using options menu for this tutorial
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}


grid_view.xml



<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>


imageadapter.java



import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return mThumbIds[position];
}

public long getItemId(int position) {
return 0;
}

// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // If it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}

// References to our images in res > drawable
public Integer[] mThumbIds = {
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_8, R.drawable.sample_9,
R.drawable.sample_10, R.drawable.sample_11,
R.drawable.sample_12, R.drawable.sample_13,
R.drawable.sample_14, R.drawable.sample_15,
R.drawable.sample_16, R.drawable.sample_17,
R.drawable.sample_18
};
}


Imageviewpager.java



import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

public class ImageViewPager extends Activity {
// Declare Variable
int position;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);

// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");

ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();

// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}

// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}


imagepageadapter.java



import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImagePagerAdapter extends PagerAdapter {

private List<ImageView> images;

public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}

@Override
public int getCount() {
return images.size();
}

@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}


view_page.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

</RelativeLayout


Strings.xml



<resources>

<string name="app_name">ViewPager GridView</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">ViewPager GridView</string>

</resources>


Manifest.xml



<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.androidbegin.viewpagergridview"
android:versionCode="1"
android:versionName="1.0" >

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

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>


Any suggestion .. please help..


>


No comments:

Post a Comment