Search code examples
androidurlimageviewpicassopopupwindow

How to add image to popup window from URL in navigation drawer Activity


I have popup window implemented in my main navigation drawer activity. What i want is to get an image from URL, set that image in imageView in activity_pop_up.xml, and show popup window in my main activity

Here is my MainActivity:

protected void onCreate(Bundle savedInstanceState)   {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

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

    setSupportActionBar(toolbar);

// HERE IS POPUP WINDOW
       LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.activity_pop_up, null);           
        pw = new PopupWindow(layout, 600, 800, true);
        findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
        public void run() {
            pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0);
        }
      });

    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();

    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().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;
    }

    if (id == R.id.galerija)
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

    // Handle navigation view item clicks here.
    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");
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

activity_pop_up.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/popup"
android:layout_width="match_parent"

android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">


     <ImageView
      android:id="@+id/baner3img"
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:layout_alignParentStart="true"
      android:layout_centerVertical="true"
      app:srcCompat="@drawable/autojedan" />
 </RelativeLayout>

Solution

  • @Nikola you can create a class which extends Dialog with a custom layout that is your activity_pop_up having imageView. You can set the requestWindowFeature(Window.FEATURE_NO_TITLE) that will make it look like a pop up. For image downloading you can use Picasso, Universal Image Loader or Glide and display it in your UI.