I have created an xml dotted line as explained in How do I make a dotted/dashed line in Android?. If I use it as the background of my TextView it shows up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
But if I use it as an accompanying drawable, it does not show up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Essentially, I couldn't care less either way, except: I need the dotted lines to appear below the text in the TextView. Please help.
Use following code to make textview with dotted line.
Create file in drawable folder named dotted.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#F59C51"
android:width="2dp"
android:dashGap="1dp"
android:dashWidth="2dp"/>
</shape>
Then set in as background of textview like below.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/dotted"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Try this. i think textview height might cause problem for you.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Related
I created a simple circular button to create - and + buttons for a custom dialog picker.
As seen in the screenshot the - and the + are shifted a little bit down and not centered in the middle of the custom background.
My custom background in the drawable/circular_button.xml looks like this.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorPrimaryDark" android:width="1dp" />
<solid android:color="#color/colorPrimary" />
<size android:width="30sp" android:height="30sp"/>
</shape>
The button is configured like this inside the LinearLayout
<Button
android:id="#+id/decrease_one"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_marginRight="20sp"
android:background="#drawable/rounded_button"
android:text="-"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
In one of the posts I read about the tag minHeight but it didn't solve my problem.Any ideas ?
Batter to use FloatingActionButton mini
<android.support.design.widget.FloatingActionButton
android:id="#+id/activity_my_digital_executor_fabDigitalex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:fabSize="mini"
card_view:srcCompat="#drawable/ic_new_plus" />
Add 'com.android.support:design:26.0.+' in your App dependencies
This is happen because of your Button's android:textSize="30sp" if you set 50sp its goes down the line more,
Just convert your Buttons to Textviews and set android:gravity="center" also keep your textsize 30sp
<TextView
android:id="#+id/decrease_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_button"
android:text="+"
android:gravity="center"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
If you are use a Imageview instead of Textview than your problem is solved.
If I want display TextView as below :
-------------display-------------
The "display" field is a dynamic data, and the View is TextView, include text and lines on both sides, is any suggestion for the xml build? Or maybe can do it programmatically?
You can use string.xml to store the dash line as text and use String.format method to put dynamic text in your textview
Put the below line in string.xml:
<string name="dash_line">-----%1$s-----</string>
Now you can use the above string to set dashed line with dynamic data in your `textview as follows:
String formattedstring = String.format(getResources().getString(R.string.dash_line), yourStringToShow);
yourTextView.setText(formattedstring );
Ya you can do like below code.
In this code, both side line will adjust according to width,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:singleLine="true"
android:text="Your text goes here" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
</LinearLayout>
If you want to show some drawables, Create a Shape drawable as line. i.e. Name below file as dashed_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:dashWidth="3dp" android:dashGap="1dp" android:color="#color/black" android:width="2dp"/>
</shape>
and use it as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dashed_line">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/white"
android:text="#string/hello_blank_fragment"/>
</LinearLayout>
Or If you simply want to put as a string, '------' on both side then just add it dynamically while setting text i.e.
String display="display";
textview.setText("---------"+display+"--------");
I wanted to draw a Text inside a Circle provided that text should be matched properly at the center of the circle. Actually I am not getting a general method/way to make text at the center.
Also it should not depend in whether the Text is in uppercase or lower case.
My attempt :-
textPaint.TextAlign = Paint.Align.Center; // basic need.
After that I tried to get Ascent and Descent of the Font and get their half and moved the text below by this value. But it is not centered :(
var fontAscent = -textPaint.Ascent(); // default value is negative for ascent
var fontDescent = textPaint.Descent();
note :- I am getting the circle at the center so its center point is correct.
Any Idea how can i do it.
I would try like
Add this line to your layout,
<TextView
android:layout_width = "100dp"
android:layout_height = "100dp"
android:background = "#drawable:circle"
android:text = "Lorem Ipsum"
android:gravity = "center" />
Then create a shape to your drawable folder.
<shape shape="circle" stroke_width = "1dp" stroke_color = "#android:color:black" />
There should be some typo bugs. I am writing it without IDE.
Good luck there.
I have similar views in my app, monas. And what I have done is in the layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.huteri.monas"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CircleView
android:id="#+id/piegraph"
android:layout_width="330dp"
android:layout_height="330dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/sumpie_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:fontFamily="sans-serif-condensed"
android:paddingBottom="50dp"
android:textAlignment="center" />
<TextView
android:id="#+id/sumpie_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:textAlignment="center"
android:textSize="30sp"
app:typeface="roboto_black" />
<TextView
android:id="#+id/sumpie_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:paddingTop="50dp"
android:textAlignment="center"
app:typeface="roboto_slab_light" />
</RelativeLayout>
Well, I have to admit this is not the best solution yet, but it works for me. You may try it in the layout instead
try this .
create a file in drawable folder as circle.xml and copy this code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="270"
android:endColor="#00FFFFFF"
android:startColor="#00FFFFFF" />
</shape>
and for your TextView set Background use this android:background="#drawable/circle"
This happens very frequently to me when starting to work on android. The text within the button element is not showing fully. What can be wrong? I tried text direction, alignment, gravity but nothing works.
The code behind it:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/latestButtonString"
android:background="#drawable/roundbutton" />
Please help and much appreciated.
Update: This is the roundbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
I had this problem, I solved it by chaning the xml for the button from using tools: to android: for the text attribute. Example below (last line is the line thats changed):
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
tools:text="App Settings" />
becomes...
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
android:text="App Settings" />
try to replace
android:text="#string/latestButtonString"
and put direct hardcode string here
as,
android:text="hello"
or
try to replace this line in roundbutton.xml:
<android:shape="rectangle" android:padding="10dp">
with
<android:shape="rectangle" android:padding="3dp">
Can you please check by removing android:background="#drawable/roundbutton" from your button attribute. If text shown fully, then you need to modify the #drawable/roundbutton" there may be you have set fixed width thus your text in button is not fully visible.
It seems that you have a paddingLeft on your Button. Can you check if this is present in your styles.xml?
If it is in your styles.xml, it is probably important for the theme of your app.
If you would just want to cancel it in that certain Button, just put
android:paddingLeft="0dp"
Faced a similar problem...following attribute made it work:
android:layout_weight="1"
please remove these line android:background="#drawable/roundbutton" but view not create like you if you want to create same view please try these code :
code
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:padding="20dp"
android:background="#drawable/roundbutton">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latestButtonString"
android:textColor="#000000"
android:textSize="18sp"/>
</LinearLayout>
I have a TextView and I want to add a bullet symbol in my text through XML. Is it possible?
You have to use the right character encoding to accomplish this effect. You could try with •
Update
Just to clarify: use `setText("\u2022 Bullet");` to add the bullet programmatically. `0x2022 = 8226`
This worked for me:
<string name="text_with_bullet">Text with a \u2022</string>
Copy paste: •. I've done it with other weird characters, such as ◄ and ►.
Edit: here's an example. The two Buttons at the bottom have android:text="◄" and "►".
Prolly a better solution out there somewhere, but this is what I did.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_column="1"
android:text="•"></TextView>
<TextView
android:layout_column="2"
android:layout_width="wrap_content"
android:text="First line"></TextView>
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="•"></TextView>
<TextView
android:layout_column="2"
android:layout_width="wrap_content"
android:text="Second line"></TextView>
</TableRow>
</TableLayout>
It works like you want, but a workaround really.
You may try BulletSpan as described in Android docs.
SpannableString string = new SpannableString("Text with\nBullet point");
string.setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Another best way to add bullet in any text view is stated below two steps:
First, create a drawable
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--set color of the bullet-->
<solid
android:color="#666666"/> //set color of bullet
<!--set size of the bullet-->
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Then add this drawable in textview and set its pedding by using below properties
android:drawableStart="#drawable/bullet"
android:drawablePadding="10dp"
This is how i ended up doing it.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/circle"
android:drawableStart="#drawable/ic_bullet_point" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Your text"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
and the code for drawbale/circle.xml is
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thickness="5dp"
android:useLevel="false">
<solid android:color="#color/black1" />
</shape>
With Unicode we can do it easily, but if want to change color of bullet, I tried with colored bullet image and set it as drawableStart and it worked
<TextView
android:text="Hello bullet"
android:drawableStart="#drawable/bulleticon" >
</TextView>
Since android doesnt support <ol>, <ul> or <li> html elements, I had to do it like this
<string name="names"><![CDATA[<p><h2>List of Names:</h2></p><p>•name1<br />•name2<br /></p>]]></string>
if you want to maintain custom space then use </pre> tag
(almost) all of the options are about using html tags.
you can use drawables for your TextView if it has only one line of text.
something like this:
<TextView
android:id="#+id/tv_with_bullet"
android:layout_width="match_parent"
android:layout_height="50dp"
app:drawableStartCompat="#drawable/ic_desired_bullet_icon" />
and add your desired bullet drawable in SVG. it literally takes no space and makes you free of adding complicated string literals. you can also download the SVG file for a bullet point in here