How to implement imageview and button in pager

602 views Asked by At

I want to implement ImageView and Button in ViewPager. But when I tried to implement it just crash my app and I am not able to implement it, Please help me as I am new for Android.

public class FullScreenImageActivity extends Activity {
private static int NUM_VIEWS = 5;

private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen_view);

    Intent i = getIntent();
    int gotbiscuit = i.getExtras().getInt("position");

    adapter = new MyPagerAdapter(this);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

}

public class MyPagerAdapter extends PagerAdapter {
    public Integer[] images1 = { R.drawable.i_3, R.drawable.i_4,
            R.drawable.i_6 };
    public Integer[] images2 = { R.drawable.i_8, R.drawable.i_9,
            R.drawable.i_10 };

    public Context mContext;

    public MyPagerAdapter(Context context) {
        super();
        this.mContext = context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images1.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return view == ((ImageView) object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((ImageView) view);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(container
                .getContext());
        View view1 = inflater.inflate(R.layout.fullscreen_view, null);
        ImageView view = (ImageView) findViewById(R.id.imgDisplay);
        Button btn = (Button) findViewById(R.id.wallb);
        // view1.setScaleType(ScaleType.CENTER_CROP);

        switch (gotbiscuit) {

        case 0:
            view.setImageResource(images1[position]);
            break;
        case 1:
            view.setImageResource(images2[position]);
            break;
        }
        view.setAdjustViewBounds(true);

        ((ViewPager) container).addView(view1, 0);

        return view1;

    }
}

}

this is my Pager xml file for pagerview

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>
2

There are 2 answers

0
Raghunandan On

This

int gotbiscuit = i.getExtras().getInt("position"); // local to onCreate

should ve

gotbiscuit = i.getExtras().getInt("position"); // already declared

You also have

setContentView(R.layout.fullscreen_view);

and inflate the same layout

 View view1 = inflater.inflate(R.layout.fullscreen_view, null);

should probabbly be

setContentView(R.layout.pager_view); // pager xml

Modify the below according to your requirement

public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);

}

class MyPagerAdapter extends PagerAdapter {

public int getCount() {
return 3;
}

public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;

view = inflater.inflate(R.layout.pagerview, null);
((ViewPager) collection).addView(view, 0);
Button btn = (Button)view.findViewById(R.id.button1);
ImageView iv = (ImageView)view.findViewById(R.id.imageView1);
btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
    }

});

return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
}

activity_main.xml

<RelativeLayout 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"
tools:context=".MainActivity" >

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

pagerview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="176dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

Snap

enter image description here

0
Jeffy Lazar On

Yes it can be done by something like this ...

i made an xml layout file and named it settings_merge.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/set_settings"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:src="@drawable/share" />

<Button
    android:id="@+id/facebook_share"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/facebook" />

Now in my viewpager activity layout i used include tag to inflate the above layout

<FrameLayout
    android:id="@+id/transparentlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    <include layout="@layout/settings_merge" />
</FrameLayout>

and then finally in your PagerActivity

    ImageView set_settings;
Button facebook_share;
    set_settings=(ImageView)findViewById(R.id.set_settings);

          //the onclick listener
        set_settings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Do whatever you want to do 
            }
        });

and you are done ..:)