Skip to content Skip to sidebar Skip to footer

How To Create A Custom Textview Background Android?

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!

Solution 1:

for that you create simple image . then Convert in to nine patch here

after set background to that drawable

Example of Nine patch Image

enter image description here

Solution 2:

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>

Solution 3:

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"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:padding="10dp"android:shape="rectangle" ><solidandroid:color="#FFFFFF" /> //This should be the yellow color you want

  <cornersandroid:radius="5dp" /> //for rounded corners

   <strokeandroid: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"

Solution 4:

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.

Post a Comment for "How To Create A Custom Textview Background Android?"