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() ...
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.
hi i have a listview that shows a list of installed applications on the device it contains icons and titles and a SwitchCompact i need to get the id of the SwitchCompact of each selected row to execute the code needed
<ImageView
android:id="#+id/app_icon"
android:layout_width="50dp"
android:layout_height="70dp"
android:gravity="center"
android:layout_marginTop="2dp"
android:layout_marginLeft="25dp"
android:contentDescription="#null"
android:scaleType="center"/>
<TextView
android:id="#+id/list_app_name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignTop="#+id/app_icon"
android:layout_marginTop="8dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_weight="85.6"
android:gravity="left"
android:paddingRight="10dp"
android:text="test1"
android:textColor="#color/colorPrimary"
android:textSize="20dp"
android:textStyle="bold" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginTop="10dp"
android:padding="15dp"
android:thumb="#drawable/swithc_thumb"
app:track="#drawable/switch_track"
android:id="#+id/Swtch"/>
i ve tried the usual way to setOnCheckedChangeListener but it returns null the default id of the switchcompact is not working for me in this case.
here is my adapter class
public class AppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> appInList;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
appInList = customizedListView;
}
#Override
public int getCount() {
return appInList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.app_item_layout, parent, false);
listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icon);
convertView.setTag(listViewHolder);
}
else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.appNameText.setText(appInList.get(position).getName());
listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());
return convertView;
}
}
static class ViewHolder{
TextView appNameText;
ImageView appIcon;
}
public class AppList {
private String name;
Drawable icon;
public AppList(String name, Drawable icon) {
this.name = name;
this.icon = icon;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
}
I just added switchCompat to your viewHolder so you can use your switchCompat
public class AppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> appInList;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
appInList = customizedListView;
}
#Override
public int getCount() {
return appInList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.app_item_layout, parent, false);
listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icon);
listViewHolder.appSwitch = (SwitchCompat) convertView.findViewById(R.id.Swtch); //added switchCompat here.
convertView.setTag(listViewHolder);
}
else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.appNameText.setText(appInList.get(position).getName());
listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());
/* you can now use your appSwitch here.
listViewHolder.appSwitch
*/
return convertView;
}
}
static class ViewHolder {
TextView appNameText;
ImageView appIcon;
SwitchCompat appSwitch; // added switch compat to your viewHolder.
}
I'm trying to get the image in the chat box when I click on any of the images from the gridview however, whenever I try to do so it comes up with a text, so please advise what can be done to get the image itself instead of the text that is appearing, please help me out I've been stuck for too long
smiles_items_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/smile_image_view"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"/>
</LinearLayout>
BottomSheetDialog_Smiles.java
public class ImageAdapter2 extends BaseAdapter {
private Context mContext;
private final int[] mThumbIds;
String[] mThumbIdsString = null;
public ImageAdapter2(Context c, int[] mThumbIds , String[] mThumbIdsString)
{
mContext = c;
this.mThumbIds = mThumbIds;
this.mThumbIdsString = mThumbIdsString;
}
#Override
public int getCount() {
return mThumbIds.length & mThumbIdsString.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView img;
}
#Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smiles_items_layout, null);
null);
holder.img = (ImageView) grid.findViewById(R.id.smile_image_view);
holder.img.setImageResource(mThumbIds[position]);
gridView2.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
Drawable drawable;
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int i, long l) {
JSONDictionary imageChat = new JSONDictionary();
LinearLayout layout= (LinearLayout)view;
ImageView imageView = (ImageView) layout.getChildAt(0);
imageView.getDrawable();
imageChat.put("message", imageView);
Communicator.getInstance().emit("new chat message",
imageChat);
}
});
return grid;
}
}
This is the result which appears when clicking on the image
Do you need an ImageView inside an image?
I used this code to get an image.
Send your view this method get as image.
protected Bitmap ConvertToBitmap(ImageView image_view) {
Bitmap map;
image_view.setDrawingCacheEnabled(true);
image_view.buildDrawingCache();
return map=image_view.getDrawingCache();
}
In ur adapter code
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = ConvertToBitmap(holder.img);;
//now get bitmap used to assign
imageChat.put("message", b);
Communicator.getInstance().emit("new chat message",
imageChat);
}
});
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;
}
}
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);