How to create input box in android android dialog
Hi, we will see how to create a inputbox in
android. to create a inputbox, the steps of
creating the dialog is used. In addition to
that we have to add a textview to that.
first we will create a button, the onlclick
event of this button will create a inputbox.
We will now create a dialog.
Now lets create a edittext, so that users
will enter the data in this edittext.
now lets add this edittext to our dialog
the dialog contains 2 type of buttons
1) positivebutton
2) negative button
we will add a positive button first.
now the positive button's text is OK
when this button is clicked, we have to
get the text entered by user into the textview,
if negative button is clicked, lets display
no text entered.
the final step is to show the dialog.
lets check
please subscribe our channel to get more
videos.
package com.example.andtest;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndTest extends Activity {
Button but;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_and_test);
but=(Button) findViewById(R.id.button1);
txt=(TextView) findViewById(R.id.textView1);
but.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder ab=new AlertDialog.Builder(AndTest.this);
ab.setTitle("Enter any string");
final EditText et=new EditText(AndTest.this);
ab.setView(et);
ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
txt.setText(et.getText().toString());
}
});
ab.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
txt.setText("No Text entered");
}
});
AlertDialog a=ab.create();
a.show();
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_and_test, menu);
return true;
}
}