Can't get transparent DialogFragment - android

I have a dialog Fragment which look like that.
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
This code get background semi transparent. But I still got a white part on the bottom. I want to get rid of the white to just have semi transparent background
I already tried a lot of stuff that I saw in other posts.
I don't know what is the object that I must change between the DialogFragment, the AlertDialog and the LinearLayout.
It may not be the LinearLayout, because when I increase margin, nothing is moving.
Here is my code :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// setStyle(DialogFragment.STYLE_NORMAL, 0);
// setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
// setStyle(STYLE_NO_FRAME, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(
R.layout.share_or_die, null);
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
ad.setCanceledOnTouchOutside(true);
ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return ad;
}
I just call it in the mainActivity when user click back button:
#Override
public void onBackPressed() {
if (isUserConnected && !hasShared) {
shareOnExitDialog = new ShareOnExitDialog();
shareOnExitDialog.setCancelable(true);
shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
} else {
finish();
}
}
Any help would be appreciated !

The issue is in default dialog theme. Based on this and this answers it's much easier to achieve your target.
The Activity should be like the following:
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
#Override
public void onBackPressed() {
MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
}
}
And fragment:
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
/**
* Create a new instance of MyDialogFragment, providing "title"
* as an argument.
*/
static MyDialogFragment newInstance(String title) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);
final Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
Also, be sure to do the following: before setContentView() in the activity, getWindow().requestFeature(Window.FEATURE_NO_TITLE) should be added in order to use *NoTitleBar style.
The result is (I've used LinearLayout with single TextView inside):

I hope it can help you
#Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}

None of the above solutions worked for me. What i found to work was to make sure that I had imported android.support.v7.app.AlertDialog instead of the generic app.alertdialog.
Hope this helps others too.

It works for me:
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Related

Is there a way to access Dialog objects from MainActivity?

I want to access the object of Dialog in MainActivity.
For example, in the MainActivity, use the Button to pop up a dialog.
case R.id.txtCalibMain:
SubMenu_Calib mCalibDialog = new SubMenu_Calib(instance);
mCalibDialog.show();
break;
In this way, but in the previous steps, before the Dialog
If you want to preserve the text content of the TextView inside the Dialog
How can you approach it?
Currently, Dialog is defined and called by using a separate class.
I'm trying to access it from the outside, so I'm importing a null value or an undefined TextView.
It is natural that an error will be raised.
Dialog Activity Code
public class SubMenu_Units extends Dialog implements View.OnClickListener {
public static SubMenu_Units mSubMenu_Units;
public static MainActivity instance;
public static List<SingleItem> singleItems;
static SingleAdapter mSingleAdapter;
public static TextView mtxtTestMenu;
public SubMenu_Units(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.submenu_units_layout);
mSubMenu_Units = this;
initialize();
mtxtTestMenu = (TextView)findViewById(R.id.txtTestCenterMenu);
}
void initialize()
{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
setCanceledOnTouchOutside(true);
ColorDrawable dialogColor = new ColorDrawable(0xFF424957); // 0xFF424957
getWindow().setBackgroundDrawable(dialogColor);
mOrientation = getContext().getResources().getConfiguration().orientation;
WindowManager.LayoutParams mPortrait_params = getWindow().getAttributes();
mPortrait_params.width = 375;
mPortrait_params.height = 945;
mPortrait_params.gravity = Gravity.CENTER | Gravity.LEFT;
mPortrait_params.x = 5;
mPortrait_params.y = 68;
getWindow().setAttributes(mPortrait_params);
}
You need to have common class where you need to copy below code
i.e; You need to extend below CurrentActivity with CommonActivity
//In Current Activity
case R.id.txtCalibMain:
exitAlert("Some Message)
break;
// Main Acitivty
public void exitAlert(final String message) {
LayoutInflater inflater = LayoutInflater.from(this);
View mExitAlertView = inflater.inflate(R.layout.alert_exit_logout_dialog, null);
final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(mExitAlertView);
dialog.setCanceledOnTouchOutside(true);
TextView txtAlertMessage = (TextView) mExitAlertView.findViewById(R.id.txtAlertMessage);
txtAlertMessage.setText(message);
mExitAlertView.setOnTouchListener((v, event) -> {
dialog.dismiss();
return false;
});
Button btnExitAlertNo = (Button) mExitAlertView.findViewById(R.id.btnExitAlertNo);
btnExitAlertNo.setOnClickListener(v -> dialog.dismiss());
Button btnExitAlertYes = (Button) mExitAlertView.findViewById(R.id.btnExitAlertYes);
btnExitAlertYes.setOnClickListener(v -> {
dialog.dismiss();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
});
dialog.show();
}

PopupWindow cannot display Unity3D views in Android, but Dialog does?

Here is some brief codes of my project, in which I'd like to display a Unity3D model to do something.
public class MainActivity extends FragmentActivity{
protected UnityPlayer mUnityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
getWindow().setFormat(PixelFormat.RGBX_8888);
mUnityPlayer = new UnityPlayer(this);
...
mTestImageButton = ***;
mTestImageButton.setOnClickListener(mTestImageButtonClickListener);
}
private ImageButton mTestImageButton;
private View.OnClickListener mTestImageButtonClickListener = new
View.OnClickListener() {
#Override
public void onClick(View v) {
showUnity3D_dialog(v);
//showUnity3D_popupwindow(v);
}
};
private void showUnity3D_dialog(View v) {
Dialog dialog = new Dialog(v.getContext(), R.style.move_dialog);
dialog.setContentView(mUnityPlayer);
Window window = dialog.getWindow();
window.setWindowAnimations(R.style.move_dialog);
WindowManager.LayoutParams lp = window.getAttributes();
window.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
lp.width = 1179;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.setAttributes(lp);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
}
});
dialog.setCanceledOnTouchOutside(true);
dialog.show();
mUnityPlayer.resume();
}
private void showUnity3D_popupwindow(View v) {
PopupWindow pop = new PopupWindow(mUnityPlayer, 1000, 800);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(false);
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
}
});
pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
mUnityPlayer.resume();
}
}
As codes above, I tried to display a Unity3D-model in a PopupWindow, but the result makes me a bit confused, since there is nothing on screen when I called showUnity3D_popupwindow(v).
But when I called showUnity3D_dialog(v), the model by Unity3D shows.
I really can't understand what caused such scenario, and how can I display my model in a PopupWindow in android? Or, is it an impossible task?
I have my project under Android 4.3 & Unity 5.4.0.
Thanks a lot.
Looks like you are currently using the PopupWindow(View contentView, int width, int height) overload function. I've seen instances where functions that take View, Context or Activity does not work in Unity and you have to try other overloads.
You've tried the one takes View, now try the Context overload:
PopupWindow(Context context)
then set the width and height later on with the setWidth and setHeight function. You can get the current Context with UnityPlayer.currentActivity.getApplicationContext().
private void showUnity3D_popupwindow(View v)
{
Context mContext = UnityPlayer.currentActivity.getApplicationContext();
PopupWindow pop = new PopupWindow(mContext);
pop.setWidth(1000);
pop.setHeight(800);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(false);
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
}
});
pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
mUnityPlayer.resume();
}

Incoming Call Screen obstruction by an Activity (Theme Dialog) over it

I am trying to show an Activity (with Theme Dialog) over an Incoming call Screen to show some information. I am done with the that, but the problem is whenever the call comes, Activity Dialog pops up over it and it covers SLIDER ( for accepting/Rejecting calls) .
I want the Activity Dialog over Incoming call screen but still wants the user to pick/reject calls.
I did use this But now unable to finish activity(Dialog).
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Warm Regards
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.notedialog);
this.setFinishOnTouchOutside(false);
initializeContent();
phone_no = getIntent().getExtras().getString("phone_no");
String note = getIntent().getExtras().getString("note");
tv_client.setText(phone_no + " is calling you");
note_mEditText.setText(note);
dialog_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.finish();
// this.setFinishOnTouchOutside(false);
System.exit(0);
}
});
No need for an Activity with dialog style.
Call this method only when you want to show the dialog.
This will create a dialog on top of your phone.
final Dialog dialog;
public void showDialog(){
dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.TOP);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.notedialog);
dialog.setCancelable(true);
dialog.show();
}
And to cancel the dialog call this method.
public void cancelDialog(){
if(dialog!= null ){
dialog.dismiss()
}
}
OR
If you insist to use an Activity dialog You can use this method to change the position of window.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
View view = getWindow().getDecorView();
WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
lp.gravity = Gravity.CENTER | Gravity.TOP
lp.x = 10;
lp.y = 10;
lp.width = 300;
lp.height = 300;
getWindowManager().updateViewLayout(view, lp);
}

Android full screen dialog fragment like calendar app

I am trying to achieve a full-screen dialog like the below image. I am able to show a full screen dialog but when the dialog is shown the status bar color changes to black and does not keep the primary-dark color.
Heres my dialog fragment
public class IconsDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.fragment_icons_dialog, container, false);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// creating the fullscreen dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
}
To get DialogFragment on full screen
Override onStart of your DialogFragment like this:
#Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
In order to setStatusBarColor you need to set the flag: FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(color);
}
}
One way is to change back your status bar color as theme programmatically whenever you open the dialog.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Your theme color);
}

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;

Categories

Resources