I am trying to make an app were the images are shown in a list view .But I am not able to loop the View Pager.Show that after the last image first image comes . Here is my code.I tried the basic java method but It is not working
package com.union.pr26;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends Activity {
ViewPager viewPager;
CustomeSwipeAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewpager);
adapter=new CustomeSwipeAdapter(this);
viewPager.setAdapter(adapter);
}
}
ADAPTER
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class CustomeSwipeAdapter extends PagerAdapter {
private Context ctx;
final int position = 0;
private LayoutInflater layoutInflater;
private int []imgID ={R.drawable.img1,R.drawable.img2,R.drawable.img3};
public CustomeSwipeAdapter(Context ctx) {
this.ctx=ctx;
}
#Override
public int getCount() {
return imgID.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view =layoutInflater.inflate(R.layout.extra,container,false);
ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
if(position>=3){
position=-imgID.length;
position--;
}
imageView.setImageResource(imgID[position]);
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
In your mainActivity:
List<Integer> galImages = new ArrayList<>();
galImages.add(R.drawable.one);
galImages.add(galImages.get(0));
galImages.add(0,galImages.get(galImages.size()-2));
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this,galImages);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1, false);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) { //this is triggered when the switch to a new page is complete
final int lastPosition = viewPager.getAdapter().getCount() - 1;
int currentPosition = viewPager.getCurrentItem();
if (currentPosition == lastPosition) {
viewPager.setCurrentItem(1, false); //false so we don't animate
} else if (currentPosition == 0) {
viewPager.setCurrentItem(lastPosition - 1, false);
}
}
}
});
In my example,i added one image in my list,then added it again.And then in onPageScrollStateChaged i loop it over and over again
You have to use an InfiniteViewPager. Here is the link for achieving the same.
GitHub Link
Related
I was trying to use Drawable Animation in one of my Viewpager. But when I try to use getDrawable() method, it gives me null pointer exception. My ViewPager has 4 page. I think this is because my XML files are made in Viewpager and I cannot access them with this method. I already set a source for an ImageView in my Viewpager but still it doesn't work. Please have a look at my codes and help me.
My Main Activity:
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
ImageView myAnimation = (ImageView)findViewById(R.id.imageView2);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}
}
My ViewPager Adapter:
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
and my enum to add new Pages:
public enum ModelObject {
RED(R.string.wp1, R.layout.wp1),
BLUE(R.string.wp2, R.layout.wp2),
LOL(R.string.wp3, R.layout.wp3),
WP(R.string.wp5,R.layout.wp4),
GREEN(R.string.wp4, R.layout.wp4);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
Please help me if you can.
sorry for bad English.
#Override
public void onPageSelected(int pos) {
//find your layout in with the position & then implement
ImageView myAnimation = (ImageView)findViewById(R.id.imageView2); // this imageview for particular viewpager page
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}
I want to make an image slider with Android Studio.
I found a new slider code, built with ViewPager, I tried it and worked..
but I want to add 2 buttons for next and previous..
When clicked button, it must go to next picture or previous picture.
here the mainactivity,
package org.trial.slider;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
ImageAdapter.java file,
package org.trial.slider;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by ismail on 30.03.16.
*/
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
#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.FIT_XY);
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);
}
}
And I have 2 XML files; fragment_pager and swipe_fragment
add button your MainActivity and activity_main.xml
add code button.setonclicklister(this); in OnCreate()
Example code here :
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
//prev button
case R.id.btn_list_detail_img_prev:
viewPager.setCurrentItem(mPager.getCurrentItem()-1,true);
break;
//nextbutton, 1st argument : pageindex, 2nd argument : Enable smooth
case R.id.btn_list_detail_img_next:
viewPager.setCurrentItem(mPager.getCurrentItem()+1,true);
break;
}
There is a method name viewPager.setCurrentItem(position).
This method take a integer value as a index and is set on viewPager object using this method you can change the position if pager item.
class MyClass extends Fragment implements ViewPager.OnPageChangeListener {
private int selectedViewPager = 0;
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.backward:
if (selectedViewPager > 0) {
viewPager.setCurrentItem(selectedViewPager - 1, true);
}
break;
case R.id.fwd:
if (viewPager.getAdapter().getCount() > selectedViewPager) {
viewPager.setCurrentItem(selectedViewPager + 1, true);
}
break;
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
selectedViewPager = position;
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
I am working on an application that have to have a GridView in the main activity containing images.
When an image is clicked, it has to open in another Activity in a bigger size. In this Activity, a ViewPager has to be implemented so that I can swipe right and left to change between the images.
The problem is that when i click on an image, the first image of the images appear firstly (argentina flag) and then the viewpager works .. how can i make it start by the image that i clicked ?
This is my main activity code :
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivityPager extends ActionBarActivity
{
static int[] FLAG;
ViewPager viewPager;
PagerAdapter adapter;
GridView gridview;
int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_pager);
FLAG = new int[] { R.drawable.flag_of_argentina, R.drawable.flag_of_brazil,
R.drawable.flag_of_england, R.drawable.flag_of_france,
R.drawable.flag_of_germany, R.drawable.flag_of_italy, R.drawable.flag_of_spain,
R.drawable.flag_of_uruguay };
AdapterGrid adapter = new AdapterGrid(MainActivityPager.this, FLAG);
gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
Intent intent = new Intent(view.getContext(), ActivityImage.class);
intent.putExtra("position", position);
count = gridview.getCount();
intent.putExtra("count", count);
startActivity(intent);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my AdapterGrid class code :
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class AdapterGrid extends BaseAdapter
{
private int[] mFlag;
private Context mContext;
public AdapterGrid(Context context, int[] flag) {
//super(context, flag);
mContext = context;
mFlag = flag;
}
#Override
public int getCount() {
return mFlag.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
imageView.setImageResource(mFlag[position]);
} else {
grid = (View) convertView;
}
return grid;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
This is my ActivityImage code :
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class ActivityImage extends FragmentActivity
{
private static int COUNT;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
Intent intent = getIntent();
COUNT = intent.getExtras().getInt("count");
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return COUNT;
}
#Override
public Fragment getItem(int position) {
return FragmentImage.init(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image, menu);
return true;
}
}
This is my FragmentImage class code :
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class FragmentImage extends Fragment {
int fragVal;
static FragmentImage init(int pos) {
FragmentImage frag = new FragmentImage();
Bundle args = new Bundle();
args.putInt("val", pos);
frag.setArguments(args);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
ImageView imageView = (ImageView) layoutView.findViewById(R.id.imageView1);
imageView.setImageResource(MainActivityPager.FLAG[fragVal]);
return layoutView;
}
}
Thanks.
You set the position intent.putExtra("position", position); but never use it.
get it and
set after
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(CurrentPosition);
I am having trouble here as I am trying to play a sound while the user performs an image swipe: no sound is being played.
I am getting anerror when attaching OnPageChangeListener to my ViewPager. Also, OnPageListener is not used.
Code related to the error:
package com.android.viewpager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static MediaPlayer mp = null;
#Override
protected 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(mOnPageListener);
}
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,};
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.eight,R.raw.nine};
#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 = MainActivity.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);
return imageView;
}
public OnPageChangeListener mOnPageListener = new OnPageChangeListener() {
#Override public void onPageSelected(int position) {
//for Sound
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(MainActivity.this, mAudio[position]);
mp.start();
}
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override public void onPageScrollStateChanged(int state) {}
};
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
I am really stuck badly here. Please tell me where the error is.
I don't know exactly what your error is, but if you say that it's "by attaching OnPageChangeListener to my viewPager", I will tell you to do that :
Maybe try to get your OnPageChangeListener mOnPageListener out of your ImagePagerAdapter, put it directly in your Activity.
Or set the OnPageChangeListener to the adapter :
viewPager.setOnPageChangeListener(adapter);
and make your Adapter implement onPageChangedListener.
Hope it will help
I am getting this problem in my android application as an "value of the field is not in used".
actually i am trying to play the sound here when user swipes the page but it continuously giving me this error. this my code.
package com.android.learning_numbers;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Numbers extends Activity {
private static final OnPageChangeListener mOnPageListener = null;
public static MediaPlayer mp = null;
#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);
viewPager.setOnPageChangeListener(mOnPageListener);
}
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,};
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,};
#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);
return imageView;
}
public OnPageChangeListener mOnPageListener = new OnPageChangeListener() {
#Override public void onPageSelected(int position) {
//for Sound
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Numbers.this, mAudio[position]);
mp.start();
}
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override public void onPageScrollStateChanged(int state) {}
};
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}