I have seen all the question related to this topic but didn't find solution, in my case viewpager is not showing anything, its simple image slider, moreover toast are appearing as i'm swaping but didn't see image and text, and when im adding these lines(as other solutions said)
((ViewPager) container).addView(iv, 0);
((ViewPager) container).addView(tv, 0);
it gives me error
android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
this is my main.xml file (inside relative layout):
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager"/>
this is viewPager adapter
public class ClsCustomPagerViewAdapter extends PagerAdapter {
int[] layouts;
String[] titles;
LayoutInflater layoutInflater;
Context context;
public ClsCustomPagerViewAdapter(Context context, int[] layouts,
String[] titles) {
this.layouts = layouts;
this.titles = titles;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ViewGroup) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View imageLayout = layoutInflater.inflate(R.layout.image_placeholder
, container);
TextView tv = (TextView) imageLayout.findViewById(R.id.tv);
ImageView iv = (ImageView) imageLayout.findViewById(R.id.iv);
Toast.makeText(context,
"Viewpagers " + titles[position] ,
Toast.LENGTH_SHORT).show();
tv.setText(titles[position]);
iv.setImageResource(layouts[position]);
((ViewPager) container).addView(iv, 0);
((ViewPager) container).addView(tv, 0);
return imageLayout;
}
#Override
public int getCount() {
return layouts.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
and this is layout for images and textviews for viewpager adapter(inside framelayout)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:text="Hello world"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_alignTop="#+id/iv"
android:layout_alignBottom="#+id/iv"
android:layout_alignLeft="#+id/iv"
android:layout_alignRight="#+id/iv"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/iv"
/>
Replace these two lines of code
((ViewPager) container).addView(iv, 0);
((ViewPager) container).addView(tv, 0);
with
container.addView(imageLayout);
EDITED:
Also
View imageLayout = layoutInflater.inflate(R.layout.image_placeholder
, container);
with
View imageLayout = layoutInflater.inflate(R.layout.image_placeholder
, container, false);
Related
I made an Adapter for my ViewPager which gets its values from an Integer in my MainActivity. The problem is I can't access the Image view inside that layout. Here you can see my codes:
///*** The Java Model:
public class ZModel {
final Integer alphabetViews;
public ZModel(Integer alphabetViews) {
this.alphabetViews = alphabetViews;
}
public Integer getAlphabetViews(){
return alphabetViews;
}
}
///*** The Java Adapter:
final Context mContext;
final ArrayList<ZModel> zModels;
public ZAdapter(Context context, ArrayList<ZModel> zModels){
this.mContext = context;
this.zModels = zModels;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(zModels.get(position).getAlphabetViews(),container,false);
container.addView(layout);
return layout;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return zModels.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
///***Main Activity 2:
if ((Integer)imageView.getTag()==R.drawable.clock1) {
alphabetViews = new Integer[]{R.layout.letter_blue, R.layout.letter_green, R.layout.letter_pink, R.layout.letter_red, R.layout.letter_yellow};
///***I wish to access the ImageViews which are in the above Layouts
}
if ((Integer)imageView.getTag()==R.drawable.clock2){
alphabetViews = new Integer[]{R.layout.letter_yellow, R.layout.letter_red, R.layout.letter_pink, R.layout.letter_green, R.layout.letter_blue};
}
///*** The (letter_blue.xml) details
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright">
<ImageView
android:id="#+id/img_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/clock1" />
</androidx.constraintlayout.widget.ConstraintLayout>
As you can see the ViewPager is working with Activites and it gets its values from an Integer and I can easily swipe it and there's no problem with it, just want to access the imageview which is in almost every layout.
is there a way to get the correct with and height of an image displayed in an viewpager?
I have tried using:
viewpager.getWidth()
viewpager.getHeight()
but these return the viewpager width/height + margine.
I have also tried using adapter.getView().getWidth() and adapter.getView().getHeight(), but they didn't work either.
Edit: Added my Adapter Class and my Layout.
My Adapter Class:
public class DetailViewPagerAdapter extends PagerAdapter {
private Fishton fishton;
private LayoutInflater inflater;
private Uri[] images;
private TouchImageView imageView;
public DetailViewPagerAdapter(LayoutInflater inflater, Uri[] images) {
this.inflater = inflater;
this.images = images;
fishton = Fishton.getInstance();
}
public TouchImageView getView() {
return imageView;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
View itemView = inflater.inflate(R.layout.detail_item, container, false);
container.addView(itemView);
imageView = itemView.findViewById(R.id.img_detail_image);
if (imageView != null
&& images[position] != null)
fishton
.imageAdapter
.loadDetailImage(imageView, images[position]);
return itemView;
}
#Override
public int getCount() {
return images.length;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (container instanceof ViewPager) {
container.removeView((ConstraintLayout) object);
}
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
detail_item.xml
<com.sangcomz.fishbun.util.TouchImageView
android:id="#+id/img_detail_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"/>
You're answering your own question. You want the width/height of the View inside the ViewPager. So you need the view.getWidth() and view.getHeight() inside the individual Fragment of your ViewPager
Edit:
Okay after you posted your code I think you want itemView.getWidth() and itemView.getHeight()
Also:
You can also achieve this by doing viewPager.getChildAt(0).getWidth() ...
I have 6 images downloaded as shown here, but the GridView in my gallery only displays 5 of those images.
I'm trying to copy how Instagram displays its gallery, with a selected image taking up 60% of the screen and the gallery images taking up the rest.
fragment_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relLayoutl">
<!--toolbar-->
<include layout="#layout/snippet_top_gallerybar"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_below="#+id/relLayoutl">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/galleryImageView"
android:scaleType="centerCrop"/>
<ProgressBar
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/progressBar"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40">
<GridView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="5"
android:verticalSpacing="1.5dp"
android:horizontalSpacing="1.5dp"
android:gravity="center"
android:layout_marginTop="1dp"
android:stretchMode="none"
android:id="#+id/gridView">
</GridView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
I created a square view to generate square cells
layout_grid_imageview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.example.sheldon.instagramclone.Util.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridViewImage"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:id="#+id/gridProgressBar"/>
</RelativeLayout>
GalleryFragment.java
public class GalleryFragment extends Fragment {
private static final int NUM_COLUMNS = 4;
private ImageView mExit;
private Spinner mSpinner;
private TextView mNext;
private ProgressBar mProgressBar;
private List<String> directories;
private GridView mGridView;
private ImageView mGalleryImage;
private HashMap<String, ArrayList<String>> directoryToImage;
private String append = "file:/";
private String mSelectedImage;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
mExit = (ImageView) view.findViewById(R.id.exitShare);
mSpinner = (Spinner) view.findViewById(R.id.shareSpinner);
mNext = (TextView) view.findViewById(R.id.shareNext);
mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
mGridView = (GridView) view.findViewById(R.id.gridView);
mGalleryImage = (ImageView) view.findViewById(R.id.galleryImageView);
mExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().finish();
}
});
mNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Navigating to next step in sharing photo");
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra("selected_image", mSelectedImage);
startActivity(intent);
}
});
init();
return view;
}
private void init() {
ImageFinder imageFinder = new ImageFinder();
imageFinder.getImages(getActivity());
directoryToImage = imageFinder.getImageMapping();
directories = new ArrayList<>(directoryToImage.keySet());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, directories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected: " + directories.get(position));
setUpGridView(directories.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void setUpGridView(String directory) {
final ArrayList<String> imgURLS = directoryToImage.get(directory);
Log.d(TAG, "setUpGridView: Displaying " + directory + " with " + imgURLS.size() + " images");
int gridWidth = getResources().getDisplayMetrics().widthPixels;
int imageWidth = gridWidth / NUM_COLUMNS;
Log.d(TAG, "setUpGridView: Image Width is " + imageWidth);
mGridView.setColumnWidth(imageWidth);
GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, append, imgURLS);
mGridView.setAdapter(adapter);
UniversalImageLoader.setImage(imgURLS.get(0),mGalleryImage, mProgressBar, append);
mSelectedImage = imgURLS.get(0);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UniversalImageLoader.setImage(imgURLS.get(position), mGalleryImage, mProgressBar, append);
mSelectedImage = imgURLS.get(0);
}
});}
I display the images using a library called Universal Image loader
GridImageAdapter.java
public class GridImageAdapter extends ArrayAdapter<String>{
private Context mContext;
private LayoutInflater mInflater;
private int layoutResource;
private String mAppend;
private ArrayList<String> imgURLs;
public GridImageAdapter(Context context, int layoutResource, String append, ArrayList<String> imgURLs) {
super(context, layoutResource, imgURLs);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
this.layoutResource = layoutResource;
mAppend = append;
this.imgURLs = imgURLs;
}
private static class ViewHolder{
SquareImageView image;
ProgressBar mProgressBar;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(layoutResource, parent, false);
holder = new ViewHolder();
holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.gridProgressBar);
holder.image = (SquareImageView) convertView.findViewById(R.id.gridViewImage);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
String imgURL = getItem(position);
Log.d(TAG, "getView: Loading position " + position + ", displaying " + imgURL + ", with image " + holder.image);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(mAppend + imgURL, holder.image, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.VISIBLE);
}
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
});
return convertView;
}
First time posting, so I apologize if there's anything wrong with this post.
Sorry for being so general and unclear in my post, I wasn't quite sure which portion of the code was the problem area. After looking over the code again, I noticed a stupid mistake. I set GridView's numColumns attribute to 5 in fragment_gallery.xml, but calculated the column width in GalleryFragment.java using private static final int NUM_COLUMNS = 4. I assume that this caused images to be displayed in a non-existent 5th column.
Iam trying display some data from web services in a Fragment,for this i need to use custom adapter because i am using pojo class to set and get data.
iam new to android and i know custom adapter to activity but i need for fragment.
My requirement is to display like the single item xml file singleitem.xml
<TextView
android:layout_width="match_parent"
android:id="#+id/txt1"
android:textSize="30sp"
android:gravity="center"
android:text="ine"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:id="#+id/txt2"
android:textSize="30sp"
android:gravity="center"
android:text="ine"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:id="#+id/txt3"
android:textSize="30sp"
android:gravity="center"
android:text="ine"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:id="#+id/txt4"
android:textSize="30sp"
android:gravity="center"
android:text="ine"
android:layout_height="wrap_content" />
which i want to display some data which contains 4 different fields in a listview row.
And i want custom adapter to setAdpater() to my Listview.
Is there any procedure to add custom adapter to Fragment or with in array adapter can i assign the values by using pojo class.Suggest me.
this is the custom adapter class iam using in Activity
can anyone suggest the possible changes for the adapter class
public class EnrollmentAdapter extends BaseAdapter {
MainActivity activity;
ArrayList<EnrollmentData> data;
LayoutInflater inflater;
public EnrollmentAdapter(MainActivity activity,ArrayList<EnrollmentData> data)
{
inflater = LayoutInflater.from(activity);
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.layout_enrollmentsingleitem,null);
TextView tv1 = (TextView)view.findViewById(R.id.txt1);
TextView tv2 = (TextView)view.findViewById(R.id.txt2);
TextView tv3 = (TextView)view.findViewById(R.id.txt3);
TextView tv4 = (TextView)view.findViewById(R.id.txt4);
tv1.setText(data.get(i).getReg_num());
tv2.setText(data.get(i).getW_list_on());
tv3.setText(data.get(i).getW_list_priority());
tv4.setText(data.get(i).getElements_to_complete());
return view;
}
I can solve my above question by using array adapter also meet my requirements
public class CourseCompleteAdapter extends ArrayAdapter<CourseCompletedInnerData> {
private Context context;
int layout;
ArrayList<CourseCompletedInnerData> data;
public CourseCompleteAdapter(Context context, int layout, ArrayList<CourseCompletedInnerData> data) {
super(context, layout,data);
this.data = data;
this.layout = layout;
this.context =context;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layout, parent, false);
}
TextView tvScore = (TextView)convertView.findViewById(R.id.txtcoursesinnerscore);
TextView tvActionstatus = (TextView)convertView.findViewById(R.id.txtcoursesinneractionstatus);
tvScore.setText(data.get(position).getScore());
tvActionstatus.setText(data.get(position).getAction_status());
return convertView;
}
}
As galley class deprecated so i try to substitute it with viewpager class , i already created my image viewpager and work nicely .
now im trying to add text description under each image , i googled alot about that with no result ,im try to use LayoutInflater with custom layout but end with nothing , it doesnt show the text .
note: this is the first time to use viewpager and try to customize it , so my code below in attempt to inflater the layout and customize it with viewpager, maybe wrong or need more adjustment ,coz of no experiance in that .
any advice will be appreciated to achieve that ,thanks.
My Code :
ImagePager:
public class ImagePager extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);}
private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f,
R.drawable.g, R.drawable.h, R.drawable.i,
R.drawable.j, R.drawable.k};}
ImagePagerAdapter:
public class ImagePagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ImagePagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act; }
public int getCount() {
return imageArray.length; }
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().
getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
layout.findViewById(R.id.caption);
ImageView view = new ImageView(activity);
view.setPadding(5, 25, 5, 5);
view.setScaleType(ScaleType.FIT_START);
view.setBackgroundColor(Color.RED);
((ViewPager) collection).addView(view, 0);
return view; }
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2); }
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1); }
#Override
public Parcelable saveState() {
return null; }
}
here
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
layout.findViewById(R.id.caption);
ImageView view = new ImageView(activity);
view.setPadding(5, 25, 5, 5);
view.setScaleType(ScaleType.FIT_START);
view.setBackgroundColor(Color.RED);
((ViewPager) collection).addView(view, 0);
return view; }
In cutome_pager design your View like this.
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/gallery_imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/gallery_imageView_dsc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello...." />
and
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
ImageView im=(ImageView) layout.findViewById(R.id.gallery_imageView);
im.setBackgroundColor(Color.RED);
TextView dsc=(TextView) layout.findViewById(R.id.gallery_imageView_dsc);
((ViewPager) collection).addView(layout, 0);
return layout; }
In the instantiateItem() method you creating a ImageView and adding it to view pager and returning it. In this method create one LinearLayout with vertical orientation and then create one ImageView and one TextView for the description, then add the ImageView and the TextView to LineaLayout and the add LinearLayout to view pager and also return the LinearLayout..