Here I have developed an app to view full screen images. I was able to develop it with swipe to move to next image. I have used a viewpager element.
How can I use onclick action to viewpager to do something.(delete, share etc..)
My code looks like below,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
Thread child=new Thread(){
#Override
public void run() {
viewPager = (ViewPager) findViewById(R.id.pager);
utils = new Utils(getApplicationContext());
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,utils.getFilePaths());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);//show the selected
btnMail=(Button)findViewById(R.id.btnMailThis);
btnRate=(Button)findViewById(R.id.btnRate);
btnMail.setVisibility(View.INVISIBLE);
btnRate.setVisibility(View.INVISIBLE);
}
};
child.start();
}
The FullScreenImageAdapter.java looks like below
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
//Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_full_image, container,
false);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
//btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
imgDisplay.setImageBitmap(bitmap);
/*
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_activity.finish();
}
});*/
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
Thanks in advance..!
You can create your own class inheriting ViewPager and override onInterceptTouchEvent like this:
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getAction() == MotionEvent.ACTION_UP) {
//Your code here
return false;
} else {
//Do this to keep swipes working. It will also make vertical swiping work. You can avoid the latter by handling MotionEvent action and x,y directions.
return super.onInterceptTouchEvent(arg0);
}
}
Don't forget to replace the ViewPager object in your xml with com.example.yourpackagename.YourViewPagerClass
Related
In my project, I have a PagerAdapter to inflate CardViews that hold text objects created by the user.
On each CardView I have edit and delete buttons to access methods to do with the ArrayList holding the text objects.
I have coded an interface in my adapter to handle clicks made within the text object creation activity. However, my instatiateItem method in my adapter is now showing on the #Override annotation "Method does not override method from its superclass". When I remove the #Override annotation I get an error in my Logcat:
java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
How do I implement a click listener for buttons on a CardView in a PagerAdapter?
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
#Override
public int getCount() {
return pagesList.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position, final onItemClickListener listener) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
listener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
listener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
Activity code:
public class CreateActivity extends AppCompatActivity {
ViewPager viewPager;
CreateAdapter createAdapter;
List<PdfPage> pdfPageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
pdfPageList = new ArrayList<>();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
}
#Override
protected void onStart() {
super.onStart();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
createAdapter.setOnItemClickListener(new CreateAdapter.onItemClickListener() {
#Override
public void onEditClick(int position) {
}
#Override
public void onDeleteClick(int position) {
pdfPageList.remove(position);
}
});
}
Your instantiateItem method does not override from it's superclass because you added an extra argument final onItemClickListener listener.
As you are using the same clickListener for all entries and passing the item position as an argument, I suggest that you pass the onItemClickListener in the adapter constructor, or just set it via
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
#Override
public int getCount() {
return pagesList.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mlistener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
mlistener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mlistener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
mlistener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
And your Activity code could stay as it is.
I've made a image slider with help of viewpager and picasso, now i want to add next, prev button on the image, how can i do that?
I saw different answers of this but all of them are showing error
This is main activity where image slider is shown:
public class MainActivity extends AppCompatActivity {
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie
};
private WebBackForwardList viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
This is ViewPageAdapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.fit()
.centerCrop()
.into(imageView);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
add this method to your code
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
then use this for next button
Button NextButton = (Button)findViewById(R.id.button1);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(+1), true); //getItem(+1) for next
}
});
and use this for the previous button
Button PreviousButton = (Button)findViewById(R.id.button2);
PreviousButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(-1), true); //getItem(-1) for previous
}
});
it will work fine ;)
I'm using the PhotoView library to have a slide view of images. That's easy. Then I wanted set as wallpaper the image I wanted, maybe on click in a button but I have a problem! The library seems that not allows create a layout but only its in this way.
<com.ex.paper.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So i can't add any button. I need detect the right array position and in some way set that image as wallpaper.. So far the code is this:
`public class MainActivity extends Activity
{
Bitmap bitmap;
int lastImageRef;
private static final String ISLOCKED_ARG = "isLocked";
private ViewPager mViewPager;
private static MenuItem menuLockItem;
private WallpaperManager wallpaper;
private static int[] sDrawables = { R.drawable.wallpapertwo, R.drawable.twixkatfirst, R.drawable.wallpaper,
R.drawable.sfondo, R.drawable.wallpapertre};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
setContentView(mViewPager);
mViewPager.setAdapter(new SamplePagerAdapter());
if (savedInstanceState != null) {
boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
((HackyViewPager) mViewPager).setLocked(isLocked);
}
}
static class SamplePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return sDrawables.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
photoView.setImageResource(sDrawables[position]);
WallpaperManager wallpaper = WallpaperManager.getInstance(container.getContext());
/*Toast number = Toast.makeText(container.getContext(), "wallpaper number "+position, Toast.LENGTH_LONG);
number.show();*/
// Now just add PhotoView to ViewPager and return it
container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
try
{
wallpaper.setResource(sDrawables[position]);
}
catch (IOException e)
{
}
return photoView;
}
#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;
}
}
}`
Now, in the try catch, I can set the wallpaper through position but without tapping any button!! And of course it's not a good way. Any solution? Maybe a button in the actionbar? But I can't find the array position at that point.
You can use FrameLayout:
<FrameLayout...>
<com.ex.paper.HackyViewPager/>
<Button/>
<FrameLayout/>
In onCreate:
setContentView(your.layout.with.frameLayout);
mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
wallpaper.setResource(sDrawables[mViewPager.getCurrentItem()]);
}
});
}
I have a PagerAdapter that displays images gathered from a string array of locations passed from the MainActivity class. I wish to make it so that I can refer to this PagerAdapter in the future, to create more views dynamically. I am curious as to how one goes about to doing this? My code for the PagerAdapter (the converted ArrayList is the one passed from the MainActivity class):
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
ArrayList<String> converted;
ArrayList<View> views = new ArrayList<View>();
LayoutInflater inflater;
public ImageView imgzoomer;
public ViewPagerAdapter(Context context, ArrayList<String> converted) {
this.context = context;
this.converted = converted;
}
#Override
public int getCount() {
return converted.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if(position==converted.size()-1){
// Declare Variables and Views
// Capture position and set to the ImageView
Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
bitmap.recycle();
imgzoomer.setImageBitmap(bitmap);
// Add layout_fullscreen_image.xml to ViewPager
container.removeAllViewsInLayout();
container.removeAllViews();
container.addView(container, position+1);
notifyDataSetChanged();
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables and Views
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
imgzoomer = (ImageView) itemView.findViewById(R.id.zoomer);
// Capture position and set to the ImageView
Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
imgzoomer.setImageBitmap(bitmap);
// Add layout_fullscreen_image.xml to ViewPager
container.addView(itemView, 0);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
container.removeView(imgzoomer);
}
}
The part where I want to refer to the ViewPager again in my MainActivityClass:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the view
setContentView(R.layout.viewpager_main);
ImageView imgzoomer;
//Doinbackground ASYNCTASK (returns the converted arraylist, which is the location of the pics in the storage)
new XMLParse().execute(url);
//Locate ViewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//PageChangeListener
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
//THIS PART HERE<------------
public void onPageScrolled(int position, float v, int i2) {
if(position>=converted.size()-1){
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
}
I'm attempting to implement a viewPager which will allow me to change the Youtube playlist (String PLAYLIST) depending on the position of the ViewPager. I was told in another StackOverflow post I need to: "update the listArray prior to calling 'notifyDataSetChanged()'. The call to notify is the signal to update the View in the adapter you are using. In your code, the getYouTube... needs to update and array object that the adapter has a reference to. You should be able to get it by implementing connections in your code."
^ - SOURCE: Use a ViewPager / PagerAdapter to change a string
According to the response to my previous SO post linked above - I belive I'll need to use something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.home, parent, false);
}
ImageView imageView = new ImageView(context);
ImageView imageView = getItem(position);
imageView.setImageResource(imageView.getImage());
return convertView;
}
^ - SOURCE: http://www.piwai.info/android-adapter-good-practices/
How My Current Source Works:
I have a JSON request which contains the String PLAYLIST that gets a response from YouTube :
#Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+PLAYLIST+"&v=2&alt=jsonc");
I would like to use a swipeable footer image (pagerAdapter) to change the value of the string PLAYLIST. My primary concern is trying to figure out how to use the PagerAdapter shown below to set the following:
String PLAYLIST = "vevo";
String PLAYLIST = "TheMozARTGROUP";
String PLAYLIST = "TimMcGrawVEVO";
String PLAYLIST = "TiestoVEVO";
String PLAYLIST = "EminemVEVO";
then immediately after the value of PLAYLIST is set by the PagerAdapter, create a playlist using the new value of PLAYLIST:
new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();
I've created a new string array containing the values I'd like to use. My question now that all that is understood is: how can I modify the source below to set the value of PLAYLIST using one of the array values and execute the new playlist? (Currently my source compiles but when I swipe the PagerAdapter - nothing happens)
Screenshot:
CURRENT SOURCE:
private class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter(Activity act, int[] mImages,
String[] stringArra) {
imageArray = mImages;
activity = act;
stringArray = stringArra;
}
// this is your constructor
public ImagePagerAdapter() {
super();
// setOnPageChangeListener(mPageChangeListener);
}
private int[] mImages = new int[] { R.drawable.selstation_up_btn,
R.drawable.classical_up_btn, R.drawable.country_up_btn,
R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
R.drawable.island_up_btn, R.drawable.latin_up_btn,
R.drawable.pop_up_btn, R.drawable.samba_up_btn };
private String[] stringArray = new String[] { "vevo",
"TheMozARTGROUP", "TimMcGrawVEVO", "TiestoVEVO",
"EminemVEVO" };
#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 = Home.this;
ImageView imageView = new ImageView(context);
imageView.setScaleType(ScaleType.FIT_XY);
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);
}
private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(final int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
mCurrentTabPosition = position;
}
};
protected void onTabChanged(final PagerAdapter adapter,
final int oldPosition, final int newPosition) {
// Calc if swipe was left to right, or right to left
if (oldPosition > newPosition) {
// left to right
} else {
// right to left
View vg = findViewById(R.layout.home);
vg.invalidate();
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
int oldPos = viewPager.getCurrentItem();
#Override
public void onPageScrolled(int position, float arg1, int arg2) {
if (position > oldPos) {
// Moving to the right
} else if (position < oldPos) {
// Moving to the Left
View vg = findViewById(R.layout.home);
vg.invalidate();
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
}
see the 'getView()' method at the bottom here, comparing it to your code where you orphan the imageView... Note that in the sample, the imageView thats populated with the bitmap BELONGS to a View that is contained in the inflated View...
R.layout.item owns R.id.text
R.layout.item owns R.id.image