Change the width of the Dialog window of Android app dynamically - android

I am creating an Android application. In that application, I am using
a Dialog window to display HTML contents which come from the sever
side. I created the Dialog window using below method.
private void displayDialogWindow(String html) {
// here I have created new dialog window
dialog = new Dialog(cordova.getActivity(),
android.R.style.Theme_NoTitleBar);
dialog.getWindow().getAttributes().windowAnimations =
android.R.style.Animation_Dialog;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
// creates a new webview to display HTML and attached to
dialog to display
webview = new WebView(cordova.getContext());
webview.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
webview.setWebChromeClient(new WebChromeClient());
webview.requestFocusFromTouch();
final String mimeType2 = "text/html";
final String encoding2 = "UTF-8";
webview.loadDataWithBaseURL("", html, mimeType2, encoding2, "");
dialog.setContentView(webview);
// get the screen size of the device
WindowManager wm = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
// set appropriate sizes to the dialog window whether there is
side menu or not
lp.copyFrom(dialog2.getWindow().getAttributes());
lp.width = width ;
lp.height = height - 100;
dialog.getWindow().setAttributes(lp2);
dialog.show();
}
Using above method I could successfully create a Dialog window.
And I am creating the Dialog window using a Thread,
Runnable runnable = new Runnable() {
public void run() {
displayDialogWindow(html);
}
};
But after display the Dialog window once, I want to change the width
of the Dialog window dynamically. (eg. when I click on another button
of my app, the width of the window should be decreased and come to the
original width when another button is pressed).
Can any one help me to change the width of the Dialog window dynamically?

I think that you have to "recreat" your dialog everytime you want to change it.
After yourDialog.show() method, you can insert this line:
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);
This allow you to resize your Dialog window and it's content.
Of course width and height are the size of your screen.

Related

Custom Popupwindow for all screensize android

I am using a custom popup window. On Load I have to pass the height and width of the window. But I want to load the popup for all screensizes. I am using the below function to load my popup.
private void loadPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.activity_pop_up_ad, null);
final PopupWindow windows = new PopupWindow(layout , 300,500,true);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout,Gravity.CENTER, 0, 0);
}
});
ImageButton close = (ImageButton) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
windows.dismiss();
}
});
}
I tried this one. But it completes the screen size. I don't want that much filled.
windows.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Any ideas?
Can't you just detect the screen height / width and then scale down by some factor so it does not fill the screen as much as you want?
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
The above code should get you your height and width of the screen and then you can just divide or subtract down the height and width to make the pop up fill as much of the screen as you want.
was this what you wanted? or did i misunderstand?

android popup window doesn't show?

I have a popup window that gets called from a listener in my fragment. Before it was called from a button press and it displayed immediately. However, after moving the method that creates it to a custom listener, it no longer pop ups instantly. But rather only after a click event or scroll event occurs anywhere on the screen. I'm guessing the screen needs to be refreshed somehow to show the popup? Here's my creating code:
/***POP UP WINDOW CODE***/
PopupWindow popupMessage;
PopupWindow pw;
#SuppressLint("NewApi")
private void popupInit(){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get screen dimensions to set the window size
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
final View layout = inflater.inflate(R.layout.add_interview, null, false);
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, (int) (width*(0.8)), (int) (height*(0.8)), true);
// display the popup in the center
layout.post(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());
}
});
}
If you don't want to delay the popup just do the following:
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());

Full width Dialog, placed just above the soft keyboard

I want to achieve the following design: A dialog with custom layout, created programatically. It will contain an EditText and a Button. I want the soft keyboard to pop up when the dialog appears, and I want the dialog to fill the screen horizontally and to be placed right above the keyboard.
Here is what I've done right now:
final AlertDialog obsDialog = new AlertDialog.Builder(ProdutoDetalheActivity.this).create();
final View obsLayout = View.inflate(getApplicationContext(), R.layout.observation_layout, null);
Button obsButton = (Button) obsLayout.findViewById(R.id.observation_button);
obsEdit = (EditText) obsLayout.findViewById(R.id.observation_edit);
obsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText(String.valueOf(obsEdit.getText()));
obsDialog.dismiss();
}
});
obsDialog.setView(obsLayout);
obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
obsDialog.show();
obsEdit.requestFocus();
But this doesn't makes the dialog full width, nor calls the soft keyboard. And I still wonder how can I align the dialog with the keyboard.
I've tried these answers with no success.
Thanks in advance for any help!
[EDIT] I've brought the keyboard up by using the following code:
obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
obsDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
[EDIT] Below is a printscreen of the design I want to achieve:
As far as I know, you have to adjust the dialog size/position in the code if you do not want the default size/position.
In you case specifically, you can fist declare adjustResize for the activity's windowSoftInputMode in the AndroidManifest.xml.
After that, you can set your dialog gravity at bottom and set the width in the onStart of the dialog fragment:
...
getDialog().getWindow().setLayout(mWidth, mHeight);
WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.x = 0; lp.y = 0;
getDialog().getWindow().setAttributes(lp);
...
the mWidth and mHeight are initialized in onCreate, but I think you can do it in the onStart as well. To get the width, I used below code:
Point size = new Point();
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getSize(size);
mWidth = size.x;
Hope this can meet your requirement. I used this to create a similar dialog, but I do not have a edit in the dialog. And I need it to be at the bottom of the screen.

How to create videoview like youtube "small screen and full screen play in android "

I want to develop activity or fragment like Youtube.
"When user click on video from list it start playing in small video view like height =200dp and width = 300dp(as example) and i want to display full screen button on MediaPlayer controller by using User can play same video in full screen with effecting video play.
Also i want to display related videoList and comments on current playing video So should i use fragments?? IF yes any sample example.
I search on google about such kind of video play but i don't find it.. there are some answer like use fill parent in height and width ... but i don't want to display full screen directly.
I want to display video in both size small and full screen. How can i do that
In image you can see fullscreen button in video controller i want to do that.
Any help is appreciated
Thank you
try to create small video surface or video view and can change the parameter of view by display matrix
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height=height-80;// -80 for android controls
params.setMargins(0, 0, 0, 50);
So to execute this code you can create your own Custom media controller or you can use set anchor by extending media controller.
1) Here is Custom media controller link
Custom media controller
2)extending media controller and set anchor tag
set anchor tag
public void setAnchorView(final View view) {
super.setAnchorView(view);
Button fullScreen = new Button(context);
fullScreen.setText("FullScreen");
Log.e("media controller","Set anchorView");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(view.getWidth(), 0, 5, 20);
params.gravity = Gravity.RIGHT;
addView(fullScreen, params);
fullScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("media controller","full screen onclick");
Intent i = new Intent("xyxyxyxhx");
context.sendBroadcast(i);
}
});
}
You have to override setAnchorView in your new java class which extend the Android's MediaController (android.widget.MediaController) and add an extra view/button to the controller. And do something like this
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
Button fullScreen = new Button(context);
fullScreen.setText("FullScreen");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT|Gravity.TOP;
addView(fullScreen, params);
}
now add clicklister to view

AlertDialog Displays Landscape-Sized View with Orientation in Landscape

My app's main activity is set in the manifest to always be in portrait view. This works fine when I load the app onto the tablet.
However, when I use my menu button and click "Setup", which opens an AlertDialog that inflates an xml layout, the tablet will display the left 2/3 or so of the entire dialog. The rest of it goes offscreen to the right. This case only occurs if I install my app on the tablet while holding it in landscape mode or if I turn the tablet to landscape mode while the app is not running (even if I go back to Portrait and click on the app). The dialog even calls this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to make sure it stays in portrait (even though the problem persists with this call or not).
In other words, the way the AlertDialog is being displayed in portrait view is that it is blowing up a landscape view (and thus some of it goes offscreen), even though the actual orientation of the AlertDialog is portrait, like it should be.
(I apologize if the wording above was confusing - ask me to clarify anything if needed.)
So why would the tablet do this? I've tested it on a few android mobile phones and this doesn't happen, so I don't understand why the tablet will do this.
SIDE NOTES:
This isn't even occurring for just this AlertDialog either.. my EULA that displays at the start of the app (another AlertDialog) also appears this way if I start the app in landscape mode.
I've even allowed the usability of landscape by getting rid of all calls to specify portrait/landscape mode, and the tablet is still expending the dialog off-screen when held in portrait view on just the tablet, but not the phones.
EDIT:
This code works well on the phone, but the tablet problem still persists. If I uncomment dialog.show(); below, the tablet and phone display the dimensions I want, but on blackness instead of the dimmed main screen. Any ideas?
Calling function does this:
showDialog(EXAMPLE_CASE);
Function that gets called by the calling function:
protected Dialog onCreateDialog(final int id) {
Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Display display = getWindowManager().getDefaultDisplay();
int mwidth = display.getWidth();
int mheight = display.getHeight();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
switch(id) {
// Example of what all my cases look like (there were way too many to copy)
case EXAMPLE_CASE:
builder.setTitle("Example")
.setMessage("Example message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
dialoginterface.dismiss();
showDialog(DIALOG_CHOICE);
}
})
.setCancelable(false);
dialog = builder.create();
break;
}
if (dialog != null) {
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = mwidth;
lp.height = mheight;
lp.x = mwidth;
//lp.y = mheight;
lp.dimAmount=0.0f;
//dialog.show();
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
removeDialog(id);
}
});
}
return dialog;
}
You could try getting the screen dimensions like this:
Display display = getWindowManager().getDefaultDisplay();
int mwidth = display.getWidth();
int mheight = display.getHeight();
And then alter the Dialog like this:
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
d.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = mwidth;
lp.height = myheight;
//change position of window on screen
lp.x = mwidth/2; //set these values to what work for you; probably like I have here at
lp.y = mheight/2; //half the screen width and height so it is in center
//set the dim level of the background
lp.dimAmount=0.1f; //change this value for more or less dimming
d.getWindow().setAttributes(lp);
//add a blur/dim flags
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Also, you say you are inflating a custom layout. Have you tried tinkering with the layout_height and layout_width of the views in that layout to see if that makes a difference?

Categories

Resources