I am trying to make a custom dialog box
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.rate_layout, null);
RelativeLayout rate = (RelativeLayout) view.findViewById(R.id.rateClick);
RelativeLayout close = (RelativeLayout) view.findViewById(R.id.closeBtn);
close.setClickable(true);
rate.setClickable(true);
final AlertDialog.Builder dd = new AlertDialog.Builder(this);
dd.setView(view);
dd.setCancelable(false);
final AlertDialog d = dd.create();
d.show();
The background image has 4 types of resolutions so I can do wrap content only.
Here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/rateClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/rate_now_button" />
<RelativeLayout
android:id="#+id/imageView1"
android:layout_width="219dp"
android:layout_height="234dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/pop_up" >
<RelativeLayout
android:id="#+id/closeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/close_button" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/rateClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="#drawable/rate_now_button" />
</RelativeLayout>
</RelativeLayout>
This is what I am getting. How am i suppose to remove the dialog layout. The white big borders.
Don't use AlertDialog.Builder. Try this instead.
Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
or
Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
You can use whatever theme meets your criteria, or create your own. See answer to this question and this other question. See also available themes.
See Dialog for documentation on setContentView(), setCancelable(), show() and other methods you might find useful.
You can try:
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(300, 300);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Double width = metrics.widthPixels*.7;
Double height = metrics.heightPixels*.7;
Window win = dialog.getWindow();
win.setLayout(width.intValue(), height.intValue());
Try something like this:
dialog_item = new Dialog(Pre_Venda_Digitacao_2.this);
dialog_item.setContentView(R.layout.layout_pre_venda_digitacao_add_produto);
Util.softKeyboard(dialog_item.getWindow(), false);
// Recuperar os componentes da janela e disponibilizar para as variáveis globais.
dialog_Item_getComponentes();
/***********************OnShow do Dialog*****************************/
dialog_item.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
//Recupera a menor fonte dentro do layout passado
float lower_font_size = AutoResizeTextView_Functions.Get_Lower_Font_Size(ll_add_produto, 1000);
//Seta menor fonte recuparada anteriormente para os textos do layout passado
AutoResizeTextView_Functions.Resize_Text(ll_add_produto, lower_font_size);
}
});
dialog_item.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
dialog_item = null;
preVenda_Item = null;
}
});
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog_item.getWindow().getAttributes());
lp.width = (int) (width - (width * 0.07) ); //Tira 7% da largura total da tela para deixar um espaço na janela
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog_item.getWindow().setAttributes(lp);
dialog_item.show();
Do this in your rate_layout.xml:
<!-- begin snippet: js hide: false -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000"
>
You also need to create and call a dialog Fragment if you didnt:
public class AddDialog extends DialogFragment
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.rate_layout, null));
return builder.create();
}
}
Then call the Dialog in a function:
public void SomeFunction()
{
DialogFragment d = new AddDialog();
d.show(getSupportFragmentManager(), "some_dialog_refrence");
}
we can set width, height of custom dialog like this.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
int screenWidth = displayMetrics.widthPixels;
android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams();
lp.copyFrom(deleteDialog.getWindow().getAttributes());
// If we need to set background color of activity
// We need to set this before calling show() method
lp.dimAmount = 0.7f;
deleteDialog.getWindow().setAttributes(lp);
deleteDialog.show();
// we need to set width, height after calling show() method
Window window = deleteDialog.getWindow();
window.setLayout(screenWidth - screenWidth/2), WindowManager.LayoutParams.WRAP_CONTENT);
Related
I've an image (image1.png)
When I click on some button, I want this image to be displayed in the middle of the screen for a second and disappear. How can I do it?
I guess that it brings me the center coordinates of the screen.
public void onClick(View button) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( dm );
int screenMiddlePointWidth = dm.widthPixels / 2;
int screenMiddlePointHeight = dm.heightPixels / 2;
}
p.s. I don't want the image to push other views on the screen so I can't set it as invisible\gone
Hi use this code in xml to place your image in center
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.MainActivity"
tools:showIn="#layout/activity_main">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image1"
android:layout_centerInParent="true"/>
</RelativeLayout>
and then in java file use the following code
ImageView img;
img=(ImageView)findViewById(R.id.imgview);
CountDownTimer timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
img.setVisibility(View.GONE);
}
};
timer.start();
you can use the timer code in onCreate method or anywhere you want.
If you want to center your image programatically use the following code.
image.setBackgroundResource(R.drawable.image1);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
img.setLayoutParams(params);
I have a linear layout with a certain amount of padding. I want an image button to be placed randomly anywhere within that linear layout without going outside the layout. Here is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="45dp"
android:text="Text 1"
android:id="#+id/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:text="Text 2"
android:id="#+id/text2" />
<LinearLayout
android:id="#+id/lay1"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_margin="20dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:src="#drawable/ib"
android:id="#+id/button1"/>
</LinearLayout>
</LinearLayout>
I want the ImageButton button1 to be placed randomly within layout lay1.
I have tried getting the width and height of the layout and feeding them into a random function which I then use to provide a left margin and top margin to the imagebutton, but my app keeps crashing whenever I do that. Here's the Java code:
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) b.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
b.setLayoutParams(params);
The above method did not work and my app always crashes when I open this activity. Can someone please help me out?
Your method looks good, but you also have to take into account the image size.
So I'd put something like :
params.leftMargin = new Random().nextInt(width - b.getWidth());
params.topMargin = new Random().nextInt(height - b.getHeight());
Now you're gonna have to be more specific about the crash you're experiencing.
EDIT : Also SnyersK is right, if you're going through that piece of code before the element got their dimensions, then you'll need to use a ViewTreeObserver.
I've got a partial solution for you.
The following code places a button at a random location BUT it can be put off screen. You should be able to prevent this by playing around with the random generator.
public class MainActivity extends ActionBarActivity {
int width, height;
LinearLayout linearLayout;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.ll);
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.v(getClass().getSimpleName(), "onGlobalLayout");
width = linearLayout.getWidth();
height = linearLayout.getHeight();
placeButton();
if (Build.VERSION.SDK_INT >= 16) {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
button = (Button) findViewById(R.id.button);
}
private void placeButton() {
Log.v(getClass().getSimpleName(), "width: " + width + " Height: " + height);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
Random rand = new Random();
params.leftMargin = rand.nextInt(width);
params.topMargin = rand.nextInt(height);
Log.v(getClass().getSimpleName(), "left: " + params.leftMargin + " top: " + params.topMargin);
button.setLayoutParams(params);
button.invalidate();
}
}
You just wait for your linearlayout to be drawn. Then you get it's width and height and put the button on a random place.
Note
This will move the button around when you rotate the screen. you could prevent this by only adding an OnGlobalLayoutListener if savedInstanceState == null
Just replace your code with this
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)l.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
l.setLayoutParams(params);
I have researched this on Stack Overflow, but I didn't find the fix to my problem. I have a view that contains the code for a vertical, yellow line on the screen. I have an activity that creates the dialog whose content view is an instance of the view. The dialog pops up, but the yellow line doesn't show. What am I missing? Here is my code:
View :
public guidelines(Context context) {
super(context);
int x = this.getWidth()/2 - 3;
int y = this.getHeight();
shape = new ShapeDrawable(new RectShape());
shape.getPaint().setColor(Color.YELLOW);
shape.setBounds(x, 0, x+6, y);
}
#Override
protected void onDraw(Canvas canvas) {
shape.draw(canvas);
}
Layout for the View :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/---
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- <--- android:background="#ccc"
android:layout_width="300dp" android:layout_height="300dp" android:paddingLeft="20dp"
android:paddingBottom="40dp" app:exampleDimension="24sp" app:exampleColor="#33b5e5"
app:exampleString="Hello, guidelines" /> -->
< ---
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/frame"/>
</FrameLayout>
Dialog Calling Code :
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
guidelines g = new guidelines(this);
dialog.setContentView(g);
dialog.show();
Thanks.
It was better to just use the drawRect function of the canvas. I did that and it worked.
I can't do addView() two times.
In fact, I want one TextView(left) and one Button(right) besides.
So how I can position them in LinearLayout?
if (success == 1) {
content = json.getJSONArray(TAG_COMMENTS);
layoutVertical.removeAllViews();
final int N = content.length();
final Button[] myButton = new Button[N];
final LinearLayout[] myLayout = new LinearLayout[N];
final TextView[] myTextView = new TextView[N];
for (int i = 0; i < content.length(); i++) {
JSONObject c = content.getJSONObject(i);
String name = c.getString(TAG_CONTENT);
final LinearLayout layoutHorizontal = new LinearLayout(
getActivity());
layoutHorizontal
.setOrientation(LinearLayout.HORIZONTAL);
layoutHorizontal.setId(i);
layoutHorizontal.setBackground(getResources()
.getDrawable(R.drawable.borderadmincomment));
final TextView rowTextView = (TextView) new TextView(
getActivity());
rowTextView.setText(name);
rowTextView.setId(i);
final Button rowButton = new Button(getActivity());
rowButton.setLayoutParams(new LayoutParams(1,
LayoutParams.MATCH_PARENT));
rowButton.setId(i);
rowButton.setBackgroundResource(R.drawable.croix);
rowButton
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
layoutHorizontal.addView(rowTextView, 0);
layoutHorizontal.addView(rowButton, 1);
rowTextView.setWidth(400);
//rowTextView.setHeight(150);
//rowButton.setHeight(150);
layoutVertical.addView(layoutHorizontal);
myButton[i] = rowButton;
myLayout[i] = layoutHorizontal;
myTextView[i] = rowTextView;
}
}
It's not works.
Thank you.
You forgot to set layout params for Horizontal layout and rowTextView.
For example:
layoutHorizontal.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Create linear layout parameter. Do something like this:-
LinearLayout LL = new LinearLayout(this);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LL.setWeightSum(2f);
LL.setLayoutParams(LLParams);
Please let me know it this will work.
Make a xml layout file and name it item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical" >
<TextView
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1" />
<Button
android:id="#+id/rowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Write following code in your Activity
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutHorizontal = inflater.inflate(R.layout.item, null);
TextView rowTextView = (TextView) layoutHorizontal.findViewById(R.id.rowTextView);
Button rowButton = (Button) layoutHorizontal.findViewById(R.id.rowButton);
layoutVertical.addView(layoutHorizontal);
I want to position a popup window as a tooltip above another view + offset according to the popup size.
I have tried few ways unsuccessfully:
1) Try 1 : using showAtPosition() twice.
// The method that displays the popup.
public void showPopup(View viewToPointAt) {
this.mViewToPointAt = viewToPointAt;
mPopUpView = mActivity.getLayoutInflater().inflate(R.layout.tooltip_share, null);
mPopup =
new PopupWindow(mPopUpView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY, 0, 0); // Displaying popup
ViewTreeObserver vto = mPopUpView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mPopUpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mArrowPoint = new Point();
mPopupRect = locatePopup(mPopUpView);
mArrowPoint.x = mPopupRect.right - DisplayUtils.dpiToPixels(10);
mArrowPoint.y = mPopupRect.bottom;
mRectToPointAt = locateView(mViewToPointAt);
mPopUpView.bringToFront();
mPopup.setFocusable(false);
mPopup.setOutsideTouchable(true);
mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY,
mRectToPointAt.right - mArrowPoint.x,
mRectTo
PointAt.top - mArrowPoint.y); // Displaying popup
}
});
}
result: the popup is still at (0,0)
2) Try 2 : using update(..)
// The method that displays the popup.
public void showPopup(View viewToPointAt) {
this.mViewToPointAt = viewToPointAt;
mPopUpView = mActivity.getLayoutInflater().inflate(R.layout.tooltip_share, null);
mPopup =
new PopupWindow(mPopUpView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
mPopup.showAtLocation(mPopUpView, Gravity.NO_GRAVITY, 0, 0); // Displaying popup
ViewTreeObserver vto = mPopUpView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mPopUpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mArrowPoint = new Point();
mPopupRect = locatePopup(mPopUpView);
mArrowPoint.x = mPopupRect.right - DisplayUtils.dpiToPixels(10);
mArrowPoint.y = mPopupRect.bottom;
mRectToPointAt = locateView(mViewToPointAt);
mPopUpView.bringToFront();
mPopup.setFocusable(false);
mPopup.setOutsideTouchable(true);
mPopup.update(mViewToPointAt, 0, 0, -1, -1);
}
});
}
result: the popup is aligned to the screen bottom. No matter how big the offset I give it
My tooltip_share.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tooltip_layout"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:background="#drawable/tip_tool_top_right"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingLeft="0dp"
android:paddingRight="25dp"
android:paddingTop="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/tip_tool_icon_plane" />
</LinearLayout>
</LinearLayout>
What do i do wrong?
I suggest you to use below library :-
https://play.google.com/store/apps/details?id=com.haarman.supertooltips
https://github.com/nhaarman/supertooltips
You have to create popup.xml as per your popup design ...after then you have to call popup whenever you want.
write code on Create :
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.popup, null);
popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
on Button Click :
popupWindow.showAsDropDown(btn_share, 50, -30);