android


how Edittext onclick listener works?


I want to make an activity Which will check the phone number validity.here I have a textview which shows a string "+91" and an edittext where we can input a phone number.
I have made this:--
public class MainActivity extends Activity {
EditText ed;
TextView textv;
Button btn;
String number = "+91";
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ed = (EditText) findViewById(R.id.ed1);
btn = (Button) findViewById(R.id.btn1);
textv = (TextView) findViewById(R.id.txt1);
textv.setText(number);
addlistener();
}
public void addlistener(){
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
String ap;
String edstr = ed.getText().toString();
String txstr= textv.getText().toString();
ap=txstr+edstr;
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber numberProto = phoneUtil.parse(ap, "");
String internationalFormat = phoneUtil.format(numberProto,PhoneNumberFormat.INTERNATIONAL);
Toast.makeText(getApplicationContext(),(phoneUtil.isValidNumber(numberProto) == true ? "Yes" : "No")
+ internationalFormat,
Toast.LENGTH_SHORT).show();
} catch (NumberParseException e) {
Toast.makeText(context,"error",Toast.LENGTH_SHORT).show();
}
}
});
}
}
This code works fine..and showing the result..My requirement is when I enter the phone number in edittext it will automatically show the validation.means in my code I use a button to check the validation.I want this validation as edittext.What I have to do now????
You can use onKeyListener for that edittext, check if pressed key is Enter, validate number.
The code snippet:
ed.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER) {
validateNumber(ed.getText().toString())
}
return false;
}
});
You can also use addTextChangedListener.
Hope this helps.
youredittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//put your validation code here
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Related Links

How to upload File to server from android?
StreetView api doesn't show some places that actually exist - Android
Execution failed for task ':app:transformClassesWithMultidexlistForFreeDebugAndroidTest'
App inventor 2 using TinyDB to store value
Single selection tag (Foursquare-CollectionPicker)
Should I use parse in my andorid app?
Save and sort date and time in SQLite
I don't want to change the position of the listview in android when I reload ListView
list is not displayed after loading the data but when the display is turned off and on list is displayed
Get Google account username of the Google Play account from which the app is installed
Why I cant click on a ListView Item when I added a checkbox next to it in Android
Loop does not delete all needed views from RelativeLayout
application has stopped after runing the application
NullPointerException ContextCompat Error
how to get android system bottom keys back?
Action bar is not hiding completely

Categories

HOME
3d
gradient
nuget
soa
alsa
vxml
regular-language
interpolation
zip
azure-container-service
little-proxy
heap
pega
line
symbol
bitnami
event-sourcing
scsi
asciidoctor-pdf
springfox
flask-sqlalchemy
short-url
bulletphysics
licensing
apktool
bringtofront
tizen-tv
mobx
python-2.6
dumpbin
gnat-gps
carriage-return
sim-toolkit
alphabetical
release
apns-php
spring-mvc-test
signals-slots
tasm
smoothstate.js
outputcache
show-hide
dapper-simplecrud
magento-2.0.7
term
sonarlint-eclipse
java-6
oracleclient
check-mk
type-theory
choice
cronexpression
docpad
builtins
file-uri
jodd
hclust
firebase-security
pre-build-event
yajsw
lz77
relativelayout
directdraw
decimalformat
poppler
toolkit
doctype
linked-tables
snoop
typeconverter
bufferedinputstream
j-security-check
apache-spark-1.3
iiop
iso-prolog
appserver
rmysql
jquery-slider
square-bracket
clickbank
multitasking
eclipse-kepler
neos-server
fb.ui
mkv
mysql-error-1205
escrow
perl5.12
flvplayback
great-circle
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App