Share via

asking for name

Islam Abuzaid 1 Reputation point
2021-10-29T00:54:41.957+00:00

so im trying to ask for user name and the user will put in his name i used to do that when i was coding on console and it was working not im trying to create an android app using xaml now i asked for the name and it all good how do i get a place for the user to put his name and have my applicatio read ita

Developer technologies | .NET | Xamarin

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-10-29T01:57:10.903+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to create a Xamarin.Android application, then you want to ask user's name in the xaml. Then get the user name 's value in the C# code?

    Firstly, you can create an Xamarin.Android project. Select a blank application.

    Then open the activity_main.xml in the Resource folder-> layout folder.

    Put following code to activity_main.xml.

       <RelativeLayout 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">  
         
           <EditText  
               android:inputType="text"  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:id="@+id/editText1"/>  
           <Button  
               android:layout_below="@id/editText1"  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:id="@+id/button1"  
               android:text="get name"/>  
       </RelativeLayout>  
    

    Then, open the MainActivity.cs, use findviewbyId to get these two controls(edittext and button) in the OnCreate method like following code.

       protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
         
                   EditText editText1 = FindViewById<EditText>(Resource.Id.editText1);  
                   Button button1 = FindViewById<Button>(Resource.Id.button1);  
         
                   button1.Click += (o, e) =>  
                   {  
                       //we can get the user name herer  
                       var UserName=editText1.Text;  
         
                       //display this username in a Toast for testing.  
                       Toast.MakeText(this, UserName, ToastLength.Short).Show();  
                       
                   };  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.