-3

If i enter 5 digits or 6 digits or 7 digits character then I just want to add a space after 2 digit or 3 digits or 4 digits character of a string. Can anyone help me figure out how to do this?

E.g. given "M11AE", I need "M1 1AE" as the result.
E.g. given "B338TH", I need "B33 8TH" as the result.
E.g. given "DN551PT", I need "DN55 1PT" as the result.


 postCodeEt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
 int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int 
   count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

           if ((!s.toString().contains(" ") && s.length() > 5)) {
                s.insert(2, " ");
            }else if (!s.toString().contains(" ") && (s.length()>6)) {
                s.insert(3, " ");
            }else if (!s.toString().contains(" ") && (s.length()>7)) {
                s.insert(4, " ");
            }

        }
    });
}
Anand Gaur
  • 35
  • 6

1 Answers1

1

this may not be the best answer, but it works. I took a textview and edittext. Next, I set the edittext font color to transparent. After that, I went to the java file and on the edittext, I set a text watcher. So whenever the user types something, your textwatcher gets the actual input. So depending on that, you modify the string and set the string on the textview. So this gives the desired illusion. Here is the partial xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:background="#FFFF00"
        android:id="@+id/myTextView"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text=""
        android:gravity="center_vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="200dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="@android:color/transparent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is the java code:

package com.applications.invisible;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView myTextView;
    EditText myEditTExt;
    String actualText;
    String visibleText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        myTextView = findViewById(R.id.myTextView);
        myEditTExt = findViewById(R.id.myEditText);

        myEditTExt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                actualText = myEditTExt.getText().toString();
                visibleText = myVisibleText(actualText);
                myTextView.setText(visibleText);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private String myVisibleText(String actualText) {
        String ret = "";
        if(actualText.length() == 5){
            ret = actualText.substring(0,2)+" "+actualText.substring(2);
        }else if(actualText.length() == 6){
            ret = actualText.substring(0,3)+" "+actualText.substring(3);
        }else if(actualText.length() >= 7){
            ret = actualText.substring(0,4)+" "+actualText.substring(4);
        }else{
            ret = actualText;
        }
        return ret;
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26