Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Created March 7, 2014 09:14
Show Gist options
  • Save ishitcno1/9408188 to your computer and use it in GitHub Desktop.
Save ishitcno1/9408188 to your computer and use it in GitHub Desktop.
A custom DialogFragment that can be positioned and set size. Make sure to use 9patch background. Ref: http://stackoverflow.com/questions/9698410/position-of-dialogfragment-in-android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/bg_user_panel">
<!-- dummy to set width -->
<View
android:layout_width="100dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginTop="4dp"
android:text="@string/register"
style="@style/TextLarge"
android:background="@drawable/bg_btn_feedback"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="@color/darkGray" />
<TextView
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:text="@string/login"
style="@style/TextLarge"
android:background="@drawable/bg_btn_feedback"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="4dp"
android:background="@color/darkGray" />
<TextView
android:id="@+id/feedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginBottom="4dp"
android:text="@string/feedback"
style="@style/TextLarge"
android:background="@drawable/bg_btn_feedback"/>
</LinearLayout>
public class UserPanelDialogFragment extends DialogFragment implements View.OnClickListener {
private boolean isLogin = false;
private TextView mRegister;
private TextView mLogin;
private TextView mFeedback;
private TextView mUserId;
private TextView mLogout;
public static UserPanelDialogFragment newInstance(boolean isLogin) {
UserPanelDialogFragment fragment = new UserPanelDialogFragment();
fragment.isLogin = isLogin;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Dialog);
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.dimAmount = 0.6f;
window.setAttributes(params);
window.setBackgroundDrawableResource(android.R.color.transparent);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = null;
if (isLogin) {
view = inflater.inflate(R.layout.user_panel_logedin, container, false);
mUserId = (TextView) view.findViewById(R.id.user_id);
mLogout = (TextView) view.findViewById(R.id.logout);
mFeedback = (TextView) view.findViewById(R.id.feedback);
mUserId.setOnClickListener(this);
mLogout.setOnClickListener(this);
mFeedback.setOnClickListener(this);
} else {
view = inflater.inflate(R.layout.user_panel, container, false);
mRegister = (TextView) view.findViewById(R.id.register);
mLogin = (TextView) view.findViewById(R.id.login);
mFeedback = (TextView) view.findViewById(R.id.feedback);
mRegister.setOnClickListener(this);
mLogin.setOnClickListener(this);
mFeedback.setOnClickListener(this);
}
setDialogPosition();
return view;
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.register) {
// todo: add response
} else if (id == R.id.login) {
} else if (id == R.id.user_id) {
} else if (id == R.id.logout) {
} else if (id == R.id.feedback) {
}
}
private void setDialogPosition() {
Window window = getDialog().getWindow();
window.setGravity(Gravity.BOTTOM | Gravity.LEFT);
WindowManager.LayoutParams params = window.getAttributes();
params.y = dpToPx(60);
window.setAttributes(params);
}
private int dpToPx(int dp) {
DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
}
@1509489
Copy link

1509489 commented May 19, 2019

You are getting null pointer exception because you are not checking if the dialog is available or not. you can do something like:
private void setDialogPosition() { if(getDialog() != null){ Window window = getDialog().getWindow(); window.setGravity(Gravity.BOTTOM | Gravity.LEFT); WindowManager.LayoutParams params = window.getAttributes(); params.y = dpToPx(60); window.setAttributes(params); } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment