Skip to content

Instantly share code, notes, and snippets.

@niftynei
Created March 18, 2016 18:56
Show Gist options
  • Save niftynei/31d99840cbc70252e69d to your computer and use it in GitHub Desktop.
Save niftynei/31d99840cbc70252e69d to your computer and use it in GitHub Desktop.
package com.electricobjects.client.onboarding;
import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
public class OnboardingMessageDialog extends CustomDimDialogFragment implements View.OnClickListener {
private View calloutView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.dialog_onboarding, container, false);
int message = R.string.message;
((TextView) view.findViewById(R.id.onboarding_message)).setText(message);
view.findViewById(R.id.onboarding_ok_button).setOnClickListener(this);
// code to add callout button
ViewTreeObserver vto = getActivity().findViewById(R.id.button_add_to_playlist).getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
FloatingActionButton addFab = (FloatingActionButton) calloutView.findViewById(R.id.button_add_to_playlist);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) addFab.getLayoutParams();
// find the view to call out in the parent activity
View originalView = getActivity().findViewById(R.id.button_add_to_playlist);
params.topMargin = originalView.getTop();
params.leftMargin = originalView.getLeft();
addFab.setLayoutParams(params);
ViewTreeObserver obs = getActivity().findViewById(R.id.button_add_to_playlist).getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
}
});
calloutView = inflater.inflate(R.layout.layout_callout_add_playlist_button, container, false);
calloutView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// add as a content view on the Activity
getActivity().getWindow().addContentView(calloutView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// set the dialog to float to the top 1/3rd of the screen (roughly)
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.gravity = Gravity.TOP;
params.verticalMargin = 0.2f;
dialog.getWindow().setAttributes(params);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
return dialog;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.onboarding_ok_button:
dismissAllowingStateLoss();
break;
default:
dismissAllowingStateLoss();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
// remember to remove the callout view on destroy view!
if (calloutView != null) {
ViewGroup parent = (ViewGroup) calloutView.getParent();
if (parent != null) {
parent.removeView(calloutView);
}
calloutView = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment