How to use vendor theme in Android application - android

I'm developing my "Hello, World" application for Android and came around something that annoys me.
The theme used for my app's AlertDialog isn't the same used for other such dialogs presented on the device. The device is a HTC Desire HD on which I've changed the theme, but I was expecting that standard UI elements (like AlertDialog) would somehow reflect the device theme. It's something that I'm missing or maybe a know problem with some vendors/models?
Here's my code:
final AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle("Delete")
.setMessage("Really Delete?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setNegativeButton("Cancel", null)
.show();
And here's how the dialog looks like in different situations:
Device's AlertDialog with default theme (Messages app)
Device's AlertDialog with changed theme (Messages app)
My app's AlertDialog (with both default and changed theme)
Thanks.

Your assumption is correct.
HTC have most likley created customized dialogs for the HTC apps.

Related

Android Studio popup text

I new to Android Studio, still a lot to learn. I'm developing a engineering related app with several different activities, most do calculations and some have results that could be 15 or so lines.
I would like to have the results on a separate page, trying to keep screen clutter down. Does AS have anything like a popup text that I could output to, or would it be better to add another activity that is just a textlist or a table and output to that.
Thanks Steve.
Try AlertDialogs. They have a scrollbar so they can display long texts too.
But for 15 lines you might want a new activity.
A simple Example:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
alertdialog.show();

Apply similar looking styles to AlertDialog box for different Android versions

I want to make the AlertDialog box for my app look like the ones on Android > 6.0.0 ( as depicted in the following screenshots) for all lower android version till 4.4.4. I have applied a custom style for the dialog box.
Following is the style in styles.xml for AlertDialog:
<style
name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:textColor">#color/blue</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">#color/dark_grey</item>
<item name="android:backgroundDimAmount">0.3</item>
</style>
Following is how I applied the style in java:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom))
.setMessage("Why is DialogBox Style Inconsistent through different android versions?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.show();
As a get around I managed to at least change the text color to white on the dark background by using:
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
style = android.R.style.Theme_Material_Dialog;
else
style = R.style.AlertDialogCustom;
So far I have tried making seperate style for lower Android version and apply them via java by detecting Android version. But, I am unable to replicate the style of AlertDialog box on Android 6+ on lower android versions till 4.4.4. And I am unable to find a solution on the internet.
There is an anomaly though; if I apply my custom style on Rate this app provided by kobakei the resulting RateThisApp dialog looks the same on all the android versions. So I believe styling the AlertDialog in a particular manner should fix the issue.
This is what I want - To make the AlertDialog box on Android 4.4.4 and 5.1.0 to look like the one in Android 6.0.0 and higher by using suitable styles. And why does the textColor and textColorPrimary attributes does not get applied on lower android versions?
If the resource provided in my question is insufficient please let me know in the comment, I will share more details.
Here are the screenshots of AlertDialog box on different Android versions with the same style applied.
Android >= 6.0.0
Android = 5.1.0
Android 4.4.4
For more flexibility, you can just create your custom dialog class with a custom layout.
It will give you better control over the way the layout looks.
You can create very complicated UI in your dialog without writing tons of code
You can simply call your dialog like this :
CustomDialog customDialog = new CustomDialog();
customDialog.show();
And you can manage click listeners and more events inside your dialog class instead of adding those code lined inside your activity and make it super big and hard to understand.
Here is an example:
public class CustomDialog extends Dialog {
private ImageView imageView;
private Context dialogContext;
public CustomDialog (#NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog); .. //your layout
dialogContext = context;
imageView = findViewById(R.id.full_size_image); //controll your views (use any view, the image view is only an example)
}
public void someMethod() {
}
}
}

AlertDialog displays briefly and disappears

I have an android app written with Xamarin which needs to notify from a class that does not have good access to an activity or context, so I am using a System Alert dialog to display the message.
In Android 4.4, the pop-up appears and the user must tap the OK button to clear it. The rest of the screen is dim and the user cannot interact with any other UI elements until the pop-up is cleared. This is the desired behavior.
In Android 5.0 (tested on a Galaxy S6 and a tablet), the pop-up will appear for about one second and then disappear without requiring any interaction whatsoever. I have done a number of Google and SO searches to no avail.
private static void ShowSystemAlert(Context context, string message)
{
var dialog = new AlertDialog.Builder(context, Android.Resource.Style.ThemeHoloLightDialog)
.SetNeutralButton("OK", (alertSender, args) => {})
.SetMessage(message)
.Create();
dialog.SetCanceledOnTouchOutside(false);
dialog.Window.SetType(WindowManagerTypes.SystemAlert);
dialog.Show();
dialog.Window.DecorView.SetBackgroundResource(Android.Resource.Color.Transparent); //remove strange small border around dialog
}
How can I get the pop-up to work like it does in Android 4.4? The best answer will work in Lollipop, Marshmallow, and Nougat.
I would also very much appreciate an explanation as to why this happened, or any background information you can give. Thanks!
Is it xamarin? Create your alertdialog with just the context, don't use the theme.

How to make a alert dialog button left and right instead of both align right without using custom view in android

Here is the general way I build an alert dialog. It is not crash just the outcome doesn't suit my expectation.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create().show();
Outcome:
But what I want is the cancel and fire button use up all the space of the dialog box rather than both align at the right side.
Just like the cancel and ok button. Ignore the check box what I need is only those button position.
Since I saw a lot of other app such as vine,twitter,foursquare etc display the alert dialog with the button which I desire so is there a way or method I can assign to the alert dialog while create it, I know there is a lot of way which can achieve this but I'm wondering is there a way without to use custom view or edit the LayoutParams of the button?
So recently I been talking to one of twitter developer what he said is every device have difference way to manage their user interface. Even there is a way better to use a custom dialog to make sure that on all device it is presenting the way what you want it to look like.
Final outcome use a custom dialog if you really want it to look like which way you want to.

Android AlertDialog.Builder displays blank unless a theme is supplied

I have recently begun work on an existing android app and noticed that one of the modals displays as a white blank square. I did some research and someone suggested that supplying a theme should fix it. I tried this and it does fix the issue but i don't understand why this was working without it and now its not. The code we use to initialize the AlertDialog looks like this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setItems(R.array.media_resume_options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//handle selection logic here
//......
}
});
builder.create().show();
I can fix it by changing the instantiation line to
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_DARK );
I don't see any changes to this part of the code that might have caused it. Could it be affected by something else? whats the potential risk that it might affect other dialogs?
It can't be blank,the textcolor and background color both are white that's why you think it is blank.
How to change default text color of dialogbox is explained here
Change dialog text color on 5.0+

Categories

Resources