My custom dialog doesn't appear when set its width - android

final Dialog dialog = new Dialog(MainScreenActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_ana_ekran_dialog);
dialog.setCanceledOnTouchOutside(true);
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 90 / 100;
dialog.getWindow().setAttributes(params);
dialog.show();
This code is in onOptionsItemSelected section when i press action bar button my custome dialog doesn't appear.
My custom dialog doesn't appear when i set it's width?

I fix the problem this is the way i fix it...
Costume Dialog Class public class HarcamaKayitDialog extends Dialog implements android.view.View.OnClickListener.
Variables
Context ctx;
EditText yorum;
EditText harcama;
Button buttonTamamDialog;
Button buttonIptalDialog;
Dialog dialog;
Constractor
public HarcamaKayitDialog(Context context){
super(context);
this.ctx = context;
this.dialog = this;
}
onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_ana_ekran_alert_dialog);
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 90 / 100;
getWindow().setAttributes(params);
findViews();
}
onClick.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonTamam:
//transactions
dialog.dismiss();
break;
case R.id.buttonIptal:
dialog.dismiss();
break;
default:
break;
}
}
findViews function.
private void findViews() {
yorum = (EditText) findViewById(R.id.editTextYorum);
harcama = (EditText) findViewById(R.id.editTextHarcama);
buttonTamamDialog = (Button) findViewById(R.id.buttonTamam);
buttonIptalDialog = (Button) findViewById(R.id.buttonIptal);
buttonTamamDialog.setOnClickListener(this);
buttonIptalDialog.setOnClickListener(this);
}

Related

Relative position of Buttons in different devices

I have a problem in relation to the relative location of Buttons in different devices. I have done a short application to show it. The Button, in relation to a red point, behaves as desire when dealing with wide measures (coordinate x). However, I don’t know why the same Button is progressively scrolling down from the red point (coordinate y) as I choose a smaller device. I choose the right size of the image for the different devices and for both, wide and height, I also choose the relative measure of the screen:
rel_btn.width = 4*width/100;
rel_btn.height = 7*height/100;
I show you the code and the resulting screen of the xlarge screen in case someone can help me:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//width and height of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
int height = metrics.heightPixels;
RelativeLayout layout = (RelativeLayout) findViewById(R.id.test); // id del XML
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rel_btn.width = 4*width/100; rel_btn.height = 7*height/100;
// x, y of the BUTTON
rel_btn.leftMargin = 15*width/100; rel_btn.topMargin = 66*height/100;
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0); // writes the ID
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
private View.OnClickListener prueba = new View.OnClickListener() {
#Override
public void onClick(View view) {
//width of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
Button button = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(Math.round((float) (0.6*width/100)), Color.BLUE);
drawable.setColor(Color.TRANSPARENT);
button.setBackgroundDrawable(drawable);
}
};
}

Create popup at the middle of the screen

I am trying to create a popup when clicking on ListView image with the same image just enlarged, this code works fine to me but I tried to position the View "vv" at the middle of the screen but the popup is still at (0,0)..
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!popu) {
View popupView = inflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txt = (TextView) popupView.findViewById(R.id.texttt);
ImageButton img = (ImageButton) popupView.findViewById(R.id.imgg);
txt.setText(imageId[position]);
img.setImageResource(imageId[position]);
View vv = new View(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
vv.setX(width / 4);
vv.setY(height / 4);
img.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
popu = false;
}
});
popupWindow.showAsDropDown(vv, 50, -30);
popu = true;
}
}
});
I am not on an activity, it is my customadapter class..
EDIT:
Fixed, see Yasin Kaçmaz answer.

dialog not setting layout params

using a dialog as a window I tried to set its layout as
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x = 120;
lp.y = 120;
lp.width = 800;
lp.height = 450;
Dialog dialog = new Dialog(cxt);
dialog.getWindow().setAttributes(lp);
dialog.setContentView(vFirst);
dialog.show();
where
View vFirst = inflater.inflate(R.layout.popup, container, false);
which is inside a button click,but it is not setting it.
I think you should use this
dialog.getWindow().setLayout(800, 450);
This will set the dialog window size.
public class Myclass extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// contents here
</LinearLayout>
According to documentation: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,
X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.
Y position for this window. With the default gravity it is ignored. When using TOP or BOTTOM it provides an offset from the given edge.
So to make x and y work you should set Gravity attribute before. For example:
lp.gravity = Gravity.TOP;
lp.gravity = Gravity.LEFT;
lp.y = 120;
lp.x = 120;
//dialog width and height are set according to screen, you can take you want
Dialog myDialog = new Dialog(context, R.style.CustomTheme);
myDialog.setContentView(R.layout.share_post_dialog);
int[] widthHeight = getDeviceWidthAndHeight(context);
int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
int dialogWidth = widthHeight[0] - widthHeight[0] / 5;
WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
params.width= dialogWidth;
params.height = dialogHeight;
myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);
myDialog.show();
//This is used to get height width of screen
#SuppressWarnings("deprecation")
public static int[] getDeviceWidthAndHeight(Context context) {
int widthHeght[] = new int[2];
int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
}
return widthHeght;
}

Setting Window Dim only working in onCreate()

I have a little problem with my Window dimming.
Basically, I want to set the dimAmount of my Activity to zero as soon as I swipe it into one direction.
Please be aware that my Activity is displayed as a dialog, and can be swiped to the left or right. This works perfectly fine. But the setWindowDim() method has no effect when called after swipe.
If i call the setWindowDim inside my onCreate method, it works perfectly fine.
I have no idea why it doesnt work anywhere else.
public class FinishDialog extends Activity {
private FrameLayout frame = null;
private GestureDetector gd = null;
private int displaywidth = 0;
private int original_x = 0;
private int dialog_width = 0;
private final int POS_LEFT = -1;
private final int POS_MID = 0;
private final int POS_RIGHT = 1;
/** the dialogs current position, POS_MID on startup */
private int current_pos = POS_MID;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
frame = new FrameLayout(this);
gd = new GestureDetector(this, new TouchManager(this));
// fill the challenge layout into the framelayout
frame.addView(LayoutInflater.from(getBaseContext()).inflate(R.layout.finishdialog, null));
setContentView(frame);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) frame.getLayoutParams();
GameLogic g = new GameLogic();
displaywidth = g.getDisplayWidth(this);
lp.width = displaywidth - displaywidth / 6;
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
LinearLayout llFinish = (LinearLayout) findViewById(R.id.llFinishDialog);
LinearLayout.LayoutParams lpLL = (LinearLayout.LayoutParams) llFinish.getLayoutParams();
lp.height = lpLL.height;
frame.setLayoutParams(lp);
// setWindowDim(0.0f); ---> if i set the dim here, it works perfectly fine
original_x = (int) frame.getX();
dialog_width = lp.width;
}
private void setWindowDim(float dim) {
Log.i("FinishDialog", "Setting window dim to " + dim + ".");
WindowManager.LayoutParams lpWin = getWindow().getAttributes();
lpWin.dimAmount = dim;
getWindow().setAttributes(lpWin);
}
/**
* move the dialog, depending on the swipe direction and the
* current position of the dialog
* #param action
*/
public void interpretTouch(int action) {
switch(action) {
case TouchManager.MOVE_DIALOG_RIGHT:
if(frame != null) {
if(current_pos == POS_MID) {
swipeDialog(600, frame, 0, dialog_width, 0, 0);
current_pos = POS_RIGHT;
setWindowDim(0.0f); // here setting the dim has no effect
} else if (current_pos == POS_LEFT) {
swipeDialog(600, frame, - dialog_width, original_x, 0, 0);
current_pos = POS_MID;
setWindowDim(0.9f); // here setting the dim has no effect
} else if (current_pos == POS_RIGHT) {
// if we are on the right and swipe right, nothing happens
}
}
Log.i("Challenge", "Moving dialog to the right.");
break;
case TouchManager.MOVE_DIALOG_LEFT:
if(frame != null) {
if(current_pos == POS_MID) {
swipeDialog(600, frame, 0, -dialog_width, 0, 0);
current_pos = POS_LEFT;
setWindowDim(0.0f); // here setting the dim has no effect
} else if (current_pos == POS_LEFT) {
// if we are on the left and swipe left, nothing happens
} else if (current_pos == POS_RIGHT) {
swipeDialog(600, frame, dialog_width, original_x, 0, 0);
current_pos = POS_MID;
setWindowDim(0.9f); // here setting the dim has no effect
}
}
Log.i("Challenge", "Moving dialog to the left.");
break;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);
}
private void swipeDialog(int time, View framelayout, int fromDeltaX, int toDeltaX, int fromDeltaY, int toDeltaY) {
Log.i("FinishDialog", "Dialog swiped.");
TranslateAnimation trans = new TranslateAnimation(fromDeltaX, toDeltaX,
fromDeltaY, toDeltaY);
trans.setFillAfter(true);
trans.setFillEnabled(true);
trans.setDuration(time);
framelayout.startAnimation(trans);
}
}
Starting this activity right after I change the Window dim finally did the job.
Unfortunately, now the Screen flashes pretty "ugly" when the RefreshActivity gets started, does anyone have a solution for that?
import android.app.Activity;
import android.os.Bundle;
public class RefreshActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
finish();
}
}

Pop up android app

I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);

Categories

Resources