i have a design which demands a background like the image below for the number on the right hand side. Is there any we can achieve this in Android ?
Will i need to make a 9 patch image and set it as a background ?
First, create a shape to have rounded corners.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="5px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
Then apply this as the background to your Views:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edges">
<TextView
android:id="#+id/mytext"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="blah blah blah blah"
android:padding="6dip"
android:textColor="#000000" />
</LinearLayout>
You may need to do some tweaking. You may even be able to discard the LinearLayout and set the android:background of the TextView to #drawable/rounded_edges
First create a drawable resource.
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_textview.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#cccccc"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
Then reference this drawable in your layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="center"
android:background="#drawable/rounded_textview" />
</LinearLayout>
One of the good Reference :
TextView with rounded corners
Thanks.
I know this is an old question but for anyone who struggles with this, I strongly recommend ViewBadger library
Related
i am creating a rounded linear layout with some buttons and textviews, i have used a custom selector as background of layout. My problem is that layout have extra space on corners how to remove this? any help will be appreciated
here is my code
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp"
>
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
/>
<stroke android:width="2dip"
android:color="#color/#275D69"/>
</shape>
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector"
/>
Current output:
Dont want that space where i have placed red marks.
With below code you have rounded background with least space on corner
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="5dp" />
<stroke
android:width="2dip"
android:color="#275D69" />
</shape>
this may include space on corner but it's too low.
just reduce radius value.
Try to use this approach, is not the most clear solution but for me works:
Create a Layout container with transparent background, and use it as a container of your rounded layout
<LinearLayout
.....
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
......>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector"
android:orientation="vertical">
<!-- content of the rounded ll -->
</LinearLayout>
</LinearLayout>
The white spaces should be disappeared
Here is a solution you need to follow:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
<stroke
android:width="2dip"
android:color="#275D69"/>
</shape>
Now in main xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:margin="10dp"
android:background="#drawable/selector"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Problem solved!
You can make rounded corners using cardView
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
app:cardCornerRadius="5dp"
app:cardBackgroundColor="#D3D3D3"
app:cardPreventCornerOverlap="false"
card_view:cardPreventCornerOverlap="false">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp" />
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
<stroke android:width="0dip"
android:color="#color/#275D69"/>
</shape>
<corners android:radius="6dip" />
Put corner radius whatever you want.
After getting pissed for 2 days I finally found it .
It cannot be solved by any drawable files or by editing the canvas but simply by making the layout as base layout.
There are several methods and stuffs under values you have to choose the right ones and add it in your styles.
First add these to your styles.xml
<style name="AppBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">#style/CustomBottomSheetStyle</item>
</style>
<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">#drawable/drawable_round_bottom_sheet</item>
</style>
Now the most important part is to call the AppBottomDialogue for this I used this
override fun getTheme(): Int {
return R.style.AppBottomSheetDialogTheme
}
Now you can choose whatever to do with your method I used ImagesView inside ConstraintLayout you can directly clip it no problem.
I am working on an Android app where I need to show some text inside circle like
The text is dynamic.Similarly I want to implement textview inside rectangle shape like
I found out that shape xml file can be used to draw some shapes like
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="20dip"/>
<stroke android:color="#color/colorPrimary" android:width="5dip"/>
<solid android:color="#android:color/transparent"/>
</shape>
But it is not proper as I want some empty space between text and circle.I also tried to use android:background in textview but could not achieve the above mentioned scenario.
What is the Best way to implement above scenario?
How such similar scenarios can be implemented?
Can I use some custom images so that circle/rectangle shapes can be changed and how to achieve this?
You can use something like this..
btn_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="50dp" />
<solid android:color="#000"/>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Total Count"
android:layout_marginTop="40dp"
android:textSize="36dp"
android:background="#drawable/btn_bg"
android:textColor="#fff"
android:id="#+id/btn"
/>
</RelativeLayout>
Try this:-
In your xml:-
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/test">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11"
android:layout_centerInParent="true"/>
</RelativeLayout>
#drawable/test
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="20dip"/>
<size android:height="40dp" android:width="40dp"/>
<stroke android:color="#color/colorPrimary" android:width="2dip"/>
<solid android:color="#android:color/transparent"/>
</shape>
I am currently trying to create a list view which will show details about a post. For that, I have created xml file for designing the list item.
This is the final look I want for my activity. So far I have succeeded dealing with the rounded corners, image buttons and the texts, but I am trying for a while to deal with the left side of the text box. I want it to have a sharp tip. But I still can't handle it.
I will add the xml files to show what I did.
**This is the xml file for the list item**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="160dp">
<LinearLayout android:id="#+id/desclayout"
android:layout_width="240dp"
android:layout_height="140dp"
android:background="#drawable/layout_my_posts_selector"
android:layout_marginRight="5dp">
<TextView
android:id="#+id/myPostDescription"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:maxLength="120"
android:hint="#string/description"
android:inputType="textMultiLine"
android:lines="4"
android:maxLines="5"
android:paddingBottom="8dp"
android:textColorHint="#f7941e"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp"
android:textColor="#6d6e71"
android:scrollbars="vertical"
android:textSize="14sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_toRightOf="#+id/desclayout"
android:background="#drawable/description_layout_my_posts_selector"
android:orientation="vertical"
android:paddingLeft="7dp"
android:layout_height="140dp">
<ImageButton
android:id="#+id/acceptMyPostBtn"
android:layout_width="40dp"
android:background="#drawable/my_posts_button_selector"
android:layout_marginTop="5dp"
android:layout_height="40dp"
android:src="#drawable/v"/>
<ImageButton
android:id="#+id/closeMyPostBtn"
android:layout_width="40dp"
android:background="#drawable/my_posts_button_selector"
android:layout_below="#id/acceptMyPostBtn"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:src="#drawable/x"/>
<ImageButton
android:id="#+id/showOnMapMyPost"
android:layout_width="40dp"
android:src="#drawable/location"
android:background="#drawable/my_posts_button_selector"
android:layout_marginTop="5dp"
android:layout_height="40dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_below="#+id/desclayout"
android:paddingLeft="6dp"
android:background="#2d2d2d"
android:layout_height="20dp">
<TextView
android:layout_width="fill_parent"
android:textColor="#android:color/background_light"
android:text="Finished at 19/01/2013 13:55"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
layout_my_posts_selector
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f1f1f2"/>
<stroke android:width="3dp" android:color="#2d2d2d" />
<corners android:topLeftRadius="7dp" android:bottomLeftRadius="7dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
description_layout_my_posts_selector
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#e7e8ea"/>
<stroke android:width="3dp" android:color="#2d2d2d" />
<corners android:topRightRadius="7dp" android:bottomRightRadius="7dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
This is how it currently looks
If you can please guide me how to deal with the sharp tip I will be thankful!
While using a separate background xml file is a good idea, i would suggest that you use a drawable instead for background.
So you can design your background image with the 'sharp tip' on the left in photoshop or paint.net and set the property android:background=#drawable/background_design where 'background design' is the name of your design drawable file.
Programming it will take more resources to render the design. Not a good practice to use in a listview. For best performance go for a drawable for a static layout.
Well, I don't understand why my post have got -1,
but anyway I will post the way I chose to do it.
I have crated xml file named: triangle
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="87%"
android:pivotY="115%"
android:toDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#f1f1f2"/>
</shape>
</rotate>
</item>
</layer-list>
After this I have added linear layout to my list_item_xml with
the background of the triangle xml.
Thanks for your help, have a nice day
You can use 9 patch as a background.Create one here
Does it possible to create multi-color stroke with the help of XML? I want to create exact view attached here.
Thanks
You could use a trick by creating a drawable shape with gradient and make it a parent's background... something like this:
gradient_stroke.xml (in res/drawable):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="180"
android:startColor="#fff96200"
android:centerColor="#fffff60f"
android:endColor="#fff96200"
android:centerX="50%"
>
</gradient>
<corners android:radius="5dp"></corners>
</shape>
</item>
your_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:padding="2dp"
android:background="#drawable/gradient_stroke"
android:layout_height="wrap_content">
<Button android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/green"
android:id="#+id/button"/>
</LinearLayout>
Just an idea - play around with it to get desired values...
How can I create a pointy button like iPhone back button in Android XML ?
I want to create using like this
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#f8bb49"
android:endColor="#f7941d"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#8c8382" />
<corners.....
Path and PathShape can build whatever you want. By layering a couple together and using a gradient fill you can create the 3D effect (recessed) effect.
However, you will need a class that you can use to create a drawable reference if you want to be able to use an XML definition. Sorry I have not worked out the exact code, but it's something I'm planning to do (which is how I found your question).
Looks like you have an image you can work with. Read the docs on 9-patches to set margins and resizable areas.
http://developer.android.com/guide/developing/tools/draw9patch.html
Create image like that and use ImageButton instead of Button
and , use this property for ImageButton
android:background="#drawable/createdimage"
I did this for the following back button
https://drive.google.com/file/d/0B1cySRpqElgyTkptOFpQa3NsdDQ
using the XML files:
left_arrow.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="-45"
android:toDegrees="-45"
android:pivotX="89%"
android:pivotY="-22%" >
<shape
android:shape="rectangle" >
<stroke android:color="#color/calendarOrange" android:width="3dp"/>
<solid
android:color="#color/drakBlue" />
</shape>
</rotate>
</item>
</layer-list>
button_body.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:left="-4dp">
<shape android:shape="rectangle" >
<stroke android:color="#color/calendarOrange" android:width="3dp"/>
<solid
android:color="#color/drakBlue" />
</shape>
</item>
</layer-list>
Getting them together:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/left_arrow"
android:layout_weight="0.7"
>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_body"
android:layout_weight="0.3"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/white"
android:text="Back"
android:gravity="center"
android:textStyle="bold"
android:textSize="22dp"
/>
</RelativeLayout>
</LinearLayout>
PS: You might need to change the android:pivotX and android:pivotY parameters depending on your conteiner layout