How to remove strange border from DialogFragment with AlertDialog? - android

I am trying to implement AlertDialog based on DialogFragment with this code:
public class AlertDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
}
}
It working well, but I get a strange white border around AlertDialog:
How to remove this border (preferably programmatically)?
UPDATE: my styles.xml contains this code:
<style name="AppBaseTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:alertDialogTheme">#style/MyDialogTheme</item>
</style>
<style name="MyDialogTheme" parent="#android:style/Theme.Holo.Light.Dialog"></style>
When I remove <item name="android:alertDialogTheme">#style/MyDialogTheme</item> row, this issue is gone away. But I need this row because I want to customize AlertDialog. How to fix this style?

I have just checked your code ,but have not get any issue like this.
I call the class like
package com.example.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
public class A extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AlertDialogFragment al=new AlertDialogFragment();
FragmentManager fm = getSupportFragmentManager();
al.show(fm, "test");
}
class AlertDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
}
}
}

Related

How Can i hide the MainActivity while pop up AlertDialog?

I want to hide MainActivity when AlertDialog is shown.
I create new Activity and put my code in it and call it from MainActivity by using Intent.
My Code of AlertDialog :
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder1 = new AlertDialog.Builder(Dialog.this);
builder1.setTitle("RONQ");
builder1.setMessage(MainActivity.mesgList.get(MainActivity.currentMsgNumber));
builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder1.show();
}
}
And Intent code is :
Intent intent = new Intent();
intent.setClass(MainActivity.this, Dialog.class);
startActivity(intent);
You don't need Intent:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message")
.setPositiveButton(getString(R.string.update), (dialogInterface, i) -> {
//do something
}).setNegativeButton(getString(R.string.cancel), (dialogInterface, i) -> {
//do something
}).setCancelable(false).create().show();

Cast Dialog to AlertDialog

I had the exact same code in antoher project, but it continues to crash here. I have implementation 'com.android.support:design:26.1.0' in my gradle. I'm really unsure of what the problem is. I've tried switching the gradle version, changing from v7 Dialog to the app Dialog. All fails regardless
java.lang.ClassCastException: android.app.Dialog cannot be cast to android.support.v7.app.AlertDialog
at com.example.weather.CreateCityDialog.onResume(CreateCityDialog.java:58)
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CreateCityDialog extends DialogFragment {
public interface NewCityHandler {
void onNewCityCreated(String cityName);
}
private NewCityHandler newCityHandler;
private EditText etName;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof NewCityHandler)
newCityHandler = (NewCityHandler) context;
else
throw new RuntimeException("Error");
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add new city");
View rootView = getActivity().getLayoutInflater().inflate(R.layout.activity_create_city_dialog, null);
etName = rootView.findViewById(R.id.etName);
builder.setView(rootView);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return super.onCreateDialog(savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
final AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(etName.getText())) {
newCityHandler.onNewCityCreated(etName.getText().toString());
d.dismiss();
} else {
etName.setError("Empty field");
}
}
});
}
}
}
You have to return AlertDialog from onCreateDialog().
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add new city");
View rootView = getActivity().getLayoutInflater().inflate(R.layout.activity_create_city_dialog, null);
etName = rootView.findViewById(R.id.etName);
builder.setView(rootView);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}

App stopped working android

I don't understand why my "App" has stopped working?
This is my MainActivity.class code:-
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}
and my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.nishant.iwillguessyournumber.MainActivity">
<Button
android:id="#+id/cmdPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="219dp" />
<Button
android:id="#+id/cmdExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="328dp" />
</android.support.constraint.ConstraintLayout>
Whenever I am running this app on Genymotion Android:6.0.0 and API 23, it is showing "Unfortunately, (app name) is not working"
What is the problem in my code/platform?
I've tried restarting my apps, looked up a few questions but couldn't yet get my answer when I open a new Activity using Intent, the second activity can't parse the xml file.
The source of your app crash is the following 2 lines,
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
The method findViewById() should be called inside the onCreate() after the setContentView(). The following code should work,
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay;
private Button btnexit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplay = (Button)findViewById(R.id.cmdPlay);
btnexit = (Button)findViewById(R.id.cmdExit);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}

My Dialog on Android comes up Blank

So I'm trying to create a dialog pop up. Here's the Dialog fragment I created in a separate DialogClass.Java file
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class DialogClass extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder newAlertDialog = new AlertDialog.Builder(getActivity());
newAlertDialog.setTitle("Dialog");
newAlertDialog.setMessage("This is a dialog");
newAlertDialog.setPositiveButton(("OK"), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), ("You clicked Ok"), Toast.LENGTH_SHORT).show();
}
});
newAlertDialog.setNegativeButton(("Cancel"), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), ("You clicked Cancel"), Toast.LENGTH_SHORT).show();
}
});
return super.onCreateDialog(savedInstanceState);
}
}
Then when I use this class to create a method it brings us a blank dialog box when I run the app. This is how I use it:
Note: myButtinClick is a method that runs on the click of a button.
public void myButtonClick(View view) {
DialogFragment myFrag = new DialogClass();
myFrag.show(getFragmentManager(), "");
}
Please can anyone figure out what I am doing wrong?
See a screenshot of what the Dialog looks like via this link.
http://i.stack.imgur.com/xsQQj.png
You've only created the Dialog builder, but not the Dialog itself.
You should return newAlertDialog.create() and rename newAlertDialog to dialogBuilder or something like that.

how to use alertdialog

package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..
You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.
It looks like you could just use showDialog(x) to me unless there is more to this question.

Categories

Resources