How to put a text into the center of its TextView? - android

I want to put a text exactly in the center of its containing box, and in preview that's OK, but in emulator it has a problem.
<TextView
android:id="#+id/textView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#000"
android:gravity="center"
android:text="E"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="75dp"
android:textStyle="bold" />
Any suggestions how to do that?

Add android:includeFontPadding=false to your TextView
<TextView
android:id="#+id/textView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#000"
android:gravity="center"
android:includeFontPadding=false
android:text="E"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="75dp"
android:textStyle="bold" />
http://developer.android.com/reference/android/widget/TextView.html#attr_android:includeFontPadding
Fonts have an inherent padding in them and this is what seems to be causing the problem.
Potentially it's just a bug in the preview why it's not showing up there but is in the emulator.

Change layout_height to wrap_content

Related

ConstraintLayout take height of another view android

I am using constraintLayout
I wanted one of my view to take height of another view
I wanted my right Textview to take the height of left TextView
My code :
<TextView
android:id="#+id/tv_cal_consumed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/button_selector_curved"
android:drawableLeft="#drawable/ic_add"
android:drawablePadding="5dp"
android:gravity="start"
android:padding="10dp"
android:text="#string/cal_consumed"
android:textColor="#color/white"
android:textSize="#dimen/text_small"
android:textStyle="normal"
android:typeface="normal"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/tv_cal_burnt"
app:layout_constraintTop_toBottomOf="#+id/con_info" />
<TextView
android:id="#+id/tv_cal_burnt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/button_selector_curved"
android:drawableLeft="#drawable/ic_add"
android:drawablePadding="5dp"
android:gravity="start"
android:padding="10dp"
android:text="#string/cal_burnt"
android:textColor="#color/white"
android:textSize="#dimen/text_small"
android:textStyle="normal"
android:typeface="normal"
app:layout_constraintTop_toTopOf="#+id/tv_cal_consumed"
app:layout_constraintLeft_toRightOf="#+id/tv_cal_consumed"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/tv_cal_consumed" />
Current output:
Change the layout_height to 0dp (match constraints) for tv_cal_burnt. That will cause the TextView to grow to the same height as the other view. Your top and bottom constraints are already set correctly to make this work. See this.
Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
<TextView
android:id="#+id/tv_cal_burnt"
android:layout_height="0dp"
...
Update: If you don't know in advance which view will be taller/wider due to localization, etc., I suggest taking a look at this answer to a similar question. The concept outlined in that answer works for heights and widths - just interchange app:layout_constraintWidth_min="wrap" for app:layout_constraintHeight_min="wrap". (As of ConstraintLayout 2.0.4)
Try this following code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/tv_cal_consumed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#a7a7a7"
android:drawableLeft="#android:drawable/ic_input_add"
android:drawablePadding="5dp"
android:gravity="start"
android:layout_margin="5dp"
app:layout_constraintHorizontal_weight=".1"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="calorie Consumed"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="normal"
android:typeface="normal"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/tv_cal_burnt" />
<TextView
android:id="#+id/tv_cal_burnt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#a7a7a7"
android:layout_marginTop="5dp"
app:layout_constraintHorizontal_weight=".1"
android:drawableLeft="#android:drawable/ic_input_add"
android:drawablePadding="5dp"
android:gravity="start"
android:padding="10dp"
android:text="Calorie Burnt"
android:layout_margin="10dp"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="normal"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="#+id/tv_cal_consumed"
app:layout_constraintLeft_toRightOf="#+id/tv_cal_consumed"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/tv_cal_consumed" />
</android.support.constraint.ConstraintLayout>
if you want to make text as center use android:gravity="center" Instance of this android:gravity="start"
I hope this may help you.
Output here:
Try minimum height of the text view.
android:minLines = "1"
The simplest solution depends on if you feel comfortable making the assumption that the left button is always bigger.
If so, constrain the top of the right button to the top of the left button, and the bottom of the right button to the bottom of the left button ;)

Controlling margins of Android Chronometer

I have a layout with a Chronometer between 2 TextView:
<TextView
android:id="#+id/feed_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current"
android:textSize="14sp"
android:textColor="#color/feed_last_and_current"
android:layout_alignTop="#+id/feed_last"
android:layout_centerHorizontal="true"
android:padding="0dp" />
<Chronometer
android:id="#+id/feed_chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#color/panel_text"
android:layout_alignLeft="#id/feed_current"
android:layout_below="#id/feed_current"
android:padding="0dp" />
<TextView
android:id="#+id/feed_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:textSize="25sp"
android:paddingBottom="3dp"
android:textColor="#FFFFFF"
android:layout_below="#+id/feed_detail"
android:layout_alignLeft="#+id/feed_chronometer"
android:layout_alignStart="#+id/feed_chronometer" />
I've tried everything but Chronometer seems to create a margin between it and its surroundings.
Do you have any idea how to remove those gap?
Thanks
You can have negative margins for your items.
so for your bottom TextView you can have layout_marginTop="-25" or whatever margin is needed.

Android - TextView alignment for random text

I am developing an app. In that I am getting data from webservice and I am inserting it in TextView. But, the alignment of the textview is getting scrambled. Please check the image.
First 3 rows(black colored) is in proper order, from the 4th it is scrambled. I googled on it so many times but not getting the exact solution as I wanted.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/schedule_1_textView_day_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KKR"
android:layout_weight="1.5"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_matchno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_weight="1.5"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="6"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_team1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="4"
android:textSize="7sp"
android:textColor="#color/Black"
/>
<TextView
android:id="#+id/schedule_1_textView_team2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_venue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_blank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_marginTop="20dp"
android:textColor="#color/Black"
android:textSize="6sp"
android:visibility="invisible"
/>
</LinearLayout>
Try this..
You forget to add android:layout_weight="1" for below two TextView Use android:singleLine="true" for all TextView
<TextView
android:id="#+id/schedule_1_textView_venue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="6"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp"
/>
<TextView
android:id="#+id/schedule_1_textView_blank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="B"
android:layout_marginTop="20dp"
android:textColor="#color/Black"
android:textSize="6sp"
android:visibility="invisible"
/>
you might wanna assigh android:layout_weight property to the TextViews like:
<TextView
android:id="#+id/schedule_1_textView_venue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:layout_marginTop="10dp"
android:textColor="#color/Black"
android:textSize="7sp" />
you will need to assign weight according to your requirement. i.e for bigger views you want to assign higher values.
[EDIT] and for the last TextView you may want to set android:layout_gravity="center|right" and android:layout_gravity="center|left" for the first for the text alignment for the rest you should use android:layout_gravity="center"
Add gravity to your TextView like:
android:gravity="center_vertical|center_horizontal"
change gravity to left, right or center as you need...
have you tried adding weight to schedule_1_textView_venue?
It's a little more work, but did you try using TableLayout? This is very useful tutorial that can help you organizing the correct order of views.
EDIT: I think i know what causes the mismatch of the TextViews. It comes from the different length of the values in your last column named VENUE. Take for example the values HYDEBARAD and DELHI - the first one is much longer than the second which makes all the TextViews on the row to go left. I am sure that if you put identical padding to the TextViews you will align them. For example: take your TextViews in MATCH column and write the following: myTextView.setPadding(40, 0, 0, 0) (padding on the left side) this will put them exactly in a straight row. Do the same with the rest TextViews with the proper padding value, take into consideration the length of the values in the other columns... It is a little bit tricky though. :)

Android: Space between image and text

This is my code:
<TextView
android:id="#+id/error_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:gravity="center"
android:drawableRight="#drawable/error_image"
android:visibility="invisible"
android:textColor="#aa0000"
android:textStyle="bold"
android:textSize="16dp"/>
There is space between image and text and I want reduce this space, how can I do it?
As you had set android:layout_width="match_parent", TextView took all the width so the drawablePadding which you set will not work. So you need to set android:layout_width="wrap_content".
Try following code :
<TextView
android:id="#+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="0dp"
android:layout_marginBottom="2dp"
android:drawableRight="#drawable/ic_launcher"
android:gravity="center"
android:textColor="#aa0000"
android:textSize="16dp"
android:text="adsfasdfa"
android:textStyle="bold"
android:visibility="invisible" />
You should set the property android:drawablePadding="0dp"
Or in your java code, call textView.setCompoundDrawablePadding(0);

how to set text upside of image in button in android

i want to set text upside of image in button property.
here is my code for it but i get it text is behind of image which property i have to set ?
<RelativeLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:background="#drawable/nav_bar">
<Button
android:id="#+id/btnHome"
android:layout_width="60dp"
android:layout_height="70dp"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="bottom|center_horizontal"
android:drawableTop="#drawable/btn_home"
android:background="#null"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingBottom="10dp"
android:includeFontPadding="true"
/>
use android:drawableBottom instead of android:drawableTop there.
try this...
<Button
android:id="#+id/btnHome"
android:layout_width="wrapcontent"
android:layout_height="70dp"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="bottom|center_horizontal"
android:drawableBottom="#drawable/btn_home"
android:background="#null"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingBottom="10dp"
android:includeFontPadding="true" />
There is problem with your 'layout width and height' that's why your text appears behind the image.
<Button android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="top|center_horizontal"
android:drawableBottom="#drawable/app_icon"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingTop="10dp"
></Button>
try this one.instead of specifying width and height better to use wrap_content property.bcoz in future if u want to change the image that will be automatically fix it.

Categories

Resources