Border texture for a View - android

Is there any way to set border texture for LinearLayout or any other View? I can't just set an image as the background because of different screen sizes.
Here is the sample of the texture I'm talking about:
I tried to use Nine-patch, but it stretches the white lines that are supposed to be repeated.
Screenshot from Draw 9-patch tool:

You can use shape drawable like this:
<shape android:shape="rectangle">
<solid android:color="#FAFAD2" />
<stroke android:width="1sp" android:color="#000000"
android:dashWidth="7sp" android:dashGap="5sp" />
</shape>
and this will result you something like this:
You can change color, width, border etc according to your requirements.

Related

Create circle button with custom background image

So I have a background texture (image) that I put on all of my buttons. I want to create a circular button with an icon that retains this background texture.
So far, I have noticed it isn't supported to do this on the shape side:
<shape android:shape="rectangle">
<solid android:color="#drawable/myTexture" /> <!-- this isn't supported -->
</shape>
Lastly I would otherwise try to use <layer-list>, however as I stated above, I need to still crop the texture to my shape I'm creating.
Is the only way to do this to make my own circular images that combine the background texture and the button icon? This seems a bit excessive, as I would think there should be a programmatic way to accomplish this, but I could be wrong.
Here is an example of what I mean above:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/myTexture" />
<item>
<shape android:shape="rectangle">
<corners android:radius="40dp" />
<size
android:height="80dp"
android:width="80dp" />
<solid android:color="#color/black" />
</shape>
</item>
</layer-list>
In the picture bellow: black is the circle shape, silver is the texture/image (black for contrast)
I haven't solved the programmatic way, but I did end up creating new graphics that combined the icon and the background texture. Using this singular image, I was able to create circular ImageButton's.

I have made a shape in xml and I have a problem giving it transparent and blur color

this is my output:
[1]: https://i.stack.imgur.com/MXUAv.png
this is expected :
[2]: https://i.stack.imgur.com/VrnDA.png
I have made a shape in xml and I have a problem giving it transparent and blur color. how should I do it here's my code to the xml:
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#FFFFFF"/>
<corners android:radius="8dp"/>
</shape>
You can't give blur using shape, the shape can max be transparent but can't be a blur. To easily blur just use some library instead of creating bitmap on canvas and then tweaking the properties.
Here is some example.
Blur view

Layout Background partially transparent

I want to set a transparent background. Maybe 20% of gray
but i also want to have a small part of the background being completely transparent (a rectangle or a circle in the layout):
Is that possible? later i want the rectangle (or the circle) to be moveable and scaleable.
This is easily achievable with a custom drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="1.9"
android:useLevel="false">
<solid android:color="#android:color/transparent"/>
<stroke
android:width="125dp"
android:color="#CC000000"/>
</shape>
When you overlay that drawable on top of an image the results would looks something like this:
By creating that drawable dynamically in code you can adjust it at runtime. For example:
ShapeDrawable drawable = new ShapeDrawable(new RectShape());
drawable.getPaint().setColor(0xCC000000);
drawable.getPaint().setStyle(Paint.Style.STROKE);
drawable.getPaint().setStrokeWidth(dpToPixel(125.0f));
someView.setBackground(drawable);
You can influence the shape of the hole in the middle by combining multiple layers in a LayerDrawable.
You can also influence the general shape of the hole by setting the android:shape tag of the <shape /> element in the custom drawable.

Custom Shapes with text in Android

I am new to android. I want to draw a custom shape in android. When someone chooses a date(via date picker) I want to display the result in a square box rather than the regular way.How do I achieve this?
You can use a textview to show the selected date. Since you seem to know the coding for that, I focus on the design you can apply for your textview.
By default, a textview has a rectangular shape with sharp edges and points, but you can achieve a rounded-corner textview using a style , like this one :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/red"/>
<corners android:radius="7dp" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
<solid android:color="#color/white"/></shape>
The corners tag, is responsible for the rounded corners, you can tweak th radius to make more or less rounded.
Stroke tag, defines the width and color of the border for the textbox.
and Solid tag determines the bakcground color of the view.
In order to use this style, you should save it as an XML file and save it in drawable folder (normally drawable-hdpi will do). Then , in your tetxview or edittext you should set the background property as the following :
android:background="#drawable/thesavedxmlfile"

rounded view's corners android, NOT round background

I know how to give round borders using XML in Android. I thought it would be cool to make the radius of one corner of my textView excessively large. The problem is, the text keep spilling out. Can I make my textview have a TRULY round corner? (Not just the background). If this was CSS this would be so easy. I am new to Android.
So in terms of CSS, I want to set my overflow to hidden so to speak.
Please help me.
In short, no. All Views are rectangular and fit in bounding boxes.
The best way to achieve rounded corners is the way you mention; using a shape drawable with corner radius set as the background to your TextView.
Like ataulm said, all Views are rectangular.
Create a shape drawable allows you to create a background with rounded corners. You'd have to use padding to make sure the text doesn't clip in certain areas.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="4dp"
android:color="#ff0000"/>
<padding android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>

Categories

Resources