Guest User

Untitled

a guest
Dec 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/rl"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:padding="16dp"
  9. tools:context=".MainActivity"
  10. android:background="#f5f1e0"
  11. >
  12. <Button
  13. android:id="@+id/btn"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Show Popup Window"
  17. />
  18. </RelativeLayout>
  19.  
  20. <?xml version="1.0" encoding="utf-8"?>
  21. <RelativeLayout
  22. xmlns:android="http://schemas.android.com/apk/res/android"
  23. android:id="@+id/rl_custom_layout"
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. android:padding="2dp"
  27. android:background="#ab2fc4"
  28. >
  29. <ImageButton
  30. android:id="@+id/ib_close"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:src="@drawable/ic_close_white_24dp"
  34. android:layout_alignParentEnd="true"
  35. android:layout_alignParentRight="true"
  36. android:background="@null"
  37. />
  38. <TextView
  39. android:id="@+id/tv"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="This is a sample popup window."
  43. android:layout_centerInParent="true"
  44. android:padding="25sp"
  45. />
  46. </RelativeLayout>
  47.  
  48. package com.cfsuman.me.androidcodesnippets;
  49.  
  50. import android.app.Activity;
  51. import android.content.Context;
  52. import android.os.Build;
  53. import android.support.v7.app.AppCompatActivity;
  54. import android.os.Bundle;
  55. import android.view.Gravity;
  56. import android.view.LayoutInflater;
  57. import android.view.View;
  58. import android.widget.Button;
  59. import android.widget.ImageButton;
  60. import android.widget.PopupWindow;
  61. import android.widget.RelativeLayout;
  62. import android.view.ViewGroup.LayoutParams;
  63.  
  64.  
  65. public class MainActivity extends AppCompatActivity {
  66. private Context mContext;
  67. private Activity mActivity;
  68.  
  69. private RelativeLayout mRelativeLayout;
  70. private Button mButton;
  71.  
  72. private PopupWindow mPopupWindow;
  73.  
  74.  
  75. @Override
  76. protected void onCreate(Bundle savedInstanceState) {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.activity_main);
  79.  
  80. // Get the application context
  81. mContext = getApplicationContext();
  82.  
  83. // Get the activity
  84. mActivity = MainActivity.this;
  85.  
  86. // Get the widgets reference from XML layout
  87. mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
  88. mButton = (Button) findViewById(R.id.btn);
  89.  
  90. // Set a click listener for the text view
  91. mButton.setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View view) {
  94. // Initialize a new instance of LayoutInflater service
  95. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  96.  
  97. // Inflate the custom layout/view
  98. View customView = inflater.inflate(R.layout.custom_layout,null);
  99.  
  100. /*
  101. public PopupWindow (View contentView, int width, int height)
  102. Create a new non focusable popup window which can display the contentView.
  103. The dimension of the window must be passed to this constructor.
  104.  
  105. The popup does not provide any background. This should be handled by
  106. the content view.
  107.  
  108. Parameters
  109. contentView : the popup's content
  110. width : the popup's width
  111. height : the popup's height
  112. */
  113. // Initialize a new instance of popup window
  114. mPopupWindow = new PopupWindow(
  115. customView,
  116. LayoutParams.WRAP_CONTENT,
  117. LayoutParams.WRAP_CONTENT
  118. );
  119.  
  120. // Set an elevation value for popup window
  121. // Call requires API level 21
  122. if(Build.VERSION.SDK_INT>=21){
  123. mPopupWindow.setElevation(5.0f);
  124. }
  125.  
  126. // Get a reference for the custom view close button
  127. ImageButton closeButton = (ImageButton) customView.findViewById(R.id.ib_close);
  128.  
  129. // Set a click listener for the popup window close button
  130. closeButton.setOnClickListener(new View.OnClickListener() {
  131. @Override
  132. public void onClick(View view) {
  133. // Dismiss the popup window
  134. mPopupWindow.dismiss();
  135. }
  136. });
  137.  
  138. /*
  139. public void showAtLocation (View parent, int gravity, int x, int y)
  140. Display the content view in a popup window at the specified location. If the
  141. popup window cannot fit on screen, it will be clipped.
  142. Learn WindowManager.LayoutParams for more information on how gravity and the x
  143. and y parameters are related. Specifying a gravity of NO_GRAVITY is similar
  144. to specifying Gravity.LEFT | Gravity.TOP.
  145.  
  146. Parameters
  147. parent : a parent view to get the getWindowToken() token from
  148. gravity : the gravity which controls the placement of the popup window
  149. x : the popup's x location offset
  150. y : the popup's y location offset
  151. */
  152. // Finally, show the popup window at the center location of root relative layout
  153. mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
  154. }
  155. });
  156. }
  157. }
Add Comment
Please, Sign In to add comment