banner



How To Get Text From Each Line From Edittet Andriod

In android, EditText is a user interface control which is used to let the user to enter or modify the text. While using EditText command in our android applications, we need to specify the blazon of data the text field tin have using theinputType attribute.

For example, if it accept evidently text, then we need to specify the inputType every bit "text". In instance if EditText field is for password, then we need to specify the inputType as "textPassword".

In android, EditText control is an extended version of TextView control with additional features and it is used to allow users to enter input values.

In android, nosotros can create EditText command in two means either in XML layout file or create it in Activity file programmatically.

Create a EditText in Layout File

Post-obit is the sample mode to define EditText control in XML layout file in android application.

<? xml version= "one.0" encoding= "utf-viii" ?>
< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :orientation= "vertical" >
<
EditText
android :id= "@+id/txtSub"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :hint= "Subject"
android :inputType= "text" />
</
LinearLayout >

If you find above code snippet, here we defined EditText command to accept plain text past using inpuType as "text" in xml layout file.

Create EditText Control in Activity File

In android, we can create EditText command programmatically in an activeness file to permit users to enter text based on our requirements.

Following is the instance of creating EditText control dynamically in an activity file.

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.
activity_main );
LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.
linearlayout );
EditText et =
new EditText( this );
et.setHint(
"Field of study" );
linearLayout.addView(et);
}
}

Set up the Text of Android EditText

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activeness file.

Post-obit is the case to set the text of TextView command while declaring it in XML Layout file.

< EditText
android :id= "@+id/editText1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Welcome to Tutlane" />

If you lot notice above example we used android:text belongings to the set required text for EditText control in XML Layout file.

Following is another way to set the text of EditText command programmatically in activity file using setText() method.

EditText et = (EditText)findViewById(R.id. editText1 );
et.setText(
"Welcome to Tutlane" );

If you observe above code snippet, we are finding the EditText control which we defined in XML layout file using id property and setting the text using setText() method.

Go Text of Android EditText

In android, we can become the text of EditText control by using getText() method in Activity file.

Following is the instance to go text of EditText command programmatically in action file using getText() method.

public course MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Package savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.
activity_main );
EditText et = (EditText) findViewById(R.id.
txtSub );
String name = et.getText().toString();
}
}

If you lot observe above code snippet, nosotros are finding the EditText control which nosotros defined in XML layout file using id property and getting the text of EditText control using getText() method.

Android EditText Attributes

The post-obit are some of the commonly used attributes related to EditText control in android applications.

Attribute Description
android:id It is used to uniquely identify the control
android:gravity Information technology is used to specify how to align the text like left, right, heart, top, etc.
android:text Information technology is used to set the text.
android:hint It is used to display the hint text when text is empty
android:textColor It is used to modify the color of the text.
android:textColorHint It is used to change the text color of hint text.
android:textSize Information technology is used to specify the size of the text.
android:textStyle It is used to change the style (bold, italic, bolditalic) of text.
android:groundwork It is used to set the background color for edit text control
android:european monetary system It is used to make the textview be exactly this many ems broad.
android:width Information technology makes the TextView be exactly this many pixels broad.
android:height It makes the TextView be exactly this many pixels tall.
android:maxWidth Information technology is used to brand the TextView be at most this many pixels wide.
android:minWidth Information technology is used to brand the TextView exist at to the lowest degree this many pixels wide.
android:textAllCaps Information technology is used to present the text in all CAPS
android:typeface Information technology is used to specify the Typeface (normal, sans, serif, monospace) for the text.
android:textColorHighlight It is used to alter the color of text option highlight.
android:inputType It is used to specify the type of text being placed in text fields.
android:fontFamily It is used to specify the fontFamily for the text.
android:editable If we ready simulated, EditText won't permit the states to enter or alter the text

Android EditText Control Example

Following is the instance of using multiple EditText controls with different input types like password, phone, etc. in LinearLayout to build an android application.

Create a new android application using android studio and requite names equally EditTextExample. In case if you are not aware of creating an app in android studio bank check this article Android Hi Earth App.

Now open an activity_main.xml file from \res\layout path and write the code similar as shown below

activity_main.xml

<? xml version= "i.0" encoding= "utf-8" ?>
< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :paddingLeft= "40dp"
android :orientation= "vertical" android :id= "@+id/linearlayout" >
<
EditText
android :id= "@+id/txtName"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_marginTop= "25dp"
android :ems= "ten"
android :hint= "Name"
android :inputType= "text"
android :selectAllOnFocus= "true" />
<
EditText
android :id= "@+id/txtPwd"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :european monetary system= "ten"
android :hint= "Password 0 to 9"
android :inputType= "numberPassword" />
<
EditText
android :id= "@+id/txtEmai"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :ems= "ten"
android :hint= "Email"
android :inputType= "textEmailAddress" />
<
EditText
android :id= "@+id/txtDate"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_below= "@+id/editText3"
android :ems= "ten"
android :hint= "Date"
android :inputType= "date" />
<
EditText
android :id= "@+id/txtPhone"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :ems= "10"
android :hint= "Phone Number"
android :inputType= "phone"
android :textColorHint= "#FE8DAB" />
<
Push
android :id= "@+id/btnSend"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "submit"
android :textSize= "16sp"
android :textStyle= "normal|bold" />
<
TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :id= "@+id/resultView"
android :layout_marginTop= "25dp"
android :textSize= "15dp" />
</
LinearLayout >

If you lot observe above code we created multiple EditText controls with dissimilar inputTypes, such as password, email address, appointment, phone number, plain text.

Once we are done with the creation of layout with required controls, we need to load the XML layout resources from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.edittextexample path and write the lawmaking similar as shown beneath.

MainActivity.java

bundle com.tutlane.edittextexample;
import android.back up.v7.app.AppCompatActivity;
import android.os.Package;
import android.view.View;
import android.widget.Push button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text; public grade MainActivity extends AppCompatActivity {
Push
btnSubmit ;
EditText
proper noun , password , electronic mail , dob , phoneno ;
TextView
result ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.
activity_main );
proper noun =(EditText)findViewById(R.id. txtName );
password = (EditText)findViewById(R.id. txtPwd );
email = (EditText)findViewById(R.id. txtEmai );
dob = (EditText)findViewById(R.id. txtDate );
phoneno = (EditText)findViewById(R.id. txtPhone );
btnSubmit = (Button)findViewById(R.id. btnSend );
result = (TextView)findViewById(R.id. resultView );
btnSubmit .setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View five) {
if ( name .getText().toString().isEmpty() || password .getText().toString().isEmpty() || email .getText().toString().isEmpty() || dob .getText().toString().isEmpty()
||
phoneno .getText().toString().isEmpty()) {
event .setText( "Please Fill All the Details" );
}
else {
effect .setText( "Name -  " + name .getText().toString() + " \n " + "Countersign -  " + password .getText().toString()
+
" \north " + "E-Mail -  " + email .getText().toString() + " \n " + "DOB -  " + dob .getText().toString()
+
" \due north " + "Contact -  " + phoneno .getText().toString());
}
}
});
}
}

If you notice above code we are calling our layout using setContentView method in the form of R.layout.layout_file_name in our activity file. Here our xml file name is activity_main.xml so we used file name activity_main and nosotros are getting the text of our EditText controls whenever nosotros click on button.

Generally, during the launch of our activity, theonCreate() callback method will be chosen by the android framework to get the required layout for an action.

Output of Android EditText Example

When we run the above example using the android virtual device (AVD) nosotros will get a upshot like as shown beneath.

Android EditText Control Example Result

 If you notice the to a higher place result, the system displaying an appropriate on-screen keyboard for each EditText control based on the defined inputType aspect and displayed a message if we click on the push button without entering details in fields.

Once we enter details in all fields and click on Button we will get a result like as shown beneath.

Android EditText Control Example Result

This is how we can employ EditText control in android applications to allow the user to enter or alter the text based on our requirements.

Source: https://www.tutlane.com/tutorial/android/android-edittext-with-examples

0 Response to "How To Get Text From Each Line From Edittet Andriod"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel