Images in Android ActionBar Spinner are Large - android

I have an image spinner in my Android action bar, but when I click it, the images are not scaled down. This is a screenshot of my application:
Here is my spinner layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside" >
</ImageView>
</LinearLayout>
This is the item declaration in menu.xml:
<item
android:id="#+id/menu_spinner"
android:actionViewClass="android.widget.Spinner"
android:showAsAction="ifRoom"
android:title="Share"/>

Use wrap_content instead of match_parent for ImageView in both android:layout_width and android:layout_height as below...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside" >
</ImageView>
</LinearLayout>

Maybe someone could give an comprehensive answer, but you need to use resource filtering for screen dpi.
E.g "drawable-mdpi", "drawable-hdpi"
Just place appropriate(scaled) sizes inside folders
Edit:
Alright i tried similar layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff920f">
<ImageView
android:id="#+id/main_spinner_item_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/abc_ic_cab_done_holo_light"
android:scaleType="fitCenter"/>
</FrameLayout>
in a sample project and everything worked fine. As i mentioned before you need to scale your icon for screen dpi. There is a folder - size list below
"drawable-mdpi", 32x32
"drawable-hdpi", 48x48
"drawable-xhdpi", 72x72
"drawable-xxhdpi", 96x96
For more sample, see Action Bar Icon Pack here

Related

Android: Center splash screen image

I have an app with a splash screen and the splash screen looks good on small devices but looks screwed up in a big tablet(emulator). so I changed the background to wrap_content. but it looks aligned to the side of the screen, Can some one tell me a way to center background images? here is my splash.xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/splash">
</LinearLayout>
I am using a background image for my activity. First create a theme in your styles.xml:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
Then create a new drawable with the name background_splash.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorWhite"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/logo_splash"/>
</item>
</layer-list>
Then set your activity to use this theme in your AndroidManifest.xml:
<activity android:name="SplashActivity" android:theme="#style/SplashTheme" >
This worked for me.
You don't actually need neither LinearLayout nor RelativeLayout here, use a more lightweight FrameLayout instead.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/splash"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
Slight change in code as it requires adding a second element for best fit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2A7800"<---- optional background color to fit the image. to make it blend in
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"<--- centers
android:adjustViewBounds="true"
android:scaleType="fitXY"<--- makes sure the image fits into the layout my matching the screen size.
android:src="#drawable/splash" />
</LinearLayout>
Adding an ImageView is best practice as it is better performance wise than using android:background and setting an image. I discovered this with a ScrollView and the master parent had android:background set to an image. It slowed down the ScrollView extremly.
Additionally, there is no need for a specific layout with this method. You can use RelativeLayout, FrameLayout, LinearLayout - anything that works. The exception might be ListView, GridView
You can use relative layout, try the below code..
<?xml version="1.0" encoding="utf-8"?>
<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:background="#android:color/white">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
Please try out the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash">
</LinearLayout>

DensityPixel doesn't seem to work with ImageButton

For a game I have six buttons which look good on mdpi. The 4 left buttons are 100x100px:
If I switch to hdpi, it looks like this:
The xml looks like this (I omitted all but one button to keep it simple):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:orientation="vertical" >
<com.mydomain.mygame.game.GameSurface
android:id="#+id/gameSurface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_game" >
</com.mydomain.mygame.game.GameSurface>
<RelativeLayout
android:id="#+id/controlButtonContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button_left"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:contentDescription="#string/button_left"
android:background="#drawable/control_button" />
</RelativeLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/emptyBar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
These attempts didn't work:
I tried to use 100dp instead of wrap_content. I tried src instead of background as well as I tried different scaleType attributes.
Probably I could calculate the scaling factor in code, but I can't imagine that this is the proper way to do that. Why does it not scaled automatically, because what are dp then for? So how can I configure the right ratio in xml?
Please avoid use of specific size like 100dp . Try to use WRAP_CONTENT or MATCH_PARENT . and for different different screen sizes please use drawable-folders like drawable-hdpi and drawable-mdpi etc . And Put corrosponding images of android:background="#drawable/control_button" in all its specific folders .
Refer these linkes for multiple screen sizes :
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html

Image doesn't scale properly in Android.

I'm using a xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/splashscreen"
android:tileMode="disabled" android:gravity="top" >
</bitmap>
this is my imageView in the layout:
<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">
<ImageView
android:id="#+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
</RelativeLayout>
but no matter what I do, it doesn't fit the screen, and only appears in the centre as a small image. Where am I going wrong?
Maybe not the best solution since different devices has different resolutions, but "wrap_content" will display the image in its original resolution.
<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"
>
<ImageView
android:id="#+id/splash_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/splashscreen"
/>
</RelativeLayout>
Try using fill_parent
Each device have different screen size so you need to make different splash image for each size and resolution i guess the problem is you are using image of small size(height width) and the display is big
so you need to scale image to fit to display size, this might pixelate the image

Centering an ImageView in FullScreen mode

I am simply trying to center an ImageView in a FullScreen app. I've read several posts on this site on people asking what seems like same question, and I've tried everything I've read, but I can't seem to get it to center.
Here is my code for the layout. I realize there might be some overkill here to get the centering, as a result of trying various other posts. This code puts the image on the top left part of the screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="#000000"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="false"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:background="#android:color/black"
android:contentDescription="#string/descImage"
android:cropToPadding="false"
android:gravity="center"
android:src="#drawable/placeholder" />
Here is my full screen code in AndroidManifest.xml.
android:screenOrientation="landscape"
android:theme="#style/Theme.FullScreen">
Theme.FullScreen is defined as
<style name="Theme.FullScreen" parent="#android:style/Theme.Holo">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">true</item>
</style>
Thanks.
Try this. It works find on my galaxy nexus w/ 4.2 (I just changed width & height of image view as wrap_content and remove unnecessary & meaningless attributes.)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:background="#android:color/black"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
and for theme...
<style name="Theme.FullScreen" parent="android:Theme.Holo.NoActionBar.Fullscreen"/>
That seems very excessive for what you're trying to do. Try this?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/descImage"
android:src="#drawable/placeholder" />
</FrameLayout>
If you use fill_parent for the size, the entire view will not be centered in the parent, because it is the size of the parent.
If you change the size to wrap_content, the bounding box of the view will change, and that will be centered inside of the parent.
You could also use the android:scaleType property for the imageView specifying how the image resource itself should be layed out inside of the view.
The default scaleType will cause the resource image to appear in the top left corner of the ImageView, if the bouding box is larger then the actual image itself.
You can also use the android:adjustViewBounds property to have the OS automatically resize the bounding box for you, based on the image resource.
Use scaleType="fitCenter" on the ImageView.
fill_parent has been replaced with match_parent in later Android versions and I've using an SVG drawable resource, so I had to modify the answers above slightly.
This is what worked for me:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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"
tools:context="com.example.MyActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
app:srcCompat="#drawable/my_drawable" />
</android.widget.RelativeLayout>

Improper Header Placement in Layout

I have a large header image that I'd like to use for both tablet devices and phones. The image looks fine on a tablet but when viewed on a phone it looks like this:
Here is my 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="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/searchresults_header" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
How can I get this header image to scale correctly on both screen sizes?
Your image is getting resized automatically, but the viewbounds stay as they were originally.
Add this to your ImageView and you should see the difference:
android:adjustViewBounds="true"
Can't remember which one but try these scaleType param values

Categories

Resources