Geeky bean
Geeky bean

Reputation: 525

popupWindow doesn't show?

I'm trying to create a popup that will show at a press of a button. I found a lot of tutorials and searched StackOverflow, but none of those solutions helped me and I can't figure out why.

Here is my code:

    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View newValueView = inflater.inflate(R.layout.change_value_popup,null);
    newValuePopupWindow = new PopupWindow(newValueView, RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    newValuePopupWindow.showAtLocation(findViewById(R.id.main),Gravity.CENTER,0,0);

it doesn't seem to work, I tried to add:

newValuePopupWindow.setFocusable(true);

but that didn't help either.

here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#CCE5FF"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/enter_value_title_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Enter new value"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/new_value_editText"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<EditText
    android:id="@+id/new_value_editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="500dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="OK"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</RelativeLayout>

I tried with RelativeLayout and with ConstraintLayout.

Is there a chance that the popup appears behind the main layout?

Upvotes: 0

Views: 1155

Answers (3)

Geeky bean
Geeky bean

Reputation: 525

The problem occurred because of a duplicated layout (for landscape). For some reason, the link to the onClick method deleted in one of the layouts (I always checked the main one). So silly mistake by me :/ Thanks for the helpers :)

Upvotes: 0

Deep Naik
Deep Naik

Reputation: 491

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View newValueView = inflater.inflate(R.layout.change_value_popup,null);
newValuePopupWindow = new PopupWindow(newValueView, RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
newValuePopupWindow.showAtLocation(newValueView,Gravity.CENTER,0,0);

make above change and try.

Upvotes: 1

XcombeX
XcombeX

Reputation: 117

I will write for you one simple of popUp window, I hope it will help you.

You have to add this lines in your AndroidManifest.xml file:

<activity
        android:name=".Pop"
        android:theme="@style/AppTheme.CustomTheme" />

Here I make simple theme for popUp window, we can change that...we define that in styles.xml file :

<style name="AppTheme.CustomTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

You have also make one layout for popUp window, here is code for layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Pop">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="217dp"
    android:text="@string/asdfdasdasdasdas" />

Also you have to make java file, here is code for that:

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;

public class Pop extends Activity{

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

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.7),(int) (height*.7));
}
}

In the end, I open popUp when button is clicked, here is code (add in MainActivity.java file) :

 Button button = findViewById(R.id.info);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, com.example.user_pc.zavrsnitri.Pop.class);
            startActivity(intent);
        }
    });

Upvotes: 2

Related Questions