Don't stretch ViewPager background - android

I am trying to set a background image to my ViewPager (android.support.v4.view.ViewPager). It can be done via android:background="#drawable/bg" - however, it stretches the image.
For ImageView's I can set scaleType="centerCrop" to prevent this but this doesn't seem to work for PagerView. How can I prevent the background from stretching? I've tried adding an ImageView as a subview of my ViewPager but that doesn't seem to work either.
Thanks in advance!

Sorry, there are no 'centerCrop' option for background drawable. How I do it is set your use FrameLayout having the back contain the ImageView using centerCrop, then the front(top) will be your content with transparent background.

Related

How to handle RecyclerView items of different sizes

I have a recyclerView with an adapter inflated inside a cardview. Each item in my list has a different size, because of different image sizes. I am facing visibility problem as cards get recycled, in my cardviews as shown:
How can I fix this problem?
[EDIT] My CardView layout: https://drive.google.com/file/d/1zdM5H54CtfxZBxk6x72uYnSbCzxCnT-_/view?usp=sharing
In your <ImageView> you need to add scaletype property for it. Add it like below
<ImageView
.......
android:scaleType="CENTER_INSIDE" >
It will change the ratio of your image but full image is visible. fitxy also visible whole image as well. Choose more suitable one according to your needs
You can see a good example here. https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide find out what scale y=type you want and add it.
As Lucifer said:
In your you need to add scaletype property for it
If you don't like center inside you can use CENTER_CROP or try anyone of the options.

How to insert an image in the linear layout?

I just wanted to insert an image in linear layout similar to this
Can anyone help me with this???
You are probably looking for ImageButton since your picture shows something similar to that.
Alternatively, take a look at ImageView
set layout's orientation to horizontal, then add 4 images in xml, one after another, add ids like image1, image2 etc, then call them in your onCreate like ImageView image
ImageView image1 = (ImageView) findViewById(R.id.image1)
and so on, then you can either set "src" attribute in yoru xml or say image1.setImageBitmap or any other method available in ImageView class to set the image you want from your res/drawable/
you can use ImageButton in horizontal LinearLayout
make background of the ImageButton #null
then put the margins between them

Scrollview stretches background Android

I have read many question on this topic but found no answer.
the problem is that I have a ScrollView as mainView in my xml and if I set a background it is stretched.
ScrollView(background)-->content
I also tried:
Scrollview-->content(background)
To solve the issue I need to wrap the scrollView inside a LinearLayout.
LinearLayout(background)-->ScrolView-->content
Now the background (if applied to the mainLinearLayout) is no more stretched but I have the warning:
This ScrollView layout or its LinearLayout parent is possibly useless; transfer the background attribute to the other
view
I'd like to have the background not stretched and also get rid of this warning.
Probably I've not fully understood how this Views work...
Thanks for your help
Not a beautiful answer. But it should work.
You can set the background activity in styles xml file.
Remove useless LinearLayout.
Set the background of scrollview to transparent.
in your first case, use android:layout_height="wrap_content" for the scrollview and it won't stretch the background picture anymore

how to make the change the background when ListView is Scroll up

i am trying to implement ListView. Problem is that when i am showing items of list less that the scree size the remaining is showing black. I want to set any image so that when items are less than screen size then background should be a picture
put your <ListView /> inside <LinearLayout/> ,
add background to Linear layout, with LL Height as match_parent and hieght of Listview as wrap_content
I have not done this myself, but this is what I think should work.
Place the image in /res/drawable
Then add these lines to activity in manifest or to parent layout of ListView
android:scaleType="fitCenter"
android:background="#drawable/image_name"
Let me know how that goes.

My spinner isn't visible while using an imageview

I have a TabView in android. The first tab has an Image and a Spinner. The Spinner is a simple list that shows up in the first tab if there is no image, but disappears when the Image is there.
Update:
I've figured out that the imageView is 'pushing' the spinner out of the frame. I just needed to make my image a "background view"
Thanks for the help
keep the spinner inside a Layout (ex. FrameLayout) and set the image as background for that Layout.
It seems that you are using
LinearLayout
as base Layout which hosts ImageView and Spinner.
the default orientation of LinearLayout is horizontal..
change it to vertical just by adding
android:orientation="vertical"
to <LinearLayout > tag

Categories

Resources