Example usage for android.widget ImageView setImageBitmap

List of usage examples for android.widget ImageView setImageBitmap

Introduction

In this page you can find the example usage for android.widget ImageView setImageBitmap.

Prototype

@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) 

Source Link

Document

Sets a Bitmap as the content of this ImageView.

Usage

From source file:reportsas.com.formulapp.Formulario.java

public void CapturaF() {
    if (parametroCam == null) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);

    } else {/*from   w ww.  ja v a 2 s.  c om*/

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.dialog_imagen);
        dialog.setTitle("Captura de Formulario");
        byte[] decodedByte = Base64.decode(parametroCam.getValor(), 0);

        ImageView imageview = (ImageView) dialog.findViewById(R.id.ImaVcaptura);
        Button Button1 = (Button) dialog.findViewById(R.id.NuevaToma);
        Button Button2 = (Button) dialog.findViewById(R.id.btn_cerrar);

        imageview.setImageBitmap(BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length));
        Button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
                dialog.dismiss();
            }
        });

        Button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog.show();

    }

}

From source file:com.acc.android.util.widget.adapter.ImageAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // if (false) {
    // if (this.accFileCallback == null) {
    // return null;
    // }/* w  w  w  .j a va  2s  . com*/
    ACCFile accFile = this.imageData.getAccFiles().get(position);
    ImageView imageView = !this.isBig ? new ImageView(this.context)
            : new AutoImageView(this.context, position == 0, position == this.getCount() - 1,
                    this.onSingleTapListener);
    Bitmap bitmap = this.bitmapProviderManager.getBitmap(accFile);
    // if (bitmap == null || bitmap.isRecycled()) {
    // bitmap = BitmapManager.getInstance(context).getBlankBitmap();
    // }
    // LogUtil.systemOut("bitmap == null:");
    // LogUtil.systemOut(bitmap == null);
    // if (true) {
    if (this.isBig) {
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    }
    // this.isBig ?
    // :autoImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    // imageView.setBackgroundResource(R.drawable.login_input);
    // }
    // System.out.println(position);
    imageView.setImageBitmap(bitmap);
    if (this.isBig && accFile.getProgress() != null) {
        ((AutoImageView) imageView).setProgress(accFile.getProgress());
    }
    // }
    return imageView;
}

From source file:com.nd.pad.GreenBrowser.util.ImageDownloader.java

/**
 * /*from www.  ja va  2 s  .  c  o m*/
 * dDD{]?C
 * @param url
 * @param imageView imageview
 * @param width 
 * @param height 
 * @param loadingDrawable_ID 
 * @param failDrawable_ID 
 * @param isSaveLocal 
 * @param action 
 *  MyGoPlusV3 
 * <pre>
 *             
 * 2013-6-13   ?      
 * </pre>
 */
public void download(String url, ImageView imageView, int width, int height, int loadingDrawable_ID,
        int failDrawable_ID, boolean isSaveLocal, ImageDownLoaderAction action) {
    //        Bitmap bitmap = getBitmapFromCache(url);
    Bitmap bitmap = null;
    /*  */
    if (bitmap == null && isSaveLocal) {
        bitmap = getBitmapFromLocal(url, Constants.FILE_CACHE, width, height);
    }
    /*  */
    if (bitmap == null) {
        forceDownload(url, imageView, width, height, Constants.FILE_CACHE, loadingDrawable_ID, failDrawable_ID,
                isSaveLocal, action);
    } else {
        cancelPotentialDownload(url, imageView);
        imageView.setImageBitmap(bitmap);
        if (action != null) {
            action.downFinish();
        }
    }
}

From source file:es.upv.riromu.arbre.main.MainActivity.java

private void updateVisibility()

{
    Boolean done = false;/* w w  w.ja  v  a2s  .  c om*/
    TextView imc = (TextView) findViewById(R.id.textView);
    //   imc.setBackgroundColor(Color.rgb(255, 255, 255));
    RangeSeekBar<Integer> rangeSeekBar = (RangeSeekBar<Integer>) findViewById(R.id.rangeseekbar);
    rangeSeekBar.setSelectedMinValue(MIN_TH);
    rangeSeekBar.setSelectedMaxValue(MAX_TH);

    ImageView imv = (ImageView) findViewById(R.id.image_intro);

    if (state[CROP_IMAGE]) {
        rangeSeekBar.setVisibility(View.VISIBLE);
    }
    if (!state[CROP_IMAGE]) {
        rangeSeekBar.setVisibility(View.GONE);
    }

    if (state[TREAT_IMAGE] && state[CROP_IMAGE] && (!state[SHOW_HISTOGRAM])) {
        state[TREAT_IMAGE] = false;
        imv.setImageBitmap(croppedimage);
        done = true;
    }

    if (state[TREAT_IMAGE] && (!state[CROP_IMAGE]) && (!state[SHOW_HISTOGRAM]) && (!done)) {
        state[TREAT_IMAGE] = false;

        imv.setImageBitmap(image);
        ImageButton cropButton = (ImageButton) findViewById(R.id.button_crop);
        cropButton.setVisibility(View.VISIBLE);
        imc.setVisibility(View.GONE);
        ImageButton sendButton = (ImageButton) findViewById(R.id.button_send);
        sendButton.setVisibility(View.GONE);
        ImageButton histButton = (ImageButton) findViewById(R.id.button_histogram);
        histButton.setVisibility(View.GONE);
        ImageButton revertButton = (ImageButton) findViewById(R.id.button_revert);
        revertButton.setVisibility(View.GONE);
        ImageView imagePalette = (ImageView) findViewById(R.id.palette);
        imagePalette.setVisibility(View.GONE);
        done = true;
    }
    if ((!state[TREAT_IMAGE]) && state[CROP_IMAGE] && (!state[SHOW_HISTOGRAM]) && (!done)) {
        state[CROP_IMAGE] = false;
        try {
            InputStream is = getContentResolver().openInputStream(image_uri);
            imv.setImageBitmap(BitmapFactory.decodeStream(is));
            is.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found" + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "File not found" + e.getMessage());
        }
        croppedimage.recycle();
        done = true;
    }
    if ((!state[TREAT_IMAGE]) && (!state[CROP_IMAGE]) && (!state[SHOW_HISTOGRAM]) && (!done)) {
        image.recycle();
        image_uri = null;
        imv.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.platanus_hispanica));
        done = true;
    }
    if ((state[SHOW_HISTOGRAM]) && (!done)) {
        state[SHOW_HISTOGRAM] = false;
        rangeSeekBar.setVisibility(View.VISIBLE);
        imv.setImageBitmap(treatedimage);
        imc.setVisibility(View.VISIBLE);
        //                imc.setBackgroundColor(Color.rgb(colours[COLOUR_R], colours[COLOUR_G], colours[COLOUR_B]));

    }
}

From source file:m2.android.archetype.example.FacebookSdk.widget.GraphObjectAdapter.java

private void processImageResponse(ImageResponse response, String graphObjectId, ImageView imageView) {
    pendingRequests.remove(graphObjectId);
    if (imageView == null) {
        // This was a pre-fetch request.
        if (response.getBitmap() != null) {
            // Is the cache too big?
            if (prefetchedPictureCache.size() >= MAX_PREFETCHED_PICTURES) {
                // Find the oldest one and remove it.
                String oldestId = prefetchedProfilePictureIds.remove(0);
                prefetchedPictureCache.remove(oldestId);
            }/*from  w w  w . j  ava  2 s  .  co  m*/
            prefetchedPictureCache.put(graphObjectId, response);
        }
    } else if (imageView != null && graphObjectId.equals(imageView.getTag())) {
        Exception error = response.getError();
        Bitmap bitmap = response.getBitmap();
        if (error == null && bitmap != null) {
            imageView.setImageBitmap(bitmap);
            imageView.setTag(response.getRequest().getImageUrl());
        }
    }
}

From source file:com.nd.pad.GreenBrowser.util.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used. Kept private at the moment as its
 * interest is not clear.//from   ww w. j a va2 s  . c o  m
 */
private void forceDownload(String url, ImageView imageView, int width, int height, String parentFullFileName,
        int loadingDrawable_ID, int failDrawable_ID, boolean isSaveLocal, ImageDownLoaderAction action) {
    // State sanity: url is guaranteed to never be null in
    // DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageResource(failDrawable_ID);
        return;
    }
    if (cancelPotentialDownload(url, imageView)) {
        /*modeCORRECT*/
        switch (mode) {
        case NO_ASYNC_TASK:
            Bitmap bitmap = downloadBitmap(url);
            addBitmapToCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
            break;

        case NO_DOWNLOADED_DRAWABLE:
            imageView.setMinimumHeight(156);
            BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, failDrawable_ID, isSaveLocal,
                    action);
            task.execute(url);
            break;

        case CORRECT:
            task = new BitmapDownloaderTask(imageView, width, height, parentFullFileName, failDrawable_ID,
                    isSaveLocal, action);
            DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task, loadingDrawable_ID);
            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumHeight(156);
            try {
                task.execute(url);
            } catch (RejectedExecutionException localRejectedExecutionException) {
            }
            break;
        }
    }
}

From source file:com.pongme.utils.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not
 * used. Kept private at the moment as its interest is not clear.
 *//*from ww  w. j  a  v  a  2  s  .  c o  m*/
private BitmapDownloaderTask forceDownload(String url, ImageView imageView, BitmapDrawable tempImage) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    BitmapDownloaderTask task = null;

    if (url == null) {
        imageView.setImageDrawable(null);
        // imageView.setImageResource(R.drawable.home_img_download);
        return null;
    }

    int w = 0, h = 0;
    LayoutParams params = imageView.getLayoutParams();
    if (params != null) {
        w = params.width;
        h = params.height;
    }

    // Change Task attributes if exist intead of Cancel

    if (refactorPotentialDownload(url, imageView, w, h)) {
        switch (mode) {
        case NO_ASYNC_TASK:
            Bitmap bitmap = downloadBitmap(url, w, h);
            addBitmapToCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
            break;

        case NO_DOWNLOADED_DRAWABLE:
            imageView.setMinimumHeight(96);
            imageView.setMinimumWidth(128);
            task = new BitmapDownloaderTask(imageView, w, h);
            task.execute(url);
            break;

        case CORRECT:
            task = new BitmapDownloaderTask(imageView, w, h);
            DownloadedDrawable downloadedDrawable = null;
            if (tempImage != null) {
                downloadedDrawable = new DownloadedDrawable(task, tempImage);
            } else {
                downloadedDrawable = new DownloadedDrawable(task);
            }

            int minWith = 0;
            int minheight = 0;
            if (params != null) {
                minWith = params.width;
                minheight = params.height;
            }

            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumWidth(minWith);
            imageView.setMinimumHeight(minheight);

            task.execute(url);
            break;
        }
    }

    return task;
}

From source file:com.example.sans.myapplication.Utility.MainActivity.java

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

    shares = getSharedPreferences("SHARES", 0);

    Client client = new Client();

    loc_array = "";

    RequestParams params = new RequestParams();
    params.put("id", shares.getInt("ID", -1));
    params.put("loc_array", loc_array);

    client.addHeader("Authorization", shares.getString("ACCESS_TOKEN", ""));

    client.post("driver/updateLocation", params, new JsonHttpResponseHandler());

    Fragment newFragment = new OrderListFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.frame, newFragment);
    transaction.addToBackStack(null);//  w  w  w.  jav  a 2 s. c  o  m
    transaction.commit();

    LocationRequest lr = LocationRequest.create();
    lr.setInterval(1000);
    lr.setFastestInterval(500);
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    stored_locations = new ArrayList<Location>();

    controller = (AppController) getApplication();

    controller.startGetStatus();
    controller.startAquireOrder();

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    order_num = (TextView) toolbar.findViewById(R.id.order_num);

    order_num.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nav = true;
            Intent i = new Intent(MainActivity.this, MenuItemActivity.class);
            i.putExtra("MENU_ITEM", R.id.timetable);
            MainActivity.this.startActivity(i);
        }
    });

    toolbar.findViewById(R.id.toolbar_calendar).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nav = true;
            Intent i = new Intent(MainActivity.this, MenuItemActivity.class);
            i.putExtra("MENU_ITEM", R.id.timetable);
            MainActivity.this.startActivity(i);
        }
    });
    setSupportActionBar(toolbar);

    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    //Location
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    ll = new myLocationListener();

    startLocationUpdates();

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu

    header = navigationView.inflateHeaderView(R.layout.header);
    JSONObject driver_data = null;
    try {
        driver_data = new JSONObject(shares.getString("DRIVER_DATA", "ERROR"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    TextView driver_name_header = (TextView) header.findViewById(R.id.username);
    TextView car_id_header = (TextView) header.findViewById(R.id.car_id);
    final ImageView pp_header = (ImageView) header.findViewById(R.id.profile_image);

    try {
        car_id_header.setText(driver_data.getString("license"));
        driver_name_header.setText(driver_data.getString("name"));
        client = new Client();
        client.get(driver_data.getString("image"),
                new FileAsyncHttpResponseHandler(MainActivity.this.getApplicationContext()) {
                    @Override
                    public void onFailure(int i, Header[] headers, Throwable throwable, File file) {

                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, final File response) {

                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Bitmap image = BitmapFactory.decodeFile(response.getPath());

                                pp_header.setImageBitmap(image);

                            }
                        });

                    }

                });
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        SharedPreferences.Editor editor = shares.edit();

        final Client client1 = new Client();
        RequestParams params1 = new RequestParams();

        editor.putBoolean("ONLINE", false);
        editor.putBoolean("LOGIN", false);
        editor.commit();
        Intent intent = getApplicationContext().getPackageManager()
                .getLaunchIntentForPackage(getApplicationContext().getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(MainActivity.this, MenuItemActivity.class);
            i.putExtra("MENU_ITEM", 0);
            MainActivity.this.startActivity(i);
            drawerLayout.closeDrawers();
        }
    });

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state

            nav = true;

            menuItem.setChecked(false);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment;
            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
            case R.id.timetable:
            case R.id.service:
            case R.id.history:
            case R.id.mission:
            case R.id.points:
            case R.id.notice:
                Intent i = new Intent(MainActivity.this, MenuItemActivity.class);
                i.putExtra("MENU_ITEM", menuItem.getItemId());
                MainActivity.this.startActivity(i);
                break;
            case R.id.login:
                Toast.makeText(getApplicationContext(), "Logout Selected", Toast.LENGTH_SHORT).show();
                SharedPreferences.Editor editor = shares.edit();

                final Client client = new Client();
                RequestParams params = new RequestParams();
                params.put("id", shares.getInt("ID", 0));
                params.put("status", 999);
                client.post("driver/setStatus", params, new JsonHttpResponseHandler());

                editor.putBoolean("ONLINE", false);
                editor.putBoolean("LOGIN", false);
                editor.commit();
                Intent intent = getApplicationContext().getPackageManager()
                        .getLaunchIntentForPackage(getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                break;

            }
            return false;
        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.openDrawer, R.string.closeDrawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            drawerLayout.setSelected(false);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

}

From source file:com.example.sans.myapplication.Utility.Map.MapsActivity.java

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

    shares = getSharedPreferences("SHARES", 0);

    controller = (AppController) getApplication();

    receiver = new MyReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.test");
    this.registerReceiver(receiver, filter);

    Intent i = getIntent();/*from   w  ww .j  a  va  2 s .com*/

    Bundle bundle = i.getExtras();

    try {
        j = new JSONObject(bundle.getString("data", "ERROR"));
        shares.edit().putString("ORDER", bundle.getString("data", "ERROR")).commit();
    } catch (JSONException e) {
        try {
            j = new JSONObject(shares.getString("ORDER", "ERROR"));
        } catch (JSONException e1) {
            SharedPreferences.Editor editor = shares.edit();
            final Client client = new Client();
            RequestParams params = new RequestParams();
            params.put("id", shares.getInt("ID", 0));
            params.put("status", 999);
            client.post("driver/setStatus", params, new JsonHttpResponseHandler());

            editor.putBoolean("ONLINE", false);
            editor.putBoolean("LOGIN", false);
            editor.commit();
            Intent intent = getApplicationContext().getPackageManager()
                    .getLaunchIntentForPackage(getApplicationContext().getPackageName());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }

    try {
        Log.e("PHONE_NO", String.valueOf(j.getJSONObject("passenger_data")));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    pass_phone = (ImageView) findViewById(R.id.pass_phone);
    pass_phone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent phoneIntent = null;
            try {
                phoneIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("tel:" + j.getJSONObject("passenger_data").getString("tel")));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            startActivity(phoneIntent);
        }
    });
    pass_msg = (ImageView) findViewById(R.id.pass_msg);
    pass_msg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent msgIntent = new Intent(Intent.ACTION_VIEW);

            try {
                msgIntent.setData(Uri.parse("sms:" + j.getJSONObject("passenger_data").getString("tel")));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            startActivity(msgIntent);
        }
    });

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

    setSupportActionBar(toolbar);

    TextView actionBarTitle = (TextView) toolbar.findViewById(R.id.action_bar_title);

    try {
        actionBarTitle.setText(j.getJSONObject("passenger_data").getString("family_name")
                + j.getJSONObject("passenger_data").getString("title"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    setUpMapIfNeeded();

    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    header = navigationView.inflateHeaderView(R.layout.header);
    JSONObject driver_data = null;
    try {
        driver_data = new JSONObject(shares.getString("DRIVER_DATA", "ERROR"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    TextView driver_name_header = (TextView) header.findViewById(R.id.username);
    TextView car_id_header = (TextView) header.findViewById(R.id.car_id);
    final ImageView pp_header = (ImageView) header.findViewById(R.id.profile_image);

    try {
        car_id_header.setText(driver_data.getString("license"));
        driver_name_header.setText(driver_data.getString("name"));
        Client client = new Client();
        client.get(driver_data.getString("image"),
                new FileAsyncHttpResponseHandler(MapsActivity.this.getApplicationContext()) {
                    @Override
                    public void onFailure(int i, Header[] headers, Throwable throwable, File file) {

                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, final File response) {

                        MapsActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Bitmap image = BitmapFactory.decodeFile(response.getPath());

                                pp_header.setImageBitmap(image);

                            }
                        });

                    }

                });
    } catch (JSONException e) {
        e.printStackTrace();
    }

    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(MapsActivity.this, MenuItemActivity.class);
            i.putExtra("MENU_ITEM", 0);
            MapsActivity.this.startActivity(i);
            drawerLayout.closeDrawers();
        }
    });

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state

            nav = true;

            menuItem.setChecked(false);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment;
            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
            case R.id.timetable:
            case R.id.service:
            case R.id.history:
            case R.id.mission:
            case R.id.points:
            case R.id.notice:
                Intent i = new Intent(MapsActivity.this, MenuItemActivity.class);
                i.putExtra("MENU_ITEM", menuItem.getItemId());
                MapsActivity.this.startActivity(i);
                break;
            case R.id.login:
                //Toast.makeText(getApplicationContext(), "Logout Selected", Toast.LENGTH_SHORT).show();
                SharedPreferences.Editor editor = shares.edit();

                final Client client = new Client();
                RequestParams params = new RequestParams();
                params.put("id", shares.getInt("ID", 0));
                params.put("status", 999);
                client.post("driver/setStatus", params, new JsonHttpResponseHandler());

                editor.putBoolean("ONLINE", false);
                editor.putBoolean("LOGIN", false);
                editor.commit();
                Intent intent = getApplicationContext().getPackageManager()
                        .getLaunchIntentForPackage(getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                //Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
                break;

            }
            return false;
        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.openDrawer, R.string.closeDrawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            drawerLayout.setSelected(false);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();
}

From source file:com.example.sans.myapplication.AssignActivity.java

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

    final SharedPreferences shares = getSharedPreferences("SHARES", 0);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//from w ww  .  j  a  v a 2s.c  om

    //requestWindowFeature(Window.FEATURE_NO_TITLE);

    actionBarTitle = (TextView) toolbar.findViewById(R.id.action_bar_title);

    Intent i = getIntent();

    Bundle bundle = i.getExtras();

    try {
        j = new JSONObject(bundle.getString("data", "ERROR"));

    } catch (JSONException e) {
        Toast.makeText(AssignActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    new Thread(new Runnable() {
        @Override
        public void run() {

            for (int i = 30; i > 0; i--) {

                try {
                    Thread.sleep(1000);
                    final int finalI = i;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            actionBarTitle.setText("" + finalI + "");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            Handler mainHandler = new Handler(AssignActivity.this.getMainLooper());

            Runnable myRunnable = new Runnable() {

                @Override
                public void run() {
                    final Client client = new Client();

                    RequestParams params = new RequestParams();

                    client.addHeader("Authorization", shares.getString("ACCESS_TOKEN", "ERROR"));

                    client.post("OrderDistributor/rejectOrder", params, new JsonHttpResponseHandler());
                }
            };

            mainHandler.post(myRunnable);

            AssignActivity.this.finish();

        }
    }).start();

    setUpMapIfNeeded();

    //Initializing NavigationView

    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    header = navigationView.inflateHeaderView(R.layout.header);
    JSONObject driver_data = null;
    try {
        driver_data = new JSONObject(shares.getString("DRIVER_DATA", "ERROR"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    TextView driver_name_header = (TextView) header.findViewById(R.id.username);
    TextView car_id_header = (TextView) header.findViewById(R.id.car_id);
    final ImageView pp_header = (ImageView) header.findViewById(R.id.profile_image);

    try {
        car_id_header.setText(driver_data.getString("license"));
        driver_name_header.setText(driver_data.getString("name"));
        Client client = new Client();
        client.get(driver_data.getString("image"),
                new FileAsyncHttpResponseHandler(AssignActivity.this.getApplicationContext()) {
                    @Override
                    public void onFailure(int i, Header[] headers, Throwable throwable, File file) {

                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, final File response) {

                        AssignActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Bitmap image = BitmapFactory.decodeFile(response.getPath());

                                pp_header.setImageBitmap(image);

                            }
                        });

                    }

                });
    } catch (JSONException e) {
        e.printStackTrace();
    }

    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(AssignActivity.this, MenuItemActivity.class);
            i.putExtra("MENU_ITEM", 0);
            AssignActivity.this.startActivity(i);
            drawerLayout.closeDrawers();
        }
    });

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state

            nav = true;

            menuItem.setChecked(false);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment;
            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
            case R.id.timetable:
            case R.id.service:
            case R.id.history:
            case R.id.mission:
            case R.id.points:
            case R.id.notice:
                Intent i = new Intent(AssignActivity.this, MenuItemActivity.class);
                i.putExtra("MENU_ITEM", menuItem.getItemId());
                AssignActivity.this.startActivity(i);
                break;
            case R.id.login:
                Toast.makeText(getApplicationContext(), "Logout Selected", Toast.LENGTH_SHORT).show();
                SharedPreferences.Editor editor = shares.edit();

                final Client client = new Client();
                RequestParams params = new RequestParams();
                params.put("id", shares.getInt("ID", 0));
                params.put("status", 999);
                client.post("driver/setStatus", params, new JsonHttpResponseHandler());

                editor.putBoolean("ONLINE", false);
                editor.putBoolean("LOGIN", false);
                editor.commit();
                Intent intent = getApplicationContext().getPackageManager()
                        .getLaunchIntentForPackage(getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                break;

            }
            return false;
        }
    });

    reject = (Button) findViewById(R.id.reject);

    reject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Client client = new Client();

            RequestParams params = new RequestParams();

            client.addHeader("Authorization", shares.getString("ACCESS_TOKEN", "ERROR"));

            client.post("OrderDistributor/rejectOrder", params, new JsonHttpResponseHandler());

            AssignActivity.this.finish();
        }
    });

    accept = (Button) findViewById(R.id.accept);

    accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Client client = new Client();

            final RequestParams params = new RequestParams();

            try {
                params.put("orderId", j.getJSONObject("order_data").getDouble("id"));

                client.addHeader("Authorization", shares.getString("ACCESS_TOKEN", "ERROR"));

                client.post("OrderDistributor/takeOrder", params, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable error,
                            JSONObject response) {
                        Log.i("Status Code", "" + statusCode);
                    }
                });

                AssignActivity.this.finish();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.openDrawer, R.string.closeDrawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            drawerLayout.setSelected(false);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();
}