I want to simply add a new header-style line in Settings top-level. Not an icon, like the other entries have to the left of the section text, but a screen-width image. This would be implemented elsewhere, but for now, I just want to be able to have an image be shown at all
This does not seem to be easily done, and I have not found a complete guide for something this simple anywhere.
For this I will need both the XML edits, AND the matching java counterpart.
Here is my current Java part:
setImageDrawable(R.id.logo);
And this is my XML side:
<ImageView
android:id="#+id/logo"
android:src="#drawable/romlogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
My image is currently in /res/drawable-xhdpi/, and is named "romlogo.png" (461 x 113)
As it sits, it doesn't even compile. Not sure if I'm missing an import or what.
You have to parse a Drawable to the setImageDrawable(...) method. However, the XML code looks good. Do not set the image programmatically, it is already there stated in the property android:src="#drawable/romlogo". If you want to set it with code use something like this:
// fetch the imageview and afterwards set the drawable
// here consider the difference between R.id and R.drawable
ImageView iv = (ImageView) findViewById(R.id.logo);
iv.setImageDrawable(R.drawable.romlogo);
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000"
android:gravity="center" >
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:paddingTop="0.1dp"
android:paddingLeft="0.1dp"
android:paddingRight="0,1dp"
android:scaleType="fitXY"
android:id="#+id/iv"
android:src="#drawable/organic"
android:layout_weight="30"/>
</RelativeLayout>
This will be the XML code for the appropriate output.Image as an header and of screen width
Related
So I am trying to import a custom image as the background of an android app however it does not fill in all of the edges. The option I have seen is to set a second layout to be a frame layout and then place an image view within it with additional coding along the lines of android:scaleType="centerCrop". This will not fill the entire screen up for me however, heres a screenshot of what ends up happening -
Heres the xml code being used:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/backgroundImage"
android:background="#drawable/backgroundImgr"
android:scaleType="centerCrop"
/>
</FrameLayout>
Another issue is that different users use different resolutions so the background image has to be able to accommodate them all without being cut off. I'm not sure how to do this.
Set ScaleType fitXY and add android:adjustViewBounds ="true"
Please use android:src="#drawable/ instead background .I hope it will helps you.
I have the following XML:
<ImageButton
android:id="#+id/SwapTextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:background="#drawable/my_gradient_button"
android:scaleType="fitCenter"
android:src="#drawable/extended_text" />
And it outputs a button which looks like this:
Is there any way to increase the size which the src resource takes up? By that I mean maintain the src within the area of the button, but make it larger than it is now.
I have tried changing the scaleType, but any other setting causes the src resource to display parts of itself outside the button boundary (aka it becomes too large).
I've had a research and there seems to be solutions based in java, but ideally I'd like to keep the solution in xml too.
Can someone suggest a fix/point me in the right direction?
Thanks
<ImageButton
android:id="#+id/SwapTextButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="0.5"
android:background="#drawable/my_gradient_button"
android:scaleType="fitXY"
android:src="#drawable/extended_text" />
Try this, fitXY for scaletype should "fill" your ImageButton
This worked for me:
android:padding="0dp";
Problem Description:
I have an ImageView which changes in size due to changing heights of different devices.
Aim:
I am looking to keep the bottom edge of the drawable in line with the bottom of the image view and crop the rest from the top.
What I've Done:
I've tried using centerCrop but it only crops from top and bottom:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/llHomeScreenButtonBar"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_home" />
I've also tried copying the Extended ImageView class from: Chris Arriola
However, I've encountered an error from eclipse saying:
Custom view CustomImageView is not using the 2- or 3-argument View constructors; XML attributes will not work
The XML code fragment looks like this:
<com.misc.CustomImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/llHomeScreenButtonBar"
android:layout_alignParentTop="true"
android:src="#drawable/bg_home" />
What's the best way to achieve this?
Thanks!
Implement the 2- and 3-argument constructors.
The one-argument constructor public CustomImageView(Context) can't be used by the XML inflation process.. XML inflation uses one of the other two constructors, with one or two more arguments.
see https://stackoverflow.com/a/9195858/357951
In my application, I want to set bubbles to a text view, in the text view I add the setBackgroundResource() as you can see in the code.
With this code i'm getting an image like this:
I want a bubble shape image like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#EDF5E4" />
<corners android:bottomLeftRadius="#dimen/corner_radius"
android:bottomRightRadius="#dimen/corner_radius"
android:topLeftRadius="#dimen/corner_radius"
id:topRightRadius="#dimen/corner_radius" />
Please tell me how to make this in my setBackgroundResource() XML.
What you need to use is essentially a 9-patch image (As already pointed out by Ken Wolf in this comment).
To get you started, I am including a set of 9-patch images from one of my apps along with a brief piece of code on how to use it when creating a layout XMl. ;-)
The 9-patch Image Set:
(These are named: bubble_white_normal_mdpi.9, bubble_white_normal_hdpi.9 and bubble_white_normal_xhdpi.9 respectively. Remove the _mdpi, _hdpi and _xhdpi from the file names after placing them in their respective drawable folders.
The XML:
<LinearLayout
android:id="#+id/linlaUserOther"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="2dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="top|center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgvwProfileOther"
android:layout_width="42dp"
android:layout_height="42dp"
android:adjustViewBounds="true"
android:contentDescription="#string/content_desc_user_profile"
android:scaleType="centerCrop"
android:src="#drawable/ic_contact_picture" >
</ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/bubble_white_normal"
android:gravity="top|center"
android:orientation="vertical" >
.... // OTHER STUFF HERE THAT IS NOT NECESSARY IN THIS CODE SNIPPET ON SO
</LinearLayout>
</LinearLayout>
NOTE 1:
Although, I am including a working set of Images (almost spoon feeding, if you will), I would strongly urge you to create your own set of images that fit in your scheme of things. Plus, this will also equip you to build your own resources in the future. The only reason I am going the extra mile is because I personally lost 3 days getting the speech bubble looking and working right. :-(
NOTE 2:
I am setting the image as a background to a LinearLayout. You, however, will need to set it to the TextView you need looking like a Speech Bubble.
Additional Sites (Tutorials):
http://blog.booleanbites.com/2012/12/android-listview-with-speech-bubble.html
https://github.com/AdilSoomro/Android-Speech-Bubble
http://developer.android.com/reference/android/graphics/NinePatch.html
http://developer.android.com/tools/help/draw9patch.html
I think you are going to have a hard time trying to do it using just shape drawables.
I would use a 9-patch png.
http://developer.android.com/reference/android/graphics/NinePatch.html
Basically you either find/buy an image or create one in your favourite drawing program. Then you define the stretchable regions using the draw9patch tool which allow it to scale properly in your View.
Here is a tutorial, it's even specific to speech bubbles!
http://adilsoomro.blogspot.co.uk/2012/11/android-how-to-use-9-patch-png.html
It takes a bit of time but it is a crucial technique in making more designed visual interfaces.
Can you give some projects how to insert an image like that on android?I want to add that image in my xml.
<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" >
</RelativeLayout>
How can I display it my image?
Use an ImageView. It takes all the standard formats.
You can use an ImageView:
<ImageView
android:id="#+id/logo_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/logo" />
and place your image file into res/drawable/logo.gif (or in a resolution specific drawable directory)
If you want to display moving GIF then there is not direct aproach, You have to try a way around, Refer bellow link for more details.
REF GIF in Android
OR you can load the image in a web View and display it. ( web view should be able to display the animation )