How to Transparent in Custom Alert Dialog in android - android

I created the custom alert dialog for my project. When I click the button, the custom alert dialog is opened but a white background appears on my custom alert dialog. How can I make it transparent?
.java:
package com.example.bskes.customalertdialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button adbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adbtn = (Button) findViewById(R.id.btnCustomAD);
adbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.rtr_mem_profile_layout, null);
builder.setPositiveButton("Thank You", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "It's My Pleasure", Toast.LENGTH_SHORT).show();
}
});
builder.setView(view);
builder.show();
}
});
}
}

You should use this ,this will help to show transparent background of dilog
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
First line show the transparent layer on background and second line provide the actual size of the your window view.
it help me, use it

define what follows in the styles.xml file
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
and pass it as argument to the AlertDialog constructor
AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);
Or programmatically, through the Dialog instance you can call
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))

Add this to your styles.xml file
<style name="MyDialog">
<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">false</item>
<item name="android:background">#android:color/transparent</item>
</style>
Add this to your java file :
Dialog dialog = new Dialog(this, R.style.MyDialog);

Related

Customizing a DialogFragment

The first image is what I want. The second is what I get
I've this class to create a dialog
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;
public class AlertFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
// Set Dialog Icon
.setIcon(R.drawable.androidhappy)
// Set Dialog Title
.setTitle("Alert DialogFragment")
// Set Dialog Message
.setMessage("Alert DialogFragment Tutorial")
// Positive button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
// Negative Button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something else
}
}).create();
}
}
In my view
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
.......
final FragmentManager fm = getSupportFragmentManager();
.......
AlertFragment alertdFragment = new AlertFragment();
// Show Alert DialogFragment
alertdFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
alertdFragment.show(fm, "Alert Dialog Fragment");
This is my style file
<resources>
<!-- Base application theme. -->
<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>
<item name="android:spinnerItemStyle">
#style/spinnerItemStyle
</item>
<!-- For each individual Spinner list item once clicked on -->
<item name="android:spinnerDropDownItemStyle">
#style/spinnerDropDownItemStyle
</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="spinnerItemStyle" parent="#android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:padding">0dp</item>
<item name="android:textSize">#dimen/fld_txt_size</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">0dp</item>
<item name="android:textSize">#dimen/fld_txt_size</item>
</style>
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">#color/btn_text</item>
</style>
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
<item name="android:headerDividersEnabled">true</item>
</style>
</resources>
I've posted all the style to see if there's anything going wrong. Regardless that I would like the separator between the header and the body of the dialog I do not understand why the buttons do not appear.
Edit:
I read this
Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)
and I realized that i need to define a style for my dialog.
Infact if i set this
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
<item name="android:buttonBarStyle">#style/Widget.AppCompat.ActionButton</item>
</style>
and then initialize dialog in this way
AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this,R.style.dialog_theme);
I can see change of color and text. But wich is property to show title, separator and buttons?
if you want a customized layout. You need to create an instance of Dialog. i.e. Dialog dialog = new Dialog; and set the contentview for that dialog: dialog.setContentView(R.layout.my_customized_dialog_layout);
you can then create buttons inside that layout and initialize them.

How to remove the divider in AlertDialog

I am using android.support.v7.app.AlertDialog.However, I'm not able to remove the divider.Can anybody tell me how to remove it? thanks.
this is my style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
</style>
this is my code:
AlertDialog.Builder builder = new AlertDialog.Builder(mSettingsView.getBaseActivity());
builder.setTitle("Ringtone");
builder.setSingleChoiceItems(list, -1, listener1);
builder.setPositiveButton("OK", listener2);
builder.setNegativeButton("Cancel", listener3);
builder.show();
AlertDialog divider is different in pre-lollipop and lollipop devices. I found, divider colour is Gray in pre-lollipop(pre-material design) devices. So it is visible. But for material design(lollipop) devices, divider colour is transparent, so it seems not visible/present.
To make divider visible across all devices, explicitly make the colour Gray or any other colour.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setDivider(new ColorDrawable(Color.GRAY));
listView.setDividerHeight(1);
alertDialog.show();
Are you using the AlertDialog inside a android.support.v4.app.DialogFragment? I always use it this way and I never get the divider in your screen:
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance(){
return new MyDialogFragment;
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate my custom layout
View layout = inflater.inflate(R.layout.my_custom_layout, null);
// Initialize my layout components
...
// Build dialog
builder.setTitle("TITLE")
.setView(layout)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", listener);
return builder.create();
}
}

Getting the error expected resource of type attr

I am trying to use the Home Key Locker https://github.com/shaobin0604/Android-HomeKey-Locker
I want to be able to detect and prevent homescreen button click on the lock screen. Majority of the answers mentioned that it cannot be disabled.
This is the HomeKeyLocker Class :
package com.example.harshilshah.screenonoff;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.FrameLayout;
import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
}
}
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
public static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F; // transparent
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
protected final void onCreate(Bundle bundle) {
super.onCreate(bundle);
FrameLayout framelayout = new FrameLayout(getContext());
framelayout.setBackgroundColor(0);
setContentView(framelayout);
}
}
}
This is the styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="OverlayDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
I am getting an error Expected resource of type Attr in the following lines
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);//ERROR
How do i solve this?
I just had this issue. What caused it for me was that I imported the android.R class, instead of the R class already defined in my app. So it wasn't using the styles.xml defined in my app's values, where the OverlayDialog was implemented, but the standard one defined in the Android system that obviously didn't have it. Once I deleted import android.R and added import com.example.myapp.R it worked just fine.

How to change the theme color of default fragment dialog

I have shown a dialog fragment. I have done it, but I want to change background (Theme) color grey to white.
My dialog code:
public class TestDialog extends DialogFragment {
public static TestDialog newInstance() {
return new TestDialog();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return container;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dailog_fragment, null))
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TestDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
use this
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
and then make the custom MyDialog style.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#color/orange_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:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
</resources>
note that it should be in style.xml.
In your manifest file, the activity definition of the dialog activity, add the android:theme attribute as follows.
<activity
android:name=".YourActivityName"
android:theme="#styles/style"
/>
I Got The Solution:
I am using below style theme for set the white background color.
<style name="MyFragment">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackground">#android:color/white</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
We can also use Holo Theme
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileEditActionBar.this, AlertDialog.THEME_HOLO_LIGHT);

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