Can't get my DialogFragment background to be transparent - java

I have created a custom DialogFragment like it is described in the developer guide.
Now what I am trying to do sounds simple enough, but I cannot get it to work.
I have defined: android:background="#android:color/transparent" in my layout xml which I am loading like this (in my onCreateDialog):
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.pausedialog, null);
setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
As you can see I also tried to set a custom style in the DialogFragment which is defined like this:
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:alwaysDrawnWithCache">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And I also tried getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
which leads to a null pointer exception.
I am using android.support.v4.app.DialogFragment. Can this be the cause?
Or am I doing something else wrong?

Try to set style and theme in onCreate method of your dialogFragment class implementation.
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int style=DialogFragment.STYLE_NO_TITLE;
int theme=android.R.style.Theme_Translucent;
setStyle(style, theme);
}
Or
if you are using Dialog class then you can also set Style and theme on dialog instance.

This worked for me. I created a new style:
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And then set this style in my DialogFragment's onCreate() method like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}

This is the style I use to totally remove the Dialog`s background.-
<style name="Theme.Dialog" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
As for the Dialog creation, you're creating a DialogBuilder but then you manually inflate a view, I guess that's the problem. Try this instead.-
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme));
AlertDialog dialog = builder.create();
dialog.show();
EDIT
Another approach is extending AlertDialog.-
public class CustomDialog extends AlertDialog {
public DialogParent(Context context) {
super(context, R.style.CustomDialog);
setContentView(R.layout.pausedialog);
// More initialization stuff
}
}
And then 'manually' instantiate it.-
AlertDialog dialog = new CustomDialog(getActivity());
dialog.show();

Related

How to set round corners for a custom Dialog?

This is code for my custom Dialog:
public class DialogBrightness extends AppCompatDialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/*Build and create the dialog here*/
}
}
I followed instructions from other answers by first creating this drawable xml called dialog_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#fff5ee"/>
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
Then setting it as the background of the layout_dialog xml:
android:background="#drawable/dialog_bg"
But I can't do the final step which is to set the dialog's root view to transparent:
dialogBrightness.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
because there's no getWindow() function.
Also when they say root view do they mean the one that I set to null in the inflate function above?
You can use the MaterialAlertDialogBuilder included in the Material Components library:
import androidx.fragment.app.DialogFragment;
public class CustomDialog extends DialogFragment {
//...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new MaterialAlertDialogBuilder(getActivity(),R.style.MaterialAlertDialog_rounded)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", null)
.create();
}
}
with:
<style name="MaterialAlertDialog_rounded" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
I highly recommend #GabrieleMariotti's answer but I am writing here to see how you could have implemented in your original way.
there's no getWindow() function.
You could have used requireView() instead as :
dialogBrightness.requireView().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Also when they say root view do they mean the one that I set to null in the inflate function above?
Yes, it is. Generally, it is not recommended to pass null as a root parameter as root view is needed to calculate the layout parameters of the view that is currently being inflated by the LayoutInflater. Instead, you should use it like this :
View view = LayoutInflater.from(requireContext()).inflate(R.layout.layout_dialog, rootView, false);
Here, 3rd parameter is attachToRoot which is passed as false.

Resize DialogFragment when keyboard is visible

(I am working with Xamarin C# but the code is identical to Java)
I created a DialogFragment which inflates a custom layout. The OnCreateView looks like this :
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.dialog_feedback, container);
int scale = Context.Resources.DisplayMetrics.HeightPixels;
int height = scale / 2;
Dialog.Window.SetLayout(ViewGroup.LayoutParams.MatchParent, height);
Dialog.Window.SetGravity(GravityFlags.Bottom | GravityFlags.CenterHorizontal);
return view;
}
And the OnCreate looks like this :
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetStyle(0, Resource.Style.dialog_no_border);
}
Here's the style :
<style name="dialog_no_border" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
The layout that the dialog is inflating contains a ConstraintLayout as the parent and has a AppCompatEditText inside it.
When i open the dialog and click on the EditText, the keyboard overlaps the Edittext. What i'm trying to do is, when the keyboard pops up, it should push up the DialogFragment and show the EditText above itself. How do i achieve that ?
I tried setting android:windowSoftInputMode="adjustNothing", android:windowSoftInputMode="adjustPan", android:windowSoftInputMode="adjustResize" in the activity tag inside the manifest, but with no luck!
So, how do i achieve this ?

Android Actionbar is not showing

After I created my own Style, my Actionbar doesnt appear.I'm using a Pageviewer with tabhost which is visible but my actionbar isn't.I think it is caused by my Styles.xml.I also tried to edit the theme with the Android theme Editor but it did not help.
This is my Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Newme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_newme</item>
<item name="popupMenuStyle">#style/PopupMenu.Newme</item>
<item name="dropDownListViewStyle">#style/DropDownListView.Newme</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.Newme</item>
<item name="actionDropDownStyle">#style/DropDownNav.Newme</item>
<item name="actionBarStyle">#style/ActionBar.Solid.Newme</item>
<item name="actionModeBackground">#drawable/cab_background_top_newme</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_newme</item>
<item name="actionModeCloseButtonStyle">#style/ActionButton.CloseMode.Newme</item>
<item name="actionBarWidgetTheme">#style/Theme.Newme.Widget</item>
</style>
<style name="ActionBar.Solid.Newme" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">#color/theme_color</item>
<item name="backgroundStacked">#color/theme_color</item>
<item name="backgroundSplit">#color/theme_color</item>
<item name="progressBarStyle">#style/ProgressBar.Newme</item>
</style>
<style name="PopupMenu.Newme" parent="#style/Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_newme</item>
</style>
<style name="DropDownListView.Newme" parent="#style/Widget.AppCompat.ListView.DropDown">
<item name="android:listSelector">#drawable/selectable_background_newme</item>
</style>
<style name="ActionBarTabStyle.Newme" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/tab_indicator_ab_newme</item>
</style>
<style name="DropDownNav.Newme" parent="#style/Widget.AppCompat.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/spinner_background_ab_newme</item>
<item name="android:popupBackground">#drawable/menu_dropdown_panel_newme</item>
<item name="android:dropDownSelector">#drawable/selectable_background_newme</item>
</style>
<style name="ProgressBar.Newme" parent="#style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressDrawable">#drawable/progress_horizontal_newme</item>
</style>
<style name="ActionButton.CloseMode.Newme" parent="#style/Widget.AppCompat.ActionButton.CloseMode">
<item name="android:background">#drawable/btn_cab_done_newme</item>
</style>
<style name="Theme.Newme.Widget" parent="#style/Theme.AppCompat">
<item name="popupMenuStyle">#style/PopupMenu.Newme</item>
<item name="dropDownListViewStyle">#style/DropDownListView.Newme</item>
</style>
I hope some of you can help me.
EDIT:
Here is my Mainactivity:
package com.miguel.fitnessapp;
public class MainActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener {
private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.theme));
}
initialiseTabHost();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
// Fragments and ViewPager Initialization
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
#Override
public void onPageSelected(int arg0) {
}
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Training").setIndicator("Training"));
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Ernährung").setIndicator("Ernährung"));
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Körper").setIndicator("Körper"));
mTabHost.setOnTabChangedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu_edit, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
int id = item.getItemId();
if (id == R.id.benutzer){
}
return super.onOptionsItemSelected(item);
}
}

How to automatically apply material primary and accent colors to dialog

I have custom theme with changed primary and accent colors. But AlertDialog is not picking up those colors automatically when I create dialog using:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
This is what I get - dialog with default colors:
And this is what I would like to have - dialog with custom colors:
I can create custom AlertDialog theme and apply it during AlertDialog creation:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme_Dialog);
I would like to skip calling constructor with theme parameter.
Is there a way to force custom colors on all themes without deriving them in styles, including AlertDialog base theme?
Minimup API level I have to support is 15.
My activity code
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public void dialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
My customized style
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
My colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Add this in your the App theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">#style/AppTheme.Dialog</item>
And then,
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);

Icon istead of three dots Android Menu

Im trying to do a menu with only one icon, but instead of that icon, i am getting the typican three dots, and inside my option. So, i only want one icon. That is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context="com.bartek.gestionpea.MainActivity">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
app:showAsAction="ifRoom"/>
</menu>
Here goes my class:
public class MainActivity extends Activity {
PeniaSQLiteHelper usdbh;
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle("GESTION");
Button btnCrearPena = (Button) findViewById(R.id.btncrearpena);
btnCrearPena.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CrearNueva.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_principal, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.ayuda:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
You are using some things for the native action bar (e.g., inheriting from Activity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction). Choose one and stick with it:
If you wish to use the native action bar, use android:showAsAction
If you wish to use the appcompat-v7 action bar backport, inherit from AppCompatActivity (in the latest appcompat-v7) or ActionBarActivity (for prior versions of appcompat-v7)
Since you use the standard Activity, try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
android:showAsAction="always" />
</menu>
You can modify the app theme (in themes.xml):
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/your_icon_here</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"options"</item>
</style>
More info here
Note that when using appcompat themes, you sometimes need to set xml attibutes twice:
once with and once without the android namespace
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>

Categories

Resources