- Home
- >> Nerd Digest
- >> Android
-
How to Show Dialogue Box at Clicked Place Without Dull Background in Android
over 7 years ago
To attain such Dialogue box please go through the steps mentioned below:-
1) Declare a button/ImageView in your Activity xml file, you can place this button on top right corner or left corner or in center wherever you wish you can place it. Here in this tutorial I'm placing the ImageView on left corner at top as shown below in xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CBCBCB">
<ImageView
android:id="@+id/ivHamIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ivCompanyLogo"
android:layout_marginLeft="10dp"
android:paddingTop="@dimen/ten"
android:src="@drawable/hamburg" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#CBCBCB"> <ImageView android:id="@+id/ivHamIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/ivCompanyLogo" android:layout_marginLeft="10dp" android:paddingTop="@dimen/ten" android:src="@drawable/hamburg" /> </RelativeLayout>
2) Now apply the setOnClick listener to the button/ImageView in your Activity java file. The setOnClickListener provides you the View with the help of which you can take out the x axis as well as the Y axis of the place where you performed the click on your Screen.
ImageView ivHamIcon = (ImageView) findViewById(R.id.ivHamIcon);
ivHamIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = view.getLeft();
int y = view.getBottom();
hamburgerDialogue(x, y, BaseActivity.this, "Search");
}
});
ImageView ivHamIcon = (ImageView) findViewById(R.id.ivHamIcon); ivHamIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int x = view.getLeft(); int y = view.getBottom(); hamburgerDialogue(x, y, BaseActivity.this, "Search"); } });
3) Now with the help of these X and Y axis we will place our dialogue box to appear near these axis by giving these axis at runtime.
private void hamburgerDialogue(int x, int y, final Activity context, final String activity) {
final Dialog dialog = new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.hamburger_dialogue);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams ll = dialog.getWindow().getAttributes();
ll.gravity = Gravity.TOP | Gravity.LEFT;
ll.x = 0;
ll.y = y - 40;
dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
dialog.show();
}
private void hamburgerDialogue(int x, int y, final Activity context, final String activity) { final Dialog dialog = new Dialog(context); dialog.setCancelable(true); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.hamburger_dialogue); dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); WindowManager.LayoutParams ll = dialog.getWindow().getAttributes(); ll.gravity = Gravity.TOP | Gravity.LEFT; ll.x = 0; ll.y = y - 40; dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); dialog.show(); }
haburger_dialogue.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#20365F"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/thirty_dpo"
android:layout_marginStart="@dimen/ten"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="#fff" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/home"
android:textAllCaps="true"
android:textColor="#fff" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvUpcomingAuction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvBiddingOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:textAllCaps="true"
android:text="@string/search"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvItemsWatching"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvMyAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvInformation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvPreferences"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvChangePaswd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
<com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView
android:id="@+id/tvSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/five"
android:text="@string/search"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one_dp"
android:layout_marginBottom="@dimen/five"
android:layout_marginTop="@dimen/five"
android:background="@android:color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#20365F" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="@dimen/thirty_dpo" android:layout_marginStart="@dimen/ten" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="#fff" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvHome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/home" android:textAllCaps="true" android:textColor="#fff" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvUpcomingAuction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvBiddingOn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:textAllCaps="true" android:text="@string/search" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvItemsWatching" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvMyAccount" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvInformation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvPreferences" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvChangePaswd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> <com.kurtkaptein.android.kurt.fonts.Lucida_SansRegularTextView android:id="@+id/tvSearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/five" android:text="@string/search" android:textAllCaps="true" android:textColor="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="@dimen/one_dp" android:layout_marginBottom="@dimen/five" android:layout_marginTop="@dimen/five" android:background="@android:color/white" /> </LinearLayout> </LinearLayout> </LinearLayout>
4) Now the last thing that we wish to do is that our background screen dose not go dim when our dialogue box appear for this we just need to clear a flag that do this here is the line of code for the same .
0 Comment(s)