O
O
Oleksiy2015-12-10 19:19:11
Android
Oleksiy, 2015-12-10 19:19:11

Why does the hamburger move to the left instead of turning into an arrow?

The code
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    int mPosition = -1;
    String mTitle = "";

    // Array of strings storing way names
    String[] mWays ;

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] mSimbols = new int[]{
            R.drawable.ml_green,
            R.drawable.ml_violet,
            R.drawable.ml_blue,
            R.drawable.ml_orange,
            R.drawable.ml_pink
    };

    // Array of strings to initial counts
    String[] mCount = new String[]{
            "", "", "", "", "" };

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private LinearLayout mDrawer ;
    private List<HashMap<String,String>> mList ;
    private SimpleAdapter mAdapter;
    final private String WAY = "way";
    final private String SIMBOL = "simbol";
    final private String COUNT = "count";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.ic_launcher);
        // Getting an array of country names
        mWays = getResources().getStringArray(R.array.ways);
        // Title of the activity
        mTitle = (String)getTitle();
        // Getting a reference to the drawer listview
        mDrawerList = (ListView) findViewById(R.id.drawer_list);
        // Getting a reference to the sidebar drawer ( Title + ListView )
        mDrawer = ( LinearLayout) findViewById(R.id.drawer);
        // Each row in the list stores country name, count and flag
        mList = new ArrayList<HashMap<String,String>>();
        for(int i=0;i<5;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put(WAY, mWays[i]);
            hm.put(COUNT, mCount[i]);
            hm.put(SIMBOL, Integer.toString(mSimbols[i]) );
            mList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { SIMBOL,WAY,COUNT };

        // Ids of views in listview_layout
        int[] to = { R.id.simbol , R.id.way , R.id.count};

        // Instantiating an adapter to store each items
        // R.layout.drawer_layout defines the layout of each item
        mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from, to);

        // Getting reference to DrawerLayout
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

        // Creating a ToggleButton for NavigationDrawer with drawer event listener
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ml_3line, R.string.drawer_open,R.string.drawer_close){

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) {
                highlightSelectedWay();
                supportInvalidateOptionsMenu();
            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(R.string.app_name);
                supportInvalidateOptionsMenu();
            }
        };

        // Setting event listener for the drawer
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // ItemClick event handler for the drawer items
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {

                // Increment hit count of the drawer list item
                incrementHitCount(position);

                if(position < 5) { // Show fragment for ways : 0 to 4
                    showFragment(position);
                }else{ // Show message box for ways : 5 to 9
                    Toast.makeText(getApplicationContext(), mWays[position], Toast.LENGTH_LONG).show();
                }

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawer);
            }
        });

        // Enabling Up navigation
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

// Setting the adapter to the listView
        mDrawerList.setAdapter(mAdapter);
        }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    public void incrementHitCount(int position){
        HashMap<String, String> item = mList.get(position);
        String count = item.get(COUNT);
        item.remove(COUNT);
        if(count.equals("")){
            count = " 1 ";
        }else{
            int cnt = Integer.parseInt(count.trim());
            cnt ++;
            count = " " + cnt + " ";
        }
        item.put(COUNT, count);
        mAdapter.notifyDataSetChanged();
    }

    public void showFragment(int position){

        //Currently selected ways
        mTitle = mWays[position];

// Creating a fragment object
        WayFragment cFragment = new WayFragment();

// Creating a Bundle object
        Bundle data = new Bundle();

// Setting the index of the currently selected item of mDrawerList
        data.putInt("position", position);

// Setting the position to the fragment
        cFragment.setArguments(data);

// Getting reference to the FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();

// Creating a fragment transaction
        FragmentTransaction ft = fragmentManager.beginTransaction();

// Adding a fragment to the fragment transaction
        ft.replace(R.id.content_frame, cFragment);

// Committing the transaction
        ft.commit();
    }

    // Highlight the selected country : 0 to 4
    public void highlightSelectedWay(){
        int selectedItem = mDrawerList.getCheckedItemPosition();

        if(selectedItem > 4)
            mDrawerList.setItemChecked(mPosition, true);
        else
            mPosition = selectedItem;

        if(mPosition!=-1)
            getSupportActionBar().setTitle(mWays[mPosition]);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, getString(R.string.action_settings), Toast.LENGTH_LONG).show();
                break;
            case R.id.action_search:
                Toast.makeText(MainActivity.this, getString(R.string.action_search), Toast.LENGTH_LONG).show();
                break;
            case R.id.action_send:
                Toast.makeText(MainActivity.this, getString(R.string.action_send), Toast.LENGTH_LONG).show();
                break;
            case R.id.action_info:
                Toast.makeText(MainActivity.this, getString(R.string.action_info), Toast.LENGTH_LONG).show();
                break;
            case R.id.action_cash:
                Toast.makeText(MainActivity.this, getString(R.string.action_cash), Toast.LENGTH_LONG).show();
                break;
        }

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

        return super.onOptionsItemSelected(item);
    }
}

Screen1
5bd901cf53fe4bf0a6479595470ad4b7.png
Screen2
69ee4f319a7f4189a191f0ce4c130102.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Stolyarov, 2015-12-10
@xox_ua

Use ActionBarDrawerToggle from version 7

O
Oleksiy, 2015-12-10
@xox_ua

Exactly, thanks a lot!
Change version and remove "R.drawable.ml_3line, ".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question