How can I set the position of the text?

I have a problem with the fact that I want to set the position of the text in a specific place.

For example: I have this text in my text view: abcd efgh ijkl mnop qrstu vwxyz

Now I want this text to show from a location efghand not abcdHow can I perform this operation in a textview? I want to change the position of the text, not the position of the text view.

thank

+3
source share
1 answer

Nikki, just add the marginLeft attribute to your TextView, for example:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="Testing"
    />

where you simply substitute whatever value you want for the left margin.

EDIT: for code, you should be able to use the following:

TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom); //use ints here
tv.setLayoutParams(lp);

And damn it, here's how to do it in one line :)

TextView tv = new TextView(this).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT).setMargins(left, top, right, bottom));
+5

Source: https://habr.com/ru/post/1778895/


All Articles