Get User Input in Android · 1. Create an Android Project. To create an android project, · 2. Add TextView for Label. Select Graphical layout view ...
Since
2008
GetUserInputinAndroid
LastmodifiedonNovember30th,2014byJoe.
IhavewrittenanAndroidHelloWorldtutorialsometimeback.Wouldn’titbeniceifweaddsomesimpleuserinteractiontoit.Assimpleasgetatextinputfromuseranddisplayit.ThistutorialwillhelpyoudothatandwillserveasanintroductiontouseofAndroidformwidgets.
1.CreateanAndroidProject
Tocreateanandroidproject,
GotoFilemenu,thenselectnew->projectorelseclicknewiconintoolbar.
SelectwizardasAndroid->AndroidApplicationprojectandclickNext.
CreatenewApplicationwindowwillbeopen.EnterApplicationName,ProjectNameandPackageandclickNexttocontinue.
SelectlaunchericonforyourapplicationandclickNext.
SelectActivityfromgiventwotypesasBlankactivityandMasterDetailsflowactivity.
EnterActivityName,Layoutnameandotherdetailstocreateactivity.
FinallyclickFinishtocompletethisstep.
Note:
TheBlankActivityandMasterDetailFlowactivityistocapturestylepropertytodesigngoodlookingappquickly.
AppLayout
Aftercompletingabove,thelayoutiscreatedasaresourcefileinthispathworkspace/GetUserInput/res/layout/activity_frm.xml.Bydefaultitcontainsthefollowingcodeandweneedtoworkongraphicalviewofthelayouttogivecontrolwithsomebasicformwidgets.
2.AddTextViewforLabel
SelectGraphicallayoutviewandremovedefaultstringandfollowthestepsbelow,
DraganddroptwoTextview(Large,Medium..)fromformwidgetinleftpanel(Oneforlabelofinputandtheanotheroneforwelcomemessage)
SelectTextpropertyinrightpanelandBrowsetoaddnewString.OnClickbrowseResourceChooserwindowwillopen.
Select“NewString..”andentertheStringtobedisplayedandresourceidentifierandclickOK.
Then,choosethenewlyaddedresourcefrom‘ResourceChooser’andclickOK.
NowfollowingcodewillbecreatedtodisplayTextview,
3.AddEditTextforUserInput
SimilarlydragEditTextboxfromlistofTextFieldsandputitinlayoutnexttolabel.Thisboxhastheinputtypeofpersonname.Afterthis,followingcodewillbegeneratedinthexmlfile.
4.AddButtonforUserSubmission
Toaddbutton,theformwidgetmenuhastobeexpandedandbuttoncanbedraggedfromthere.Text,textcolorandbackgroundcolorofthebuttonischangedby‘Referencechooser’windowandpropertybar.Aftercreatingthebuttoncodewillbeasfollows.
5.ButtonHandler
Tillthisstep,everythingisdonewithdesignview.ButthisstepisaccomplishedbyaddingaListenerintosourcefile(java)whichwillbeinpathworkspace/GetUserInput/src/com/javapapers/android/form/FrmActivity.javaThisistheActivityfile.
ToaddListenerfollowingcodehastobeadded,
mButton=(Button)findViewById(R.id.button1);
mButton.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewview){
....
....
....
}
});
OnClickListeneriseventhandlerwhichwillbeinvokedonclickingtheSubmitbutton.
Viewclassinstanceisresponsibleforthehandlingtheevent.
6.ReadInputfromEditBoxControl
Userinputisreadbyinstancesofformcontrols.TheinputenteredbytheuserisreadbygetText()methodthatiscalledbyEditboxinstancemEdit.
ButtonmButton;
EditTextmEdit;
TextViewmText;
mEdit=(EditText)findViewById(R.id.editText1);
mEdit.getText().toString();
Andthen,theinstanceofTextviewiscreatedtoshowthewelcomemessage.Thiswillbedonebythefollowingcode.
mText=(TextView)findViewById(R.id.textView1);
mText.setText("Welcome"+mEdit.getText().toString()+"!");
CompleteSourceCodeandOutput
DownloadAndroidProjectSourceCodeforGettingUserInput
Activityfrm.xml
FrmActivity.java
packagecom.javapapers.android.form;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
publicclassFrmActivityextendsActivity{
ButtonmButton;
EditTextmEdit;
TextViewmText;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frm);
mButton=(Button)findViewById(R.id.button1);
mButton.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewview){
mEdit=(EditText)findViewById(R.id.editText1);
mText=(TextView)findViewById(R.id.textView1);
mText.setText("Welcome"+mEdit.getText().toString()+"!");
}
});
}
}
Output
PopularArticles
AndroidSQLiteDatabase
AndroidHelloWorld
UsingDatabasefromanAndroidApplication
Commentson"GetUserInputinAndroid"
Priyasays:
05/09/2012at10:31pm
Iamnotanexpertinjavaandthoughtcreatingandroidapplicationwasdifficult.
AlsoIfearedlearningmobileapplicationdevelopmentasitiscompletelynewforme.Idon’thaveanandroidmobiletoo.
Withallthese,justreadingtwoarticlesfromyourblogmadeHUGEdifference.Yourandroidhelloworldandthisarticlehasgivenmegreatconfidence.Sosimpleeasytounderstandandcreateandroidapplication.
AlreadyIamfeelingthatIknowthebasicsofandroid.Thanks,Thanks.
DeepeshUniyalsays:
05/09/2012at11:05pm
HiSir,
Greattutorialinsimplewords,
IamnewinandroidbutIknowjava,Inandroid,designingpartisdifficultformeItstakingtoomuchtime.
sopleasesuggestsomedesigningtricks.
thanks
Vincysays:
06/09/2012at10:00am
@DeepeshUniyal,
Toalignthecomponents,insteadofmovingaroundusingmouse,Iprefertousethepropertiespadding/margin/widthtofixtheposition.Thiswillbeeasiertodesign.
Propertycanbechangedusingdesignxml(activity_frm.xml)withgraphicview.
Draganddroptheneededformcontrolsfromleftpalletandusepropertypanelinoutlineviewforsettingvalues.
Raghunandanasays:
06/09/2012at1:44pm
NiceJoe
RashiCoorgsays:
06/09/2012at7:29pm
NiceOne..Thanksalot..
Dasarathansays:
06/09/2012at7:45pm
Whatistheeclipseversiontobeusedinordertocreateandroidapp
SUNILKUMARsays:
06/09/2012at9:17pm
NiceWork:)
manisays:
07/09/2012at7:08pm
ThanksJoe,ThisArticleissimplysuperb!!!andveryeasytounderstand.pleaseprovideanarticleaboutJPAandJDO.
sashisays:
09/09/2012at9:01am
HiJoe,
Thisarticlereallyniceoneforbeginnersofandroidapplication.
Niceeffort..
karansays:
09/09/2012at9:03pm
keepwrtingimlookingforwardforsuchandroidexample
vidyasagarsays:
10/09/2012at12:10pm
GoodworkJoe..!Nowyoucankeepabuttonforandroidarticlesinthemainmenu.
Vincysays:
12/09/2012at10:54am
@Dasarathan,
EclipseJunoforJ2EEprogrammerwillbesuitableforandroidapplicationdevelopment.
Anonymoussays:
17/09/2012at12:36pm
HiJoe,
Goodwork.Reallysimpleandeasytounderstand.
Ifpossiblecanyoupleasewriteaboutintentsintentfiltersandbroadcastreceiversinandroid,astheseareveryconfusingandIamnotabletounderstandthemclearly.
ThankYou
Harshalsays:
21/09/2012at1:00pm
Heyyourguidelinesarereallyhelpfulformeinmyproject.MyrequestyoutowritesimilarondisplayingIndiclanguages(likeHindi,Marathi,etc)inandroid.Doesandroidsupportunicodecharacters?
Joesays:
21/09/2012at9:36pm
@Harshal,
Androidhassupport,sureverysoonIwillwriteatutorialonthis.
Harshalsays:
06/10/2012at12:34pm
ThanksJoe.
Iameagerlywaiting…….
Amysays:
07/01/2013at4:50am
Thankyousomuch.Everyothertutorialforcreatinganinputtextboxwasconfusing.YourswasexactlywhatIneeded.Thankyou!
jyotimaimtsays:
11/01/2013at12:30pm
thnksss..nicetutorial.butineedhowtheusergettedinputtosetlarge
akkisays:
05/02/2013at1:02pm
hicouldutellmehowtovalidatefullnameforpersonfieldusingedittextinandroidusingxmlcode
AndroidActivitysays:
19/05/2013at3:50pm
[…]inEclipseandbysettingappropriatepropertiesforthem.Youmayreferourpreviousarticleongetuserinputtoknowaboutcreatingtheseelementsin[…]
EverythingInJavaFanaticsays:
20/05/2013at11:44am
Ireallylikedthistutorial,outofthemanyXMLbasedinputmethodsavailable.AlthoughImuchprefercodingcompletelyinjava,Iknowthisispossibleandamcurioseifyoucoulddoasimilartutorialonthatratherthanusingthird-partyXMLfiles.
Evensoyourtutotialwasveryhelpful,thankyou.
Notonlythatbutitwaswellformatted(withsyntaxpluginsetc).
So,thankyoubutpleaserespondtomyrequest,andIwouldbeextremelygatefulifyoueithermadeanewtutorialonthesubject,linkedmetoone,orevenexplaineditwithinaresponce.
Thanks,
:)
nandinisays:
30/05/2013at12:47pm
itisveryhelpfulforme.iamnewinbothjavaandandroid.thanks.
MRVsays:
08/06/2013at1:57pm
it’sveryhelpfullforme..thnaxjavapaer.com
sanasays:
13/07/2013at3:25pm
Newone
shitalpachpandesays:
26/08/2013at11:13am
Hisir,
Itsverynicepresentation.
Itsveryeasytounderstand.
Thanxjavapaer.com.
Jacquettasays:
11/12/2013at6:48pm
I’mnotthatmuchofainternetreadertobehonestbutyoursites
reallynice,keepitup!I’llgoaheadandbookmarkyour
websitetocomebacklater.Manythanks
yuvasays:
06/03/2014at4:27pm
Higuysiwillstartanewcompanyforandroidteachingmethodologiesverysoon.andyouguysmayget100appsfromthereit’scompletelyfree.
AndroidDatePickersays:
22/07/2014at4:47pm
[…]havealreadyseenthisstepmultipletimes.ReferGetUserInputinAndroidtutorialtorecollectthose[…]
Commentsareclosedfor"GetUserInputinAndroid".
Subscribe:
JavapapersFacebookPage
©2008-2019Javapapers