Border effect to LinearLayout - android

I would like to get linearlayout border effect like the image below:
Is this possible? I don't wanna use one layout inside another layout. If so how do I do it?
I tried the following:
<corners android:radius="4dp" />
<stroke
android:width="4dp"
android:color="#color/layoutcolor_net" />
<gradient
android:endColor="#BDBDBD"
android:gradientRadius="250"
android:startColor="#E0E0E0"
android:type="radial" />
<padding
android:bottom="30dp"
android:left="30dp"
android:right="30dp"
android:top="30dp" />
I apply the gradient properly but my text is not shown.
I am applying this to relativeLayout:
<RelativeLayout
android:id="#+id/receivedLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:background="#drawable/gradientShow"
android:gravity="center" >
<ImageView
android:id="#+id/receivedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/recieved"
android:paddingTop="5dp" />
<TextView
android:id="#+id/receivedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/receivedImage"
android:gravity="center"
android:paddingTop="5dp"
android:text="#string/transaction_received"
android:textSize="14sp" />
</RelativeLayout>
Thanks!

you should checkout the 'shape drawables' section of the reference guide.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
There are lots of things you can do with shapes. For example:
<shape
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
save that file as res/drawables/gradient.xml folder and refer to it like this in a textview or some other view:
<TextView
android:background="#drawable/gradient"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

Related

How to make drawable circle shape with count

I really appreciate if someone can help me with using how to drawable circle view with count as like my below image.
I wrote some code for my requirement but it's showing rectangular shape but i want circular shape..
linear_layout_border(drawable file)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/colorPrimary" />
<solid android:color="#color/colorLightSky" />
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
xml:
<FrameLayout
android:id="#+id/group_attachment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="3dp"
android:background="#drawable/linear_layout_border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:src="#drawable/ic_group"
android:tint="#color/rosecolor" />
</RelativeLayout>
<TextView
android:id="#+id/members_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginBottom="3dp"
android:background="#drawable/test_circle"
android:gravity="center"
android:minWidth="15dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="0"
android:textColor="#fff"
android:textStyle="bold"
android:visibility="visible" />
</FrameLayout>
image:-
Try this it looks as you want
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners
android:radius="10dip"/>
<solid
android:color="#2196F3" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:left="2dip"
android:right="2dip"
android:top="2dip"
android:bottom="2dip" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Use this code to draw a circle and put it as background of your TextView and make its android:gravity="center"
Change height and width according to your need.
Try following drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item xmlns:android="http://schemas.android.com/apk/res/android">
<shape>
<solid android:color="#58050505" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
/>
</shape>
</item>
</selector>
Use this for the TextView background
inside drawable folder
badge_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#color/purple"
android:startColor="#color/purple"></gradient>
<corners android:radius="100dp"></corners>
<stroke
android:width="0.5dp"
android:color="#color/white" />
</shape>
The TextView
<TextView
android:id="#+id/serial_no"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|start"
android:layout_marginTop="5dp"
android:background="#drawable/badge_background"
android:gravity="center|center_vertical"
android:maxLines="1"
android:text="1"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="bold" />

How to create square button in android for background?

img
I want to create square shape in background. Android studio shape doesn,t support square.How to create manually in border.xml?
Here is my code
border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />
</shape>
my.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#9294a3">
<TextView
android:id="#+id/tv_2"
android:layout_width="wrap_content"
android:text="2"
android:textColor="#c2bbbb"
android:textSize="30sp"
android:background="#drawable/border"
android:layout_height="wrap_content" />
</LinearLayout
Button like this
xml
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/border"
android:text="2"
android:textSize="50sp"
android:gravity="center"
android:textColor="#ffffff"
android:padding="5dp"/>
Style (drawable/border.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorGray"/>
<stroke android:color="#color/colorBlack"
android:width="4dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
<TextView
android:id="#+id/input_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="right"
android:direction="center"
android:gravity="center"/>
you should specify shape='Rectangle' for square it will be better.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />

Android: design fragments as cards

I have a ViewPager which contains a couple of Fragments which are available by swiping left/right. I want these Fragments to look like cards but I can't seem to get it right.
The fragment occupies the whole space and the shadow is surrounding the card. Instead I would like to have some padding between the fragment layout and the ViewPager one so when I scroll there would be some space between fragments. This is what I want it to look like more or less
This is my layout for the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Main"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="35dp"
android:layout_marginBottom="35dp"
android:background="#drawable/bg_card"
>
<EditText
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<EditText
android:layout_marginTop="150dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<EditText
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</RelativeLayout>
And this is the card drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp"/>
<solid android:color="#aaa" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp" />
<solid android:color="#fff" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
</layer-list>
Can anyone help me with this? Thanks!
You might be looking for CardView: http://developer.android.com/reference/android/support/v7/widget/CardView.html
https://developer.android.com/training/material/lists-cards.html#CardView

Text over image in Android layout like messages button in Facebook

I want to make a notification Button in Android Activity as the same as notification button in Facebook. I tried ImageButton and Button to do this but I failed to display the text drawn over the image in the same layer. Why?
If I'm getting your question right and you need the red circle containing the count of notifications, you create a drawable and use it as a background for your TextView
Here is a sample rounded-edges rectangular shape
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<solid android:color="#FFFFFF"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp"/>
<solid android:color="#F00"/>
</shape>
</item>
</layer-list>
Then all you need is to use that as a background for your TextView
Edit: use FrameLayout to make the notification appear over the ImageView
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
<TextView
android:id="#+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:background="#drawable/red_notification_frame"
android:gravity="center"
android:minWidth="15dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:textColor="#fff"
android:textStyle="bold" />
</FrameLayout>
And change the layout_gravity for the TextView to control its position over the image.
If you want a permanent text to be displayed over an Image, a shortcut option is to use a Image editing software and write the text over the Image.
Then use a ImageButton and place the Image.
I modified an answer and came up with a solution
<RelativeLayout
android:layout_height="102dp"
android:layout_width="102dp"
android:padding="15dp">
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/envelope" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="1dp"
android:layout_height="12dp"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="43dp"
android:layout_marginLeft="43dp"
android:gravity="center"
android:text="0"
android:background="#drawable/rounded_corners"
android:textColor="#android:color/white" />
</RelativeLayout>
The xml style, put it in the drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#android:color/holo_red_dark" />
<solid android:color="#android:color/holo_red_light" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="20dp" />
</shape>
In my case when the text is beyong three chars, I say 9+

how to add bottom border in relativelayout

<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:background="#drawable/settings_border" android:padding="1dp"
android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip">
<RelativeLayout android:id="#+id/map_refresh"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:background="#drawable/settings_selector_up"
android:padding="15dp">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Map refresh period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
<RelativeLayout android:id="#+id/own_location"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:padding="15dp"
android:background="#drawable/settings_selector_mid">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Own location update period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
I want set only bottom border in relativelayout. I want to dispaly listview style but not using listview. Lets say each listitem is relativlayout. I want set only bottom border so its look like a listview's divider.
I hope I understood what you said.
in the res folder create a new folder (if you don't already have it) named drawable
there create an xml named "borders.xml"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#2f481c" />
<stroke android:width="2dp" android:color="#999999" />
<padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />
<corners android:radius="10px"/>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#4c8a39" />
<stroke android:width="1dp" android:color="#FFFFFF" />
<padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />
<corners android:radius="10px"/>
</shape>
</item>
</selector>
You can further edit it as you like.
Then select the layout from the Outline and click Background properties, and select the borders xml that you created.
This will create borders for all 4. Alternatively, you can add a simple
<View android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#FFFFFF" />
line and add it to the bottom of your layout and change the color/size to your liking.
For some reason the other solutions didn't work for me - all borders were shown no matter how I changed it. This worked:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/gray" />
<solid android:color="#color/gray" />
</shape>
</item>
<item android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/white" />
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
The color of the border is gray and the background is white for the container (LinearLayout in my example). You can simply change the second item to make the border thicker or have border on the top/left/right.
This is how the layout xml looks like:
<LinearLayout
android:id="#+id/searchWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/bottom_gray_border"
android:layout_alignParentTop="true">
<EditText
android:id="#+id/searchEditText"
style="#style/EditTextSearch"
android:hint="#string/find"
android:layout_marginLeft="5dp"/>
</LinearLayout>
I got the idea from here: Is there an easy way to add a border to the top and bottom of an Android View?
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#FF8000" />
<solid
android:color="#00FFFFFF"
android:paddingLeft="10dip"
android:paddingTop="10dip"/>
<corners android:radius="10px"/>
<padding
android:left="10dip"
android:top="10dip"
android:right="10dip"
android:bottom="10dip" />
</shape>
You can save this as borderframe.xml in the res/drawable folder (create it if it doesnt exist yet), and reference it like so: android:background="#drawable/borderframe".
A simple way to display a border is to create a horizontal LinearLayout, and to set its background color and height according to the border you want.

Categories

Resources