03 Aug Android EditText Control
Android EditText is a text view, which can be edited. It is a subclass of TextView.
Usage
Use Android EditText Control for entering values from user, such as, Name, search box etc. Under EditText, you can also mention a text that gives user a hint, for example, Enter Name, Enter Age, etc.
Example
Now, we will write the code xml file.
Follow the below code,
activity_main.xml
For src\main\res\layout\activity_main.xml,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:layout_centerInParent="true" android:layout_below="@id/textView1" /> </RelativeLayout> |
MainActivity.java
The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView. Through this, Set the main layout as your main content view,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.studyopedia.studyopediaapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
AndroidManifest.xml
The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.studyopedia.studyopediaapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
strings.xml
Here’s the code for res/values/strings.xml. The app name and any hardcoded string will be visible,
1 2 3 4 5 6 |
<resources> <string name="app_name">StudyopediaApplication</string> <string name="name">Enter your name</string> </resources> |
Output
It shows our EditText, which asks user to enter the name.
We will also see how it looks when the value is added to the EditText i.e. name,
Now, enter the name under EditText control,
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments