dialog.show() crashed InputMethodService? - java

Sorry for bothering again, but in my Android keyboard I have
public class zyz extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private LinearLayout mInputView;
#Override public View onCreateInputView() {
mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
AddKeys("/layout.txt");
return mInputView;
}
...
final Dialog dialog = new Dialog(this,R.style.myBackgroundStyle);
...
linLayBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
dialog.setContentView(R.layout.dialog);
dialog.show();
...
Which is supposed to show a dialog when a button is presses. Yet it crashes the application...
08-30 17:04:41.554: ERROR/AndroidRuntime(15712): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Any ideas how to fix it? Thanks!
dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/frame_left" android:id="#+id/frameLeft"></ImageView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#drawable/frame_center" android:id="#+id/LLdialog4letter">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/bun_r" android:id="#+id/ivDialogLetter" android:layout_marginTop="3px" android:layout_marginRight="9px" android:layout_marginLeft="9px"></ImageView>
</LinearLayout>
<ImageView android:layout_height="wrap_content" android:src="#drawable/frame_right" android:layout_width="wrap_content" android:id="#+id/frameRight"></ImageView>
</LinearLayout>

This will work for you Enjoy
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
// Do something with the selection
}
});
AlertDialog alert = builder.create();
Window window = alert.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alert.show();

Related

How to show Cancel and Accept button in AlertDialog Android

I am having troubles setting the two buttons on the lower part of the alert what I am trying to do is get a similar look to the IOS look of the buttons.
here is my current code:
public void openInstructions() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setNewDateOnView();
dialog.cancel();
}
}).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).create();
TextView myMsg = new TextView(getActivity());
myMsg.setText("ExampleText ");
myMsg.setTextSize(15);
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
dialog.setView(myMsg);
dialog.setTitle("Confirm Date of Purchase");
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextColor(0xFFFF0000);
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(0xFF0000FF);
}
});
dialog.show();
}
Example of how it looks right now:
How I wanted it to look:
To design Dialog like ios , we can create custom dialog using custom layout like this :
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical"
app:layout_constraintHeight_max="wrap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title"
android:gravity="center"
android:text="Confirm Date of Purchase"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/back_ll"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/rebooking_single_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textAlignment="center"
android:text="#string/dummuy"
android:textColor="#android:color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/back_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<LinearLayout
android:id="#+id/rebooking_back_button_ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/ok_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".49"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ok"
android:textAlignment="center"
android:textColor="#FF6363"
android:textSize="17sp"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_weight=".01"
android:layout_height="match_parent"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<TextView
android:id="#+id/cancel_btn"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAlignment="center"
android:paddingRight="10dp"
android:text="Cancle"
android:textColor="#7BC5FF"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Main_Activity
final Dialog dialog=new Dialog(FirstActivity.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_custom);
TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
cancel_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
TextView ok_btn=dialog.findViewById(R.id.ok_btn);
ok_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
dialog.show();
Well, as a start, google recommends you use the Android Design Guidelines when creating Android apps, really you should be sticking to those as that's what your users will be used to (not Apple's).
Also there's no reason to be assigning the dialog a TextView if all you're doing is showing text, the AlertDialog provides this functionality with setMessage.
If that hasn't convinced you to stick to the normal design rules, the only other option would be creating a Custom View and assign it to the AlertDialog with setView(similar to how you're doing it with the TextView).
Here's a tutorial on creating a Custom View
You can create a custom view such as the one sent and inflate it
Example:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
mBuilder.setView(mView);
Button close = (Button) mView.findViewById(R.id.close);
Button OK = (Button) mView.findViewById(R.id.ok);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//close the dialog
hideDialog1();
}
});
OK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Use .setNeutralButton that make place left most
TextView textview;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)
.setTitle("Alert Dialog Box Title")
.setMessage("Are you sure( Alert Dialog Message )")
.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("NO", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
===============================================

How to call removeView() on dialog box number one so as to show dialog number 2

I am working with an application that involves to dialog boxes that should appear one after the other. What I mean is that, when I click on the positive button of the first dialog box it should then bring up another second dialog box. The issue I am facing is with the second dialog box, the second one will not pop up and it causes the application to crash.
I get an error that says "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
public static void showBusinessOrPrivateStartDialog(final Context context, final CallLogEntry call)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View view = inflater.inflate(R.layout.dialog_call_type, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
builder.setTitle("Was this a private or a business call?");
builder.setMessage("Please select a call type");
builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
//builder.setView(view);
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// Occurs when user cancels the dialog, or clicks somewhere else in the screen or presses the back button
Log.i(TAG, "cancelled");
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", true, "");
}
});
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setNegativeButton("Private", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveCallEntry(context, call, CLASSIFICATION_TYPE_PRIVATE, "", false, "");
}
});
AlertDialog dialog = builder.create();
// Rather use something like TYPE_SYSTEM_ALERT, if possible.
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
This is the second method for the second dialog box and my app crashes on dialog.show
public static void showBusinessCallPopup(final Context context, final CallLogEntry call)
{
// NEW NRF
if (AppPreferences.isNRF(context)) {
CallItem callItem = saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", false, "");
// SHOW NEW FORM
Intent intent = new Intent(context, CallClassificationFormActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
EventBus.getDefault().postSticky(callItem);
context.startActivity(intent);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_call_details, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
String toOrFrom = call.Type == CallLog.Calls.INCOMING_TYPE ? "from" : "to";
//builder.setTitle("Business call");
builder.setCustomTitle(view);
//builder.setMessage("Please enter reference details for the call " + toOrFrom + " " + call.Number);
//builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
builder.setView(view);
final EditText etReference = (EditText)view.findViewById(R.id.etReference);
final EditText etComment = (EditText)view.findViewById(R.id.etComment);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String reference = etReference.getText().toString();
String comment = etComment.getText().toString();
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, reference, false, comment);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.i(TAG, "cancelled");
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
and here is my xml. I have included the xml in this post because I read somewhere that the way the height and width of my textboxes is defined
might be causing an issue.
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".calls.CallClassification">
<TextView
android:id="#+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:text="Please enter reference details for the call "
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"/>
<AutoCompleteTextView
android:id="#+id/etReference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Matter Code *"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="255"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<CheckBox
android:id="#+id/cbBillable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:button="#null"
android:buttonTint="#color/colorAccent"
android:checked="true"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="Billable"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"/>
<EditText
android:id="#+id/etComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Enter comment"
android:imeOptions="actionDone"
android:inputType="text"
android:lines="1"
android:maxLength="1000"
android:maxLines="4"
android:minLines="1"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/bCancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"/>
<Button
android:id="#+id/bOk"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"/>
</LinearLayout>
</LinearLayout>
After hearing to your experience, I could think of one more solution. Rather than declaring AlertDialog variable inside each of the methods, declare one global variable so that you can make sure that only one Dialog is shown at any instance.
While declaring, make
AlertDialog dialog = null;
in class
Then in,
showBusinessOrPrivateStartDialog() {
if(dialog == null) {
dialog = build.create();
}
}
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
dialog = null;
}
}
Then again in showBusinessCallPopup(),
if(dialog == null) {
dialog = build.create();
}
Hope this helps you.
Before calling
CallClassification.showBusinessCallPopup(context, call);
you need to call
dialog.dismiss();
within the same onClick().
Hope this solves your case.
In your second code you are adding view twice, maybe you should remove one of them.
builder.setCustomTitle(view); -> Here
builder.setCancelable(true);
builder.setView(view); --> Here
:)

How to insert a text from an EditText into a database?

I'm trying to get the text from editText and input it into my database. The problem I am having is a NullPointerException once i try to submit the text. Any help would be appreciated.
ERROR:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
This is from my MainActivity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add_task:
final EditText text = (EditText)findViewById(R.id.editText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
my custom_view.xml:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a task"
android:id="#+id/editText" />
my activity_main.xml:
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tablayout"
android:background="#color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:backgroundTint="#android:color/darker_gray"
android:visibility="visible">
<ListView
android:id="#+id/lstTask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/tasks"
android:textSize="30sp" />
</android.support.v4.view.ViewPager>
Well since I cant see the xml for MainActivity layout, here it's what most likely it's causing the NPE.
final EditText text = (EditText)findViewById(R.id.editText);
Your searching your edit text in you activity view hierarchy when (assuming from what is shown in the Dialog creation) you should be searching it in the Dialog view hierarchy.
.setView(R.layout.custom_view)
There you are setting that view as the view of the dialog and thats probably what is causing the exception. Try fining the reference to the edit text in the onClick method like this:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
Here is what fixed my problem:
I changed this line:
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
To this:
final EditText text = (EditText) ((AlertDialog)dialog).findViewById(R.id.editText);

Android - Click Button Inside alertDialog

I wonder how to click a Button inside of AlertDialog in Android and this is my code
activity_float_info.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
MainActivity.java
QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);
MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(Boolean.TRUE)
.setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setView(R.layout.activity_float_info)
.show();
button = (Button)QR_.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("TEst");
}
});
}
});
I have inflate the layout..
I think the main problem is on
button = (Button)QR_.findViewById(R.id.button);
try this create a layout for dialog box
<TextView android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/black"
android:text="Please enter the email address you used for the account"
/>
<EditText
android:id="#+id/emailedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:padding="5dp"
android:cursorVisible="true"
android:singleLine="true"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:hint="Enter Mail id"
android:textSize="20dp" >
<requestFocus />
</EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="#+id/cancelbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="CANCEL"/>
<Button
android:id="#+id/okbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
Then create a dialog box using this layout and handle button clicks
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer: https://coderzpassion.com/android-show-alertdialog/
Here is a hack to use AlertDialog:
public class CustomDialog extends AlertDialog {
protected CustomDialog(Context context) {
super(context);
}
}
And then in your Activity:
CustomDialog dialog = new CustomDialog(this);
View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null);
dialog.setView(view);
Button button = (Button)view.findViewById(R.id.custom_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
And the layout of the dialog (which has nothing in it barring the Button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="New Button" />
</RelativeLayout>
And you should be able to see a Toast when you click on the button. Hope this helps.
Dialog is like a popup window to show some options to users(options like accept/decline).
Using class android.app.Dialog to create dialog.
Using dialog.xml file to create custom dialog layout.
Example:
res/layout/dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="#+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/imageDialog"/>
<Button
android:id="#+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/textDialog"
android:layout_toRightOf="#+id/imageDialog"
/>
</RelativeLayout>
Java Code
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

Customize alertdialog android api 10

I would like to know how to customize theme/style of AlertDialog using sdk api 10. I know how to do that from 11 onwards but not how to do it on the 10.
XML Layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
style="#style/dialog_theme" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/disclaimerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/disclaimer"
android:padding="5dp"/>
<CheckBox
android:id="#+id/checkDisclaimer"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/agree" />
</LinearLayout>
</ScrollView>
JAVA Resource file
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Setting Dialog Title
builder.setTitle("Disclaimer");
//Setting Dialog Message
builder.setMessage(R.string.disclaimer);
View view = (View) LayoutInflater.from(this).inflate(R.layout.layout_disclaimer, null);
builder.setView(view);
You could try this:
View myView = View.inflate(this, R.layout.customize_dialog, null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(myView);
builder.setTitle("Customize dialog");
builder.setCancelable(false);
AlertDialog alert=builder.create();
alert.show();
You have to create a layout in res/layout/ called customize_dialog.xml,
you can also add to "myView" other view object.
To add tipical Alert button:
builder.setPositiveButton("Ok",new OnClickListener(){
public void onClick(DialogInterface dialog, int id){
//action to do
dialog.dismiss();
}
});
builder.setNegativeButton("Close",new OnClickListener(){
public void onClick(DialogInterface dialog, int id){
//action to do
dialog.dismiss();
}
});
I hope it helps you.

Categories