Sunday, June 7, 2020

How to get Edittext data on button click?

Hello,
This article is for Beginner and people who want to learn Android .This article is about How to get Edittext Data on Button click? Example. 

1. XML File .
activity_main
<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:gravity="center"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"/>
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>

</LinearLayout>

2.Java File
MainActivity -
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 {
EditText edt;
Button btn_show;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=findViewById(R.id.edt);
btn_show=findViewById(R.id.btn_show);
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getdata=edt.getText().toString();
Toast.makeText(MainActivity.this, ""+getdata, Toast.LENGTH_SHORT).show();
}
});




}
}

No comments:

Post a Comment

If you have any doubts,Please let me know

How to exit Application when click on Back Button Android?

Hello, This article is for Beginner and people who want to learn Android .This article is about How to exit Application when click on Back B...