To create a SMS character counter for an edittext text in your app, all you need is a Text Watcher. Text Watcher which listens to the change in text in the edit text. Through it you can listen to three events, they are: beforeTextChanged, onTextChanged, afterTextChanged. In this process we listen to onTextChanged. Here is the whole code you need to create the character counter:
public class MainActivity extends ActionBarActivity {
int textLeft;
int smsNo = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView counter = (TextView) findViewById(R.id.textView1);
EditText edittext = (EditText) findViewById(R.id.edit_text1);
final TextWatcher watcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int sms = s.length() / 160;
float d = (float) s.length() / 160;
double textLeftt = 160 * (1 - (d - sms));
textLeft = (int) Math.round(textLeftt);
smsNo = sms + 1;
counter.setText(textLeft + "/160 (" + smsNo + ")");
}
public void afterTextChanged(Editable s) {
}
};
edittext.addTextChangedListener(watcher);
}
}
Here, SMS is the no of sms text messages, 160 is the standard no of characters an sms messages has. Emojis occupy two characters in an SMS message.
Your story is truly inspirational and I have learned a lot from your blog. Much appreciated.
ReplyDeleteselenium Training in chennai
amazon web services Training in chennai
Block Chain Training in velachery
The admin of this blog has really shared a needed information in a simple way. Kindly continue sharing this king of useful information.
ReplyDeleteTOEFL Training Institute in Adyar
TOEFL Classes in ECR
TOEFL in Shasthri Nagar
TOEFL Training near me
TOEFL Coaching in T-Nagar
TOEFL Classes at Ashok Nagar
TOEFL Coaching near me
It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
ReplyDeleteData science Course Training in Chennai | No.1 Data Science Training in Chennai
RPA Course Training in Chennai | No.1 RPA Training in Chennai
AWS Course Training in Chennai | No.1 AWS Training in Chennai
Devops Course Training in Chennai | Best Devops Training in Chennai
Selenium Course Training in Chennai | Best Selenium Training in Chennai
Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
ReplyDeleteAWS Training in Chennai | Best AWS Training in Chennai
Best Data Science Training in Chennai
Best Python Training in Chennai
Best RPA Training in Chennai
Digital Marketing Training in Chennai
Matlab Training in Chennai
<
og youtube
ReplyDelete