Why am i getting this white background under my alert dialogbox. I been trying to figure out the problem for an hour and had no luck. Can someone please help me?
Also, why is that the left and right sides of the title has a little dark shade.
protected void onPostExecute(String result) {
//progressDialog.dismiss();
try {
JSONObject json = new JSONObject(result);
String status = json.getString("status");
String message = json.getString("message");
if(status.equals("true")) {
Intent intent = new Intent(context, HomeActivity.class);
intent.putExtra(LoginActivity.EXTRA_MESSAGE, status);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle("Error")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Import
android.support.v7.app.AlertDialog instead of android.app.AlertDialog
Change your code as -
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialog.show();
Or you can add theme to your existing code.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
When initializing dialog builder, pass second parameter as the theme.
So Change
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
to
AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
It is old answer, now
Import android.support.v7.app.AlertDialog instead of android.app.AlertDialog
as given in accepted answer.
if you can access Dialog class, try this:
alertDialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
before:
after:
Related
I am taking input from user in the edittext. Now I want to show the desired output in the other text box, but if user inputs wrong values, a dialog box should open mentioning all the incorrect values...box is opening again and again till it detects all the incorrect values. eg-if i add three wrong values in the edit box it is opening box for 3 times.
String s=editText1.getText().toString();
String z[]=s.split("\\s");
editText2.setText("");
String a = "";
String b = " Not valid";
for(int i=0;i<z.length;i++)
{
int j=Integer.parseInt(z[i]);
if(j>=65 && j<=97)
{
editText2.setText(editText2.getText() + "" + String.valueOf((char) j));
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
a += z[i]+"\t";
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(a+b)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
boolean is_open_dialog=false;
for(int i=0;i<z.length;i++)
{
int j=Integer.parseInt(z[i]);
if(j>=65 && j<=97)
{
editText2.setText(editText2.getText() + "" + String.valueOf((char) j));
}
else {
is_open_dialog = true;
a += z[i]+"\t";
}
}
if(is_open_dialog){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(a+b)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
I want when user click on Uninstall Button,there prompt a password dialog. This Dialog is coming only one time.I'm using this code:
public void run() {
Looper.prepare();
while (!exit) {
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::" + activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
//Toast.makeText(context, "Uninstall Clicked", Toast.LENGTH_LONG).show();
Intent startIntent = new Intent(this.context, Alert_Dialog.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(startIntent);
exit = true;
} else if (activityName.equals("com.android.settings.ManageApplications")) {
Toast.makeText(this.context, "Back", Toast.LENGTH_LONG).show();
exit = true;
}
}
Looper.loop();
}//Run
I want whenever user click on Unistall Prompt should come , below is the code in onClick,
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.edit_text);
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("abc"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Toast.makeText(myContext,"PAssword Correct",Toast.LENGTH_LONG).show();
Alert_Dialog.this.finish();
//Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.create().show();
Alert_Dialog.this.finish();
}
}
});
// .setPositiveButton("Cancel",
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// Alert_Dialog.this.finish();
//
// }
//
// }
// );
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Once the dialog calls dismiss(), the view you set for the dialog also would be destroyed.
In your case, this line set the view,
alertDialogBuilder.setView(promptsView);
But when the dialog is closed, the view promptsView is destroyed,
The promptsView should be re-created once again you use it.
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
promptsView = new PromptsView();//new or inflate the view
//....
alertDialogBuilder.setView(promptsView);
Here I use this Activatedialog method in onPostExecute inside registration >activity , and need to start a login activity when the alert dialog dissmiss ..however I tried to do this, but I get error.
public void Activatedialog(String jsonmsg, final String name)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(ct);
alert.setTitle("Activate Account");
alert.setMessage(jsonmsg);
// Set an EditText view to get user input
final EditText input = new EditText(ct);
alert.setView(input);
alert.setCancelable(true);
Log.d("MYLOG","before +ve button click");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String code = input.getText().toString();
Log.d("MYLOG","before try");
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",name ));
params.add(new BasicNameValuePair("code",code ));
JSONObject json = jsonParser.makeHttpRequest(ACTIVATION_URL,"POST", params);
Log.d("MYLOG","after request for json");
stcode = json.getJSONObject("activate").getInt("statuscode");
stmsg = json.getJSONObject("activate").getString("statusmessage");
Log.d("MYLOG","after json parsing : json data is "+stmsg+"stcodeis "+stcode);
if(stcode == 1)
{
dialog.dismiss();
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Intent i = new ("LOGIN");startActivity(i);![i get this error][1]
Toast.makeText(ct, " "+stmsg, Toast.LENGTH_SHORT).show();
}
else if(stcode == 0)
{
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Toast.makeText(ct, " "+stmsg+" "+stcode, Toast.LENGTH_SHORT).show();
}
}
catch(JSONException e)
{
e.printStackTrace();
Toast.makeText(ct, ""+e, Toast.LENGTH_LONG).show();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// Canceled.
Toast.makeText(ct, "Your Account is not Activated.Login to Activate", Toast.LENGTH_LONG).show();
}
});
alert.show();
}
}
To start an Activity, the intent must have following parameters. A context and target activity. Lets start activity "Login.java" .
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
you have created intent incorrectly.
1> add the relevant activity you want to open say Loginby adding new activity to the project.
2>create the intent as below
Intent intent = new Intent(getApplicationContext(), Login.class);
3>start activity using the intent created
startActivity(i);
You can do something like this:
AlertDialogManager alert = new AlertDialogManager(StartActivity.this);
AlertDialogManager constructor:
public void AlertDialogManager(final Context context) {
activity = (Activity) context;
}
and you can launch the activity like this:
Intent intent = new Intent(activity , Login.class);
activity.startActivity(intent);
I'm getting an error in this method: "The constructor Object(EditText) is undefined"
private void setupAlert()
{
Log.d(TAG, "setupAlert()");
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.emaildialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText)promptsView.findViewById(R.id.editTxtEmailAddress);
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
updatePref(userInput.getText().toString());
Log.d(MainActivity.TAG, "setupAlert: getPreference()=" + getPreference());
takeSnapNow();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.colour_selector);
Log.d(TAG, "onCreate: getPreference()=" + getPreference());
if (getPreference().equals("")) {
setupAlert();
takeSnapNow();
} else {
takeSnapNow();
}
}
private void takeSnapNow()
{
String fileName = "TempImage.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Captured by XYZ app");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
Please suggest the solution to this error.
Edit: Corrected the above error by removing UserInput as suggested by others. However the alertDialog is still not shown. The logic is to check the sharedpref initially in onCreate, and if not found, call setupAlert() method to get email ID from user.
Any clues? Changed code updated in the above snippet.
DialogInterface.OnClickListener doesn't take an EditText as a constructor parameter and you don't need to do that ! you can use userInput without passing it to DialogInterface.OnClickListener, just use it as follow
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
Why are you using "new DialogInterface.OnClickListener(userInput)"?
Just use new DialogInterface.OnClickListener()
I'm trying to show an alert dialog when a user fails to enter valid email and password to log in. Since the login process has to deal with HTTP request and JSON, I put it all in an AsyncTask and I'm not sure if that's the problem why my alert dialog can't show (I hope not). So this is method checkUser, which is called within onPostExecute of the AsyncTask class:
public void checkUser(JSONObject json){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
if (tag == login_tag){
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
// code omitted
} else{
// Error in login
mProgressDialog.dismiss();
builder.setMessage("Incorrect username/password")
.setTitle("Error")
.setNeutralButton("OK", new OnClickListener() {
public void onClick(final DialogInterface dialog,final int which) {
dialog.cancel();
}
}).create().show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Somehow my alert dialog can never show and I don't know why. If you have any idea what the reason of this problem is, please help me out. Thanks a lot!
EDIT: Turns out I only close my Progress Dialog after executing this method, so I decided to dismiss it before showing the alert dialog. But it still hasn't solved the problem.
So I solved this myself this way: somehow the Alert dialog only works outside the method checkUser. So I move the bits within the error if clause outside, below the dismiss() of Progress dialog in onPostExecute():
protected void onPostExecute(JSONObject json) {
checkUser(json);
mProgressDialog.dismiss();
try {
if (json.get(ERROR_MSG) != null){
String errorMsg = json.getString(ERROR_MSG);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMsg)
.setTitle("Error")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And voila, it works!
P.s: I still don't know why the dialog can't show when put within checkUser though.
missing builder.create()
// Error in login
builder.setMessage("Incorrect username/password")
.setTitle("Error")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
}).create().show();