Home > Software engineering >  How to set LineSpacing programmatically?
How to set LineSpacing programmatically?

Time:09-30

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

CodePudding user response:

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));

CodePudding user response:

I suppose you want to achieve something like this:

private int textSize = 20; 

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