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>
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>
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 (eg '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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.