Monthly Archives: May 2017

Back Navigation for ActionBar on Android

AndroidManifest.xml

        <activity android:name=".ProfileActivity">
            <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>
        </activity>

ProfileActivity.java

public class ProfileActivity extends AppCompatActivity {

    Toolbar toolbarProfile;

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

        toolbarProfile = (Toolbar) findViewById(R.id.toolbarProfile);
        setSupportActionBar(toolbarProfile);
        ViewCompat.setLayoutDirection(toolbarProfile, ViewCompat.LAYOUT_DIRECTION_RTL);
        getSupportActionBar().setTitle(R.string.profile_management);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back); // for custom indicator
        
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                //Intent intent=new Intent(ProfileActivity.this,MainActivity.class);
                //NavUtils.navigateUpTo(ProfileActivity.this,intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {

        NavUtils.navigateUpFromSameTask(this);

        super.onBackPressed();
    }
}

References
https://developer.android.com/training/implementing-navigation/ancestral.html
https://developer.android.com/training/implementing-navigation/temporal.html
https://stackoverflow.com/questions/28740610/how-do-i-show-back-button-in-activity
https://stackoverflow.com/questions/5312334/how-to-handle-back-button-in-activity

Add Header Layout to NavigationView on Android

activity_main.xml

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/drawer_header">

    </android.support.design.widget.NavigationView>

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Spinner
        android:id="@+id/spinnerProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="-20dp"
        android:gravity="center" />
</RelativeLayout>

MainActivity.java

        View headerView= navigationView.getHeaderView(0);
        spinnerProfile= (Spinner) headerView.findViewById(R.id.spinnerProfile);

References
http://www.technotalkative.com/part-4-playing-with-navigationview/

Add Spinner as an item in Navigation Drawer on Android

Step 1. Please add item in menu.xml

 <item
            android:id="@+id/navigation_drawer_item3"
            android:icon="@android:drawable/ic_menu_share"
            android:title=""
            app:actionLayout="@layout/spinner"/> 

Step 2. Please create layout for spinner view

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical" />

Step 3. set spinner data in your activity file

Spinner spinner = (Spinner) navigationView.getMenu().findItem(R.id.navigation_drawer_item3).getActionView();
    spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,language));
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
          Toast.makeText(MainActivity.this,language[position],Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Step 4. please add android support design library into project if need.

References
https://github.com/mhdr/AndroidSamples/tree/master/092
https://stackoverflow.com/questions/37696653/how-to-add-spinner-as-an-item-in-navigation-drawer
https://stackoverflow.com/questions/34847464/how-to-add-spinner-on-navigationview

Select Menu Item of Navigation Drawer on Android

    NavigationView.OnNavigationItemSelectedListener navigationView_OnNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            // uncheck other menus and sub menus
            int menuSize = navigationView.getMenu().size();

            for (int i = 0; i < menuSize; i++) {
                MenuItem menuItem = navigationView.getMenu().getItem(i);

                if (menuItem.hasSubMenu()) {
                    int submenuSize = menuItem.getSubMenu().size();

                    for (int j = 0; j < submenuSize; j++) {
                        MenuItem subItem = menuItem.getSubMenu().getItem(j);
                        subItem.setChecked(false);
                    }
                } else {
                    menuItem.setChecked(false);
                }
            }

            item.setChecked(true);

            if (item.getItemId() == R.id.itemMenuExit) {

                finish();
                System.exit(0);
                return true;
            }

            return false;
        }
    };

References
https://github.com/mhdr/AndroidSamples/tree/master/091

Right to Left Menu Items of Navigation Drawer on Android

navigationView= (NavigationView) findViewById(R.id.navigationView);
ViewCompat.setLayoutDirection(navigationView,ViewCompat.LAYOUT_DIRECTION_RTL);

Or

<android.support.design.widget.NavigationView
android:layoutDirection="rtl"
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

References
https://stackoverflow.com/questions/38848821/android-right-to-left-navigationdrawer-menu-items-arent-rtl
https://github.com/mhdr/AndroidSamples/tree/master/090