I'm just starting out in Android and was making a splash screen. I tried to change the default background colour to grey, but apparently I can't use the same attribute twice. How can I add a background image and change the background colour? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/reddit_alien"
android:background="#color/grey"
>
</LinearLayout>
One way to do this, which would be to use 2 layouts.. The outer one having a background of the color and the inner one having the image as the background.
Alternatively you could use a layer list drawable resource file in xml which defines one layer as being the background color and another as the background image. see here: http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
No that's not possible to declare same property twice. Either she them image as background or color.If you want to set the color and background image then create two views. Set the bottom view background to color or image and set the top view background to color or image.
Related
I am trying to figure out how to fill background color of a card from bottom to top based on the processing of a particular task. I want the background color of card to be filled slowly from bottom to top approach based on some timer or processing. How to achieve this scenario. Please help me here.
You can easily achieve this with Clipping. This is a modified code from Android Drawable Resources.
First specify the clipping view in XML file saved at res/drawable/clip.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/android"
android:clipOrientation="vertical"
android:gravity="bottom" />
It basically means that whatever drawable you assign to this xml, it will be clipped vertically starting from the bottom. Then in your cardview XML set this drawable as a background.
<CardView
android:id="#+id/cardview"
android:background="#drawable/clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Then in your activity do something like this
CardView cardview = (CardView) findViewById(R.id.cardview);
ClipDrawable drawable = (ClipDrawable) cardview.getBackground();
drawable.setLevel(/*your level*/);
Regarding the level:
Increasing the level reduces the amount of clipping and slowly reveals
the image.The default level is 0, which is fully clipped so the image
is not visible. When the level is 10,000, the image is not clipped and
completely visible.
Hope this helps.
I have a multi pane view: much like this:
The way I got this is by using elevation:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="4dp"
android:background="#android:color/white">
That scroll view is the detail view on the right. Android is assigned a darker background on my list view on the left to 'show' the elevation. Which is great, view looks perfect. However I want to highlight the selected item in the list.
List View:
Selected state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/some_color" android:state_activated="true"/>
</selector>
My problem is I need to define some color and I want to make it a little darker than the color of the items in the list. However I am not 100% sure what that color really is.
Can I find that by debugging? Or better yet does anyone know what elevation is really doing to that views color?
Android isn't actually coloring the background differently based on elevation; the only effect an elevation attribute has is showing a shadow on 5.x devices. It just looks darker because you gave the ScrollView a white background and the default background of an Activity is an off-white color. If you want to determine that color, I'd recommend simply taking a screenshot and using an app to see what color those pixels are; I find Color Picker works very well. If you want that background color to be consistent everywhere, I'd manually set your ListView's background to that color.
I intend to make a custom seekbar, with my own set of images for the background and progress drawables. I've made 2 9-patch images, one each for the background and progress drawables. Here is the code I am using for the progressDrawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/background"
android:drawable="#drawable/experience_seekbar_background" />
<item android:id="#+id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left|top"
android:drawable="#drawable/experience_seekbar_progress" />
</item>
</layer-list>
Also, here is the xml definition of my seekbar:
<?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:orientation="vertical"
android:padding="5dp" >
<SeekBar
android:id="#+id/experienceSeekBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:max="2"
android:progress="1"
android:progressDrawable="#drawable/experience_seekbar" />
</LinearLayout>
The problem is, the progress portion of the drawable is drawn below the background portion, instead of directly overlapping it.
Expected/Desired: (http://i.imgur.com/C27dQZK.png)
Actual Output: (http://i.imgur.com/ftNlMMI.png)
Having gone through tens of custom seekbar tutorials, I am still stuck at what could I possibly be doing wrong here. It might be something very trivial, but I am just not able to spot it.
It turns out there were several things at play here.
1) I used 9-patch images for the seekBar background and progress drawables. However, I did not know that 9-patch images also have, apart from the stretch lines at the top and left edges, padding lines at the right and bottom edges. These define the content area of the image, and the area left automatically becomes the padding area (i.e. the non-content area counts towards the padding of the view for which this image will be used as background). Also, if no padding lines are specified, android uses the stretch lines as padding lines.
2) In a layer-list, the padding of items stacks up. This means that if the first item in a layer-list has some padding, this padding will add up to the padding of all items after it, the second item's padding adds up to padding of all items below it, and so on.
So in my case, the 9-patch image used as seekBar background (first item in the layer-list) got some unintended padding (because stretch lines were being used as padding lines), and the seekBar progress drawable got this padding added to its own unintended padding (again, since it was a 9-patch) which caused it to be displayed below the background.
To correct this, I changed the 9-patch images and set the padding lines to their full length i.e. I defined the entire image as the content area. There was no other option, as any padding would lead to twice the padding for the progress drawable, displacing it from its intended position (which is exactly above the background drawable).
I'm using actionbarsherlock.
In order to get action modes on bottom(action modes on bottom) I use
android:uiOptions="splitActionBarWhenNarrow"
But I need to add a button to main action bar, so I use custom view.
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_view, null);
getSupportActionBar().setCustomView(customNav, new
ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
Here is custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_day"
android:onClick="onMoreClick"/>
It doesn't metter if I use ImageButton or ImageView with the same properties, so I'll talk about ImageButton.
If I use the ImageButton, I get what I want except of that background is gray.
If I add a line to previous custom_view.xml, in order to get transperent background(or use ImageView instead)
android:background="#00000000"
The alpha in icon will be "cropped" and size of button will change.
The question is. How to get button with such sizes as on first image(with default gray background) but with transperent background?
One of the ways to solve a problem is to make a color of the button's background the same as a color of actionbar and it will look like transperent, but I don't think it is a good solution.
You can use image with transparent background. It can be done in photoshop.
Have you considered using an ImageView instead of an ImageButton? Just set your required image in the imageview and use an onclicklistener to detect clicks like you would do for an ImageButton.
Additionally, you can also just specify the width of your ImageButton and set the padding as required (this is if you want to use android:background="#00000000").
i think that you can add to the button
android:background="yourimagethere"; or maybe android:background=NULL
and it will solve the problem
I have a child layout inside its parent layout. If i set any background color for parent, it sets the same color for its child also. But i don't want to set the same color for child. (I want the default android black color as background for child). Please help me.
EDITED:
I need to capture the child layout and create a bitmap later, So i should not set any background color for child (but its parent should has some color). It should set to default android black only.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFA500" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="15dp"
android:layout_weight="1"
android:gravity="center" >
</RelativeLayout>
</LinearLayout>
Parent linear layout has some BG color. I didn't set any BG color for child relative layout but it is taking parent's BG color. So now, how can i set android default BG color for child ?
If i set any background color for parent, it sets the same color for
its child also
No it doesn't. Your child just has a transparent background by default, so it looks like it's inheriting the color from its parent. Just set it to something else other than transparent.
Edit:
Sorry, just read your edit.
If you want to add an image to your child view later, instead of setting setBackgroundDrawable or something, use setImageBitmap. It will retain the transparent color of your child view, while displaying the image you specified.
You can set the background color of the child layout to transparent.
Set:
android:background="00000000";
First 2 hex numbers are the alpha component for your background.