androidline-spacing

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


Solution

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