1

does anybody has any idea to increase or decrease the word spacing in the text set on the textview in android. I want to do the word spacing and line spacing by providing any integer value...

Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
  • See http://stackoverflow.com/questions/6863974/android-textview-padding-between-lines. Answers to this question describe how to set line spacing. – Roman Sep 25 '13 at 13:00
  • Thanks for your response.. Its fine with line spacing but wht to do for the word spacing. I really need to do this as per implementation of my app.... thanks again .. as you have solved half of my problem..... – Manmohan Soni Sep 25 '13 at 13:07

2 Answers2

1

May be this could help

By setting this property to your xml you can get it done

android:lineSpacingExtra 
  • 1
    Its fine with line spacing but wht to do for the word spacing. I really need to do this as per implementation of my app.. – Manmohan Soni Sep 25 '13 at 13:08
1

hi i had solved the problem of word spacing by replacing the single space by multiple spaces according to requirement. I am giving an example in which there are one textview and two button one is for increasing the space and one is for decreasing the space. the code is given below and used android:lineSpacingExtra for line spacing as suggested above.

 package com.example.wordspacingexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private TextView mTextView;
    private Button mIncrease;
    private Button mDecrease;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.textView1);
        mIncrease = (Button) findViewById(R.id.btn_increase);
        mDecrease = (Button) findViewById(R.id.btn_decrease);

        mTextView.setTag(" ");
        mIncrease.setOnClickListener(this);
        mDecrease.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.btn_increase) {
            String space = (String) mTextView.getTag();
            String text = mTextView.getText().toString();
            mTextView.setText(text.replace(space, (space += " ")));
            mTextView.setTag(space);

        } else if (v.getId() == R.id.btn_decrease) {
            String space = (String) mTextView.getTag();
            String text = mTextView.getText().toString();
            if (space.length() > 2) {
                mTextView.setText(text.replace(space,
                        space = space.substring(0, space.length() - 2)));
                mTextView.setTag(space);
            } else if (space.length() == 2) {
                mTextView.setText(text.replace(space, " "));
                mTextView.setTag(" ");
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

This the code of the activity with the implementation.

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/hello_world" />


<Button
    android:id="@+id/btn_increase"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Click to increase word space" />

<Button
    android:id="@+id/btn_decrease"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_increase"
    android:layout_centerHorizontal="true"
    android:text="Click to decrease word space" />

Code of the xml file used. ScreenShots are given as:

Image of plane textImage of clicking the increase button Image of clicking more on increase button. Showing the spaced text.

Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29