duggu
duggu

Reputation: 38439

How to open custom layout dialog box below on click of button in android?

I want to make drop down custom dialog box below on click button.

enter image description here

Click on counter button and layout will show below button.

I seeing so many link but they open only list :-

Action Bar dropdown open and closed item styles

New Quick Action package

above link not use full to me.

But when I'm using dialog box it appear front of button.

so if you know then help me.

Upvotes: 3

Views: 7053

Answers (4)

Ashish Tyagi
Ashish Tyagi

Reputation: 81

public class MainActivity extends AppCompatActivity {

    private Button button;
    private WindowManager.LayoutParams layoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            ((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.drop_down_layout, null, false);
                final PopupWindow popUp = new PopupWindow(mView, LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, false);
                popUp.setTouchable(true);
                popUp.setFocusable(true);
                popUp.setOutsideTouchable(true);

            //Solution
                popUp.showAsDropDown(findViewById(R.id.button));
            }
        });
    }
}

Upvotes: 2

Phong Nguyen
Phong Nguyen

Reputation: 7107

I tried to wrap_content in PopupWindow and get surprised cause it still works.

PopupWindow popupWindow = new PopupWindow(testView, LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, false);

Upvotes: 0

andrei
andrei

Reputation: 2940

PopupWindow also has a method showAsDropDown which shows the popup as a dropdown below a view

Upvotes: 2

Mani
Mani

Reputation: 777

I think you can achieve what you are looking for by using

getLocationOnScreen() api &

PopUpWindow component.

An example code can be as follows.

int[] location = new int[2];
counterView.getLocationOnScreen(location);
final View mView = inflater.inflate(R.layout.xxxx, null, false);
final PopupWindow popUp = new PopupWindow(mView, Width, Height, false);
popUp.setTouchable(true);
popUp.setFocusable(true);
popUp.setOutsideTouchable(true);
popUp.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1]);

Upvotes: 6

Related Questions