Yahj Rodriguez Yahj Rodriguez - 8 months ago 46
Android Question

How do I change the position of my new Activity (Display Metrics)

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)

Answer Source

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);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download