Alert Dialog shows very slowly - android

I'm using Alert Dialog to show the custom view , but alert dialog comes very slowly. It approximately takes 10 or 15 seconds to show . Here my code what is wrong in that .
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vDialog = layoutInflater.inflate(R.layout.dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(vDialog);
AlertDialog ad = builder.create();
ad.show();
Here my dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height ="match_parent" >
<CalendarView
android:layout_width="50dp"
android:layout_height ="50dp">
</CalendarView>
</FrameLayout>

Try This Code:
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
dialog.show();

// Try this way,hope this will help you to solve your problem.
dialog.xml
<CalendarView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height ="match_parent"/>
Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.dialog);
dialog.show();

Related

How to create custom full screen ovarlay dialog in android

I need to create a custom full-screen dialog with overlay background. Here is the image of the expected result:
I tried the below code but it does not show like the above image.
My Dialog open dialog code is bellow
lateinit var dialog: Dialog
dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) // before
dialog.setContentView(R.layout.dialog)
dialog.setCancelable(true)
val lp = WindowManager.LayoutParams()
lp.copyFrom(dialog.getWindow()?.getAttributes())
lp.width = WindowManager.LayoutParams.MATCH_PARENT
lp.height = WindowManager.LayoutParams.MATCH_PARENT
dialog.getWindow()!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val d: Drawable = ColorDrawable(Color.BLACK)
d.setAlpha(130)
dialog.getWindow()!!.setBackgroundDrawable(d)
dialog.show()
dialog.getWindow()!!.setAttributes(lp)
Here is my dialogx.ml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:orientation="vertical">
</LinearLayout>
Here is my color code
**fill color #000000 with alph 50%**
It shows the transparent background. How to make the dialog above image. Please help me.
Dialog dialog=new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog);
dialog.show();

Display a viewpager on popup window

having trouble while displaying an viewpager on popup window.
Wants to display multiple images on a viewpager which is actually a popup window. But unable to remove the side spaces of the window.
Viewpager
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
ImageView
<com.utility.TouchImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
xmlns:android="http://schemas.android.com/apk/res/android" />
Try this It will remove side spaces and show your dialog as full screen(No side margins)
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Above line is the most important, don't miss to include it.
final Dialog dialog = new Dialog(getActivity(), R.style.DialogAnimation);
dialog.getWindow().getAttributes().windowAnimations =
R.style.DialogOpenAnimation;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_mediagallary);
ImageButton back_arrow = (ImageButton) dialog.findViewById(R.id.back);
viewPagerPhotoSlideShow = (ClickableViewPager) dialog.findViewById(R.id.photolist);
adapterPhotoViewPager = new AdaterViewPagerAllmeidagallary(getActivity(), pagerListItems);
viewPagerPhotoSlideShow.setAdapter(adapterPhotoViewPager);
viewPagerPhotoSlideShow.setCurrentItem(defaultPagerItemPosition);
back_arrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER;
Window window = dialog.getWindow();
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
If you are using Dialog containing viewpager
Either you can set the following style to that dialog as
dialog.setStyle(STYLE_NO_TITLE, R.style.DialogStyle);
DialogStyle
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
Or you can canculate the screen width and pass that to the dialog width as
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
popUpLayout = Globals.layoutInflater.inflate(R.layout.pop_up_layout,
null);
dialog.setContentView(popUpLayout);
dialog.getWindow().setLayout(Desired_Width, Desired_Hight);
dialog.show();

How to make the switch button center in dialog

I want to switch button center in dialog,but it's failed...
Why is this?
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
sw.setGravity(Gravity.CENTER_HORIZONTAL);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(sw);
Try with below code
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
linearLayout.addView(sw);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(linearLayout);
myDialog.show();
Use the custom_layout instead.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use LayoutInflater to inflate the layout in dialog
View v = LayoutInflater.from(getActivity()).inflate(R.layout.custom_layout, null);
myDialog.setView(v);
Use RelativeLayout instead, set android:centerInParent=true for SwitchButton

To set background of Custom Layout with an ImageView to Transparent

I am building a Custom dialog box using the Android Developer docs link , for this i made a layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rotatelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80000000"
>
<ImageView
android:id="#+id/dialogimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
and i am inflating this dialog,
AlertDialog dialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(TabsActivity.this);
LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.rotate_dialog_layout, (ViewGroup) findViewById(R.id.rotatelayout));
ImageView image = (ImageView) view.findViewById(R.id.dialogimage);
image.setOnClickListener(new RotateLockListener());
image.setBackgroundColor(Color.parseColor("#80000000"));
if(locked)
{
image.setImageResource(R.drawable.lock_icon);
}
else
{
image.setImageResource(R.drawable.unlock_icon);
}
builder.setView(view).setCancelable(true);
dialog = builder.create();
dialog.setView(view, 0, 0, 0, 0);
timerDelayRemoveDialog(time, dialog);
dialog.show();
but still it appears as
I have tried all the help provided at the stack over flow
Setting ImageView background to transparent,
Setting transparency by #80000000 and
Setting Dialog window transparency
But none of them worked, it still showing up.
Or you can use Dialog class for this,
Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.temp);
dialog.show();
Try using this as the style of your View :
<style name="Dialog_Fullscreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
</style>

ImageView in a Custom Dialog - fill_parent does not work and my dialog is small

I have a simple request: I have to put some pictures (some small resolution, some big resolution) in a dialog and display them fullscreen.
I tried this:
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.custom_dialog);
int picId = Integer.valueOf(webcamCursor.getString(webcamCursor
.getColumnIndex("_id")));
dialog.setTitle(webcamCursor.getString(webcamCursor
.getColumnIndex("City")));
image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
new DownloadPicTask().execute(Snippets.getUrlFromCat(picId, cat));
dialog.show();
And my layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
>
<ImageView android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
/>
</LinearLayout>
As I wrote fill_parent everywhere, shouldn't the dialog take the full screen?
Try that:
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
Hope it helps.
try this will work
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);

Categories

Resources