1. How to use simple TextView in android
Write this code in .xml
file
<LinearLayout 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#195ced"
android:textAlignment="center"
android:text="Hello android"/>
</LinearLayout>
2. How to use simple EditText in android
Write this code in .xml
file
<LinearLayout 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:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:hint="Enter password here"
/>
</LinearLayout>
3. How to get the value from EditText in android
Step 1:Write this code in .xml
file
<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/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the data"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Value"/>
</LinearLayout>
Step 2:Write this code in .java
file
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.et);
button=(Button)findViewById(R.id.btn);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String value=editText.getText().toString();
Toast.makeText(this,"Value is "+value,Toast.LENGTH_SHORT).show();
}
}
4. EditText input to TextView output
Step 1:Write this code in .xml
file
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your text"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Get Value"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#195ced"
android:textAlignment="center"
android:text=""/>
</LinearLayout>
Step 2:Write this code in .java
file
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editText;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext);
button=(Button)findViewById(R.id.btn);
button.setOnClickListener(this);
textView=(TextView)findViewById(R.id.textview);
}
@Override
public void onClick(View view) {
String value=editText.getText().toString();
textView.setText("Your text is "+value);
}
}
5. Build a simple Calculator using EditText and TextView in android
Step 1:Write this code in .xml
file
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.example.toto.mycalc.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="50sp"
android:text="Calculator"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="First Number"/>
<EditText
android:id="@+id/firstno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:inputType="number"
android:hint="Enter the First Number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="30sp"
android:text="Second Number"/>
<EditText
android:id="@+id/secondno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter the Second Number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:weightSum="4"
android:orientation="horizontal">
<Button
android:id="@+id/badd"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add (+)"/>
<Button
android:id="@+id/bsub"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sub (-)"/>
<Button
android:id="@+id/bmul"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mul (*)"/>
<Button
android:id="@+id/bdiv"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Div (/)"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tvresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
</LinearLayout>
Write this code in .java
file
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button badd, bsub, bmul, bdiv;
private TextView tvresult;
private EditText firstno, secondno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
badd = (Button) findViewById(R.id.badd);
bsub = (Button) findViewById(R.id.bsub);
bmul = (Button) findViewById(R.id.bmul);
bdiv = (Button) findViewById(R.id.bdiv);
firstno = (EditText) findViewById(R.id.firstno);
secondno = (EditText) findViewById(R.id.secondno);
tvresult = (TextView) findViewById(R.id.tvresult);
badd.setOnClickListener(this);
bsub.setOnClickListener(this);
bmul.setOnClickListener(this);
bdiv.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String num1 = firstno.getText().toString();
String num2 = secondno.getText().toString();
switch (view.getId()) {
case R.id.badd:
int addition = Integer.parseInt(num1) + Integer.parseInt(num2);
tvresult.setText("Addition : " + String.valueOf(addition));
break;
case R.id.bsub:
int subtraction = Integer.parseInt(num1) - Integer.parseInt(num2);
tvresult.setText("Subtraction : " + String.valueOf(subtraction));
break;
case R.id.bmul:
int multiplecation = Integer.parseInt(num1) * Integer.parseInt(num2);
tvresult.setText("Multiplecation : " + String.valueOf(multiplecation));
break;
case R.id.bdiv:
try {
float division = Float.parseFloat(num1) / Float.parseFloat(num2);
tvresult.setText("Division : " + String.valueOf(division));
} catch (Exception e) {
tvresult.setText("Can not Division posible");
}
break;
}
}
}