Ell_18
Ell_18

Reputation: 71

How to set the layout and size of a PopupWindow

I'm writing my first program from android, and having difficulty getting a PopupWindow to be positioned and sized as I intend. As it currently is the popup will be activated after clicking a button on the menu bar. After clicking I was hoping to have the popup display centralised, however currently when clicked the result is the picture below (can't post image due to <10 reputation):

https://www.box.com/s/7d4qk8tqlvhiog7576mc

Java Popup Method and Listener:

public void showPopup(View add){
    PopupWindow popup = new PopupWindow(this);
    setContentView(R.layout.add);
    popup.setBackgroundDrawable(null);
    popup.showAtLocation(add, Gravity.CENTER,0,0);
    popup.setFocusable(true);
    popup.setOutsideTouchable(true);
    View hideAdd = findViewById(R.id.add_task);
    hideAdd.setVisibility(View.INVISIBLE);

}
public boolean onOptionsItemSelected(MenuItem menuItem){
    switch(menuItem.getItemId()){
        case R.id.add_task:
            View addTaskView = findViewById(R.id.add_task);
            showPopup(addTaskView);
            return true;
        default:
            return false;
    }
}

}

Add Layout Xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:orientation="horizontal">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/task_title"
        android:textSize="25sp"
        android:textColor="@android:color/holo_blue_bright"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/holo_blue_bright"/>

    <EditText android:id="@+id/task_name"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/task_prompt">

         </EditText>
 </LinearLayout>

Any help will be greatly appreciated.

Upvotes: 4

Views: 7122

Answers (3)

amalBit
amalBit

Reputation: 12181

There is something called a custom dialog... For example.:

  1. You can design a layout and set it to a dialog..

     Dialog dialog=new Dialog();
     dialog.setContentView(the layout u designed here);
    
  2. Convert an activity into dialog.. check my answer to this post..

.. So there is endless possibilities with the dialogs..

Upvotes: 1

Dracosni
Dracosni

Reputation: 87

I think it's because you set the visibility of the view underneeth to invisible. try commenting that and running again.

Upvotes: 0

kiwi
kiwi

Reputation: 487

Try to change all "android:layout_width="match_parent"" in your xml to "android:layout_width="wrap_content""

Upvotes: 1

Related Questions