Raising text inside a textview - android

This is a simple question but its kicking my butt all around xml land.
All I want to do is to have the two edit text fields separated by a colon and to have the colon be centered like the following:
00:30
Right now when it displays the colon is slightly lower than the 00 and 30 of the edit text fields
I have tried using android:layout_marginBottom= "10dp" but that doesn't work. Any ideas? Thanks
Here is my code:
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_below="#+id/text_minute"
android:layout_marginLeft="45sp"
android:inputType="number"
android:text="#string/edit_minute"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/edit_minute"
android:layout_below="#+id/text_minute"
android:textAlignment="center"
android:text="#string/text_colon"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="105dp"
android:layout_height="85dp"
android:layout_below="#+id/text_minute"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_second"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />

Put Layout_height of Both edittext to wrapcontent without giving a specific height then the three views would be aligned.....
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute"
android:layout_marginLeft="45sp"
android:inputType="number"
android:text="08"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/edit_minute"
android:layout_below="#+id/text_minute"
android:textAlignment="center"
android:text=":"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="30"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />

I would first put them all in a RelativeLayout, and place this layout under "text_minute".
Then in the following I centered all, and the colon's height is adjusted using the attributes layout_alignTop and layout_alignBottom that force the TextView to follow the first EditText height:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_minute" >
<EditText
android:id="#+id/edit_minute"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_toLeftOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_minute"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignTop="#+id/edit_minute"
android:layout_alignBottom="#+id/edit_minute"
android:gravity="center_horizontal"
android:text="#string/text_colon"
android:textSize="70sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edit_second"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:layout_toRightOf="#+id/text_colon"
android:inputType="number"
android:text="#string/edit_second"
android:textColor="#0D4F8B"
android:textSize="70sp"
android:textStyle="bold" />
</RelativeLayout>

You can place your EditText TextView and EditText all in a LinearLayout and use android:baselineAligned and android:baselineAlignedChildIndex.
If you require your parent view is a RelativeLayout then you can use android:layout_alignBaseline on each of the child views.

Related

How to ellipsize a TextView in between an ImageView and a TextView?

Here is my layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginStart="20dp"
android:gravity="center_vertical|start"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18.9dp"
android:lineSpacingExtra="64sp"
android:textColor="#484848"
android:textSize="16sp"
tools:text="அனைத்து அனைத்துஅனைத்து" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:lineSpacingExtra="64sp"
android:text="0"
android:textAlignment="center"
android:textColor="#color/red_500"
android:textSize="14sp" />
</LinearLayout>
All other languages are working fine, except tamil language is very big.
I need an icon at beginning and sometimes it show number of notifications at end. So I need to set ellipsize to text on middle.
How to achieve this?
you can use android:ellipsize atrribute to do that.
like this :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18.9dp"
android:lineSpacingExtra="64sp"
android:textColor="#484848"
android:textSize="16sp"
android:ellipsize="end"
android:maxLines="1"
tools:text="அனைத்து அனைத்துஅனைத்து" />
note: don't forget to use android:maxLines attribute to set the line number of your text.

EditText height changes according to the text size

If I change the textsize in editext, the height of edittext changes accordingly. I want the size of edittext to be same and I want a smaller sized hint or text to be in it. What should I do? Following is the code and I am also posting the screenshot of this layout :
<RelativeLayout
android:id="#+id/normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4594e4"
android:orientation="horizontal">
<ImageView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_marginTop="05dp"
android:padding="5dp"
android:src="#drawable/bari" />
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true" />
<EditText
android:id="#+id/etDestination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etOrigin"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:onClick="wow"
android:text="Where to go ?"
android:textColor="#fff"
android:textColorHint="#fff" />
<ImageView
android:id="#+id/swipe"
android:layout_width="30dp"
android:layout_height="27dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/_5sdp"
android:background="#drawable/flip" />
</RelativeLayout>
Screenshot of the layout before changing:
Screenshot of layout after changing textsize:
Try using android:minHeight and android:maxHeight attributes :
<EditText
android:id="#+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_username"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:maxLines="1"
android:gravity="center_vertical"
android:minHeight="?actionBarSize"
android:maxHeight="?actionBarSize"
android:textColor="#color/textPrimary"
android:textSize="12sp"/>
<EditText
android:id="#+id/etUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_username"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:maxLines="1"
android:gravity="center_vertical"
android:minHeight="?actionBarSize"
android:maxHeight="?actionBarSize"
android:textColor="#color/textPrimary"
android:textSize="14sp"/>
Use padding for this :-
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true" />
Give fix height to the layout android:layout_height="75dp"
<EditText
android:id="#+id/etOrigin"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginRight="#dimen/_35sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_toEndOf="#+id/set"
android:layout_toRightOf="#+id/set"
android:background="#5fa7f1"
android:inputType="textPersonName"
android:text="My Location"
android:textColor="#434343"
android:textIsSelectable="true"/>
Here, try this third party library.
Your TextView height would be fixed but your textView size will be adjusted
dependencies {
compile 'me.grantland:autofittextview:0.2.+'
}
Enable any View extending TextView in code:
AutofitHelper.create(textView);
Enable any View extending TextView in XML:
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</me.grantland.widget.AutofitLayout>
Use the built in Widget in code or XML:
<RootElement
xmlns:autofit="http://schemas.android.com/apk/res-auto"
...
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
For more information, visit below link:
https://github.com/grantland/android-autofittextview
You can explicitly set the height and size of your EditText view as well as the text in it.
To set the heigh of the EditText simply use :
android:layout_height="<height_you_want>dp"
And to set height of the text in your EditText view :
android:textSize="<height_you_want>sp"
And you can simultaneously view the changes in the preview tab of AndroidStudio.
One Solution : include these two lines in your XML file
android:maxLines="1"
android:singleLine="true"
Using the EditText inside of a ConstraintLayout will fix this.
...
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="17">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="1"
android:textSize="24sp"
android:text="foo"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

Align row data considering decimal point in listview

I have a table format containing columns and multiple rows like
M.wt
D.wt
Cs.wt
12.000
2.340
0.997
Here M.wt ,D.wt and Cs.wt is the heading column and the values are has shown,i am not able to align the values considering decimal point one below the other,i am getting like
12.000
2.566
5.666
My layout is like code is like
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="12dp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView21"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#000000"
android:textStyle="bold"
android:typeface="serif"
/>
<TextView
android:id="#+id/textView22"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#000000"
android:textStyle="bold"
android:typeface="serif"
/>
</LinearLayout>
How to handle this,it should be handle in layout or java file ?
My screen is actually like so:
You need to format the text that is you are setting to the TextView for the 3 decimal points like below
mwt = 0.2f;
textView2 = (TextView)findViewById(R.id. textView2);
textView2.setText(String.format("%.3f",mwt));
dwt = 0.1f;
textView21 = (TextView)findViewById(R.id.textView21);
textView2.setText(String.format("%.3f",dwt));
Add the gravity property as end and your text view will be
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textColor="#000000"
android:textSize="12dp"
android:textStyle="bold"
android:typeface="serif" />

Android align drawable and text in EditText

i've an edittext :
<EditText android:id="#+id/chp"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="340dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="top"
android:hint="#string/hintCommentaire"
android:textSize="25sp"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/ic_comment"
android:layout_margin="5dp" />
The problem is, i've my hint at the top of the edittext, but the drawable stays in the center..
I tried with "drawableStart", but it's not working.
Someone to help me ?
EDIT : I want the text and drawable in the top(same line). (i want to keep the height of my edittext)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etMobile"
android:hint="Mobile"
android:fontFamily="sans-serif-normal"
android:singleLine="true"
android:textSize="15sp"
android:gravity="center|start"
android:textCursorDrawable="#null"
android:drawableStart="#drawable/ic_profile_bio"
android:drawablePadding="10dp"
app:met_floatingLabel="normal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
Set this Property android:gravity="center|start" in above EditText

Determine which textview should be cut off

In a relativelayout I am using the code below. If the total width is too high tvProgress should be cut off. The problem is that I don't know how to determine which view should be cut off. The current code lets list_lib_episodes_watched_total cut off. How can I determine that tvProgress is cut off instead?
...
<TextView
android:id="#+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/list_lib_et_episodes_watched"
android:layout_toRightOf="#+id/list_lib_cover"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:lines="1"
android:singleLine="true"
android:text="Progress:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/list_lib_et_episodes_watched"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/list_lib_status_spinner"
android:layout_toRightOf="#+id/tvProgress"
android:hint="0"
android:imeOptions="actionSend"
android:inputType="number"
android:maxEms="4"
android:maxLength="4"
android:text="0" />
<TextView
android:id="#+id/list_lib_episodes_watched_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/list_lib_et_episodes_watched"
android:layout_toLeftOf="#+id/list_lib_episodes_watched_increase"
android:layout_toRightOf="#+id/list_lib_et_episodes_watched"
android:lines="1"
android:singleLine="true"
android:text="/0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#707070" />
<ImageButton
android:id="#+id/list_lib_episodes_watched_increase"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/list_lib_et_episodes_watched"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/list_lib_et_episodes_watched"
android:layout_marginLeft="6dp"
android:layout_marginRight="8dp"
android:adjustViewBounds="true"
android:background="#drawable/btn_transparant"
android:contentDescription="Increase by 1"
android:minWidth="60dp"
android:scaleType="centerInside"
android:src="#drawable/ic_increase" />
...
Why cut any view?? It is wrap_content so you cannot determine like that. Put everything inside . Then nothing will be cut. You can scroll and see everything.
ScrollView is like this:
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
---
---
---
</ScrollView>

Categories

Resources