I have the ability to make my own popup, with dialogs, but I don't need anything complex. I was wondering if there was a simple function I could call that would make a popup where the user entered text and then return to me that text for use.
Sorta like these popups
But with one where the user would enter text.
Fairly new so if there is something like this I wouldn't mind an example to go with it. Thanks.
If you are using Android native UI, then you can easily create an AlertDialog and add a EditText to the dialog.
EditText et = new EditText(this);
AlertDialog.Builder ad = new AlertDialog.Builder (this);
ad.setTitle ("Type text");
ad.setView(et); // <----
ad.show();
Related
I want to have the message of an alertdialog displayed with a monospace font, so I wrote this code, which work nice:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("message");
...
AlertDialog dialog = builder.create();
dialog.show();
TextView messageView = dialog.findViewById(android.R.id.message);
messageView.setTypeface(Typeface.MONOSPACE);
My question is about the order of the calls: you need dialog.show() to be called in order to call dialog.findViewById, otherwise you get a null pointer.
It does not sound logical to show something and then to change it. I would have preferred to build it with the right style and then show it.
Is there a way to do it like that ?
Prepare your own TextView or any View Programmatically or in XML as a custom View the show it like:
builder.setView(getLayoutInflater().inflate(R.layout.YourLayout, null))
Now YourLayout should have all the styles you like from now. Otherwise it will be hard to call findViewById without the Dialog view on the Screen!!
I have tried to show the dialog box while the user giving the wrong username or password, using the below code.
private void showAlert(String title, String msg) {
customDialog = new Dialog(LoginActivity.this,
android.R.style.Theme_Dialog);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.custom_alert_dialog);
tvTitle = (TextView) customDialog
.findViewById(R.id.dialog_title);
tvMsg = (TextView) customDialog
.findViewById(R.id.dialog_message);
btnNeutral = (Button) customDialog
.findViewById(R.id.closeAlert);
tvMsg.setText(msg);
tvTitle.setText(title);
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
tvMsg.setFocusable(true);
btnNeutral.setText("Close");
btnNeutral.setVisibility(View.VISIBLE);
btnNeutral.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customDialog.dismiss();
}
});
customDialog.show();
tvMsg.requestFocus();
}
The code working fine but my concern is, when i am trying to use the android talkback. It reads only the title of the dialog box. The talkback needs to read the content(message) of the dialog box instead of title. Can anyone help me to do this?
First, announcing just the title of a new dialog is very standard. Doing otherwise would probably be counter productive in terms of accessibility. This sounds to me like an accessibility requirement from someone motivated to do good, that doesn't really understand the needs of users with disabilities. Shoving focus around arbitrarily is usually bad. Let the operating system do what it wants with focus, it is what Assistive Technology (TalkBack) users will be accustomed to.
This said there are two overarching issues with your code. First, when you say focus, you mean accessibility focus.
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
tvMsg.setFocusable(true);
tvMsg.requestFocus();
All of these lines are referring to keyboard, or input focus, none of which are particularly meaningful for a TextView. These are only meaningful for active elements like Buttons and EditText boxes. Will this work if you do it correctly, yes. But, it comes with awkward side effects, like a TextView being added to Tab ordering, which is awkward for Keyboard only users, because TextViews don't have focus highlights, so Focus navigation disappears. What you really want is the following event type:
AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
Now, for the second point. You're doing all of this before your view actually renders. Replace this line:
tvMsg.requestFocus();
With this line:
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
Delete the other lines mentioned above, and you should be golden. Though, again, my ultimate recommendation would just be dropping all of this, and removing those three lines outright, and forgetting about this. Let the operating system do its thing!
I want to show an AlertDialog containing an EditText that auomtically capitalizes words.
Following this question and that one, I managed to get the AlertDialog show the keyboard automatically when the dialog is shown, and also capitalize the first letter when the user clicks on the EditText. But until the user clicks, the keyboard shows in lowercase mode.
How can I make the keyboard open automatically in upper-case (auto-capitalize words) mode?
My relevant code is as follows:
input = new EditText(context);
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Also tried to requestFocus() in the dialog's onShowListener but it didn't help.
You just need to use the following code after your AlertDialog declaration.
AlertDialog.Builder alert = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
final EditText edittext = new EditText(getApplicationContext());
// Just use it here, there is much more options in the link below
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
Look for other options here
A workaround that worked for me was to avoid using setMessage(), and adding the message into my own view instead (using setView()).
I'm creating a scenario test framework for Android Cordova/PhoneGap applications that builds on JUnit. There is one scenario I would like to test: you click on some button in the webpage (shown in an Android WebView) and it opens a popup (an AlertDialog). I want to check the message on this popup and click one of the buttons. Therefore I need the view object of this AlertDialog.
I know you can use findViewById, but you have to give an id as parameter, which I don't have since the dialog is created with the following code:
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx.getContext());
dlg.setMessage(message);
dlg.setTitle(title);
....
dlg.create();
dlg.show();
Any idea how I can access the correct view?
Thanks!
The TextView containing the AlertDialog's message is always identified by android.R.id.message. If you capture the result of dlg.create(), you should be able to get a reference to the message TextView by calling .findViewById(android.R.id.message) on it, from which you could then get the text.
I am very new to Android.
Is it possible to implement a TextBox , Button and a dynamic list inside popup in Android.
Please help me in this.
Thanks.
Yes you can make a custom dialog of your own.
I've used following code in my java file
Dialog add_themedialog = new Dialog(addthemecontext);
add_themedialog.setContentView(R.layout.add_new_theme);
add_themedialog.setTitle("Add New Theme");
final EditText et_entertheme = (EditText)add_themedialog.findViewById(R.id.et_dialog_addtheme);
Button btn_addtheme = (Button)add_themedialog.findViewById(R.id.btn_dialogaddtheme);
Button btn_canceltheme = (Button)add_themedialog.findViewById(R.id.btn_canceltheme);
The xml file add_new_theme contains a textbox,an edittext and 2 buttons..
Ask me if any thing is not clear or if you want complete source code for it. :-)