Android GradientDrawable setColor does not work - java

I have an annoying problem setting a color to a layout in Android;
This is how it looks my element in layout.xml (FrameLayout (painted element) in a LinearLayout ):
<LinearLayout
android:id="#+id/farPD_mid_linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<!-- top margin layout-->
<FrameLayout
android:id="#+id/farPD_top_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/white">
</FrameLayout>
I want to paint the FrameLayout in white and I do something like this in Java:
GradientDrawable gd3 = new GradientDrawable();
gd3.setColor(Color.parseColor("#ffffff"));
FrameLayout topBorder = (FrameLayout) findViewById(R.id.farPD_top_margin);
topBorder.setBackground(gd3);
I also tried with setColors(). Alpha is 255. The problem is that it doesn't paint in WHITE! it paints in some sort of grey :|.(*and nor it's the color set in layout). Plus, if i comment the Java code, and let only the color set in the layout, which is also white, I get the same output: FrameLayout painted in GREY!
Can someone help me please? Thank you!

You can also set gradient background using xml
gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#color/selected_blue_color"
android:endColor="#color/default_blue_color"
android:angle="270" />
</shape>
</item>
</selector>
layout.xml
LinearLayout
android:id="#+id/farPD_mid_linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<!-- top margin layout-->
<FrameLayout
android:id="#+id/farPD_top_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/gradient_bg">
</FrameLayout>
</LinearLayout>

Not 100% related to this question, but I thought I'd post what I found out for anybody out there still struggling. I was just having an issue with the setColors function of GradientDrawable and I stumbled upon this question hoping to find help. As you said, it wouldn't take the colors that I was giving it and would choose something intermediate. In my scenario, I have a list of Cards in a RecyclerView, and I'm assigning a different gradient color to the background of each. But it always seems as if the color of one Card would propagate to the other Cards for some reason.
Solution
After struggling with it for a bit, I ended taking a look at the documentation (which I probably should have done first) and found out you may need to call .mutate() on any instance of GradientDrawable before making any changes to it. That's because, unless you specify that the instance is mutable, it may be linked to other existing GradientDrawable instances and the changes made to one may propagate to the others.

Related

Android Programming crop background image

I want to display an image as background in my app. Now I used this statement: android:background="#drawable/background" in <LineraLayout>. Here is the .xml Code:
<LinearLayout 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:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/background">
</LinearLayout>
Now I get this output:
But I want it like this:
Maybe someone of you can help me. Thanks.
There is BitmapDrawable, which allows to do it. First you need to create a drawable resource as follows (let it be a file in the project resources res/drawable/mybg.xml ):
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:gravity="center" />
And then specify it as a background resource in your layout:
<LinearLayout 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:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/mybg">
</LinearLayout>
You need to change the image's size itself in photoshop or something to achieve the desired result (still this can be very difficult because of various screen sizes of android smartphones). A workaround can be to change your parent layout to Relativelayout and then use a an imageview to show your picture like this:
<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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent" //make sure its match parent
android:layout_height="match_parent"// and this one also
android:src="#drawable/yourBackgroundImage"
android:scaleType="..."/>
<LinearLayout>put your childviews here</LinearLayout>
there are 4-5 options available for android:scaleType... experiment with them, till you get your desired result. Also note that you need to use android:src rather than android:background attribute of the imageview
You can accomplish what you're trying to do by using BitmapRegionDecoder.
From the docs:
BitmapRegionDecoder can be used to decode a rectangle region from an
image. BitmapRegionDecoder is particularly useful when an original
image is large and you only need parts of the image.
It allows you to decode a Rect area of a particular Bitmap fairly easily, the only thing you need to do is calculate your Rect region to decode relative to the Bitmap.
In android the position of an ImageView is determined by the top left side of the image. I am assuming that the x direction is 1/3rd of the width from the start point. Now we can set the pivot. Basically the location of the pivot point, is a location around which the view will rotate and scale.
int displacementX = ImageView.getWidth() / 3;
int displacementY = 10;
imgview.setPivotX((float)displacementX);
imgview.setPivotY((float)displacementY);
Now that we have set the pivot we can use a scale type to fit the image.
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);

Android game: remove padding either side of screen

I am relatively new to Android and am in the process of making a pretty basic game. I want to know how to remove the padding at either side of the main game screen, so that the game will take up the whole screen of whatever device is being used.
I originally thought it had something to do with the dimens.xml file:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
I tried changing both of these to 0dp but the white padding remains on the screen.
Also - in activity_play_game.xml I have the following:
android:layout_width="match_parent"
android:layout_height="match_parent"
And I'm thinking this may have something to do with it? Does anybody know where I'm going wrong?
Many Thanks!
Add this as your Activit layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
</RelativeLayout>
You should see whole screen filled with black. Then try to add this inside
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:text="Margin Padding Test" />
And try to add android:padding="20dp" and android:layout_margin="20dp" into both RelativeLayout and TextView. When you try different combination of those attributes you should be able to update your layout to look as you need.
Margin change how layout is positioned in its parent.
Padding change how layout is positioned within itself.

Why image changes when imported to eclipse?

First I should say that I'm completely new to android so maybe my question is very ordinary for you.
Problem : I've designed the following image (png-24 with transparent background) in the Photoshop
and added it to the res/drawable in my project to use it as the source of a ImageButton but it appears like this:
As you see the rounded shape has changed completely and a gray background is added to the image.
Please tell me how can I fix this? Is the way that I've used this image wrong or the image format or some thing else?
Edit: I just resized the image(simple png) as I've added to my post and suddenly a corner of the image appeared,isn't it due to size or something like that?
This is the layout of the containing activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/showHistBtn"
android:src="#drawable/showhistory" />
<ImageButton
android:id="#+id/findWordBtn"
android:src="#drawable/find_aword" />
<ImageButton
android:id="#+id/insertNewWordBtn"
android:layout_width="315dp"
android:layout_height="200dp"
android:src="#drawable/insertnewword" />
</LinearLayout>
You can simply draw a 9-patch image with Photoshop drawing the shadows and put it into the drawable folders and set this in your ImageButton in xml:
android:background="#drawable/your_9patch_image"
I always do it by using Photoshop, it's super simple. For more details see this: Draw 9-patch
After our long discussion in comments, see what a 9 patch can do (it's just to give you an idea of what you can really achive with just 1 image):
By using this image (/res/drawable/my_btn.9.png):
I got this result
You'll notice that the text isn't well centered: it's because I was in a hurry, and didn't make it perfect (while resizing and black-bordering for 9 patch).
And the starting image wasn't high-quality.
I used a 16sp font, probably you'll use a 32sp or even a bigger one.
Note: mind the extension: .9.png
I just added these to the ImageButton tag in the activity layout xml file and it fixed :)
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
I found the answer completely by intuition but with trial and error found that none of the above statements should not get omitted (for future viewers).

Filling a background image to screen without changing aspect ratio

I'd like to display an image as a background for my activity, but would like to make it fill to screen without changing the aspect ratio of the image (something like this).
I tried scaling the image, but that doesn't seem to work (because I'm using the image as a background, not defining it in an imageview, or so seems the problem).
How do I go about doing that? I even tried to define the background image in its own xml file, but that still didn't work.
If you uses a RelativeLayout you can put a ImageView than fill all layout and put the rest over it. Something like that:
<?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" >
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
// Your full layout
</LinearLayout>
</RelativeLayout>
AFAIK the only solution is the one with ImageView you mentioned. Why it is not appropriate for you? You can also use Bitmap.createBitmap() method to resize your bitmap do desired size.

Fit a ImageView (and its src) to the layout width and make its height proportional

Can you help me to configure my layout? I don't achieve what I'm looking for:
I've put the imageview background to red for explain what I want:
The image's width is smaller than the layout width. I'd like the image grow proportionally keeping its aspect ratio until reach the aspect at the first image (I modified it manually with photoshop) but when setting width="fill_parent" the result is the second image and setting both width and height to "fill_parent" the result is the third image.
I tried to combine with ScaleType with no success
How can I achieve the first option? Thanks
Use android:adjustViewBounds=true attr in xml for ImageView
This question is solved here by extending the ImageView class.
Android ImageView adjusting parent's height and fitting width
I created a sample. It works for me. Here is the xml layout file.
<?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">
<ImageView android:src="#drawable/hydrangeas"
android:layout_height="wrap_content" android:id="#+id/imageView1"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:layout_width="wrap_content"/>
</LinearLayout>
It works for fitCenter too. Have a look at the snapshot.
Use the attribute fit_center into your ImageView tag.
<ImageView ... android:scaleType="fitCenter" .. />

Categories

Resources