EditText and TextView are the subclass of View Class.
EditText takes the input from the user and TextView shows the output to the user.
actvity_main.xml
output
EditText takes the input from the user and TextView shows the output to the user.
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:id="@+id/name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:textColor="@android:color/holo_orange_dark" android:textSize="18dp" /> <EditText android:id="@+id/take_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:hint="Please enter your name" /> <Button android:id="@+id/click_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="selfDestruct" android:text="@string/button" /> </LinearLayout>
MainActivity.java
package sravani.com.buttonexample;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button click_button;
EditText take_name;
TextView name_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click_button = (Button) findViewById(R.id.click_button);
take_name = (EditText) findViewById(R.id.take_name);
name_tv = (TextView) findViewById(R.id.name_tv);
click_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name_tv.setText("Welcom to " + take_name.getText().toString()+" Android Tutorial");
}
});
}
}
output
No comments:
Post a Comment