punjabidharti
punjabidharti

Reputation: 35

How to set LineSpacing programmatically?

i am new to Android Studio . I want to increase or decrease textView's line spacing on click. currently i am using this one and it works but i want when user click + than line spacing increased and decreased for - .

here is my code.

textView.setLineSpacing(0,1.1f);

i have tried this but not works

private int textSpace = (int) 1.0f; 
    private int diff = (int) 0.1f;

and than this

textSize = textSpace+diff;
textView.setLineSpacing(0,textSize);

same for minus, but its not working , please help

Upvotes: 0

Views: 335

Answers (2)

Patryk Kubiak
Patryk Kubiak

Reputation: 1969

Here is a checked example. I hope everything is well described

// Here put ids of your views
TextView textView = findViewById(R.id.textView);
Button plusButton = findViewById(R.id.plus);
Button minusButton = findViewById(R.id.minus);
        
// The multiplier may be unchanged
final float multiplier = textView.getLineSpacingMultiplier();
        
// Set the appropriate offset
final float offset = 1f;
plusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() + offset, multiplier));
minusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() - offset, multiplier));

Upvotes: 1

Bracadabra
Bracadabra

Reputation: 3659

I suppose you want to achieve something like this:

private int textSize = 20; 

button.setOnClickListerner {
    textSize += diff;
    textView.setLineSpacing(0, textSize);
}

Upvotes: 0

Related Questions