dialogframgent width - android

I am using dialogFragment to make my own custom fragment but its size does not look like what is in priview
this is what is showing in preview
and this what happens when the dialog is shown
I want it to look exactly like my preview - what should I do?

Try this
final Dialog dialog = new Dialog(mContext, R.style.Theme_Transparent);
View view = LayoutInflater.from(mContext)
.inflate(R.layout.layout_custom_dialog, null);
dialog.setContentView(view);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.width = wlp.MATCH_PARENT;
wlp.gravity = Gravity.TOP;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
window.setAttributes(wlp);
And add this to styles.xml file
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>

Related

Cannot change background color of AlertDialog button

I'm trying to change the color of the AlertDialog with likely all solutions defined for this issue in stackoverflow and it still doesn't work.
This is the code I'm using to try to achieve that:
val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
var view = factory.inflate(R.layout.sample, null)
[...]
vacationDialog.setView(view)
val window = vacationDialog.create()
vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
dialog.dismiss()
}
/**Listener called when the AlertDialog is shown**/
vacationDialog.show()
window.setOnShowListener {
/**Get the positive button from the AlertDialog**/
val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)
/**Set your color to the positive button**/
positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
}
This is the style DialogTheme defined in styles.xml, in which, by the way, I cannot find a way to change the default button color (black), and seems to override any change tried from code.
<style name="DialogTheme" parent="android:Theme.Holo">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:gravity">center</item>
</style>
Any idea on how to approach this issue?
If you are using an AppCompat theme you can use something like:
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFCC00</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#5fa3d0</item>
</style>
With the new Material components for android library you can use the new androidx.appcompat.app.AlertDialog.
Just use something like:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
You can customize your theme with something like:
<!-- Alert Dialog -->
<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">#style/Widget.MaterialComponents.Button.TextButton.Dialog</item>
</style>
and:
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#color/selector_bt</item>
</style>
If you use custom layout then you can change anything easily. Follow the code below:
private fun showAlertDialog(activity: Activity){
val dialog = Dialog(activity)
dialog.setContentView(R.layout.dialog_layout)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}
You can create your custom layout for AlertDialog and inflate it in your custom class extending AlertDialog.
Use androidx.appcompat.app.AlertDialog rather than normal android.app.AlertDialog

How to make an Alert dialog in full screen in Android?

How to make an Alert Dialog in full screen in Android?
Try below code
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
you must create a custom style for your dialog
in your style.xml
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
while inflating your dialog set this theme
dialog = new Dialog(this, R.style.DialogTheme);
【Method 1 | Use Custom Style】
In styles file:
<style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar"> //no actionbar, but status bar exists
<item name="android:windowFullscreen">true</item> //remove status bar
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item> //smooth animation
<item name="colorAccent">#color/colorAccent</item> //change button text color
</style>
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.myFullscreenAlertDialogStyle); //second argument
【Method 2 | Use Built-In Style】
In java file:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
This method will make the button text become green color, you can use dialog.getButton().setTextColor() to change it.
if you're using DialogFragment to build AlertDialog you may set MATCH_PARENT on LayoutParams in onResume():
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}

how to remove top strip in dialog box

I open the activity in dialog box.how to remove top strip.
my themes code:
<style name="My_search_dailog" parent="Base.V7.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/darker_gray</item>
</style>
out put:
please help me
Use LayoutInflater for your custome dialog view.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final LayoutInflater inflater = activity.getLayoutInflater();
final View view = inflater.inflate(R.layout.caution_dialog, null);
builder.setView(view);
override +ve button and -ve button
AlertDialog alert = builder.create();
alert.show();
Update your style
<style name="My_search_dailog" parent="Base.V7.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
I faced the same problem and solved it by adding "requestWindowFeature" attribute in Java file where i am showing this dialog.
Dialog dialog = new Dialog();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

Title of the dialog is too high

How can I reduce the height of the green title?
Activity:
final Dialog dialog = new Dialog(EditorActivity.this, R.style.dialog);
dialog.setContentView(R.layout.preference_timerange);
dialog.setTitle(getString(R.string.zmien_czas_wyprawy));
Styles.xml
<style name="dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
<item name="android:windowBackground">#color/dialog_background</item>
<item name="android:textColor">#color/dialog_text</item>
</style>
<style name="dialog_title_style" parent="android:TextAppearance.WindowTitle">
<item name="android:textSize">16dp</item>
<item name="android:background">#color/dialog_title_background</item>
<item name="android:textColor">#color/dialog_title_text</item>
</style>
I don't know how to change the height of the Dialog's title, but you can remove the whole title and create a smaller one in your layout preference_timerange.xml:
final Dialog dialog = new Dialog(EditorActivity.this, R.style.dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.preference_timerange);
// Set R.string.zmien_czas_wyprawy as the title in your layout now

Custom Progress Dialog Animation

I am working on Custom Progress Dialog. I've set its style like done in this SO question Positionning the spinning wheel in a custom progress dialog My progress dialog is showing no animation at all. When I changed the background to black then it was only showing a black box. I want my progress dialog to animate.
In activity I am doing something like this
public MyProgressDialog(Context context) {
super(context, R.style.NewDialog);
}
and the NewDialog is as:
<style name="NewDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:width">50dip</item>
<item name="android:height">50dip</item>
<item name="android:background">#android:color/transparent</item>
</style>
Any help is appreciated in advance
import android.app.ProgressDialog;
import android.content.Context;
import android.view.animation.Animation;
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context,R.style.NewDialog);
// TODO Auto-generated constructor stub
}
}
//RESOURCE STYLE FILE res->values->mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackground">#ffffff</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:width">600dip</item>
<item name="android:height">100dip</item>
<item name="android:textColor">#FF0000</item>
</style>
</resources>
//AND USE THIS CODE ANYWHERE IN YOUR APPLICATION TO GET CUSTOM PROGRESS DIALOG
MyProgressDialog pd = new MyProgressDialog(YouActivity.this);
pd.setMessage("Loading. Please wait...");
pd.show();
Well there isn't any difference in your code and mine as I have run your code just to test whether you are experiencing any problem or not but still I am posting for you so that you can sought out the problem.
Thanks :)
public class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context, int resourceIdOfImage) {
super(context, R.style.TransparentProgressDialog);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
resourceIdOfImage = R.drawable.loading_spinner_icon;
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
iv = new ImageView(context);
iv.setImageResource(resourceIdOfImage);
layout.addView(iv, params);
addContentView(layout, params);
}
}
You will get complete example with animation HERE

Categories

Resources