Site icon WebArtDeveloper

Basic Concepts of EditText control

In this tutorial basic concepts of EditText control of Android is shown

Code:

activity_main.xml

<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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<EditText
android:id=”@+id/et1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”95dp”
android:ems=”10″
android:hint=”Enter a word” />

<Button
android:id=”@+id/b1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:text=”Click Here” />

<TextView
android:id=”@+id/tv1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/b1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”60dp” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.text.InputFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity{

EditText et1;
TextView tv1;
Button b1;
String s;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
tv1=(TextView)findViewById(R.id.tv1);
b1=(Button)findViewById(R.id.b1);

et1.setFilters(new InputFilter[] {new InputFilter.AllCaps(),new InputFilter.LengthFilter(5)});

b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
s=et1.getText().toString();
tv1.setText(s);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

https://youtu.be/3XsK3euAe00

Exit mobile version