0

In my app, I want to count the number of charters in the edittext..,and after the character count has reached to a limit, counter should again start with 140 and I want to display it in the format of " 140/1 ",after character limit reached to 0 .,it again stat with 140 but with increment in 1 by 1,and display as "140/2".how can i do that any suggestion..??

edMessage.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {

                // Display Remaining Character with respective color
                 count = MAX_COUNT - s.length();
               Word_counter.setText(Integer.toString(count) + "/" + Integer.toString(word)  );

               Word_counter.setTextColor(Color.BLACK);
                if(count < 10)
                {
                     Word_counter.setTextColor(Color.YELLOW);
                }


            }
        });
Kailas
  • 7,350
  • 3
  • 47
  • 63
user3683036
  • 135
  • 1
  • 2
  • 15

1 Answers1

0

Try this way,hope this will help you to solve your problem.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private EditText editText;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textView);

        editText.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.length()/140 == 0){
                    textView.setText("140");
                }else{
                    textView.setText("140/"+String.valueOf((s.length()/140)+1));
                }
            }
        });

    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • @ Haresh i want it display as 140/1 when s.length() ==140,user can type again but,display as 140/2.,and here 140 is the number of character he can type in one message. – user3683036 Jun 30 '14 at 06:13
  • @user3683036,can you please explain me more specific in my code i show no of character count user type after 140 he can not able type more character. – Haresh Chhelana Jun 30 '14 at 06:28
  • yes but i want after 140 characters user can type but and s.length again starts with 140 count..and a toast appear that tell him that 2nd msg has been start – user3683036 Jun 30 '14 at 06:36
  • @ Haresh not showing me the number of character i typed in edittext from 140 and remaining i left with .,u made it fix as 140.. – user3683036 Jun 30 '14 at 07:02