EDIT: The application's interface
I've created a Pop-up window and class that when I click this onInfoWindowClick in the google maps, a pop-up window will appear OR the next activity will appear. I used Display Metrics for this but it only shows up in the center screen.
What I want to happen is: when the activity shows up, I would like it to show up in the upper-top or at least have the power to change the position of the activity in any part of the window.
My problem: The display metrics shows the activity only in the center.
My codes in my Pop-Up window:
public class PopUp extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_layout);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.5));
}
}
Note: Is there an another method to do this? Can you give me link on how to do it. (Only if there is an another method to do it)
Just add this line at the end:
getWindow().setGravity(Gravity.TOP);
P.S. you can use other values (not only Gravity.TOP) according to your needs. Also you can combine multiple options, for instance, getWindow().setGravity(Gravity.TOP|Gravity.START);
Related
I have a dialog fragment with an edit text in it. I set the height,width of a dialog fragment using -
#Override
public void onResume(){
super.onResume();
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
getDialog().getWindow().setLayout((int)(width * 0.8),(int)(height * 0.8));
}
The problem is that when I click on edittext of the dialog fragment, the key board appears and closes the edittext and I cannot see what I am typing. To see what I have typed, I have to close the keyboard. How do I make my dialog fragment adjust when the keyboard opens up.
Have you already tried this: android:imeOptions="flagNoExtractUi". You can add this to your EditText. I hope that solves your problem.
How do I determine the dimensions (width and height) of an activity during onCreate. The dimension must not be that of the entire screen but only of area that is used by the activity. A few methods I tried posted on SO all end up giving the device's maximum screen sizes.
Have you tried (not exactly onCreate):
activityRootView = findViewById(R.id.yourRootView);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//activityRootView.getHeight();
//...
}
}
});
And ofcourse, your activitys main layout has to match parent or fill content
The entire user content goes to android.R.id.content so what you can do is
final Rect rectgle = new Rect();
LinearLayout mContent = (LinearLayout)findViewById(android.R.id.content).getParent();
mContent.getWindowVisibleDisplayFrame(rectgle);
// and use
//rectgle.top;
//rectgle.left
//rectgle.right
this includes action bar if you want to exclude actionbar also directly use
//findViewById(android.R.id.content);
which returns a FrameLayout
I have a custom view with a public function that adds a control as a child of the view, and I want to call it from my activity. The problem is that I need to know the size of the view in the function in order to place the control. I can't override onMeasure to get the measures because my view inherits from another custom view in which this function is final. I tried overriding measureChildren, but it gets called too late (even after onResume on the activity in which the view is placed in). What can I do in order to have the size before the activity calls the function in the view?
One possibility is to measure your view back in the activity then set properties of the view for it's internal methods to use.
Using a global layout listener has always worked well for me. It has the advantage of being able to remeasure things if the layout is changed, e.g. if something is set to View.GONE or child views are added/removed.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// at this point, the UI is fully displayed
}
}
);
setContentView(mainLayout);
http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html
If you want the the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
I'm trying to make a app that allows you to drag shapes around. It works fine on smart phones, but not on my Acer a500 tablet
When I get the height by calling
ih=getWindowManager().getDefaultDisplay().getHeight()-25;
I get a value thats only 1/3 of what it should be, thus I can only drag the sahpes 1/3 the way down. If the tablet is horizonatl it goes 1/2 way down.
Why is this methed returning the wrong values for the height on my tablet??
public class cPlay extends cBase implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.play);
int w=getWindowManager().getDefaultDisplay().getWidth()-25;
int h=getWindowManager().getDefaultDisplay().getHeight()-25;
BallView ballView=new BallView(this,w,h);
setContentView(ballView);
} // end function
public void onClick(View v) {
finish();
} // end function
} // end class
Try this:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int Width = displaymetrics.widthPixels;
Methods getWidth() and getHeight() are deprecated in Display.
Try to use DisplayMetrics.
Also you might want to get the size of your view's container by calling getMeasuredWidth() and getMeasuredHeight() after onMeasure() was called to get more precise size of your view.
When my application loads into an emulator or device, it renders the screen I want it to just fine, including a View with id 'graph'. Suppose I then hit the menu button, and WITHOUT selecting any menu options, hit the back button. The screen no longer shows the View 'graph'.
It is of type View_PieChart, which extends View. Here is the relevant chunk of the XML resource file:
<org.test.View_PieChart
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/graph"
android:background="#000"
/>
Here's the onResume() function I'm using. Again, it shows 'graph' when the application initially launched, but once another Activity is launched, then the Back button hit by the user, it fails to draw 'graph'.
protected void onResume() {
super.onResume();
int MaxCount = 100;
View_PieChart graph = (View_PieChart) findViewById(R.id.graph);
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
graph.setGeometry(width, width, 15, 15, 15, 15);
graph.setData(PieData, MaxCount);
}
I've combed through lots of documentation (including the activity lifecycle documentation) but have a feeling that there's something in the View "lifecycle" that I'm not understanding. Any suggestions appreciated.
Did you intend to pass "width" twice in setGeometery()?
Also, could the problem be that at the point onResume() is called, the view layouts have not been completed and so manipulating the geometery does not work as intended. The same issue exists in onCreate() too.
I've had similar problems and found the solution was to create an OnGlobalLayoutListener() that will be called when layout is complete. For example,
mTopView = (RelativeLayout)findViewById(R.id.topView);
mTopView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
// Do here anything that relies on view dimensions being settled
}
});