android:shape="line" not visible - android

I have the following drawable shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1dp"
android:color="#e0e0e0" />
</shape>
And use this with this image:
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:src="#drawable/line" />
When I set android:layout_height to 1dp, the shape is not visible. If the android:layout_height is set to 2dp, the shape is visible.
Why do I have to use a height of 2dp?
Ralph

It's a stroke, so it goes around the shape. Meaning it passes the line on both sides, so it will need twice the width of the stroke.
You could just set <size> tag in your shape and put that on 1dp and <solid android:color=""> for the color

Related

Android Stroke Shape Drawable Left for TextView

Following is my drawable xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#color/button_blue" />
Following is textView xml
<TextView
android:id="#+id/tvOrigin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:drawableLeft="#drawable/circle_cab"
android:drawablePadding="16dp"
android:drawableTint="#color/deal_disc_bg"
android:ellipsize="end"
android:lineSpacingExtra="2sp"
android:maxLines="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="ksjkdjksjkdjkj"
android:textSize="14sp" />
I found that drawable is not appearing on left side of textview
Then I have added following to drawable xml
<size
android:width="10dp"
android:height="10dp" />
After adding this line I a getting drawable in left but it is drawing solid circle and not stroke
Any idea how to add stroke circle as drawable left to textview only through XML and not pragmatically.
I think that all circle it is a stroke. what I Mean that you width of stroke is exactly the size (consider left stroke 1 and right stroke 1) to fix it try to increase the size or decrease the width of the stork.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="0.5dp"
android:color="#color/button_blue" />

How to set background in circular imageView

I have a circular ImageView as you can see below, this references a vector drawable, I would like to fill the inside of the circle with another View, without having that rectangle outside the circle, is this possible in any way?
<FrameLayout
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#android:color/black"
>
<com.myapp.MySpecialBackground
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/icon"
android:tint="#color/blue"/>
</FrameLayout>
You can use a ShapeDrawable as background resource. Add a drawable resource file to your project and set the "padding" so the circular shape will not show outside of your blue circle:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<padding
android:left="30dp"
android:top="30dp"
android:right="30dp"
android:bottom="30dp" />
<size
android:width="150dp"
android:height="150dp" />
<solid android:color="#ff0000"/>
</shape>
I used 30dp but that's hard to tell from your screenshot, so most likely you'll have to experiment. Just keep in mind that you'll get a circle if your ImageView is a square and all padding values are equal.
(For older android versions: the size tag is relatively new, in earlier versions the width and height attributes went into the shape tag)

How to create a 1px border grid

I would like to create a clean grid.
I already have something that work, but I have a 2px border.
Here is my code :
In an activity :
setContentView(R.layout.activity_game);
GridView gridview = (GridView) findViewById(R.id.gvGame);
ButtonAdapter buttonAdapter = new ButtonAdapter(GameActivity.this, myList.getList());
gridview.setAdapter(buttonAdapter);
activity_game.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/gvGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:gravity="center_vertical"
android:numColumns="3"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth" />
</RelativeLayout>
Somewhere in ButtonAdapter :
button.setBackgroundResource(R.drawable.grid_button);
grid_button.xml (drawable) :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#android:color/black" />
</shape>
</item>
<item
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
Result :
How can I get a 1px bordered grid everywhere ?
Note: each square must be clickable.
How about giving a 1px border on your RelativeLayout's top and left side and 1px border on each cell's right and bottom side?
This will make the effect you want, but not sure what you want the result to be if the number of cells in the last row is less.
I'd suggest to use the spacing attributes of the GridView instead to achieve uniformly spaced grid.
<GridView
...
android:horizontalSpacing="1px"
android:verticalSpacing="1px"
... />
This creates a space between the GridView's items with the size of 1px.
If you want the spacing to be black, you have to set the GridView's (or its parent's) background to black.
Of course now you have to remove the black border of your button's background.

Trying to setBackgroundColor to drawable in Adapter not working as expected

I have a linearlayout background set to a drawable file in order to create that rounded corner look as such:
<LinearLayout
android:id="#+id/layout_top"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/rectangle_topcorner">
<TextView android:layout_width="fill_parent"
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:text="Impact"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:layout_marginLeft="10dip" />
</LinearLayout>
In my adapter, after I inflate the layout item, I am trying to change the backgroundColor to a 'different' drawable, in order to change the background color:
LinearLayout top = (LinearLayout) view.findViewById(R.id.layout_top);
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);
The problem is, after doing that, the rectangle loses its rounded corner look and its just a plain old square.
The 2 drawables:
rectangle_topcorner
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/background_shape" >
<corners android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#005577"/>
</shape>
rectangle_topcorner_high
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/background_shape" >
<corners android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#83162D"/>
</shape>
I'm missing something to preserve the rounded corners ?
Try using:
top.setBackgroundResource(R.drawable.rectangle_topcorner_high);
setBackgroundColor are for color resources
Thought I would share my eventual fix -
As I was trying variants of
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);
The correct (working) code is:
top.setBackground(context.getResources().getDrawable(R.drawable.rectangle_topcorner_high));

Android Shape Line

I have the following code:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp"/>
<size android:height="1dp" />
<solid android:color="#FFF"/>
</shape>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background ="#drawable/line"/>
I have two questions:
Why the line is black instead white? I have tried putting it inside a ImageView but the result is the same.
How can i set the opacity of the shape?
1) In order to set the color of the line, you need to define android:color on the <stroke>. The <solid> element is for the background.
2) You can set the opacity of the shape by using color values with an alpha layer, aka #ARGB. In your case, maybe #7FFF.
The simplest example of the line for 2 density pixels with black color.
Just save the xml in drawable folder: res/drawable/shape_black_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#000000" />
<size android:height="2dp" />
</shape>
Use LinearLayout and background to show the line.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/divider">
</LinearLayout>
I hope this helped.
A tricky way can be like this:
1) Add a View where you want have a line.
2) set android:background to a hex value that you can set both color and opacity.
For example:
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="#77f27123"
android:orientation="vertical" />

Categories

Resources