I need to center the title in a progressDialog, I am trying to do it using styles
<style name="progressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:layout_height">wrap_content</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
<item name="android:textColor">#color/popup_main_title</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:textAlignment">center</item>
<item name="android:textColorSecondary">#color/popup_main_title</item>
<item name="android:textColorPrimary">#color/popup_main_title</item>
</style>
progressDialog = new ProgressDialog(context, R.style.progressDialogTheme);
progressDialog.setTitle(context.getResources().getString(R.string.firmware_downloading_firmware_title));
progressDialog.setMessage(context.getResources().getString(R.string.firmware_downloading_firmware_message));
Set a custom title:
progressDialog = new ProgressDialog(context, R.style.progressDialogTheme);
TextView tvTitle = new TextView(context);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(context.getResources().getString(R.string.firmware_downloading_firmware_title));
progressDialog.setCustomTitle(tvTitle);
progressDialog.setMessage(context.getResources().getString(R.string.firmware_downloading_firmware_message));
Of course you can change other attributes of the title TextView, like text color and background color.
Try this:
<style name="progressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:layout_height">wrap_content</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
<item name="android:textColor">#color/popup_main_title</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:textAlignment">center</item>
<item name="android:textColorSecondary">#color/popup_main_title</item>
<item name="android:textColorPrimary">#color/popup_main_title</item>
<item name="android:windowTitleStyle">#style/DialogTitle</item>
</style>
<style name="DialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
ProgressDialog is deprecated so you can use AlertDialog as ProgressDialog refer below code for the ProgressDialog. This function you need to call whenever you show a progress dialog.
Code:
public void setProgressDialog() {
int llPadding = 30;
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams(llParam);
llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
TextView tvText = new TextView(this);
tvText.setText("Loading ...");
tvText.setTextColor(Color.parseColor("#000000"));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setView(ll);
AlertDialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(layoutParams);
}
}
Output:
Related
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>
This is my code to show a custom dialog in android.
private void showCouponCodeDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialoge_apply_coupon);
dialog.setTitle(R.string.apply_coupon);
final ProgressBar progressBar =(ProgressBar) dialog.findViewById(R.id.progressBar);
Button btnApplyCoupon = (Button) dialog.findViewById(R.id.btnApplyCoupon);
btnApplyCoupon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
}
});
dialog.show();
}
How to set title of the dialog in center horizontally?
Set a custom style , set gravity as you want to the specific one
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
Assign the style to dialog
Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
do other stuff as normal
or set custom title manually
// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewname, null);
builder.setView(view);
TextView title = new TextView(this);
// You Can Customise your Title here
title.setText("Custom Centered Title");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
builder.setCustomTitle(title);
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 center vertical the "Login" title?
This is the style:
<style name="ABGAlertDialog" parent="#android:style/Theme.DeviceDefault.Dialog">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorHint">#android:color/darker_gray</item>
<item name="android:background">#color/corporativo_red</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:maxLines">1</item>
<item name="android:textSize">20sp</item>
<item name="android:bottomOffset">5sp</item>
</style>
I've tried gravity, textAlignment and some others items with no result.
You can't centre a normal alert dialog's title. You will need to create a custom dialog preferably with a dialog fragment or dialog activity depending on the functionality you need.
Set your text style in the android:textAppearance
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:bottomOffset">5sp</item>
<item name="android:textAppearance">#style/YourTextStyle</item>
</style>
Change both text and background color in title bar (Android)?
Textview: using the dialog title style: #android:style/TextAppearance.DialogWindowTitle
If Alert dialog is normal without any of your custom layout or theme than you ca get object by passing id with android namespace
AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity());
alertDialogBuild.setMessage("message text align center");
alertDialogBuild.setTitle("Title text Align Center");
alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = alertDialogBuild.show();
TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android"));
title.setGravity(Gravity.CENTER);
TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android"));
message.setGravity(Gravity.CENTER);
static void centerTitleHorizontally(final TextView title)
{
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
try {
title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} catch (NoSuchMethodError x) {
title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect bounds = new Rect();
title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds);
title.setPadding((title.getWidth() - bounds.width()) / 2, 0, 0, 0);
}
});
}
I need the dialog to fill the screen except for some space at the top and the bottom.
I've search for a solution but couldn't find one probably because I'm declaring it in an onClickListener.
Can someone please give a solution?
Activity code:
sort.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
// Get the layout inflater
LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
View sortView = inflater.inflate(R.layout.sort_layout, null);
sort.setView(sortView);
sort.create().show();
}
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:background="#color/white"
android:orientation="vertical" android:layout_gravity="center">
+ some more stuff here I dont think it's relevant
</LinearLayout>
here set your screen width and width to the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = width;
lp.height = height;
dialog.getWindow().setAttributes(lp);
or in your styles create a style like this then
<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>
create a dialog object like this
dialog = new Dialog(this, R.style.DialogTheme);
Edit ::
get width and height like this for any device it will fills..
WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}
first Make custom style for dialog in style.xml file:
<style name="full_screen_dialog" parent="#android:style/Theme.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/transparent</item>
</style>
Then after set this theme to your Alert Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.full_screen_dialog));
Or You can call new Activity and Give this Custom style to that activity as a theme in your manifest file...
This may helpful for someone.
I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:
mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);
after adding this, my dialog appears in full width of screen.
Make object like below theme
Dialog objD = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog2.getWindow().setBackgroundDrawable(null);
dialog2.setContentView(R.layout.dialog_img);
WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
Window window = dialog2.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
lp.gravity = Gravity.CENTER;
final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
Picasso.with(context)
.load(arrayImages.get(position).get("image"))
.resize(800,1000)
.centerInside()
.into(imgprofile, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
imgprofile.setImageResource(R.drawable.user);
}
});
dialog2.show();
And in your dialog_img.xml file add an Imageview with scaleType(fitXY).
I think you shoud try this one:
Inside the dialogFragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
The simplest way of creating a full screen dialog, create a style like this:
<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
</style>
then:
dialog = new Dialog(this, R.style.DialogFullScreenTheme);