ListView divider is not dividng items - java

I have a listview:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_weight="1"
android:divider="#null"
android:dividerHeight="8dp"/>
And I also have a drawable to draw round corners for the listview items:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/listViewItemBackground"/>
<corners android:radius="5dp" />
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>
With the above setup, the items are touching each other.
What am I missing?

You've set android:divider="#null" so obviously you aren't seeing a divider.
Look at how this person has set up their ListView divider for an example what you should be doing.

try like below...
android:divider="#color/redBackground"
and color value is inside colors.xml:
<color name="redBackground">#C60202</color>
Please see below link for more details...
http://developer.android.com/reference/android/widget/ListView.html#attr_android:divider
android:divider
Drawable or color to draw between list items.
May be a reference to another resource, in the form "#[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
This corresponds to the global attribute resource symbol divider.

Related

Top rounded corner on an ImageView

How to add top rounded corners to an ImageView.
I've tried with the following method and it doesn't work.
imgView.setClipToOutline(true);
its better to create drawable file with radius just on top and set as a background.
You can create custom backgroung
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#android:color/darker_gray"/>
<corners android:topRightRadius="10dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="10dp"
android:bottomLeftRadius="0dp"/>
</shape>
set in imageview like this
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#drawable/shape"/>
you can create a drawable for this or use this library for better result
https://github.com/vinc3m1/RoundedImageView

LinearLayout with rounded corners and background color

CompileSdkVersion 26, if it's important. I want to create LinearLayout with rounded corners, and background color.
I created 2 LinearLayouts, one with corners, and one with color, but their shapes do not match my expectations.
Code from activity
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/whitegrey"
android:layout_marginTop="340dp"
android:id="#+id/layout"
android:layout_marginStart="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_border"
android:orientation="vertical"
android:padding="6dp"
android:textColor="#color/black">
...
</LinearLayout>
</LinearLayout>
custom_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
<stroke android:width="2dp" android:color="#444444" />
</shape>
Actually, there is rounded border, and behind, there is rectangular layout with target color. I want to fill with target color only what is inside borders.
Is there any way to set the fill color, preferably in custom_border.xml?
Here, what I have at the moment:
Here, what I want to achieve:
View from phone, the letters on the left are cut off:
From the code samples you provide us, it doesn't seem necessary to me to have two LinearLayout. Only the layout with android:background="#drawable/custom_border" is required.
To achieve the expected result, just add the property <solid android:color="#color/whitegrey" /> to your custom_border.xml :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
<stroke android:width="2dp" android:color="#444444" />
<solid android:color="#color/whitegrey" />
</shape>
I wish I was able to help you!

Add number in circle within text in textview

The number 4 circle
As you see in the image there is number '4' in a circle within a text and it has a background color. It works as a reference when you click it opens a link in the browser. Is that something can be done in textview using spannable or any other way?
You can achieve this using Drawable.
Simply create background.xml in Drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/colorAccent"/>
</shape>
And apply background in TextView,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="04"
It will generate the result like this,

What's making the button background white, shouldn't it be transparent?

As you can see here, the background to the button is transparent. I can't seem to find where it's set to white in either xml files. The button is identified as btnCapture the code is below.
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="#+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/btnCapture" />
<Button
android:text="#string/capture"
android:id="#+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="92dp"
android:background="#drawable/rounded_button"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
tools:layout_editor_absoluteX="157dp"
tools:layout_editor_absoluteY="202dp" />
</RelativeLayout>
rounded_button.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#80000000"
android:width="2dp"></stroke>
<stroke android:color="#D3D3D3"></stroke>
<size android:width="60dp"
android:height="80dp"></size>
</shape>
You do not change anything related to the UI of the button in your Java code, so no, it's not because of the Java code. It's XML related.
Your background of the button is a drawable named 'rounded_button', but you haven't posted the contents of that drawable file. So all we can do is guess that the rounded_button drawable is white?
There are two quick steps you can take to make this work the exact way you desire:
First, change your view from a Button to a TextView and apply your rounded-button.xml background. This should work instantly. It's a bit tricky trying to set a background to a button as seen here.
Secondly, I noticed that your rounded-button.xml file has no solid element. The solid element allows you to set the background of a layout. For this reason, I changed one of your stroke elements to a solid element (there shouldn't be two stroke elements anyway). Your updated rounded_button.xml code is as shown below:
PS: Don't forget to change the view in your activity_main.xml from Button to TextView!
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#80000000"
android:width="2dp">
</stroke>
<solid
android:color="#D3D3D3">
</solid>
<size
android:width="60dp"
android:height="80dp">
</size>
</shape>
I hope this helps. Merry coding!

2 attributes on 1 view android layout

I am trying to set a selectable background on a linear layout with a background color. I know the usual way is android:background="?android:attr/selectableItemBackground" but i already have another code in background. Here is the snippet of the code.
<LinearLayout
android:id="#+id/number"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:background="#color/category_colors"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
The question is how do i set selectable background, there?
Try this code snipped on for size:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
the android foreground attribute is relatively newly added XML attribute, but it does not work for API 22 and below! If this is the case then looks like we are going to have to stack attributes in a custom XML file,but don't worry, it is easier than it sounds!
1)In your project view go to res/drawable folder
2)right click the Drawable folder itself and select new>>>drawable resource file
3)Enter a file name my_custom_button.xml(the root doesn't really matter because you will replace it with the below code)
4)Click on the XML text tab (as opposed to the design view) if you aren't already there
5)Select all text and basically replace with the following:
(creating a custom color border is basically the same steps).
Feel free to change and play with the colors or to replace the gradient (mixture of colors) to a custom color of your own!
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/category_colors">
<item android:id="#android:id/ripple">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/colorPrimaryLight"
android:startColor="#color/colorPrimary"
android:type="linear" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
</ripple>
HOPE THIS HELPS!!!
You should background selector XML file like below code and apply your initial colour along with press and pressed colour.
Then you would have to create the res/drawable/bg_selector.xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/color_056DAE" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/color_056DAE" /> <!-- focused -->
<item android:color="#color/color_333333" /> <!-- default -->
</selector>
then you have to apply this file to the background of your layout in XML.
android:background="#drawable/bg_selector"

Categories

Resources