Android AlertDialog.Builder Custom title bar with Edit text - android

Here is the issue, I want to create a custom alert dialog box with 4 parts,
Title Bar
Header part (Most of the time I plan to use EditText box for
searching purpose)
List item(Single click or Multi Click)
Footer part(Cancel or OK buttons)
I try do this using separate layout, but list view item not showing properly(shrinking). So then I plan to use AlertDialog and inside I have implemented ListView and footer button part in single LinerLayout and set it using setView method, it solved my list view and Footer issue.
Problem is I want to add Header part top of the list view.
Image Link : https://dl.dropboxusercontent.com/u/4048078/device-2013-07-31-141546.png

You can try this
Dialog dialog_help = new Dialog(getActivity());
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog);
EditText et_1 = (EditText) dialog_help.findViewById(R.id.et_help_1);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
dialog_help.setCancelable(true);
dialog_help.setTitle(" Help ");
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

It's not a good practice to set editBox in the title of the alert dialog.
To set title use setTitle and setIcon builder methods.
EditText it the control that's better to place in dialog,s body.
Use AlertDialog.Builder.setView(android.view.View) for this.

Related

Temporary EditText and keyboard on bottom of screen

I'm trying to create EditText on bottom of screen and open keyboard (then this EditText will move above keyboard with RelativeLayout + ALIGN_PARENT_BOTTOM), it works fine with SetContentView but with addContentView the RelativeLayout + ALIGN_PARENT_BOTTOM doesn't work for me and the keyboard overlap the EditText.
In some way I want to generate this temporary EditText independently the layout being displayed on screen, so If I press any button on any layout I call this function to create a temporary EditText on bottom of screen and bring the keyboard.
I can't change the whole main layout because this layout is generated by Unity3D. I need a way to generate this EditText and act independently the layout is being displayed on screen.
My code:
public void openInput()
{
runOnUiThread(new Runnable()
{
EditText inputName = new EditText(context);
inputName.setInputType(InputType.TYPE_CLASS_TEXT);
inputName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40)} );
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(600, RelativeLayout.LayoutParams.WRAP_CONTENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.addView(inputName, editTextParams);
addContentView(relativeLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
inputName.requestFocus();
InputMethodManager msg = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
msg.showSoftInput(inputName, InputMethodManager.SHOW_IMPLICIT);
});
}
Any idea how to solve this issue?
Thanks
put your whole layout in a scroll view, (as your set contentview)
and then when you do addContentView the layout will scroll up to the focused view, rather than push the view up
I'm using a pop-up dialog window to edit long list of parameters one by one. That's also helps to add more helpful information on the parameter being edited, like instructions, hints, etc...

Changing contents of an EditText widget in AlertDialog without onPrepareDialog()

I'm using an AlertDialog with a custom view that contains an EditText widget. I want to change the contents of the EditText widget before the AlertDialog is shown. I know that this is usually done by overriding onPrepareDialog() and setting the text there. However, as far as I can see, this will not work in my specific case because I use AlertDialog.show() instead of Activity.showDialog().
So how should I do this with a dialog that is shown using AlertDialog.show()? One solution is to set the text after the dialog has been brought to the front, i.e.:
AlertDialog alertDialog = builder.create();
alertDialog.show();
EditText editText = (EditText) alertDialog.findViewById(R.id.text);
editText.setText("Foo bar");
However, I don't think that this is a nice solution because the dialog is first shown and then the text is set. I'd like to set the text before the dialog is actually shown.
Is there any way to achieve this? I cannot do it before alertDialog.show() because findViewById() returns null when called before alertDialog.show().
Thanks for any help!
AlertDialog alertDialog = builder.create();
alertDialog.show();
Since you have access to the AlertDialog.Builder object, simply change the layout before calling builder.create().
Addition
I have an EditText widget in my XML file which I inflate using builder.setView(inflater.inflate(R.layout.mydialog, null)). How do I change the text of this EditText widget without calling findViewById()?
Break that line into a series of commands. Specifically: inflate the XML, alter the layout, and pass it to setView().
View view = inflater.inflate(R.layout.mydialog, null);
EditText editText = (EditText) view.findViewById(R.id.text);
editText.setText("Foo bar");
builder.setView(view);

Android Onclicking textView the editText comes without numpad.How to get the numpad on single click on textView?

I have a textview.On clicking the texstview the edit text comes up with focus. But to get the numpad another click is required.
I want that when i click on TextView,the edit text should come up with the numpad togeter withoutanother click on the editText.Not through xml.But manually
In the xml i have already given it as android:inputType="number"
(the text view is made invisible on click)
The on click method----
case R.id.txtPhoneNo:
hintPhoneNum.setVisibility(View.VISIBLE);
phoneNum.setVisibility(View.GONE);
edtPhoneNum.setVisibility(View.VISIBLE);
edtPhoneNum.requestFocus();
edtPhoneNum.setText("");
WHen the editText gets focus how to get the numpad forcefully?
The correct answer is
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
for your EditText add the following XML:
android:inputType="number"
EDIT
Try requesting focus just after your textView was set invisible:
yourTextView.setVisibility(View.INVISIBLE); //or View.GONE
yourEditText.requestFocus();
This should display the keypad immediately
when TextView is shown or touched first it gets focus and when you click on it again it performs onClick() so you have to handle Foucus of TextView to achieve what you want.

footer for custom dialog with a android default theme

I want to create context menu as follows , i am sure this is not traditional the context menu , its a kind of alert dialog which is being customized.
I tried following code but it adds the footer end of the ListView not to the dialog / context menu reference thread is ContextMenu with footer view (to add checkbox for 'make default' option)
dialog2.getListView().addFooterView(new CheckBox(this))
I have gone through the followig tutorial which has custom context menu , is it possible
to use this code to set a footer
http://www.tanisoft.net/search/label/Tutorial
I want following features , preciously the checkbox in the footer
EDIT
I reached to this part now there are only two issues
1) Dialog Title Icon
2) Dialog Bottom Blue Color ( which is a default
color of android )
and i don;t know how to achieve above two task
Here is my code to create dialog
contactDlg = new Dialog(this);
contactDlg.requestWindowFeature( Window.FEATURE_LEFT_ICON );
contactDlg.setTitle(contactStore.getContactName());
contactDlg.setContentView(R.layout.contact_dialog);
contactDlg.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.bottom_bar);
contactAdapter = new ContactAdapter(this,contactStore.getContactNumber());
modeList = (ListView) contactDlg.findViewById(R.id.contactDlgList);
modeList.setBackgroundResource(android.R.color.white);
modeList.setCacheColorHint(Color.WHITE);
modeList.setAdapter(contactAdapter);
contactDlg.show();
No need to do any thing special for this.This can be achieved easily by creating your own layout containg the list shown in this pic and the footer view in the dialog itself with thye color and style you want.Then just make the property of the dialog named "windowFrame" to be null by < item name="android:windowFrame">#null< / item>Also you can make a separate "theme.xml" in the "values" folder and define this and similar kinda properties in that file.In code, when you instantiate the dialog object, set this theme to it.The basic purpose of doing this is to achieve the total control on what to show and what not in the dialog you create.

Can we have an edit text as a menu item in android 3.0 honeycomb

Can we have an edit text box inside our menu which gets displayed in the action bar
and if we can then how can we get click events on this edit text ?
I found the solution. It is done like this (only for android 3.0 honeycomb)
Placing an edit text in action bar
for placing the items
use this TAG in your item in the menu xml
android:actionLayout="#layout/search_bar"
define edit text & button in the search_bar layout.
Now for getting listeners on them do it like this -
in onCreateOptionsMenu()
et = (EditText) menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.searchbar);
findBtn = (ImageButton)menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.find);
and then place listeners for these items. It will work fine
You can add a search bar to the action bar but not as a menu item... what do you even meen with "as a menu item"?
I havent tried it but are you able to use actionlayout from code? in that way you could use get the Text box this way:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
LinearLayout wrapper = (LinearLayout) inflater.inflate(R.layout.random_layout, null);
EditText editText = (EditText )wrapper.findViewById(R.id.edit);
//
//code to set the listener
//
//
//set the android:actionLayout to wrapper etc.
//
Im at work so I can't make some research so this is just speculations...

Categories

Resources