We’re not going to use ViewOverlay here. We’ll use Dialog instead.
The overlay does not show on button click. What I did was let the overlay appear when the activity is loaded.
First, create the XML file for the contents of that dialog overlay.
\app\src\main\res\layout\popup_content.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#78000000" android:padding="@dimen/activity_margin" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:textColor="#ffffff" android:text="This is your dialog with transparent background." android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_close_overlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:textColor="#ffffff" android:text="Dismiss"/> </LinearLayout> |
Then add the following code into your activity’s Java file. Please edit as needed in your project.
Notes:
You might need to add lines of “import” codes yourself for the classes I used.
You might need to fill in missing @color and @dimen values yourself.
app\src\main\java\com\yourpackagename\yourappname\activities\MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MainActivity extends AppCompatActivity implements View.OnClickListener { Dialog dialog; private Button btn_close_overlay; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar); showOverlay(); btn_close_overlay = (Button) dialog.findViewById(R.id.btn_close_overlay); btn_close_overlay.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ hideOverlay(); } }); } private void showOverlay(){ dialog.setContentView(R.layout.popup_content); dialog.show(); } private void hideOverlay(){ dialog.dismiss(); } } |