make text at the centre of the circle - android

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"

Related

Android custom text style

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+"--------");

Background drawable doesn't match its size with its parent layout

I am having troubles applying background color onto a list item layout. I am doing a sample app to learn android. My problem is that it shows the colored background, but it is not the size of the list item layout. Its a bit small. Its not working for some reasons, every one here suggested to set a background drawable, set it like this:
view.setBackgroundResource(R.drawable.ic_green);
I am doing this inside CursorAdapter.bindView(View view, Context context, Cursor cursor). Because of course I am dealing with list. Each item has a corresponding color data that I need to reflect on each list item. I need to do this programmatically. Since I must allow my self to change the background color as I please.
It looks like this:
My drawable resources like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFCC" />
</shape>
And this is how my list item layout looks like:
<?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="wrap_content"
android:layout_gravity="end"
android:layout_margin="5sp"
android:background="#drawable/note_style"
android:padding="5sp"
android:paddingLeft="10sp"
android:paddingRight="10sp" >
<TextView
android:id="#+id/textview_note_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:gravity="start"
android:maxLength="18"
android:maxLines="1"
android:text="#string/text_sample_short_note"
android:textSize="18sp" />
<TextView
android:id="#+id/textview_note_remind_every"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textview_note_short"
android:layout_alignStart="#id/textview_note_short"
android:layout_below="#id/textview_note_short"
android:text="#string/text_sample_remind_every"
android:textSize="12sp" />
<TextView
android:id="#+id/textview_note_date_created"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/textview_note_short"
android:text="#string/text_sample_date"
android:textSize="12sp" />
<ImageView
android:id="#+id/imageview_note_notification_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/textview_note_remind_every"
android:layout_alignRight="#id/textview_note_date_created"
android:layout_alignEnd="#id/textview_note_date_created"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_alarm" />
<ImageView
android:id="#+id/imageview_note_notification_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/imageview_note_notification_alarm"
android:layout_toLeftOf="#id/imageview_note_notification_alarm"
android:layout_toStartOf="#id/imageview_note_notification_alarm"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_due" />
</RelativeLayout>
It does change the background color at whim but, as you can see on the picture, the green background doesn't hit the edges of layout which should match the width of the screen and height-wrap its content.
Any ideas? Thank you!

android text not showing in button

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>

using xml dotted line in android TextView

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" />

Modify the Size of an Android Spinner Drop down part Size?

how can i modify the size of the dropdown part of the spinner??
do i have to do that in XML or in the code itself?
In xml, I believe you set it with android:dropDownWidth. This worked for me:
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dropDownWidth="dp_value_you_desire" />
You can not change the size of spinner as it is default widget. But you can make it custom using background image. Here is my code:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:drawSelectorOnTop = "true"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:layout_below="#+id/placeCity"
android:paddingLeft="5dip"
android:background="#drawable/myspinner_background"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/spinner"
android:layout_alignTop="#+id/spinner"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:src="#drawable/down" />
This is written in XML. And make another file called myspinner_background.xml in drawable folder. Inside that, write this code:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:color="#f269be"
android:width="2dp" />
<solid
android:color="#ECC8EC" />
You can use this Spinner property which limits the height of the dropdown.
android:dropDownHeight="100dp" <!--change 100dp to your requirement-->
you can also modify width...
android:dropDownWidth="100dp" <!--change 100dp to your requirement-->
This works with AutoCompleteTextView as well.

Categories

Resources