Android – How to load NavigationView menu item icon from URL using Picasso

    // this set prevents collecting targets by garbage collector
    final Set<Target> targets = new HashSet<>();

    private void loadMenuIcon(final MenuItem menuItem, String url) {

        Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);
                menuItem.setIcon(mBitmapDrawable);
            }

            @Override
            public void onBitmapFailed(Drawable drawable) {}

            @Override
            public void onPrepareLoad(Drawable drawable) {}
        };

        targets.add(target);

        Picasso.with(this).load(url).into(target);

    }

Usage:

            Menu menu = navigationView.getMenu();
            MenuItem item = menu.add(0, id, i, name);
            loadMenuIcon(item, url);

Android Studio 2.2

%d1%81%d0%bd%d0%b8%d0%bc%d0%be%d0%ba-%d1%8d%d0%ba%d1%80%d0%b0%d0%bd%d0%b0-2016-09-21-%d0%b2-1-16-51

New version of Android Studio published. Following improvements are made:

– New layout editor
– Constraint layout support
– Sample code search
– Instant Run improvement
– APK analyzer
– build cache
– Virtual sensors in emulator
– Espresso Test recorder
– GPU debugger

Happy Android coding =)

About Salsa

Salsa came from Latin America and USA in 70s.
There are several types of salsa:
– Los Angeles (starts on 1)
– London (starts on 1)
– New York (starts on 2)
– Puerto Rico (starts on 2)
– Palladium (starts on 2)
– Cuban (starts on any)
– Colombian (starts on any)

The important parts of salsa: rhythm, musicality, partners

3 salsa styles:
– Circular: Cuban – can be started at any bit. Accent – percussion instrument.Tap on 4 and 8. Size – 4/4. Fast temp. Rhythm – tumbao and clave. Also called ‘salsa casino’ – based on Ruda de casino.
– Linear: Los Angeles – founded by Luis Vazces. Starts on 1.
– Other: Rueda de casino

PS. This is just note for myself. Source: Wikipedia

Android – How to get tweets JSON from Twitter API?

This code uses this library: Android Asynchronous Http Client

    String url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=manutd";
    AsyncHttpClient client = new AsyncHttpClient();

    private void getTweets() {

        RequestParams requestParams = new RequestParams();
        requestParams.put("grant_type", "client_credentials");
        AsyncHttpClient httpClient = new AsyncHttpClient();
        httpClient.addHeader("Authorization", "Basic " + Base64.encodeToString((CONSUMER_KEY + ":" + CONSUMER_SECRET).getBytes(), Base64.NO_WRAP));
        httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        httpClient.post("https://api.twitter.com/oauth2/token", requestParams, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(responseBody));
                    client.addHeader("Authorization", jsonObject.getString("token_type") + " " + jsonObject.getString("access_token"));
                    client.get(url, handler);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }

        });
    }

    AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            String json = new String(responseBody);
            int length = json.length();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    };

Android – How to avoid runtime exception when implementing home button to navigate back?

I faced problem when I tried to implement home button as back button.

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

That’s why I have removed these two lines, override onOptionsItemSelected method:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

Now it works without runtime exceptions!