ImageView in android not filling whole page using ViewPager - android

I am facing problem while loading an Image in ViewPager. The image gets loaded with 5px gap at right and left side. Here is my code:
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(PoemActivity.mPoems[position]);
container.addView(imageView);
return imageView;
}
Here is my xml file as asked by Hari.
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Related

Problem to create ViewPager programmatically

I'm having the problem that I can not add a ViewPager (
Images Carousel) programmatically in my android app using Android Studio. So I built a simpler schema than my production app for testing, except that in this test I discovered that it only works at certain times.
Let's go to the code and explain what I wanted and what's going on.
ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter{
Context context;
List<Bitmap> images;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, List<Bitmap> images) {
this.context = context;
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item,container,false);
ImageView image;
image = (ImageView)itemView.findViewById(R.id.imageView);
DisplayMetrics dis = new DisplayMetrics();
image.setImageBitmap(images.get(position));
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View)object);
}
}
viewpager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Linear.java
public class Linear extends AppCompatActivity {
ViewPager viewPager;
ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linerar_activity);
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.scrollView);
scrollView.setFillViewport (true);
ImageView imageView;
List<Bitmap> bitmapList = new ArrayList<>();
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play);
bitmapList.add(bitmap);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play2);
bitmapList.add(bitmap);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play3);
bitmapList.add(bitmap);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play4);
bitmapList.add(bitmap);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.play5);
bitmapList.add(bitmap);
LinearLayout ln = findViewById(R.id.ln);
TextView tv = new TextView(Linear.this);
tv.setText("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n" +
"BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla");
ln.addView(tv);
viewPager = new ViewPager(Linear.this);
adapter = new ViewPagerAdapter(Linear.this,bitmapList);
viewPager.setAdapter(adapter);
imageView = new ImageView(Linear.this);
imageView.setImageResource(R.drawable.play);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//ln.addView(imageView);
imageView = new ImageView(Linear.this);
imageView.setImageResource(R.drawable.play);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//ln.addView(imageView);
ln.addView(viewPager);
//ln.addView(tv);
//ln.addView(imageView);
}
}
linerar_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
So what he really wanted to happen was:
Regardless of where it happened ln.addView (imageView) or ln.addView (tv) or ln.addView (viewPager) appeared in the order that was instantiated in LinearLayout.
What actually occurs:
The TextView and ImageView they can be instantiated as many times as they want, yet they will appear in the order in the scrollView Example:
ln.addView (imageView);
ln.addView (tv);
ln.addView (imageView);
ln.addView (tv);
the end product of this scrollView will be an image, a text, an image and finally a text.
The problem is when I give an ln.addView (viewPager) because it only appears when the scrollView content does not exceed the height of the device, Example:
1 - Only an ln.addView (viewPager): the viewPager will fill the entire screen of the mobile phone without the need to use the scrollView.
2 - Inserting some content + ln.addView (viewPager): in this example I'm using a textView that occupies 50% of the screen and the other 50% is occupied by viewPager, if the textView occupies 80% of the screen the viewPager would occupy 20 % of screen, if the screen was occupied 100% the scrollView would start working and the viewPager regardless of where it was instantiated it would not appear anymore.
Summarizing the problem:
That is, when the scrollView starts to work the viewPager will not work anymore.
Does anyone have any ideas for helping me?

how do I add margin at the end of a ViewPager?

I'm dealing with legacy code that uses a viewpager to display an horizontal list of images(instead of say a listview or recyclerview) in a fragment. The viewpager uses a custom PagerAdapter which overrides the appropriate methods(instantiateItem, getItemPositiion etc). My issue right now is that I want to add some space at the end of the final element of the viewpager, I've attempted to do this by adding a margin to this final elment.
public Object instantiateItem(ViewGroup collection, int position) {
FrameLayout itemView = ViewManager.createView(position, height, type);
int viewWidth = itemView.getLayoutParams().width;
RelativeLayout itemViewWrap = new RelativeLayout(mContext);
if(postion == items.size()-1){
RelativeLayout.LayoutParams itemViewparams = RelaiveLayout.LayoutParams)itemView.getLayoutParams();
itemViewparams.setMargins(0, 0, viewWidth, 0);
}
itemViewWrap.addView(itemView);
collection.addView(itemViewWrap);
return itemView;
}
Unfortunately this does not work and the final element is just not shown. How do I properly achieve this? Here is the xml for the fragment in which this viewpager resides.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.playground.test.views.ItemViewPager
android:id="#+id/viewpager"
android:layout_gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</RelativeLayout>
You could override the last page's width using ViewPagerAdapter# getPageWidth. To be more than the rest of the page's with the margin you want.
https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#getPageWidth(int)

Image Slideshow using ViewPager

I want to implement the following screen :
In the screen shot you can see that below MyAdvisor TextView i have an image.On swiping this image different image will be displayed .To create Swipe Gallery i am using view pager here.I am using an Adapter which is providing images to display through view pager.
Adapter:
public class ImageAdapter extends PagerAdapter {
private Context context;
private int[] images = new int[]{R.drawable.imgone,
R.drawable.img2,
R.drawable.img3};
public ImageAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return images.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.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(images[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Below is the xml file of demo project of mine which contains two text view and a view pager.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_above="#+id/txt"
android:layout_marginTop="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:text="Hello2"
android:layout_above="#+id/pager"
android:layout_marginTop="15sp"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height = "wrap_content"
></android.support.v4.view.ViewPager>
My problem is that the view pager is having height as match_parent.Even after changing it as wrap_content ,the other views are not displayed .Please guide me how can i implement this screen.
I suggest you to use Heterogenous Layouts inside RecyclerView for that approach.If you're not familiar with the concept, go thru this tutorial.And for the viewpager part, there is this awesome library which comes up with so many built in features that you will absolutely love.Just inflate the Sliderlayout of this library inside onCreateViewHolder() of adapter and provide corresponding data.No need to write your own custom pageradapter and handle events.You just have to know how to use Recyclerview and you're pretty much done.

Transparent image(gif) has black background in ViewPager

I have fragment which has ViewPager:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="350dp"
/>
Adapter:
public class ImageAdapter extends PagerAdapter
...
#Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
Ion.with(context)
.load(path)
.withBitmap()
.intoImageView(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
...
}
If image is transparent, it has black background.
Set white bg for fragment root view or activity root view did not help.
I think transparent gifs are not supported on android as per this link
https://code.google.com/p/android/issues/detail?id=62016
so it is better to use png

ViewPager of views (not fragments) as header of a gridview (Etsy's StaggeredGridView) in a fragment

So, I'm in a situation where I want to add a ViewPager of views (not fragments) as the header of a gridview in a fragment, but my ViewPager doesn't appear when I set it as my headerview. I'm currently using Etsy's StaggeredGridView to allow my gridview to have a header, and I'm using a PagerAdapter instead of FragmentStatePagerAdapter to be able to scroll through views instead of fragments. I was going to just use fragments, but I wasn't sure if this would be good since, I assume, that would be considered as having nested fragments, which are only supported in 4.2+ and through the support library: my app supports 4.0 and above, and at the moment, I'm using native fragments.
I also tried setting a view programatically, but still nothing shows.
I set a toast in the ViewPager to see if anything is actually getting created, and the toast shows up as expected, but still no header. The view that I'm attempting to inflate shouldn't be the problem because if I use it as a header on its own it shows. Here's what I have so far:
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate fragment which contains the gridview. Have a pointer to the gridview.
View v = inflater.inflate(R.layout.fragment_menu, container, false);
mGridView = (StaggeredGridView) v.findViewById(R.id.dishesGridView);
View header = inflater.inflate(R.layout.view_pager_header, container, false);
ViewPager mPager = (ViewPager) header.findViewById(R.id.pager);
//list of views to test header
final List<View> views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.menu_header, null));
mPager.setAdapter(new PagerAdapter() {
public View getView(int position) {
return views.get(position);
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = getView(position);
((ViewPager)collection).addView(v);
Toast.makeText(getActivity(),
"Hello ViewPager",
Toast.LENGTH_SHORT).show();
return v;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == (View) arg1;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return views.size();
}
});
mGridView.addHeaderView(mPager);
return v;
}
Here's the view_pager_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Here's the menu_header.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" >
<FrameLayout
android:id="#+id/resHeader"
android:layout_width="wrap_content"
android:layout_height="100dp" >
<com.platey.IonPlatey.RectangleImageView
android:id="#+id/restaurant_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<ImageView
android:src="#drawable/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<TextView
android:id="#+id/resType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="15dp"
android:text="ResType Here"
android:textColor="#d82727"
android:textStyle="bold" />
</RelativeLayout>
Reason:
Basically the issue is caused by ViewPager android:layout_height="match_parent".
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The match_parent is wrong here for sure, because you don't want the header to fill entire parent. But regardless of that - match_parent will not work here in StaggeredGridView.
This is the same as in standard ListView items etc. - match_parent is not allowed and it will be replaced with just wrap_content while performing layout.
Unfortunately wrap_content is not supported by ViewPager because ViewPager cannot wrap it's content (it is not aware of children size).
Solution:
You have two ways to properly display a ViewPager there:
Just specify a static height (for example 50dp)
Extend your ViewPager and perform your own measuring if you don't want static height.
Screenshots
Here is also a screenshots with match_parent. You can see that ViewPager is nor displayed (has zero height):
and with static height:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="50dp" />

Categories

Resources