I need to draw outline stroke on text in MaterialTextView. I saw that it is not that easy to do in older questions.
(In this for example: How do you draw text with a border on a MapView in Android?).
Is something changed right now? If we need to set it programmatically, can anyone help me with Kotlin method for this and explain how to make it? I saw answers but I don't know where to create attrs.xml file I never used it before.
You could define a rectangle shape in the res/drawable folder, let's call it rect_border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp"
color="#000" />
</shape>
And use it as android:background for your MaterialTextView:
<MaterialTextView
...
android:background="#drawable/rect_with_border" />
Related
I have some type of chat in my app and want to achieve, to have and outlined speech-bubble as background for my messages.
I´ve already created a shape, which looks like this, but i cant achieve, to have this real chat-speech-bubble-shape like this (i painted this with windows paint program).
So my question: Is there wa way to create this shape as outlined drawable xml file, and if so, can anyone help me? I´ve found just the shapes: line, oval, rectangle. Can we create with these such an speechbubble. Here´s the code for my xml drawable (without speechbubble-shape):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" android:color="#color/colorPrimary"/>
<corners
android:radius="7dp">
</corners>
</shape>
I am struck at this problem of creating the UI in android. I need a xml-based solution to this problem. I don't want to use any SVG or PNG image to achieve the solution. Is there any way to achieve this of view in android.
You should be using a <layer-list> element of xml to draw such a UI.
Following code will be used to create oval shape:
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#AAAAAA"></solid>
<size android:height="100dp"
android:width="60dp">
</size>
This leads to the following shape:
Once you know how to draw an oval shape using xml. Now next step for you is to learn how to use <layer-list> to achieve desired output. To learn using <layer-list> you can follow this tutorial.
By creating a new drawable file which will describe the look of the widget you want to draw,more or less.You can then set it as background for which widget you want to change the original look of. I would recommend starting with rectangle if you want to achieve the shape shown in your question. the drawable file will look something 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="#000"/>
<corners android:radius="150dp"/> <!-- The value here should specify How much ovally you want shape be -->
</shape>
and then assigning the background of the widget to this drawable should do the trick, something like this:
<LinearLayout
android:background="#drawable/oval">
Hope this was helpful :D
After the call of EditText.setBackgroundColor(Color.RED) I see only a red rectangle with text inside it, but I want to change only the text background and to keep the standard bottom border which is grey when the field has no focus and blue if it has focus. Can I do it programmatically?
I want to change the text background to red if the input is invalid and to transparent if it is valid.
I think the best way is to work with drawable/shape.xml as background and upon logic code situation call EditText.setBackground(some_other_shape.xml). Here is an example for shape file xml demonstrating how to use:
Border color ("stroke"): In this example some custom color from colors.xml
Fill color ("solid"): In this example Android default transparent color
Even image icon inside
<!--Example for custom shape file xml design-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#mipmap/icon_image" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/orange_primary" />
</shape>
</item>
</layer-list>
So just prepare several shapes for each situation you need. Another approach is to work with layout params etc. but I think this one is faster and gives more control for custom design in easy to understand code.
i design my app in photoshop and i create most of my design but i cant create this shape:
but i only can create rectangle with code below
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="80dp" android:height="30dp" />
<solid
android:color="#color/blue"/>
</shape>
anyone can help me?
thanks for yr reply
Do you absolutely need it to be an android shape? Or could you simply use a png?
Check this out on how to make a custom button: http://developer.android.com/guide/topics/ui/controls/button.html
Or for the most flexibility add an imageview:
http://developer.android.com/reference/android/widget/ImageView.html
And then set an ontouch listener to it:
How to implement touch listener on image?
Hope this helps.
I want to show edit text box with two sides. so, for that i need to create a rectangle shape with two sides. PLease help some one.
create a drawable under drawable folder and add the belwow contents (border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#color/black" />
<stroke android:width="1dip" android:color="#color/white"/>
</shape>
Now set the Background of the EditText to this draw able like :
android:background="#drawable/border"
I think the best way to do this is to create a 9-patch in PhotoShop with required border sides... there are also several other ways... depends on your design.