I am developing a android app where I have created Customize alert dialog. I declare Globally alert dialog and and AlertDialog.builder as follow. Now I am calling three method f1(), f2(),f3(), in button click.
btn_my_order.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
f1();
f2();
f3();
return false;
}
});
I Declared orderDialog and builde globally as follow :-
private AlertDialog orderDialog = null;
AlertDialog.Builder builder;
My f1() block is as follow :-
F1{
builder = new AlertDialog.Builder(MainScreen.this);
mContext = getApplicationContext();
/**
* by the help of inflater my ordre is showing in list view
*/
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);
ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);
bclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
orderDialog.dismiss();
System.out.println(" click on close button");
}
});
/**
* click of place order to kitchen
*/
bPlaceOrder.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Place order click");
palceMyOrdertoServer();
new SendOrderFromTable().execute();
System.out.println("place order to server is called");
String msg = "Your Order is Successfully placed to Kitcken";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
orderDialog.dismiss();
}
});}
My f2() is for some Cursor work with data base
F2{
// many stuff to be here populate data from cursor and bind it with adapter
// no any issue in this mehod
}
Now finally I am calling f3()
F3{
builder.setView(orderDialogLayout);
orderDialog = builder.create();
orderDialog.show();
}
Now i am going to explain all my problem f1() method is for initialization f2() is for populate data and f3() to show customize alert dialog . Why my
orderDialog.dismiss();
is not working for me.Even though i am able to see my logcat with message
"Click on close button"
That means execution is going on dismiss() method then why customize alert dialog didn't close at click. Thanks in advance to all
You should add final in your orderDialog private variable.
Related
I have an activity with a button, when the user clicks on the button, an AlertDialog appear with 2 EditText where you put email and password to login.
When I try to get the text from the EditText i always get only empty strings.
The layout login_alert is the layout of the AlertDialog.
Here the code:
View view = getLayoutInflater().inflate(R.layout.login_alert, null, false);
String email = ((EditText) view.findViewById(R.id.emailEditText)).getText().toString();
String password = ((EditText) view.findViewById(R.id.passwordEditText)).getText().toString();
System.out.println("DEBUG: "+email+", "+password); // Empty strings
EDIT:
Activity code:
public class MainActivity extends FragmentActivity {
public static final String mAPP_ID = "...";
public static final String USER_DB_URL = "...";
AssetsExtracter mTask;
private MainFragment mainFragment;
private List<User> usersList = new ArrayList<User>();
private User currentUser = null;
private Button labLoginButton;
private EditText emailET;
private EditText passwordET;
private ProgressDialog dialog;
private View alertView; /* THIS IS THE SOLUTION */
boolean userIsLogged = false;
static {
IMetaioSDKAndroid.loadNativeLibs();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
/*View view = getLayoutInflater().inflate(R.layout.login_alert, null, false); BEFORE*/
alertView = getLayoutInflater().inflate(R.layout.login_alert, null, false);
emailET = (EditText) view.findViewById(R.id.emailEditText);
passwordET = (EditText) view.findViewById(R.id.passwordEditText);
labLoginButton = (Button) findViewById(R.id.loginLabButton);
updateLoginButton();
dialog = new ProgressDialog(this);
dialog.setMessage("Signin in...");
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
mTask = new AssetsExtracter();
mTask.execute(0);
}
/* THIS METHOD IS CALLED BY THE LOGIN BUTTON IN THE MAIN ACTIVITY LAYOUT */
public void onLabLoginButtonClick(View v) {
if (userIsLogged) {
currentUser = null;
userIsLogged = false;
updateLoginButton();
Toast.makeText(this, "Disconnected from Lab", Toast.LENGTH_SHORT)
.show();
} else {
/*View messageView = getLayoutInflater().inflate(
R.layout.login_alert, null, false); BEFORE */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.icon_launcher);
builder.setTitle(R.string.login_string);
builder.setView(alertView); /* USING THE GLOBAL VARIABLE */
builder.setPositiveButton("Sign me", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface d, int which) {
dialog.show();
// Download user and return a List of User
DownloadFilesAsyncTask task = new DownloadFilesAsyncTask(USER_DB_URL) {
#Override
protected void onPostExecute(final List<User> result) {
usersList = result;
loginCheckRoutine(); //HERE I MANAGE THE LOGIN AND GETTING EMPTY STRING
}
};
task.execute();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create();
builder.show();
}
}
public void updateLoginButton() {
if (userIsLogged) {
labLoginButton.setText(R.string.logout_string);
} else {
labLoginButton.setText(R.string.login_string);
}
}
public void loginCheckRoutine() {
String email = emailET.getText().toString();
String password = passwordET.getText().toString();
System.out.println("DEBUG: " + email + ", " + password); // EMPTY
// controllo nella lista se c'รจ l'utente coi dati inseriti
for (int i = 0; i < usersList.size(); i++) {
if (usersList.get(i).getEmail().equals(email)
&& password.equals("admin")) {
currentUser = usersList.get(i);
userIsLogged = true;
updateLoginButton();
dialog.dismiss();
break;
}
}
if (!userIsLogged) {
userIsLogged = false;
updateLoginButton();
dialog.dismiss();
Toast.makeText(MainActivity.this, "Login Failed",
Toast.LENGTH_SHORT).show();
}
}
}
PROBLEM SOLVED, SOLUTION:
In the onCreate() I inflate the alert_dialog layout in a View variable. I made that View variable global (before onCreate()) and then in onLabLoginButtonClick() I don't inflate the view again, but I use that global instantiated in the onCreate(). hope its clear. thank you all!
You getText just after initialization. Untill you have text in xml you won't get the text. In onclick of alertdialog button get the text.
Declare
EdiText ed1,ed2; // before onCreate if in activity and onCraeteView in fragment
as a instance variable
View view = getLayoutInflater().inflate(R.layout.login_alert, null, false);
ed1= (EditText) view.findViewById(R.id.emailEditText))
ed2 = (EditText) view.findViewById(R.id.emailEditText);
then on Alert dialog Button click
String email = ed1.getText().toString();
String password= ed2.getText().toString()
you must get the text when you click on login button of alert dialog box
the above mentioned code you get text when you show alert dialog it always return always empty string you should follow the following procedure
first you make a custom alert box layout having two edit text and one button
user write text to edittext for login and give password and then click login button
when you call login button click listener you can get text of edittext easyly
You are trying to get the text immediately after you inflated the view. Try doing it when the user clicks the done button instead.
Before onCreate add:
EditText email;
EditText pass;
Add this in your onCreate
etEmail (EditText) view.findViewById(R.id.emailEditText);
etPass (EditText) view.findViewById(R.id.emailEditText);
Then add this to when your button is clicked
String email = etEmail.getText().toString();
String pass = etEmail.getText().toString();
Just ensure that the editText.getText.toString() method is inside the OnClick() method, eg:
TextView submit = enquiryFragment.findViewById(R.id.query_submit_button);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
query_type = query_type_editText.getText().toString();
query_text = query_editText.getText().toString();
if (query_text.length()!=0 && query_type.length()!=0) {
postQuery(query_type, query_text, store_id);
// Log.e("query_type ",query_type );
}else{
Toast.makeText(getContext(), "Enter something !", Toast.LENGTH_SHORT).show();
}
}
});
Alternatively add a TextChangedListener to you textview to change the change the string every time the textboxtext changes.
A textwatcher is also possible
you should get the text when you click on save or done button.
If you get this text on click of alert dialog button, you may end up taking it multiple times.
I am developing a android App which is totally based on request and response from servlet.I have populate some data in customize Alert-dialog Where i using two thing one is cross button that will delete item from list in alert-dialog and update view of alert dialog , second thing is close button that will suppose to dismiss this alert-dialog. I am showing full coding of my alert-dialog. I calling alert dialog on button click by all these method.
intiliazeOrderListDialog();
showOrderListDialog();
My decleration is as follow
public AlertDialog detailsDialog, orderDialog;
AlertDialog.Builder builder;
Now i am going to post my intiliazeOrderListDialog() block.
public void intiliazeOrderListDialog() {
builder = new AlertDialog.Builder(MainScreen.this);
mContext = getApplicationContext();
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);
ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);
bclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
orderDialog.dismiss();
System.out.println(" click on closowse");
}
});
bPlaceOrder.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Place order click");
palceMyOrdertoServer();
new SendOrderFromTable().execute();
System.out.println("place order to server is called");
String msg = "Your Order is Successfully placed to Kitcken";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
orderDialog.dismiss();
}
});
}
and at last i am going to post showOrderListDialog(); block
public void showOrderListDialog() {
builder.setView(orderDialogLayout);
orderDialog = builder.create();
orderDialog.show();
}
I know i have posted too many codes but its conveniences for those who want to help me . I have a very simple problem why my
orderDialog.dismiss();
is not working for me.? Thanks in advance to all .
Finally i solve my own issue . "Self help is best help ;;".
It was issue of calling method in setOnClickListener.
I just call it,
android:clickable="true"
android:onClick="clickHandler"
if (v.getId() == R.id.myOrder) {
System.out.println("Click my Order");
System.out.println("OrderListAdapter.totalCount ="
+ OrderListAdapter.totalCount);
// select COUNT(*) from CDs;
int jcount = 0;
jcount = countjournals();
System.out.println("jcount = " + jcount);
// Count implementation at my Order
if (jcount < 1) {
alertShow();
} else {
intiliazeOrderListDialog();
showOrderListDialog();
}
// startActivity(new Intent(RestaurantHome.this,
// MyOrderList.class));
}
Hi I'm an android newbie and I've been stuck for a week on this. Any help would be appreciated! I've done a lot of research and can't figure out what is wrong. I've successfully run the bluetoothchat sample code on two phones and successfully communicated via bluetooth. I've also successfully written and run a standalone app that, after a button click on the main activity, opens a custom alertdialog which accepts user input, and passes the input back to the main activity. But when I write the alertdialog code into the BluetoothChat code, nothing happens when I click the button. I've tried to step through the debugger with the phone but with no luck. It doesn't seem to step to the code containing the button click. There are no errors showing. Why won't the alertdialog pop up on button click? Here's the BluetoothChat.java code I've modified :
public class BluetoothChat extends Activity implements OnClickListener{
final Context context = this;
private Button rButton;
View rScreen;
private EditText mAlertDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Set up the window layout
setContentView(R.layout.main);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
//components from main.xml
//When button is clicked, the alert dialog is pulled up
rButton = (Button)findViewById(R.id.buttonr);
mAlertDialog = (EditText)findViewById(R.id.edittextresultm);
//add button listener
rButton.setOnClickListener(new OnClickListener() {
//#Override
public void onClick_register(View view) {
String title = "title";
String buttonOk = "OK";
String buttonCancel = "Cancel";
String madd, name;
//get review.xml view
LayoutInflater li = LayoutInflater.from(context);
View rView = li.inflate(R.layout.review, null);
//AlertDialog dialog;
AlertDialog.Builder adRegister = new AlertDialog.Builder(context);
//set review.xml to adRegister builder
adRegister.setView(rView);
//set title
adRegister.setTitle(title);
//Set EditText views to get user input
final EditText mField = (EditText)rView.findViewById(R.id.editTextm);
final EditText nField = (EditText)rView.findViewById(R.id.editTextn);
//set dialog message
adRegister.setMessage("Message")
.setCancelable(false)
.setPositiveButton(buttonOk, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String madd = mField.getText().toString();
String name = nField.getText().toString();
//get user input and set it to result on main activity
mAlertDialog.setText(mField.getText());
}
})
.setNegativeButton(buttonCancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//if this button is clicked, close current activity
dialog.cancel();
}
});
//Create alert dialog
AlertDialog alertDialog = adRegister.create();
//dialog= adRegister.create();
//show it
adRegister.show();
//dialog.show();
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
}
Write your inputDialog code in OnClick Method.
Enjoy!!
I'm trying to pop up a custom dialog when I click on a button but it won't pop up at all. my app is basically a calendar and I'm going to use sqlite to add/hold appointments and stuff to a date in the calendar using the dialog, which is where the appointment details will be specified.
The code I'm using for this is the following:
public void onClick(View v) {
// TODO Auto-generated method stub
//long a = calendar.getDate();
switch(v.getId()){
case R.id.createButton:
openCreateAppointmentDialog();
break;
}
}
private void openCreateAppointmentDialog(){
Context mContext = getApplicationContext();
Dialog createAppmntDialog = new Dialog(mContext);
createAppmntDialog.setContentView(R.layout.create);
createAppmntDialog.setTitle(R.string.createTitle);
appointmentTitle = (EditText) createAppmntDialog.findViewById(R.id.titleTextBox);
appointmentTitle.setText("hello");
appointmentTime = (EditText) createAppmntDialog.findViewById(R.id.timeTextBox);
appointmentDetails = (EditText) createAppmntDialog.findViewById(R.id.detailsTextBox);
saveAppointment = (Button) createAppmntDialog.findViewById(R.id.saveButton);
saveAppointment.setOnClickListener(this);
}
What am I doing wrong?
Call the show() method for your dialog.
createAppmntDialog.show(); //when you want the dialog to appear on the screen
Alright, so I would like to have a custom dialog, but I cannot figure out for the life of me how to make it appear when the function is called.
public void addHomework() {
final Dialog alert = new Dialog(this);
alert.setTitle("Add Homework");
alert.setContentView(R.layout.homework_item_entry);
Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);
add_button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ClassHomeworkList.this, "Adding homework", Toast.LENGTH_SHORT).show();
}
});
cancel_button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
What could I do?
I know this is an old thread, but even after reading the Android docs it also was not obvious to me how to display a custom dialog using the standard Dialog class. Basically you can call:
this.showDialog(MANAGE_PASSWORD); // MANAGE_PASSWORD static final int
from your activity. Then instantiate the custom dialog in the onCreateDialog method:
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case MANAGE_PASSWORD:
dialog= getInstancePasswordDialog();
break;
case DIALOG_ABOUT:
// do the work to define the About Dialog
dialog= getInstanceAlertDialog(); // called "the first time"
break;
default:
dialog = null;
}
return dialog;
}
The code to instantiate the dialog is in getInstancePasswordDialog(). Here is the code sample.
I think you have the problem that your two buttons cannot be found by their ID's like this (as you are trying to find them in your main activity, but they are in the layout for the dialog)
Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);
But instead need to do:
Button add_button = (Button) alert.findViewById(R.id.add_homework_button);
Button cancel_button = (Button) alert.findViewById(R.id.cancel_homework_button);
Have you read the following document: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog ?
You should override your Activity's onCreateDialog(int) method as described there and then use showDialog(int)
LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.dialog, null);
//the id is your root layout
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
alert.setContentView(layout);