0

[Image sample][1]I have popup screen implemented in my navigation drawer activity. Problem is that popup window is showing like background image of activity. What i want to popup come visible in foreground when Activity starts and after 3 seconds dismiss.

Here is my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);

    toggle.syncState();    

    try {
        LayoutInflater inflater1 = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Inflate the view from a predefined XML layout
        View layout = inflater1.inflate(R.layout.activity_pop_up,
                (ViewGroup) findViewById(R.id.relativeLayoutZaFragment));
        // create a 300px width and 470px height PopupWindow
        pw = new PopupWindow(layout, 300, 470, true);
        // display the popup in the center
        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    FragmentMainPage pocetnaFragment = new FragmentMainPage();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.relativeLayoutZaFragment, pocetnaFragment, "1");
    fragmentTransaction.commit();

    getSupportActionBar().setTitle("Auto magazin");

    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity = Gravity.LEFT;

    view = getLayoutInflater().inflate(R.layout.action_bar, null);

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.LEFT);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);

    Title.setText("Auto magazin");

    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    assert drawer != null;

    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
        return;
    }
    else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Pritisnite još jednom za izlaz", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.podesavanja)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)   {

    int id = item.getItemId();

    if (id == R.id.pocetna)       {
        FragmentMainPage fragmentMainPage = new FragmentMainPage() ;
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
               , fragmentMainPage, fragmentMainPage.getTag()).addToBackStack(null).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Auto magazin");
    }
    else if (id == R.id.info)   {
         FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
         FragmentManager manager = getSupportFragmentManager();
         manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();

        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Info");

        Bundle bundle = new Bundle();

        bundle.putString("href","http://www.magazinauto.com/category/info/");
        bundle.putString("title", "Info");
        fragmentMenuPages.setArguments(bundle);

    }

    else {
        return super.onOptionsItemSelected(item);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    drawer.closeDrawer(GravityCompat.START);

    return true;
    }
}

activity_pop_up:

<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:id="@+id/popup"
android:layout_width="match_parent"

android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
  <ImageView
   android:id="@+id/imageView3"
   android:layout_width="match_parent"
   android:layout_height="100dp"
   android:layout_alignParentStart="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/autojedan" />
</RelativeLayout>

content_main.xml:

<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:id="@+id/relativeLayoutZaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.bolt.automagazin.MainActivity"
tools:showIn="@layout/app_bar_main"
>

Nikola Božić
  • 127
  • 1
  • 3
  • 12
  • That's not your `PopupWindow`. You're inflating `activity_pop_up` directly into `relativeLayoutZaFragment`, underneath the `Fragment` that's eventually transacted on top of it. Then I would imagine you're getting an Exception for the `PopupWindow`, since you're passing it a `View` that's already attached to parent. Change the second argument in the `inflate()` call to `null`, and pass the `(ViewGroup) findViewById(...)` as the first argument in the `showAtLocation()` call. – Mike M. Apr 25 '17 at 22:08
  • Can you show me some example come pls and yes i am getting an Exception? – Nikola Božić Apr 25 '17 at 22:11
  • Change `View layout = inflater1.inflate(R.layout.activity_pop_up, null);`, and `pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0);`. – Mike M. Apr 25 '17 at 22:14
  • When i do like that popup dont show at all :( – Nikola Božić Apr 25 '17 at 22:18
  • Your `PopupWindow` wasn't ever showing. Check your logcat for an Exception. – Mike M. Apr 25 '17 at 22:28
  • relativeLayoutZaFragment is container where navigaion drawer change fragments. – Nikola Božić Apr 25 '17 at 22:34
  • OK. Are you getting an Exception? – Mike M. Apr 25 '17 at 22:34
  • No i can try without try/catch block, now when i start nothing happens – Nikola Božić Apr 25 '17 at 22:35
  • Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? in this line pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0); – Nikola Božić Apr 25 '17 at 22:37
  • Oh, right, cause you're doing that in `onCreate()`. Look here: http://stackoverflow.com/q/4187673, or http://stackoverflow.com/q/8782250. – Mike M. Apr 25 '17 at 22:40
  • Its working now!!, thanks a lot, post something , so i can check as correct answer – Nikola Božić Apr 25 '17 at 22:44
  • OK. Gimme a little bit. I'm on a phone. – Mike M. Apr 25 '17 at 22:46
  • kk np , thanks a lot again, you save my work;) – Nikola Božić Apr 25 '17 at 22:51

1 Answers1

0

What you're seeing is not actually your PopupWindow. That inflate() call is attaching the inflated View directly to the relativeLayoutZaFragment, and the Fragment is being added on top of it. Then, since that View is already attached to a parent, the PopupWindow is throwing an Exception, and is never shown.

Change the second argument in the inflate() call to null:

View layout = inflater1.inflate(R.layout.activity_pop_up, null);

And pass the RelativeLayout that's in the Activity to showAtLocation(), instead of the inflated layout:

pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment‌​), Gravity.CENTER, 0, 0);

Additionally, since you're doing this in onCreate(), you'll need to defer showing the PopupWindow momentarily, or you'll get that WindowManager$BadTokenException. You can do that by putting the showAtLocation() call in a Runnable, and posting that to a View. For example:

final View rl = findViewById(R.id.relativeLayoutZaFragment‌);
rl.post(new Runnable() {
    @Override
        public void run() {
            pw.showAtLocation(rl, Gravity.CENTER, 0, 0);
        }
    }
);
Mike M.
  • 38,532
  • 8
  • 99
  • 95