I want to create a background for my TextView to look exactly like this one(Yellow Box).
After I create the image how do I go about setting the background to the TV?
Thanks!
for that you create simple image . then Convert in to nine patch here
after set background to that drawable
Example of Nine patch Image
Or you could create a shape drawable.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/Yellow" />
<stroke android:width="#dimen/" android:color="#color/"/>
<corners android:radius="#dimen/"/>
<gradient
android:startColor="#color/"
android:centerColor="#color/"
android:endColor="#color/"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="100"
android:type="linear" />
</shape>
It can also be done using shape attribute.
I created a similar one for myself using the below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" /> //This should be the yellow color you want
<corners android:radius="5dp" /> //for rounded corners
<stroke
android:width="2dp"
android:color="#color/color_dark_blue" /> //for border if you wish any
</shape>
Create it as an xml file inside drawable (say myTextViewBg.xml).
Then add it as a background to your TextView as,
yourTextView:background="#drawable/myTextViewBg.xml"
You can set background of your textview
android:background="#drawable/myResouce"
or using java
mTextView.setBackgroundResource(R.drawable.myResouce);
But in your case create a relative layout then put an imageView and Textview inside it and adjust it according to your view.
In imageView use image which you in your question, expect text to change. And in textView use changeable text. Which you may change pragmatically as well.
Related
I'm trying to make gradient background that placed only bottom right.
(Sorry for images because I don't have enough rep for inline)
https://i.stack.imgur.com/ebgWn.jpg
First, I made radial gradient but not perfect
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerX="0.9"
android:centerY="1"
android:endColor="#00000000"
android:gradientRadius="40%p"
android:startColor="#BF000000"
android:type="radial" />
</shape>
It looks like:
https://i.stack.imgur.com/JOqFf.jpg
Then, I made linear gradient but still not perfect.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="315"
android:endColor="#CD000000"
android:startColor="#00FFFFFF"
android:type="linear" />
</shape>
https://i.stack.imgur.com/a0y9C.jpg
Because gradientRadius parameter doesn't work with linear gradient. Are there any starting/ending point parameter for this gradient? Thanks for help!
Best i have tried with
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerX="0.8"
android:centerY="1"
android:endColor="#00000000"
android:gradientRadius="80%p"
android:startColor="#BF000000"
android:type="radial" />
And then Wrap your TextView and Tick Icon together inside a layout and set the above background to it..
If you want the exact same effect as you provided in image then you better use a Image or vector file for it .. But the approach will be same setting the background to only Wrapped layout not the Root.
I want to make a textview background like in the screenshot. I have a xml file that can make rectangular background like in the screenshot but i am not able to do that below corner styling. I dont know what thing can change this.
Here is my xml file that can give a rectangular background to my textview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/orange" />
<solid android:color="#color/orange" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Your best bet here would be to make a nine-patch image. Roman Nurik has made a really nice nine-patch generator that you can use. Then you just set the image as the background and it will stretch like you think it should.
I want to have circular TextViews and I'm doing this by defining a shape drawable that works fine.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/White"/>
</shape>
I want different colors of this TextView, so I do this in my code:
setBackgroundColor(getResources().getColor(R.color.Red));
But it resets the TextView shape to rectangle!
So, the question summarizes to What's the correct way to dynamically change only the color of a view?
It becomes a rectangle because you are changing the background of the view which of course is rectangle shaped, you should change the solid color but i don't think this possible to do programmatically
the only way is to do like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/Red"/>
</shape>
and:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/green"/>
</shape>
then:
setBackgroundResource(R.color.green);
You can change background of shape of a view by programmatically by doing like this:
GradientDrawable drawable = (GradientDrawable)textView.getBackground();
drawable.setColor(getResources().getColor(android.R.color.darker_gray));
Use
setBackground(drawable);
Instead of
setBackgroundColor(getResources().getColor(R.color.Red));
How can I create a button like this in android 2.3.3 (Eclipse):
You can use HoloEverywhere. This is an open source
https://github.com/ChristopheVersieux/HoloEverywhere
You can implement with ImageView for that, you create an image with holo effect and place it as android:src="#drawable/holo_img" inside the imageview
Try to create custom button, you can create any shape by coding in xml file for example
here is my code for creating button with round corners
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFDD0"/>
<corners android:bottomLeftRadius="8dip"
android:topRightRadius="8dip"
android:topLeftRadius="8dip"
android:bottomRightRadius="8dip"
/>
<stroke android:width="1px" android:color="#000000" />
</shape>
change this code according to your requirement.
I need to implement such button for my Android app. It would be great to do this without using full image as button.
I've done almost the same button using <shape> with <gradient>. The only thing I need is to add bottom blue button shadow and include image.
Is it possible?
Thanks in advance.
You can do this by combining Shape Drawables inside a Layered Drawable. To do so, you'll have 3 xml files as below:
button.xml (the one you already have i guess)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<gradient
android:angle="270"
android:centerColor="#FFFFFF"
android:endColor="#CAEBF8"
android:startColor="#FFFFFF"
android:type="linear" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#FFFFFF" />
</shape>
button_bg.xml (to add the blue border below the button)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<solid android:color="#6FC8F1" />
</shape>
layered_button.xml (to combine the previous xml, and the drawable to use as background on your button)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/button_bg"/>
<item
android:bottom="2dp"
android:drawable="#drawable/button"/>
</layer-list>
You'll find more information on the Layer List in the Drawables Documentation
Make your button's android:background attribute to point your desired image. Create your image with the text and the little person icon. It's not possible to put a text and an image at the same time inside a button.
For the blue shadow, you can either include it in your image or achieve the same result with the gradient attribute as you already said in your question.
For the image, you should use
android:drawableRight
As for the shadow I'm not sure how this is achieved.