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..
Related
I'm trying to implement button on each row in ListView, but I saw many topics and I don't succeeded to add code to mine. Here is my MainActivity :
public class MainActivity extends AppCompatActivity {
private ArrayAdapter<String> itemsAdapter;
private ArrayList<String> items;
private ImageButton formButton;
private ListView lvMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
commonFunction();
}
public void commonFunction() {
lvMain = (ListView) findViewById(R.id.lvMain);
items = new ArrayList<String>();
readItems();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items);
lvMain.setAdapter(itemsAdapter);
formButton = (ImageButton) findViewById(R.id.btnPlus);
formButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLayoutActivity();
}
});
}
}
Here is my activity_main.xml :
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvMain"
android:layout_above="#+id/btnPlus" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="#+id/btnPlus"
android:layout_alignParentBottom="true"
app:srcCompat="#mipmap/ic_plus_foreground" />
Does someone any idea how to do ?
Create custom adapter with custom row layout file and add button on that row file and bind it to List/Recycler view. So it will inflate in all row.
Add below code in row_list.xml file.
<ImageButton
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="#+id/btnPlus"
android:layout_alignParentBottom="true"
app:srcCompat="#mipmap/ic_plus_foreground" />
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private ArrayList data;
private static LayoutInflater inflater = null;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(ArrayList d) {
/********** Take passed values **********/
data = d;
/*********** Layout inflater to call external xml layout () ***********/
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder {
public ImageButton button;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.row_list, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.button = (ImageView) vi.findViewById(R.id.btnPlus);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Button click
}
});
return vi;
}}
Hope it will solve problem.
Happy coding!!
Crate custom adapter and bind it with the ListView.
Inflate custom layout for each row in the adapter.
You can put your button in the custom layout file.
public class CustomAdapter extends ArrayAdapter<String> {
private Context context;
private int singleRowLayoutId;
public CustomAdapter(Context context, int singleRowLayoutId, String []titles, String []desc, int []images ) {
super(context, singleRowLayoutId,titles);
this.context=context;
this.singleRowLayoutId=singleRowLayoutId; //custom row layout for list view item
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View row=convertView;
CustomViewHolder holder=null;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//view object of single row of list view
row = inflater.inflate(singleRowLayoutId, parent, false);
//by using holder, we make sure that findViewById() is not called every time.
holder=new CustomViewHolder(row);
row.setTag(holder);
}
else
{
holder=(CustomViewHolder)row.getTag();
}
return row;
}
private class CustomViewHolder {
private ImageButton mbtn;
CustomViewHolder(View view)
{
mbtn=(ImageButton)view.findViewById(R.id.btn);
mbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle button click here
}
});
}
}
I think that this is what you need - create a separate layout for the rows in the list view. Then create a custom adapter, where you are adding this layout as row layout and then set this adapter to your list view. Here are the details with examples:
Android custom Row Item for ListView
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);
}
});
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);
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 have an activity with some images and I'm using swipe to load the next image. I need when I touch the image to show a button, for image saving. How can I do that? Here's my code:
public class Photo_gallery extends Activity{
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
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.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
.
.
.
R.drawable.p108
};
public int getCount() {
return mImages.length;
}
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
public Object instantiateItem(ViewGroup container, int position) {
Context context = Photo_gallery.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;
}
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
EDIT:
My XML code:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
If you just want it to show a button on the screen when you click the image, you can put a button in your layout with the parameter android:visibility="gone".
Then, when the user clicks the image (just put an OnClickListener() for the ImageView), call button.setVisibility(View.VISIBLE); to show the button. Then when the user performs any other action and you want to hide the button again, call button.setVisibility(View.GONE);