Three sliding panes - the Middle pane above Left and Right panes - android

I am trying to have the same navigation style as Viber's interface (the discussion page), without using a third-part Library such as SlidingMenu.
I thought that they have used SlidingPaneLayout to achieve this nice effect, but when I tried to code it, I noticed that the last pane is always over the second.
My questions :
Is this really a SlidingPaneLayout ?
If yes how to achieve this please ?
If no, is there an android native way to do the same thing ?!
Left Pane
Right Pane

First of all declare this all variable in your Class
/** Sliding Menu */
boolean alreadyShowing = false;
private int windowWidth;
private Animation animationClasses;
private RelativeLayout classesSlider;
LayoutInflater layoutInflaterClasses;
then inside onCreate method declare this, this will help you to get screen's height and width
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
display.getHeight();
layoutInflaterClasses = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
and then any of your button or image where by clicking you want to open slider put below code.
findViewById(R.id.slidermenu).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
and then outside the onCreate declare openSlidingMenu() as below.
private void openSlidingMenu() {
// showFadePopup();
int width = (int) (windowWidth * 0.8f);
translateView((float) (width));
#SuppressWarnings("deprecation")
int height = LayoutParams.FILL_PARENT;
// creating a popup
final View layout = layoutInflaterClasses.inflate(
R.layout.option_popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
ImageView imageViewassignment = (ImageView) layout
.findViewById(R.id.assignment);
imageViewassignment.setOnClickListener(this);
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,
true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
// to clear the previous animation transition in
cleanUp();
// move the view out
translateView(0);
// to clear the latest animation transition out
cleanUp();
// resetting the variable
alreadyShowing = false;
}
});
}
just replace
final View layout = layoutInflaterClasses.inflate(
R.layout.option_popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
this above code with your custom screen XML name and by it's ID. and here is other methos's which you need.
private void translateView(float right) {
animationClasses = new TranslateAnimation(0f, right, 0f, 0f);
animationClasses.setDuration(100);
animationClasses.setFillEnabled(true);
animationClasses.setFillAfter(true);
classesSlider = (RelativeLayout) findViewById(R.id.classes_slider);
classesSlider.startAnimation(animationClasses);
classesSlider.setVisibility(View.VISIBLE);
}
private void cleanUp() {
if (null != classesSlider) {
classesSlider.clearAnimation();
classesSlider = null;
}
if (null != animationClasses) {
animationClasses.cancel();
animationClasses = null;
}
}
remember here animationClasses = new TranslateAnimation(0f, right, 0f, 0f); you can play with this parameter for some different effect and also do not forget to change this line's ID with your current screen's ID like for example check below id
classesSlider = (RelativeLayout) findViewById(R.id.classes_slider);
here you need to replace this ID with your Current java screen's XML file's ID.
Hope this will help you.

Related

Android LayoutParams not being updated properly

So I have a scroll view, and I need to adjust the height of my scroll view to make sure it stays above a modal pop-up view. I can't use a constraint layout because this modal pop-up view is not a child of the same view parent. So I'm trying to dynamically update my scroll views layout params so its height is small enough to not get hidden behind the modal pop-up.
The pop-up view height can change at points so I have a callback that returns the new height of the modal view anytime it changes. In that callback I adjust the scroll views height like so:
someModalView.onHeightChanged = { newViewHeight ->
Log.d("TESTHEIGHT", "PreHeight = ${scrollView.height}")
scrollView.layoutParams = FrameLayout.LayoutParams(scrollView.width, scrollView.height - newViewHeight)
scrollView.requestLayout()
Log.d("TESTHEIGHT", "PostHeight = ${scrollView.height}")
}
Unfortunately the above code seems to do nothing and in my logs I can see that the PreHeight prints the same height as the PostHeight. Any reason the views height isn't getting changed?
Also, I did debug it and make sure that newViewHeight is not 0, and it isn't, it's ~800
Ended up making it work by adding padding to the view rather than changing its height like so:
someModalView.onHeightChanged = { newViewHeight ->
scrollView.setPadding(0, 0, 0, newViewHeight)
}
This works exactly how i needed it to, however it doesn't really answer the question so I will just leave it in the answer for anyone else who it might help. But it would still be nice to know why changing the layout params wouldn't update the views height.
try to see it works for you
val params = scrollView.layoutParams;
params.height = scrollView.height - newViewHeight
scrollView.layoutParams = params
Once I needed to get the height of the softKeyboard to update my view:
public class MainActivity extends AppCompatActivity {
private ScrollView sView;
private int heightDiff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sView = findViewById(R.id.scrollView);
//Here we get the height of soft keyboard by observing changes of softKeyboard height.
sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
heightDiff = sView.getRootView().getHeight() - sView.getHeight();
}
});
final EditText email = findViewById(R.id.eemail);
EditText firstName = findViewById(R.id.efirstname);
firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!isVisibleWhileSoftKeyboardShowing(email) && hasFocus) {
sView.postDelayed(new Runnable() {
#Override
public void run() {
sView.smoothScrollBy(0, 200);
}
}, 500);
}
}
});
}
/**
* check if a view is currently visible in the screen or not
*
* #param view
* #return
*/
public boolean isVisibleWhileSoftKeyboardShowing(final View view) {
if (view == null) {
return false;
}
if (!view.isShown()) {
return false;
}
final Rect actualPosition = new Rect();
view.getGlobalVisibleRect(actualPosition);
final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight() - heightDiff);
return actualPosition.intersect(screen);
}
/**
* to get screen width
*
* #return
*/
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
/**
* to get screen height
*
* #return
*/
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
}
heightDiff is the height of softKeyboard. There was 2 edit texts. I wanted to scroll if softKeyboard hided the lower one. Hope this is similar to your case.

How can I make a DialogFragment occupy the entire screen width

I have tried to create a dialog that occupies the full screen width from the old AlertDialog builder to the new DialogFragment approach in the onCreateView() and onViewCreated() to get the displayed dialog to occupy the full width of the screen. I can certainly get the width and height values of the screen but regardless of how I try to force the dialog to use these values, they are ignored. The displayed dialog is always the same width regardless of orientation.
In my latest attempt I have an xml layout that I inflate. I need to use a custom view so I cannot define that view in xml. So I add it.
Here is the most current attempt I have in my DialogFragment code. Of course this is just one of many attempts I have made trying to follow hints from posts and Slidenerd videos.
public class PopupDialog extends DialogFragment implements View.OnClickListener
{
private static final String TAG = PopupDialog.class.getName();
Button cancel = null;
Button focus = null;
View viewInput = null;
int width;
int height;
int id;
public PopupDialog()
{
}
public PopupDialog(View v, int id, int width, int height)
{
viewInput = v;
this.id = id;
this.width = width;
this.height = height;
}
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstance)
{
Log.d(TAG, "onCreateView of DialogFragment called.");
View viewDialog = inflator.inflate(R.layout.popup_dialog, null);
// RelativeLayout relativeLayout = (RelativeLayout)viewDialog;
// LayoutParams params = new LayoutParams(width, height);
// relativeLayout.setLayoutParams(params);
// Point point = new Point();
// Activity activity = getActivity();
// activity.getWindowManager().getDefaultDisplay().getSize(point);
// if(point.x > point.y)
if(width > height)
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
ViewParent parent = viewInput.getParent();
if(parent != null)
{
Log.d(TAG, "View already present. Removing.");
((ViewGroup)parent).removeView(viewInput);
}
LayoutParams params = new LayoutParams(width, height);
viewInput.setLayoutParams(params);
((ViewGroup)viewDialog).addView(viewInput, 0);
cancel = (Button)viewDialog.findViewById(R.id.btn_cancel);
focus = (Button)viewDialog.findViewById(R.id.btn_focus);
cancel.setOnClickListener(this);
focus.setOnClickListener(this);
setCancelable(false);
return viewDialog;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated of DialogFragment called.");
//getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, height);
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 10; //x position
wmlp.y = 450 * (id) + 10;
// wmlp.width = width;
// wmlp.height = height;
}
I am plotting a sine wave. The view has the correct size as the sine wave has a range of 0 to 12 but in the landscape orientation the displayed dialog box only gets a little more than half way, so 0 to 6 + is seen and then one has to wait for the wave to recycle as it plots from 6 to 12 before it becomes visible again when it goes back to 0. I AM able to place the dialog box upper left hand corner.
Does anyone know how to solve this problem? I went to the fragment because I was led to believe that the canned AlertDialog approach was fixed in width and there was nothing one could do. I am facing the same limitation with the DialogFragment.
try adding this code in on create() method after setContentView
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I gave up and created my graph in a ListView in a ViewFlipper. Not want I wanted but I got more real estate for the graph.

NullPointer Exception in getWidth(), getHeight()

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.

How to draw view on top of soft keyboard like WhatsApp?

I want to know how it's possible to add View on top of Keyboard like WhatsApp and Hangout. In chat screen, they insert emoticons view on top of the opened soft keyboard.
Does anyone know how to achieve this behavior?
Well, I have created a sample keyboard for chatting here...
Here, I use popup window for showing popup window and height of popup is calculated dynamically by height of keyboard
// Initially defining default height of keyboard which is equal to 230 dip
final float popUpheight = getResources().getDimension(
R.dimen.keyboard_height);
changeKeyboardHeight((int) popUpheight);
// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);
and height is calculated using this function :
/**
* Checking keyboard height and keyboard visibility
*/
int previousHeightDiffrence = 0;
private void checkKeyboardHeight(final View parentLayout) {
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView()
.getHeight();
int heightDifference = screenHeight - (r.bottom);
if (previousHeightDiffrence - heightDifference > 50) {
popupWindow.dismiss();
}
previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {
isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);
} else {
isKeyBoardVisible = false;
}
}
});
}
Using all these stuff i am able to make a perfect overlapping keyboard....
then i inflate popup window with viewpager and gridview for emoticons.
Also, i use spannable string for showing these emoticons in listview and chat window
After a heavy time of research and try-and-error, I've found another solution similar to the one of Chirag Jain above, but using a custom Dialog.
mDialogKeyboard = new Dialog(this,android.R.style.Theme_NoTitleBar);
mDialogKeyboard.setContentView(R.layout.your_custom_layout);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mDialogKeyboard.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp=mDialogKeyboard.getWindow().getAttributes();
lp.width=WindowManager.LayoutParams.MATCH_PARENT;
lp.height=mSoftKeyboardHeight;
lp.gravity=Gravity.BOTTOM | Gravity.LEFT;
lp.dimAmount=0;
Despite the fact that Chirag Jain answer seems to be more clean, I'll post this here for have an alternative method.
As far as I know you can draw on other applications, yes. I myself have designed such an app. As for drawing on an application such as the keyboard or any other application in specific, I guess, you'll have to define a layout with a height that's exactly that of the keyboard. So, that would vary from device to device. So, this isn't possible.
I still stick to my notion that WhatsApp merely dismisses the soft keyboard on pressing the smiley button and calls it's own fragment.
If you would still like to pursue this, here's how you draw a "window" over other applications. These should be it's layout parameters.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
Albeit, your width will change to an absolute pixel value since you'd like the activity to be over the keyboard only.
If I've misunderstood the question, please correct me.
what my thinking is they have created their own keypad for smiles, and on click of smile icon or keypad icon they are hiding smile keypad and showing the normal keypad. there are two scenarios in whats app case 1) if you don't focus 1st time of editext then you can not see the show keypad button,and the height of smile keypad is exactly same as normal keypad,we will get the keypad height only after our view layout is changed, means only after the keypad is shown, that means they are creating their own keypad.. 2) if you focus the edittext and click of smile button then it will show the option of show keypad button Please correct me if i am not right on this
I recently had to implement a view which would be above a soft keyboard. #Chirag Jain's solution is almost right, but it does not count with the system buttons in the bottom of the screen! This will make the keyboard height incorrect on some devices like NEXUS 6. This solution should work across all devices:
1) create layout which contains your view
<RelativeLayout
android:id="#+id/keyboard_info_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#color/C12"
android:padding="10dp"
android:visibility="invisible">
....
</RelativeLayout>
2) Bind view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.notifications_email_settings_fragment, container, false);
ButterKnife.bind(this, rootview);
checkKeyboardHeight(rootview);
3) keyboard check and view margin settings
private void checkKeyboardHeight(final View parentLayout) {
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
int previousHeightDiffrence = 0;
int systemBarHigh = 999999;
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView()
.getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if(systemBarHigh > keyboardHeight) {
systemBarHigh = keyboardHeight;
}
if (keyboardHeight > 250) {
int keyboardHightWithoutSystemBar = keyboardHeight - systemBarHigh;
// no need to update when the keyboard goes down
if (previousHeightDiffrence != keyboardHightWithoutSystemBar) { // if (Math.abs(previousHeightDiffrence - keyboardHeight) > 10) {
adjustKeyboard(keyboardHightWithoutSystemBar);
}
keyboardInfoContainer.setVisibility(View.VISIBLE);
isKeyBoardVisible = true;
previousHeightDiffrence = keyboardHightWithoutSystemBar;
} else {
isKeyBoardVisible = false;
if (keyboardInfoContainer != null) {
keyboardInfoContainer.setVisibility(View.INVISIBLE);
}
}
}
});
}
private void adjustKeyboard(int keyboardHeight) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) keyboardInfoContainer.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.bottomMargin = keyboardHeight;
keyboardInfoContainer.requestLayout();
}
#jirkarrr, Why don't you add the keyboardInfoContainer like this:
WindowManager wm = getWindowManager();
WindowManager.LayoutParams lps = new WindowManager.LayoutParams();
lps.x = 0; lps.y = keyboardHeight;
wm.addView(keyboardInfoContainer, lps);
I do as your code, but it cannot show out the keyboardInfoContainer.
I use a popup to put view over the keyboard:
public void showPopUpKeyboard() {
mIsPopupVisible = true;
// Initialize a new instance of LayoutInflater service
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.popup_in_keyboard, null);
mScrollView = (ScrollView) customView.findViewById(R.id.keyboard_layout_view);
// Initialize a new instance of popup window
mPopupWindow = new PopupWindow(
customView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
setSizeForSoftKeyboard();
// Get a reference for the custom view close button
Button closeButton = (Button) customView.findViewById(R.id.ib_close);
// Set a click listener for the popup window close button
closeButton.setOnClickListener((View view) -> {
// Dismiss the popup window
mIsPopupVisible = false;
mPopupWindow.dismiss();
});
mPopupWindow.showAtLocation(mParentLayout, Gravity.CENTER, 0, 0);
}
Then I try to know keyboard's height:
mParentLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
Rect r = new Rect();
mParentLayout.getWindowVisibleDisplayFrame(r);
int heightDiff = mParentLayout.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
//enter your code here
if (mIsPopupVisible) {
keepKeyboard();
mIsPopupVisible = false;
mPopupWindow.dismiss();
}
} else {
//enter code for hid
}
});
You can check this tutorial and this example in GitHub

How to show PopupWindow at special location?

I need to show PopupWindow under one Views shown on the screen.
How can I calculate coordinates of needed View and place PopupWindow under it? Code example are more than welcome. Thanks.
Locating an already displayed view is fairly easy - here's what I use in my code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
You could then use code similar to what Ernesta suggested to stick the popup in the relevant location:
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
This would show the popup directly under the original view - no guarantee that there would be enough room to display the view though.
you have getLeft() and getBottom() to get the exact position of the view in the layout. You also have getWidth() and getHeight() to know the exact space occupied by the view. If you want to position your popup window below a view.
You setLeft() and setTop() methods of the view to position the new popup Window.
To get size of the main application screen without stuff like title and notification bars, override the following method in the class generating the screen in question (sizes are measured in pixels):
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
}
To get the bottom coordinate of the view under which you want to show the popup:
View upperView = ...
int coordinate = upperView.getBottom();
Now as long as height - coordinate is large enough for your popup view, you can simply place the popup like this:
PopupWindow popup = new PopupWindow();
Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popup.showAtLocation(parent, Gravity.CENTER, 0, coordinate);
}
});
Here, showAtLocation() takes the parent view as an argument together with gravity and location offsets.

Categories

Resources