1

I have the main activity in which I have few menu buttons. When the user presses specific button I want to open a new activity which also have a couple of buttons which I need to handle their click. In other words, I need to pop up window functionality as normal activity.
I looked online and found several ways to implement this such as: just customising the size of the activity, use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. But as I am new to android I want the best way to implement it for my project.
Can someone help me achieve this?

EDIT: this is the xml file i want to use in the pop up window (for better explanation of what i want to achieve):

<?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:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="#0091cb"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="@dimen/activity_horizontal_margin"
        android:weightSum="3"
        android:id="@+id/check">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/computer"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button1"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button1_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/electrical"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button2"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button2_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/hdtv"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button3"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button3_OnClick"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal"
        android:layout_below="@id/check"
        android:paddingTop="10dp">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/bill"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button4"
            android:textSize="10dp"
            android:textColor="#fff" 
            android:onClick="button4_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/water"
            android:paddingTop="12dp"
            android:text="button5"
            android:textSize="10dp"
            android:textColor="#fff"
            android:onClick="button5_OnClick" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/electrical"
            android:paddingTop="12dp"
            android:text="button6"
            android:textSize="10dp"
            android:textColor="#fff" 
            android:onClick="button6_OnClick" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/notepad"
            android:paddingTop="7dp"
            android:text="button7"
            android:textSize="10dp"
            android:textColor="#fff"
            android:onClick="button7_OnClick" />
        </LinearLayout>
</LinearLayout>
A.T
  • 71
  • 2
  • 9
  • 2
    you can look here https://developer.android.com/reference/android/widget/PopupWindow.html and research on stackoverflow – okarakose May 23 '16 at 08:32
  • thank you. from the research i made i found out these ways: just customizing the size of the activity, use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. but im not sure what is the best way for what i want – A.T May 23 '16 at 08:37
  • Use this method to open popup window.This method i used in my app.i hope u also like. – bhumika rijiya May 23 '16 at 09:04

2 Answers2

0

create method where u want to open popup windiw in your activity Like this,here p is a specific point(location) where you want exactly open your window.

  final Point p = new Point();
  show_popup.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showpopupwindows(Activity, p);

            }
        });

then,

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    int location[] = new int[2];

    show_popup.getLocationOnScreen(location);
    p.x = location[0];
    p.y = location[1];
}

private void showpopupwindows(final Activity context, Point p) {

    LinearLayout viewGroup = (LinearLayout) context
            .findViewById(R.id.popup_menu);

    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    Display display = context.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    int popupwidth = (size.x / 2);
    int popupheight =(size.y / 2);

    View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup);

    final PopupWindow popup = new PopupWindow(context);

    popup.setContentView(layout);
    popup.setWidth(popupwidth);
    popup.setHeight(popupheight);
    popup.setFocusable(true);
    popup.setAnimationStyle(R.style.WindowAnimation);
    popup.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));

    popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight);

    detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1);

    detail_pop1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show();

        }
    });
}

detail_pop.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_menu"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@color/skyblue"
    android:gravity="center"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/detail_pop1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:drawableTop="@drawable/ic_launcher"
        android:gravity="center"
        android:text="P"
        android:textSize="@dimen/font_24" />


</LinearLayout>
bhumika rijiya
  • 440
  • 1
  • 5
  • 42
  • here in my code my layout name is detail_pop but so u can wright your layout name and also give id to all buttons then define as you can define , Button = (Button) layout.findViewById(R.id.Button); then whatever you perform to button you can do as i do. – bhumika rijiya May 23 '16 at 09:47
  • what are these objects for: LinearLayout viewGroup = (LinearLayout) context .findViewById(R.id.popup_menu); LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); and your passing showPopUpWindow() empty point, its inisialized in the onWindowFocusChanged(). can you explain the logic of how it works? – A.T May 23 '16 at 11:05
  • here popup_menu is id of a main linear layout.so u give id to your main linear layout.initiallly i just defined point but in methos i describe location of point.plz see carefully.i suggest first of all u implement whole method.then do some changes accrding to your project. – bhumika rijiya May 23 '16 at 11:18
  • see my detail_pop.xml file – bhumika rijiya May 23 '16 at 11:25
0

Depends on your needs.
If you need just to show 'classic' popup window (error while typing in input field or small color picker) - use PopupWindow class. You can use my gist.
If your message is more general (e.g. 'There is no internet connection') or user should choose yes/no - use dialog. There is cool library.
Activity with custom size is used rarely. Maybe in some dial applications. So choose implementation depends on your needs.

Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56
  • my need is to handle it like normal activity, now to show a message or alert. in the activity i want to show as a pop up i have buttons i need to handle their clicks events with functions inside their activity – A.T May 23 '16 at 09:24
  • @A.T you can have buttons in your popup layout using any of these ways – Alex Zaitsev May 23 '16 at 13:11