Aligning Text View with constraint LayoutHow do I center text horizontally and vertically in a TextView?Fling gesture detection on grid layoutHow to align views at the bottom of the screen?Can I underline text in an Android layout?Auto Scale TextView Text to Fit within BoundsAndroid - tiled backgrounds occasionally get stretchedHow do I play an audio file in Android?Right align text in android TextViewWhat's “tools:context” in Android layout files?Android Studio Layout Errors
Did depressed people far more accurately estimate how many monsters they killed in a video game?
Compressed gas thruster for an orbital launch vehicle?
Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?
How does one acquire an undead eyeball encased in a gem?
Optimization models for portfolio optimization
Would a carnivorous diet be able to support a giant worm?
Found and corrected a mistake on someone's else paper -- praxis?
What was the profession 芸者 (female entertainer) called in Germany?
Intern not wearing safety equipment; how could I have handled this differently?
Generalized Behrend version for Grothendieck-Lefschetz trace formula
No Torah = Revert to Nothingness?
Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?
Party going through airport security at separate times?
How do you move up one folder in Finder?
My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?
Appropriate conduit for several data cables underground over 300' run
I make billions (#6)
Did the Ottoman empire suppress the printing press?
primary key constraint Sorting
Adjust the Table
Is there a method for differentiating informative comments from commented out code?
Need a non-volatile memory IC with near unlimited read/write operations capability
Why did Old English lose both thorn and eth?
What was the profession 芸者 (female entertainer) called in Russia?
Aligning Text View with constraint Layout
How do I center text horizontally and vertically in a TextView?Fling gesture detection on grid layoutHow to align views at the bottom of the screen?Can I underline text in an Android layout?Auto Scale TextView Text to Fit within BoundsAndroid - tiled backgrounds occasionally get stretchedHow do I play an audio file in Android?Right align text in android TextViewWhat's “tools:context” in Android layout files?Android Studio Layout Errors
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to align my textview in the constraint layout without using margins so the layout can be responsive in all the devices but so far I am stuck in positioning. Here is my code and expected output attached in the image. I just want to make the amount align slightly with currency text.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
android textview android-constraintlayout
|
show 1 more comment
I am trying to align my textview in the constraint layout without using margins so the layout can be responsive in all the devices but so far I am stuck in positioning. Here is my code and expected output attached in the image. I just want to make the amount align slightly with currency text.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
android textview android-constraintlayout
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45
|
show 1 more comment
I am trying to align my textview in the constraint layout without using margins so the layout can be responsive in all the devices but so far I am stuck in positioning. Here is my code and expected output attached in the image. I just want to make the amount align slightly with currency text.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
android textview android-constraintlayout
I am trying to align my textview in the constraint layout without using margins so the layout can be responsive in all the devices but so far I am stuck in positioning. Here is my code and expected output attached in the image. I just want to make the amount align slightly with currency text.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintEnd_toEndOf="@+id/yearBarChartAmount"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="@id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="RM"/>
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tOTAL"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toEndOf="@id/yearBarChartCurrency"
app:layout_constraintTop_toBottomOf="@id/year_bar_chart_total"
tools:text="233.34"/>
</android.support.constraint.ConstraintLayout>
android textview android-constraintlayout
android textview android-constraintlayout
edited Mar 26 at 13:27
Cheticamp
32.4k5 gold badges33 silver badges67 bronze badges
32.4k5 gold badges33 silver badges67 bronze badges
asked Mar 25 at 23:21
Chinthaka DevindaChinthaka Devinda
3212 gold badges6 silver badges21 bronze badges
3212 gold badges6 silver badges21 bronze badges
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45
|
show 1 more comment
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45
|
show 1 more comment
2 Answers
2
active
oldest
votes
You can use guidelines to achieve this :
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
It will look like this:
I must emphasize one thing that really bothered me:
"margins so the layout can be responsive in all the devices" - this is not true and let me explain.
what makes your screen responsive to all screen sizes is constraintLayout and how you use it, I agree that a large number of margin in dp
will make your screen not responsive but small margins are recommended to use in google material design - it will actually give your app better look and prevent your views from being attached directly to the parent border.
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
add a comment |
I assume you want to align the tops of the text.
A TextView
has an internal structure based upon the typography of the font. "Android 101: Typography" has a good explanation of Android typography. This diagram is particularly useful.
So, the following layout looks like this in design view. As you can see, the tops of the text do not line up.
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
Although the fonts of the TextViews
are the same, the different font sizes prevent alignment of the tops of the actual text even though the TextViews
have their tops align. This is because the metrics differ.
So, to align the actual tops of the text, we need to determine how far below the TextView
tops the actual text starts and shift the text by those amounts. The following code does this. Comments are in the code.
MainActivity.java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
private int getTopOfText(TextView textView)
// Force measure of text pre-layout.
textView.measure(0, 0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
The following is what is displayed:
There may be other considerations, but this is the gist of the solution. It may be worthwhile to encapsulate this into a custom TextView
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347778%2faligning-text-view-with-constraint-layout%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use guidelines to achieve this :
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
It will look like this:
I must emphasize one thing that really bothered me:
"margins so the layout can be responsive in all the devices" - this is not true and let me explain.
what makes your screen responsive to all screen sizes is constraintLayout and how you use it, I agree that a large number of margin in dp
will make your screen not responsive but small margins are recommended to use in google material design - it will actually give your app better look and prevent your views from being attached directly to the parent border.
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
add a comment |
You can use guidelines to achieve this :
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
It will look like this:
I must emphasize one thing that really bothered me:
"margins so the layout can be responsive in all the devices" - this is not true and let me explain.
what makes your screen responsive to all screen sizes is constraintLayout and how you use it, I agree that a large number of margin in dp
will make your screen not responsive but small margins are recommended to use in google material design - it will actually give your app better look and prevent your views from being attached directly to the parent border.
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
add a comment |
You can use guidelines to achieve this :
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
It will look like this:
I must emphasize one thing that really bothered me:
"margins so the layout can be responsive in all the devices" - this is not true and let me explain.
what makes your screen responsive to all screen sizes is constraintLayout and how you use it, I agree that a large number of margin in dp
will make your screen not responsive but small margins are recommended to use in google material design - it will actually give your app better look and prevent your views from being attached directly to the parent border.
You can use guidelines to achieve this :
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balance"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="233.34"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
It will look like this:
I must emphasize one thing that really bothered me:
"margins so the layout can be responsive in all the devices" - this is not true and let me explain.
what makes your screen responsive to all screen sizes is constraintLayout and how you use it, I agree that a large number of margin in dp
will make your screen not responsive but small margins are recommended to use in google material design - it will actually give your app better look and prevent your views from being attached directly to the parent border.
answered Mar 26 at 11:11
Tamir AbutbulTamir Abutbul
3,4544 gold badges10 silver badges29 bronze badges
3,4544 gold badges10 silver badges29 bronze badges
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
add a comment |
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
agree @Tammir Abutbul
– Chinthaka Devinda
Mar 26 at 13:35
add a comment |
I assume you want to align the tops of the text.
A TextView
has an internal structure based upon the typography of the font. "Android 101: Typography" has a good explanation of Android typography. This diagram is particularly useful.
So, the following layout looks like this in design view. As you can see, the tops of the text do not line up.
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
Although the fonts of the TextViews
are the same, the different font sizes prevent alignment of the tops of the actual text even though the TextViews
have their tops align. This is because the metrics differ.
So, to align the actual tops of the text, we need to determine how far below the TextView
tops the actual text starts and shift the text by those amounts. The following code does this. Comments are in the code.
MainActivity.java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
private int getTopOfText(TextView textView)
// Force measure of text pre-layout.
textView.measure(0, 0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
The following is what is displayed:
There may be other considerations, but this is the gist of the solution. It may be worthwhile to encapsulate this into a custom TextView
.
add a comment |
I assume you want to align the tops of the text.
A TextView
has an internal structure based upon the typography of the font. "Android 101: Typography" has a good explanation of Android typography. This diagram is particularly useful.
So, the following layout looks like this in design view. As you can see, the tops of the text do not line up.
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
Although the fonts of the TextViews
are the same, the different font sizes prevent alignment of the tops of the actual text even though the TextViews
have their tops align. This is because the metrics differ.
So, to align the actual tops of the text, we need to determine how far below the TextView
tops the actual text starts and shift the text by those amounts. The following code does this. Comments are in the code.
MainActivity.java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
private int getTopOfText(TextView textView)
// Force measure of text pre-layout.
textView.measure(0, 0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
The following is what is displayed:
There may be other considerations, but this is the gist of the solution. It may be worthwhile to encapsulate this into a custom TextView
.
add a comment |
I assume you want to align the tops of the text.
A TextView
has an internal structure based upon the typography of the font. "Android 101: Typography" has a good explanation of Android typography. This diagram is particularly useful.
So, the following layout looks like this in design view. As you can see, the tops of the text do not line up.
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
Although the fonts of the TextViews
are the same, the different font sizes prevent alignment of the tops of the actual text even though the TextViews
have their tops align. This is because the metrics differ.
So, to align the actual tops of the text, we need to determine how far below the TextView
tops the actual text starts and shift the text by those amounts. The following code does this. Comments are in the code.
MainActivity.java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
private int getTopOfText(TextView textView)
// Force measure of text pre-layout.
textView.measure(0, 0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
The following is what is displayed:
There may be other considerations, but this is the gist of the solution. It may be worthwhile to encapsulate this into a custom TextView
.
I assume you want to align the tops of the text.
A TextView
has an internal structure based upon the typography of the font. "Android 101: Typography" has a good explanation of Android typography. This diagram is particularly useful.
So, the following layout looks like this in design view. As you can see, the tops of the text do not line up.
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4778C5"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/year_bar_chart_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.7"
android:text="Balance"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/yearBarChartCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RM"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="@+id/year_bar_chart_total"
app:layout_constraintTop_toBottomOf="@+id/year_bar_chart_total"
tools:text="RM" />
<TextView
android:id="@+id/yearBarChartAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="4sp"
android:paddingLeft="4sp"
android:text="233.34"
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/yearBarChartCurrency"
app:layout_constraintTop_toTopOf="@+id/yearBarChartCurrency" />
</android.support.constraint.ConstraintLayout>
Although the fonts of the TextViews
are the same, the different font sizes prevent alignment of the tops of the actual text even though the TextViews
have their tops align. This is because the metrics differ.
So, to align the actual tops of the text, we need to determine how far below the TextView
tops the actual text starts and shift the text by those amounts. The following code does this. Comments are in the code.
MainActivity.java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView yearBarChartAmount = findViewById(R.id.yearBarChartAmount);
TextView yearBarChartCurrency = findViewById(R.id.yearBarChartCurrency);
yearBarChartCurrency.setTranslationY(-getTopOfText(yearBarChartCurrency));
yearBarChartAmount.setTranslationY(-getTopOfText(yearBarChartAmount));
private int getTopOfText(TextView textView)
// Force measure of text pre-layout.
textView.measure(0, 0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s, 0, s.length(), bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
return bounds.top;
The following is what is displayed:
There may be other considerations, but this is the gist of the solution. It may be worthwhile to encapsulate this into a custom TextView
.
edited Mar 26 at 13:28
answered Mar 26 at 13:04
CheticampCheticamp
32.4k5 gold badges33 silver badges67 bronze badges
32.4k5 gold badges33 silver badges67 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347778%2faligning-text-view-with-constraint-layout%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Anyone got an Idea?
– Chinthaka Devinda
Mar 25 at 23:31
It is aligned at the top isn't it?
– forpas
Mar 25 at 23:38
@forpas Yeah i am trying to make it as in the picture without using any margins
– Chinthaka Devinda
Mar 25 at 23:41
Then change the fontsize because you want the amount bigger and you're done.
– forpas
Mar 25 at 23:42
@forpas Okay do you think is there a better solution than that? may be using ratios etc..
– Chinthaka Devinda
Mar 25 at 23:45