how to go to next activity after ViewPager in android? - android

I created a "Slide Image" in first activity; which has five images. After I slide the last image(fifth), it should go to next activity,say-activity_next.xml.
Below is my code to slide Image i.e., SlideActivity.java
public class SlideActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
}
I'm not getting where to pass Intent for next activity i.e., activity_next.xml
**** UPDATED CODE as per your suggestions ****
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==viewPager.getAdapter().getCount()){
Intent reg = new Intent(SlideActivity.this,activity_next.class);
startActivity(reg);
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
public int getCount() {
return mImages.length+1;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
****activity_next.java code is below ****
public class activity_next extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
**** activity_next.xml code is as below****
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Nowhere Else" />
</LinearLayout>

I'm not getting where to pass Intent for next activity i.e.,
activity_next.xml
To start Activity on reach of last item in ViewPager add OnPageChangeListener to ViewPager and in onPageSelected start next Activity:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()){
//start next Activity here with activity_next.xml layout
}
}
....
});

If you want to slide the last image to go to the next Activity, like a "dismiss" efect, you should try something like this:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
public int getCount() {
return mImages.length+1;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
and then:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()-1){
//start next Activity
}
}
});
}

I got it clear as #Chol said I just made the count to -1 as below.
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
if(position==viewPager.getAdapter().getCount()-1){
Intent reg = new
Intent(Illustrations.this,Register_Page.class);
startActivity(reg);
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
Thank you all very much ... :)

The position is image's position that comes array like slide_image[position]. So, with using position, the Intent can be create at the SliderAdapter page just writing several code.
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout, container, false);
LinearLayout layoutslide = view.findViewById(R.id.slider);
ImageView slideImageView =(ImageView)view.findViewById(R.id.slide_image);
TextView slideHeading=(TextView)view.findViewById(R.id.slide_heading);
TextView slideDescription=(TextView)view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_image[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_description[position]);
slideImageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (position == 0) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 1) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 2) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
}
});

Related

return activity result from a ViewPager

Am using a ViewPager with adapter to show some pictures (as a gallery) and I need to return the name of the picture when the user click above
my problem that I can not implement onclick from the adapter because I can't return the result to the calling activity, and when I implement the viewAdapter.addOnPageChangeListener I have to wait until the page is changed so if the user clicks on the first pic it well not work
here is my code:
for(int i=0;i<images.length;i++)
imagesArray.add(images[i]);
MyAdapter myAdapter = new MyAdapter(MyImagePicker.this,imagesArray);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(myAdapter);
//CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
//indicator.setViewPager(mPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mPager, true);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Toast.makeText(getApplicationContext()," pos : "+position,Toast.LENGTH_LONG).show();
// return result //
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
class MyAdapter extends PagerAdapter {
private ArrayList<Integer> images;
private LayoutInflater inflater;
private Context context;
public MyAdapter(Context context, ArrayList<Integer> images) {
this.context = context;
this.images=images;
inflater = LayoutInflater.from(context);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return images.size();
}
#Override
public Object instantiateItem(ViewGroup view, int position) {
View myImageLayout = inflater.inflate(R.layout.slider, view, false);
final ImageView myImage = (ImageView) myImageLayout
.findViewById(R.id.image);
myImage.setImageResource(images.get(position));
myImage.setTag(position+1);
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context," pos : "+view.getTag(),Toast.LENGTH_LONG).show();
}
});
view.addView(myImageLayout, 0);
return myImageLayout;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
There are a few approaches you could take:
1) Create a callback interface. In your activity implement the interface (create a listener) and pass it into the adapter's constructor, when the user clicks on an image, pass the image name back to the activity using the callback (mlistener.onImageChosen(view.getTag()).
public interface ImageChosenListener
{
void onImageChosen(String image);
}
2) Use a broadcast from the adapter and have a broadcast receiver in your activity.
try this.
mPager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(mPager.getCurrentItem()){
case 0: // its position 0
//somthing
break;
.
.
.
}
});
Thanks jt-gilkeson,
I think using a callBack interface is the perfect and the easiest solution :
Here is my answer :
// Create a callBack interface
public interface CallBack {
void callBack(String pos);
}
// The activity hosting my picture gallery
public class MyImagePicker extends Activity implements CallBack {
private static ViewPager mPager;
private static final Integer[] images= {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};
private ArrayList<Integer> imagesArray = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galery_image_chooser);
init();
}
private void init() {
for(int i=0;i<images.length;i++)
imagesArray.add(images[i]);
MyAdapter myAdapter = new MyAdapter(MyImagePicker.this,imagesArray,this);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mPager, true);
}
// Implement the call back function
public void callBack(String pos){
//return back result
Intent intent=new Intent();
intent.putExtra("imgName",pos);
setResult(2,intent);
finish();//finishing activity
}
}

Go to page option.. find page

I made an app which has 100 pages and change through swipe, but I want to add "Go to page" option so I can go to the specific page easily.
public class ViewPagerExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager_example);
ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = { R.drawable.page3, R.drawable.page4,
R.drawable.page5, R.drawable.page6, R.drawable.page7,
R.drawable.page8, R.drawable.page9, R.drawable.page10, R.drawable.page11,
R.drawable.page12, R.drawable.page13, R.drawable.page14,
R.drawable.page15, R.drawable.page16, R.drawable.page17,
R.drawable.page18, R.drawable.page19, R.drawable.page20, R.drawable.page21,
R.drawable.page22, R.drawable.page23, R.drawable.page24,
R.drawable.page25, R.drawable.page26, R.drawable.page27,
R.drawable.page28, R.drawable.page29, R.drawable.page30,
R.drawable.page31, R.drawable.page32, R.drawable.page33, R.drawable.page34,
R.drawable.page35, R.drawable.page36, R.drawable.page37,
R.drawable.page38, R.drawable.page39, R.drawable.page40, R.drawable.page41,
R.drawable.page42, R.drawable.page43, R.drawable.page44,
R.drawable.page45, R.drawable.page46, R.drawable.page47,
R.drawable.page48, R.drawable.page49, R.drawable.page50, R.drawable.page51,
R.drawable.page52, R.drawable.page53, R.drawable.page54,
R.drawable.page55, R.drawable.page56, R.drawable.page57,
R.drawable.page58, R.drawable.page59, R.drawable.page60, R.drawable.page61,
R.drawable.page62, R.drawable.page63, R.drawable.page64,
R.drawable.page65, R.drawable.page66, R.drawable.page67,
R.drawable.page68, R.drawable.page69, R.drawable.page70, R.drawable.page71,
R.drawable.page72, R.drawable.page73, R.drawable.page74 };
#Override
public int getCount() {
return images.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
#Override
public void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
long lastPress;
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (interstitial.isLoaded())
interstitial.show();
}
}
To switch the ViewPager to a specific page, you just need to call setCurrentItem().

Open activity or dialog box when i press image i have in my viewPAger

in My root Activity i have Slide show that displayed by the VIewPager .
in my case i want to open new activity or dialog box or something like this when i pressed image on Slide show .
Normally i used OnclickListener and assign picture id to it and don't get any Feedback .
RootActivity.java
public class RootActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
vp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.print("OK ...");
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer ;
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer){
this.context=countDownTimer;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage){
final Runnable Update = new Runnable() {
int NUM_PAGES =5;
int currentPage = curPage ;
public void run() {
if (currentPage == NUM_PAGES ) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
}
Okay, first things first...
1. This line, in RootActivity.onCreate() does not make a lot of sense:
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
You're trying to find a ViewPager object in the layout using a drawable resource id. So delete this line and everything in onCreate() that comes after it.
2. You can only attach an OnClickListener to an existing view and since there is no OnItemClickListener for PagerAdapters, you need to attach the OnClickListener to the appropriate Views from within the Adapter's instantiateItem() method.
I have attached a modified version of your code representing these points.
RootActivity.java
public class RootActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
}
}
ImageAdapter.java
Look for the // Begin Added by SpaceLord comments
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer;
Context context;
private int[] GalImages = new int[]{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer) {
this.context = countDownTimer;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
// Begin Added by SpaceLord
imageView.setOnClickListener(itemClickListener);
imageView.setTag(position);
// End Added by SpaceLord
container.addView(imageView, 0);
return imageView;
}
// Begin Added by SpaceLord
private final View.OnClickListener itemClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int tag = (Integer) v.getTag();
switch (tag) {
// Do whatever you need to for each slide click here
case 0:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on ONE");
break;
case 1:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on TWO");
break;
case 2:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on THREE");
break;
case 3:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX1");
break;
case 4:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX2");
break;
}
}
};
// End Added by SpaceLord
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage) {
final Runnable Update = new Runnable() {
int NUM_PAGES = 5;
int currentPage = curPage;
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time * 1000);
}
}
Directly set onClickListener(...) to your ImageView in your ImageAdapter like:
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your job
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}

viewpager swiping

the below code is working from left to right swiping now i would like set the same thing from the right to left also.swiping is working but the speakout is getting crashed with position.in pageAdapter forward swiping is working good.and backward swiping is also working good only but Textto speech is not correctly working
Below is the code:
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
here its not working. can any one give me suggestions and take me out of this issue
public class Learn extends Activity implements TextToSpeech.OnInitListener{
private TextToSpeech tts;
private ViewPager pic;
private Button next,back;
private TextView text;
private Context context;
private int i=0;
public Integer[] fruits={R.drawable.apple1,R.drawable.banana1,R.drawable.grapes1,R.drawable.cherry1,R.drawable.lemon1,
R.drawable.orange1,R.drawable.strwberry1,R.drawable.water1};
public String [] names={"Apple","Banana","Grapes","Cherry","Lemon","Orange","Strawberry","Water melon"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn);
tts = new TextToSpeech(this, this);
pic=(ViewPager)findViewById(R.id.view_pager);
next=(Button)findViewById(R.id.button1);
back=(Button)findViewById(R.id.button2);
text=(TextView)findViewById(R.id.text);
ImagePageAdapter adapterpic=new ImagePageAdapter(this);
pic.setAdapter(adapterpic);
}
private class ImagePageAdapter extends PagerAdapter {
public ImagePageAdapter(Context learn) {
// TODO Auto-generated constructor stub
context=learn;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
private void speakOut(String text) {
// TODO Auto-generated method stub
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
#Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
pic.setEnabled(true);
//speakOut("apple");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
}
Here is stack trace
02-27 16:39:00.994: I/TTS received:(2369): Apple
02-27 16:39:05.144: I/TTS received:(2369): Banana
02-27 16:39:06.534: I/TTS received:(2369): Grapes
02-27 16:39:24.273: I/TTS received:(2369): Banana
02-27 16:39:25.355: I/TTS received:(2369): Grapes
02-27 16:39:26.593: I/TTS received:(2369): Cherry
02-27 16:39:28.624: I/TTS received:(2369): Apple
please give me suggestions to get out of this issue
why don't you try to put sounds of objects instead of convert them text to speech.. You can try this code
public class Learn extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,R.drawable.no10,R.drawable.bg2};
private int[] mAudio = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,R.raw.ten,R.raw.one};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Numbers.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
if(position>=1){
mp = MediaPlayer.create(Numbers.this, mAudio[position-1]);
mp.start();
}
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
Here value you can change according to your requirements.

how do i use view pager with touchimageview

I am currently using TouchImageView to allow zooming in and out but now, I need also a View Pager using image view. How do I merge those two together? must I inflate the layout?
this is my viewpager
public class Main extends FragmentActivity implements AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener{
private ViewPager viewPager;
String[] mImageIds;
private Handler handler;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayImage();
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(this);
//gallery = (Gallery) findViewById(R.id.gallery);
//gallery.setAdapter(new ImageAdapter());
//gallery.setOnItemSelectedListener(this);
//textView = (TextView) findViewById(R.id.title);
handler = new Handler();
}
public void displayImage()
{
ArrayList<String> mStringList= new ArrayList<String>();
File strPath = new File(Environment.getExternalStorageDirectory() + "/Covers");
int lists = strPath.listFiles().length;
Log.d("number of items in array ",String.valueOf(lists));
File yourDir = new File(strPath, "");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
String v = strPath + "/" + name;
mStringList.add(v);
}
}
mImageIds = new String[mStringList.size()];
mImageIds = mStringList.toArray(mImageIds);
for(int i = 0; i < mImageIds.length ; i++){
//Log.d("string is",(mImageIds[i]));
}
}
// gallery item selected
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
updateUI(i);
}
// view pager item selected
public void onPageSelected(int i) {
updateUI(i);
}
private void updateUI(final int i) {
handler.post(new Runnable() {
public void run() {
//gallery.setSelection(i);
viewPager.setCurrentItem(i);
///textView.setText("Photo #" + i);
}
});
}
public void onNothingSelected(AdapterView<?> adapterView) { }
public void onPageScrolled(int i, float v, int i1) { }
public void onPageScrollStateChanged(int i) { }
public class ImageAdapter extends BaseAdapter implements OnTouchListener {
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(Main.this);
imageView.setOnTouchListener(this);
imageView.setImageDrawable(Drawable.createFromPath(mImageIds[position]));
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
public class EnableDisableViewPager extends ViewPager {
private boolean enabled = true;
public EnableDisableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(enabled)
return super.onInterceptTouchEvent(arg0);
return false;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return mImageIds.length;
}
#Override
public Fragment getItem(final int position) {
return new Fragment() {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView imageView = new ImageView(Main.this);
imageView.setImageDrawable(Drawable.createFromPath(mImageIds[position]));
return imageView;
}
};
}
}
}
and its xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout android:layout_height="0px"
android:layout_width="fill_parent"
android:layout_weight="1"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip" />
</FrameLayout>
</LinearLayout>
Create an FragmentActivity whose layout consists of View Pager.
Add a Fragment foreach image whose layout consists of
TouchImageView.
Add these list of fragments to the View pager. Thats it. Pinch Zoom and pager both works.
Like this:
#Override
protected void onStart() {
super.onStart();
List<Fragment> fragments = new Vector<Fragment>();
String url1 = "someurl1.jpg";
Bundle Image1 = new Bundle();
Image1.putString("image_url", url1);
fragments.add(Fragment.instantiate(this, ZoomImageFragment.class.getName(),Image1));
String url2 = "someurl2.jpg";
Bundle Image2 = new Bundle();
Image2.putString("image_url", url2);
fragments.add(Fragment.instantiate(this, ZoomImageFragment.class.getName(),Image2));
//and so on.
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.animate();
mPager.setAdapter(this.mPagerAdapter);
}

Categories

Resources