Reputation: 15
I am building an app with a viewpager horizontal swipe. all of the pager components function however I cannot get my button to play. There are no errors with the code, it just doesnt work. thanks for you help.
package com.test.viewpager;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class viewpager extends Activity {
public MediaPlayer mp = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
OnClickListener listener = new OnClickListener(){
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(viewpager.this, R.raw.song1);
mp.start();
}else {
mp.stop();
mp = null;
}
}};
if (btn != null)
btn.setOnClickListener(listener);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2); }
public void farLeftButtonClick(View v) {
Toast.makeText(this, "Far Left Button Clicked", Toast.LENGTH_SHORT).show();
}
public void farRightButtonClick(View v) {
Toast.makeText(this, "Far Right Elephant Button Clicked", Toast.LENGTH_SHORT).show();
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5; }
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.page1;
break;
case 1:
resId = R.layout.page2;
break;
case 2:
resId = R.layout.page3;
break;
case 3:
resId = R.layout.page4;
break;
case 4:
resId = R.layout.page5;
break; }
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view; }
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2); }
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1); }
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null; }
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}}
}
Here is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myfivepanelpager"/>
</LinearLayout>
here is one of the panes page3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/newbutton"
android:layout_gravity= "center"
/>
Upvotes: 0
Views: 788
Reputation: 45493
I cannot get my button to play. There are no errors with the code, it just doesnt work
That's because you're trying to inflate the button as part of the Activity's layout (main.xml). However, as you can already tell from the layouts you posted: that's not the case. The button is part of the 'page' that is displayed in the ViewPager; hence, move the button logic from the Activity's onCreate(...)
to the PagerAdapter's instantiateItem(...)
.
That will yield something like this:
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// switch/case omitted for simplicity
View view = inflater.inflate(resId, null);
Button btn = (Button) findViewById(R.id.button1);
if (btn != null) btn.setOnClickListener(new View.new OnClickListener(){
@Override public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(viewpager.this, R.raw.song1);
mp.start();
} else {
mp.stop();
mp = null;
}
}
});
// add and return view etc.
}
Upvotes: 1