Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How do I set a guideline at 15% of the screen on Android Studio?

So I am working on a little project in android studio and I am using a constraintlayout. I want to place a guidline but the only option that will help me with placement that I have seen uses dp and I don't know the exact dp point that will give me 15%. Is there a way for me to even do this?

1 Answer

Fatemah Tavakoli
Fatemah Tavakoli
13,797 Points

Hi Ibrahim,

If I understood correctly, you are trying to change the guideline percentage in constraintLayout programmatically.
you can use the ConstraintLayout.LayoutParams.

We try to get the specific parameter form constraintLayout and modify it.

Guideline guideLine = (Guideline) findViewById(R.id.your_guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
params.guidePercent = 0.15f; // 15% // range: 0 <-> 1
guideLine.setLayoutParams(params);

hope it helps :)

I tried the app:layout_constaruntGuide_percent and it did exactly what I was looking for. But I really appreciate your answer so much!

 <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.75" />