Overlay Dialog Box - Align Image

I want to implement help overlay for my application. It should look like this: How do I create a help overlay, as you see in several Android and ICS applications?

final Dialog dialog = new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); dialog.setContentView(R.layout.coach_mark); dialog.setCanceledOnTouchOutside(true); //for dismissing anywhere you touch View masterView = dialog.findViewById(R.id.coach_mark_master_view); masterView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); 

It works very well. The RelativeLayout of the coach_mark.xml file is displayed on top of my existing layout, which was set via setContentView (). Now I want to align the views of coach_mark.xml next to the corresponding views of the existing layout in order to dynamically respond to the resolution and screen size. This is what I tried:

 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.BELOW,R.id.view_of_main_layout); view_of_the_overlay.setLayoutParams(lp); 

But the view is displayed only in the middle of the screen. The only thing I can archive is to align the view from the bottom of the screen, but that’s not what I want to do.

Thank you for your help.

+1
android alignment layout overlay
source share
1 answer

You can use PopupWindow:

 PopupWindow popup=new PopupWindow(this); popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); popup.setWindowLayoutMode(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT); popup.setContentView(R.layout.coach_mark); View view=findById(R.id.view_of_main_layout); int[] viewLocation=new int[2]; view.getLocationInWindow(viewLocation) popup.showAtLocation(view, Gravity.TOP|Gravity.LEFT, viewLocation[0],viewLocation[1]); 

You can also configure the last two variables for the popup location, which are X and Y offest. Their values ​​may also be negative.

+1
source share

All Articles