Transparent AlertDialog Android - android

I want to make my AlertDialog transparent, the following is my code and the line where it should be transparent just doesnt work.
AlertDialog.Builder builder;
AlertDialog alertDialog;
context = getActivity();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.moreinfotable,null);
ListView list = (ListView) layout.findViewById(R.id.dialog_listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ViewImage.this.getActivity(), R.layout.interior_listview,
R.id.interior_list_text, values);
list.setAdapter(adapter);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
alertDialog = builder.create();
//THE LINE THAT DOESNT WORK// alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
can someone please help. Thank you.
This is what i want to do.

Use this way
alertDialog.show();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.argb(0, 200, 200, 200)));
I did this on custom alert dialog.
Values are like alpha, red, green, blue. Range from 0 to 255. Change values of alpha to get different degree of transparency
I don't know if this will help you. This is how i created my dialog box (its a prompt to user to try the app without log-in)
public void promptUser()
{
final Dialog requestDialog= new Dialog(MyActivity.this);
requestDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
requestDialog.setContentView(R.layout.lyt_prompt_user);
TextView requestTitle = (TextView) requestDialog.findViewById(R.id.request_title);
TextView requestText = (TextView) requestDialog.findViewById(R.id.request_text);
Button acceptButton = (Button) requestDialog.findViewById(R.id.request_accept);
Button rejectButton = (Button) requestDialog.findViewById(R.id.request_reject);
requestTitle.setText("Don't want to login?");
requestText.setText("You can still explore the app with limited functionality.");
acceptButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do something
requestDialog.dismiss();
finish();
startActivity(intent);
}
});
rejectButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
requestDialog.dismiss();
(MyActivity.this).finish();
}
});
requestDialog.setCancelable(false);
requestDialog.show();
requestDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.argb(0, 200, 200, 200)));
}
I tried changing the alpha from 0 to 90, it shows a faint white background, somewhat glassy feeling.

Related

how to create a custom dialog and receive results in android?

i have an activity that when user click on button , a dialog open. in this dialog there is a spinner that have 3 choices: Blue,Red,Green. and there is a submit button. i want that when user select a color and click on submit, in caller activity, its String color set to selected color in dialog. i try this: but not worked. please help me....
String color;
String dialogColor;
showDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("my dialog");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner);
final TextView status = (TextView) dialog.findViewById(R.id.status);
Button submit = (Button) dialog.findViewById(R.id.submit);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
dialogColor = parent.getItemAtPosition(position).toString();
status.setText("Color is: "+dialogColor);
color = dialogColor;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("Color",dialogColor);
dialog.dismiss();
}
});
dialog.show();
}
});
i use both of direct and with intent ways to assign my color String to selected value. but not worked. where i have mistake?
I think the best way to create custom dialogs now is the Dialog Fragment, because the simple dialog it's limited. For example it's the way to create a dialogs with material design. And you have a differents ways to take info from dialog fragment, the first and the second for example.
This is basic code to create a dialog fragment:
//Method to call and start dialog fragment class
public void ShowPhotoFilesDialog(Activity context,File photo){
//Declaration of classes
Custom_DialogFragment custom_dialogFragment = new Custom_DialogFragment ();
FragmentManager fragmentManager = context.getFragmentManager();
// The device is using a large layout, so show the fragment as a dialog
custom_dialogFragment.show(fragmentManager, "dialog");
}
And this is the basic dialog fragment class:
public class Custom_DialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
try {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//To hide action bar from layout
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Declaration of controls
View v = getActivity().getLayoutInflater().inflate(R.layout.my_custom_layout);
builder.setView(v);
//My code
return builder.create();
}
catch (Exception ex){
Log.e("-- Custom_DialogFragment.onCreateDialog --","",ex);
return null;
}
}
}
Tell me if I helped you, good programming!

Android dialog title and divider color not changing

I have created custom Dialog.
I wants To change my Dialog title color and Divider line color.
I have gone through many answers but still M not successful.
I have tried different methods like:
int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
titleDivider.setBackgroundColor(R.color.Red);
And used the following code:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.alertdialog_caseadd);
dialog.setTitle("Add new court");
final Resources res = getResources();
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
inal View titleDivider = dialog.findViewById(titleDividerId);
titleDivider.setBackgroundColor(res.getColor(android.R.color.holo_orange_dark));
dialog.getWindow().setBackgroundDrawableResource(R.drawable.alerttitle);
final EditText casetype= (EditText) dialog.findViewById(R.id.text);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
Button dialogButtoncancel = (Button) dialog.findViewById(R.id.dialogButtoncancel);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(casetype.getText().toString().equals(""))
Alert.showError("Error", "Please enter case type", CaseType.this);
else
if(Network.isOnline(getApplicationContext()))
new WebTask().execute();
else
Alert.showNetworkError(CaseType.this,inflater);
dialog.dismiss();
}
});
dialog.show();
dialogButtoncancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
That above code is not working, divider line and title color shows in blue color only.
Please help me change it.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.alertdialog_caseadd);
//this method help me for title color change
dialog.setTitle(Html.fromHtml("<font color='#FFFFFF'>Add new case stage</font>"));
//this method help me for divider color change
int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.white));

AlertDialog won't show in onCreate method

So I want to ask a user their name before continuing the build of an activity. Most of the activity is populated dynamically so it seems like this should be easy. For some reason, the Dialog never appears though. I've tried everything and the only thing I can think of is: Perhaps it doesn't like being in the onCreate method? It doesn't seem like this should be an issue though since it's literally the last method being called in onCreate. Check it out and let me know what you see:
onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeHotels();
FIRST_TURN = true;
clearOldBoard();
setContentView(R.layout.activity_game_board);
setUpBoardGUI();
setOnPlayerSetUpEventListener(new onPlayerSetUpEventListener() {
#Override
public void onPlayerSetUp(){
prepForFirstTurn();
}
});
startGameDialog();
}
And the startGameDialog method:
public void startGameDialog(){
Context context = getApplicationContext();
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.AppBaseTheme);
AlertDialog.Builder startGameDialog = new AlertDialog.Builder(ctw);
startGameDialog.setTitle(getResources().getString(R.string.whats_your_name));
LinearLayout dialogLayout = new LinearLayout(context);
final EditText newName = new EditText(context);
newName.setText("");
Button submit = new Button(context);
OnClickListener onClick = new OnClickListener() {
#Override
public void onClick(View v) {
GameBoardActivity.NAME = newName.toString();
setUpPlayers();
}
};
submit.setOnClickListener(onClick);
dialogLayout.addView(newName);
dialogLayout.addView(submit);
startGameDialog.setView(dialogLayout);
Dialog dialog = startGameDialog.create();
dialog.show();
dialog.setCancelable(false);
}
The create method return an instance of AlertDialog
Instead of initializing it to the Dialog when you call the create method of the AlertDialog
Dialog dialog = startGameDialog.create();
dialog.show();
pass it to the AlertDialog
AlertDialog alertDialog = startGameDialog.create();
alertDialog.show();
use the currents activity context rather than using the whole application contex.t
Context context = Your_activity.this;

change the colour and font of text of title in dialog box

I want to change the font and colour of the title in dialog box, I want change font, size, and colour, what should I do?
here is my code,
ivworknggroup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(Ourwork.this);
dialog.setContentView(R.layout.nggroup);
dialog.setTitle("N.G.GROUP");
TextView tvnggroup1 = (TextView) dialog.findViewById(R.id.tvnggroup1);
TextView tvnggroup2 =(TextView)dialog.findViewById(R.id.tvnggroup2);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(),"fonts/antennalight.ttf");
tvnggroup1.setTypeface(typeFace1);
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/antennabold.ttf");
tvnggroup2.setTypeface(typeFace);
tvnggroup2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nggroupindia.com/"));
startActivity(browserIntent);
}
});
dialog.show();
}
});
can any one help me?
thank u.
Well i had a similar situation once, but this is what got it solved for me.
Dialog sortDialog = new Dialog(getApplicationContext());
sortDialog.setContentView(R.layout.adtype_alertdialog);
sortDialog.setTitle("N.G.GROUP");
int dividerId = sortDialog
.getContext()
.getResources()
.getIdentifier("android:id/titleDivider", null,
null);
if (dividerId != 0) {
View divider = sortDialog.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(
R.color.yellow));
}
TextView tv = (TextView) sortDialog
.findViewById(android.R.id.title);
if (tv != null) {
tv.setTextColor(getResources().getColor(R.color.yellow));
}
sortDialog.show();
android:id/titleDivider & android.R.id.title in an identifier found in the alert_dialog.xml in your SDK folder
you should use custom view for title of your dialog maybe this link help you
how to include custom title view with in AlertDialog in android?
try this way
private String HALLOWEEN_ORANGE = "#FF7F27";
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Message").show();
setTitle("Title").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10);//to change font size
textView.setTextColor(Color.RED); // to change color
//to change font family
Typeface face = Typeface.createFromAsset(getAssets(),"font/fontFileName.ttf");
textView.setTypeface(face);

Why my dismiss() method not able close Customize Alert dialog

I am developing a android app where I have created Customize alert dialog. I declare Globally alert dialog and and AlertDialog.builder as follow. Now I am calling three method f1(), f2(),f3(), in button click.
btn_my_order.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
f1();
f2();
f3();
return false;
}
});
I Declared orderDialog and builde globally as follow :-
private AlertDialog orderDialog = null;
AlertDialog.Builder builder;
My f1() block is as follow :-
F1{
builder = new AlertDialog.Builder(MainScreen.this);
mContext = getApplicationContext();
/**
* by the help of inflater my ordre is showing in list view
*/
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);
ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);
bclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
orderDialog.dismiss();
System.out.println(" click on close button");
}
});
/**
* click of place order to kitchen
*/
bPlaceOrder.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Place order click");
palceMyOrdertoServer();
new SendOrderFromTable().execute();
System.out.println("place order to server is called");
String msg = "Your Order is Successfully placed to Kitcken";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
orderDialog.dismiss();
}
});}
My f2() is for some Cursor work with data base
F2{
// many stuff to be here populate data from cursor and bind it with adapter
// no any issue in this mehod
}
Now finally I am calling f3()
F3{
builder.setView(orderDialogLayout);
orderDialog = builder.create();
orderDialog.show();
}
Now i am going to explain all my problem f1() method is for initialization f2() is for populate data and f3() to show customize alert dialog . Why my
orderDialog.dismiss();
is not working for me.Even though i am able to see my logcat with message
"Click on close button"
That means execution is going on dismiss() method then why customize alert dialog didn't close at click. Thanks in advance to all
You should add final in your orderDialog private variable.

Categories

Resources