Is it possible to programmatically add an image to a toast popup?
Yes, you can add imageview or any view into the toast notification by using setView() method, using this method you can customize the Toast as per your requirement.
Here i have created a Custom layout file to be inflated into the Toast notification, and then i have used this layout in Toast notification by using setView() method.
cust_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout1"
android:background="#android:color/white">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView1" android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="PM is here"
android:gravity="center"
android:textColor="#android:color/black">
</TextView>
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="#drawable/new_logo"
android:layout_below="#+id/textView1"
android:layout_margin="5dip"
android:id="#+id/imageView1">
</ImageView>
<TextView
android:id="#+id/textView2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="This is the demo of Custom Toast Notification"
android:gravity="center"
android:layout_below="#+id/imageView1"
android:textColor="#android:color/black">
</TextView>
</RelativeLayout>
CustomToastDemoActivity.java
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.cust_toast_layout,
(ViewGroup)findViewById(R.id.relativeLayout1));
Toast toast = new Toast(this);
toast.setView(view);
toast.show();
Simply, Use the following:
Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext);
view.setImageResource(R.drawable.image_icon);
toast.setView(view);
toast.show();
You can create any view programmatically (since I am assuming you are asking on how to do this WITHOUT using a LayoutInflater) and call setView on the Toast you made.
//Create a view here
LinearLayout v = new LinearLayout(this);
//populate layout with your image and text or whatever you want to put in here
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(v);
toast.show();
Knickedi's solution is good, but if you only need an icon next to the text you can make use of the fact that the Toast has a pre-defined TextView with the same ID and set the icon on the TextView:
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
I think this is better that we show text of Toast on the image which we pass to the makeImageToast function...
so I shades Knickedi codes and :
public class utility {
public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) {
Toast toast = Toast.makeText(context, text, length);
View rootView = toast.getView();
LinearLayout linearLayout = null;
View messageTextView = null;
// check (expected) toast layout
if (rootView instanceof LinearLayout) {
linearLayout = (LinearLayout) rootView;
if (linearLayout.getChildCount() == 1) {
View child = linearLayout.getChildAt(0);
if (child instanceof TextView) {
messageTextView = (TextView) child;
((TextView) child).setGravity(Gravity.CENTER);
}
}
}
// cancel modification because toast layout is not what we expected
if (linearLayout == null || messageTextView == null) {
return toast;
}
ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER;
// convert dip dimension
float density = context.getResources().getDisplayMetrics().density;
int imageSize = (int) (density * 25 + 0.5f);
int imageMargin = (int) (density * 15 + 0.5f);
// setup image view layout parameters
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
imageParams.setMargins(0, 0, imageMargin, 0);
imageParams.gravity = Gravity.CENTER;
// setup image view
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageResId);
imageView.setLayoutParams(imageParams);
// modify root layout
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setBackgroundResource(imageResId);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setHorizontalGravity(Gravity.CENTER);
linearLayout.setHorizontalGravity(Gravity.CENTER);
//addView(imageView, 0);
return toast;
}
}
and this is use of it:
utility.makeImageToast(getApplicationContext(),
R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show();
There's always the possibility to create a custom layout. There was one fact which I disliked about that: It breaks the system default toast UI. This could differ on different platforms and implementations. There's no simple way to use the system default resource so I decided to hack the toast and force an image into it.
Hint: You can get the default resource like this:
Toast.makeToast(context, "", 0).getView().getBackground()
Here's a helper which will display an image in front of the toast message:
Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()
I use that to indicate success, info or error. Makes a toast information nicer and more expressive...
(It's worth mentioning that the hack bases on the fact that the internal toast is using a LinearLayout so isn't system and implementation independent. See comments.)
public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) {
Toast toast = Toast.makeText(context, text, length);
View rootView = toast.getView();
LinearLayout linearLayout = null;
View messageTextView = null;
// check (expected) toast layout
if (rootView instanceof LinearLayout) {
linearLayout = (LinearLayout) rootView;
if (linearLayout.getChildCount() == 1) {
View child = linearLayout.getChildAt(0);
if (child instanceof TextView) {
messageTextView = (TextView) child;
}
}
}
// cancel modification because toast layout is not what we expected
if (linearLayout == null || messageTextView == null) {
return toast;
}
ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL;
// convert dip dimension
float density = context.getResources().getDisplayMetrics().density;
int imageSize = (int) (density * 25 + 0.5f);
int imageMargin = (int) (density * 15 + 0.5f);
// setup image view layout parameters
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
imageParams.setMargins(0, 0, imageMargin, 0);
imageParams.gravity = Gravity.CENTER_VERTICAL;
// setup image view
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageResId);
imageView.setLayoutParams(imageParams);
// modify root layout
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(imageView, 0);
return toast;
}
Toast aa = Toast.makeText(getBaseContext(), "OPEN",Toast.LENGTH_SHORT);
ImageView cc = new ImageView(getBaseContext());
cc.setImageResource(R.drawable.a);
aa.setView(cc);
aa.show();
class CustomToast extends AppCompatActivity {
Button custom_toast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_toast);
custom_toast = (Button) findViewById(R.id.customToast);
custom_toast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater=getLayoutInflater();
View layout=inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
TextView toastTextView=(TextView) findViewById(R.id.toastTextView);
ImageView toastimageview=(ImageView) findViewById(R.id.toastImageView);
toastTextView.setText("Custom toast in android");
toastimageview.setImageResource(R.drawable.ic_launcher_background);
Toast toast=new Toast(CustomToast.this);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
});
}
}
Related
in the bleow posted code, i am trying to add a horizontal divider after each added view to the linearlayout as shown in the code.
the problem i have is, at run time the divider is not showing
would please let me know why the divider is not showing and how to make it appears?
code:
private void inflateView(String bez, String ges) {
LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);
//divider
View viewDivider = new View(this);
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
viewDivider.setBackgroundColor(Color.parseColor("#000000"));
LayoutInflater inflator = this.getLayoutInflater();
View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key
ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
} else {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
}
texVieBez.setText(bez);
texVieGes.setText(bez);
btnMore.setVisibility(View.VISIBLE);
linLay.addView(view);
linLay.addView(viewDivider);
}
viewDivider has height WRAP_CONTENT and as the view is empty, its height is calculated to 0.
You have to set desired height of the divider.
int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));
I hard to implement popup like softkeyboard. I mean, when you open popup in android the views under popup is disable (you can't do anything until the popup dismiss). But when then softkeyboard open, the views always above the softkeyboard.
Note : dont need trick like view.setVisibility(View.GONE) or view.setVisibility(View.VISIBLE)
EDIT
As simply, how to make layout/view up when popup display from bottom to up look like softkeyboard?
Yes, you can create Custom Dialog with two translate animation from bottom.
Check droid kid answer he is already done in your way.
Answer
For this you have to create a custom view that pop up,you have to create a different xml file for your view and define the height width of that,make height wrap content.And your views are not affected by this like other Dialog popup which disable anything in the background.
Example :- lets say you have xml for pop up named dialog_pop_up,
public void showPopUpDialog(Context context,ImageView imagebuttonPopUP) {
try {
View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_pop_up, null, false);
int[] location = new int[2];
//This is the button which triggers pop up
imagebuttonPopUP.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
Point p = new Point();
p.x = location[0];
p.y = location[1];
int popupWidth = mActivity.getResources().getDimensionPixelOffset(R.dimen.home_screen_dialog_width);//Utility.dpToPx(mActivity,133);
int OFFSET_Y = imagebuttonPopUP.getHeight();
int OFFSET_X = imagebuttonPopUP.getWidth();
final PopupWindow window = new PopupWindow(v, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
window.setWidth(popupWidth);
window.setOutsideTouchable(true);
window.setTouchable(true);
window.setFocusable(true);
window.setBackgroundDrawable(new BitmapDrawable());
//Initialize your view here.
TextView TextView1 = (TextView) v.findViewById(R.id.textview1);
TextView TextView2 = (TextView) v.findViewById(R.id.textview2);
LinearLayout Layout = (LinearLayout) v.findViewById(R.id.linearlayout2);
View dividerView = v.findViewById(R.id.view_divider);
//Click listeners of your views
TextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code here
//to dismiss window
window.dismiss();
}
});
TextView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//code
}
});
window.showAtLocation(imagebuttonPopUP, Gravity.NO_GRAVITY, p.x + OFFSET_X - popupWidth, p.y + OFFSET_Y);
} catch (Exception ex) {
Logger.e(TAG, ex.getMessage());
}
}
I wanted to know if my textview is within the visible screen region or not..
I have looked at links as:
Android - Get the visible area of a view?
Android: how to check if a View inside of ScrollView is visible?
But nothing seems to work for me.
Code that i used to check is
Rect rect = new Rect();
//textview intialized in onCreate as text1
text1.getHitRect(rect);
text1.measure(0, 0);
//layout is the parent layout (linear) in which i am adding the text view
Rect acctualView = new Rect(Math.round(layout.getX()), Math.round(layout.getY()),
Math.round(layout.getX() + layout.getWidth()), Math.round(layout.getY() +layout.getHeight()));
if(Rect.intersects(acctualView, rect))
{
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Not visible ", Toast.LENGTH_LONG).show();
}
Every width and height of textview and layout is giving me 0 "zero",
Please let me know how to fix this and know whether a view is inside the visible region or not
Here is my complete activity code
public class MainActivity extends Activity {
private TextView text1, text2, text3, text4, text5, text6;
private Context context;
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getLayoutInflater().getContext();
layout = (LinearLayout) findViewById(R.id.parentLayout);
setTextViews();
checkViewAndUpdate();
}
private void checkViewAndUpdate() {
Rect rect = new Rect();
layout.getHitRect(rect);
if (text1.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}
}
private void setTextViews() {
text1 = new TextView(context);
text2 = new TextView(context);
text3 = new TextView(context);
text4 = new TextView(context);
text5 = new TextView(context);
text6 = new TextView(context);
text1.setText("text1");
text2.setText("text2");
text3.setText("text3");
text4.setText("text4");
text5.setText("text5");
text6.setText("text6");
text1.setTextSize(30);
text2.setTextSize(30);
text3.setTextSize(30);
text4.setTextSize(30);
text5.setTextSize(30);
text6.setTextSize(30);
//layout.removeAllViews();
layout.addView(text1);
layout.addView(text2);
layout.addView(text3);
layout.addView(text4);
layout.addView(text5);
layout.addView(text6);
}
}
and my layout only contains LinearLayout (empty)
as i wrote on comment, checking visible rect at onCreate won't work.
Try this code at end of onCreate (instead of call checkAndUpdate() directly).
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
checkViewAndUpdate();
}
});
View layout does not finish though if you added view at onCreate. yes, this time user cannot see the actual activity and view's actual size are not set.
onGlobalLayout will call when view layout is ended (and activity is visible).
at that timing, we can now get size and position of them!
FYI, you can remove this listener by calling layout.getViewTreeObserver().removeOnGlobalLayoutListener(this) in onGlobalLayout()
try with this:
if (yourView.getParent().getVisibility() == View.VISIBLE) {
// visible
} else{
// hiden
}
Or this:
Rect rect = new Rect();
parentView.getHitRect(rect);
if (imageView.getLocalVisibleRect(rect)) {
// imageView is within the visible window
} else {
// imageView is not within the visible window
}
Why don't you use the for method?
for(int i = 0; i < 6; i++){
TextView tv = new TextView(context);
tv.setTextSize(30);
layout.addView(tv);
//Maybe set some listeners...
}
I'm creating a chat application, on launch it displays the twenty most recent messages in a conversation. When you scroll to the top you can press a button to display earlier messages. Pretty typical. The only problem is, I want the UI elements to maintain their position after pressing the button. At the moment they shift. I've created a stripped out version of the problem I'm having.
Before pressing the button
After pressing the button
As you might have guessed, I would like it so that after I press the button the TextView with the label 51 (TextView-51) appears as if it hasn't moved. My original plan was to get the position of TextVie-51 before the button press, and then after the button press, and set the ScrollView's Y position. However that approach doesn't work because the at the time I was checking the View hadn't inflated yet.
Here's the layout xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/TestScroller">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/mainContainer" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TestButton"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:onClick="insertMore"
android:text="Get Earlier Messages" />
</RelativeLayout>
</ScrollView>
Here's the code for the activity.
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
lastId = R.id.TestButton;
firstId = -1;
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainContainer);
//1. Create fifty TextViews and put them under the button.
for (int i = 51; i <= 100; i++)
{
TextView tv = new TextView(this);
tv.setText(Integer.toString(i));
tv.setId(i + 100);
final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WC, WC);
params.addRule(RelativeLayout.BELOW, lastId);
tv.setLayoutParams(params);
lastId = tv.getId();
if (firstId == -1)
firstId = tv.getId();
rl.addView(tv);
}
}
public void insertMore(View view)
{
//Create fifty more TextViews and insert them between the button and
//the already created textviews.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainContainer);
lastId = R.id.TestButton;
for (int i = 0; i <= 50; i++)
{
TextView tv = new TextView(this);
tv.setText(Integer.toString(i));
tv.setId(i + 100);
final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WC, WC);
params.addRule(RelativeLayout.BELOW, lastId);
tv.setLayoutParams(params);
lastId = tv.getId();
rl.addView(tv);
}
//Now make sure the textview with the label 51 under it is underneath the last
//view we just added.
TextView tv = (TextView)findViewById(firstId);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)tv.getLayoutParams();
params.addRule(RelativeLayout.BELOW, lastId);
tv.setLayoutParams(params);
//Remove the button
Button button = (Button)findViewById(R.id.TestButton);
rl.removeView(button);
}
Based on your first idea which was to scroll to the bottom of the list after adding textviews, did you try to call requestLayout() to force layout update :
public void insertMore(View view)
{
// Create and add your 50 views
...
// force Update layout
rl.requestLayout();
scrollView.post(new Runnable() {
#Override
public void run() {
// Scroll to scrollviews bottom
scrollView.scrollTo(0, scrollView.getBottom());
}
});
}
I am working on project . I need the width & Height of a LinearLayout from Activity using programming code. This Linear Layout has fixed width and Height . But when i use the following ..i am getting Nullpointer Exception
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
Log.e("getWidth",""+viewGroup.getWidth());
Log.e("getHeight",""+viewGroup.getHeight());
I need the width and height of that layout from activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="150dp"
android:layout_height="252dp"
android:background="#303030"
android:orientation="vertical" >
</LinearLayout>
Here is the Java code file
public class MainActivity extends Activity {
//The "x" and "y" position of the "Show Button" on screen.
Point p;
Button btn_show;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_show = (Button) findViewById(R.id.show_popup);
}
#Override
protected void onResume() {
super.onResume();
btn_show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Open popup window
if (p != null)
showPopup( p);
}
});
}
// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup( Point p) {
int popupWidth = 200;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.a, viewGroup);
Log.e("getWidth",""+viewGroup.getWidth());
Log.e("getHeight",""+viewGroup.getHeight());
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(getApplicationContext());
popup.setContentView(layout);
popup.setWidth(viewGroup.getWidth());
popup.setHeight(viewGroup.getHeight());
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
// Button close = (Button) layout.findViewById(R.id.close);
/* close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});*/
}
}
It may be that this ViewGroup hasn't been created yet.
Check where you're trying to get the width and height of this object is actually being called after display objects such as views etc are being created. You can debug this by either placing breakpoints in the different loading methods such as onCreate, onResume or by placing NSLog's in them instead.
Only once the method View.onSizeChanged() has been called for the first can you reliably use the getHeight() and getWidth() methods. This means you will have to change the logic of your app to take into account this fact.
You are inflating the layout just to get the width and height, aren't you? If so you don't need the viewGroup. Assuming R.layout.popup_layout points to a LinearLayout that has fixed dimensions:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = layoutInflater.inflate(R.layout.popup_layout, null);
LayoutParams params = layout.getLayoutParams();
Log.e("getWidth",""+params.width);
Log.e("getHeight",""+params.height);
After that you can set your popup:
final PopupWindow popup = new PopupWindow(getApplicationContext());
popup.setContentView(layout);
popup.setWidth(params.width);
popup.setHeight(params.height);
popup.setFocusable(true);
to wait for the views to be attached and placed in the layout you should useViewTreeObserver.OnGlobalLayoutListener.
you can register it like this:
v.getViewTreeObserver().addOnGlobalLayoutListener( new
OnGlobalLayoutListener() {
public void onGlobalLayout() {
//TODO: do your stuff here
//if you change something in the layout you have to add this
//line to avoid loops
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
in that callback you can be sure to have the views with a consistence position and dimension.