[android] custom toolbar, navigation drawer, over actionbar, navigation drawer icon badge

navigation drawer는 http://susemi99.kr/3377를 참조하는게 좋다.

새로 나온 걸 쓰는 건 기분 좋은 일이니까 ㅎㅎ

=====================================

 

https://github.com/kanytu/Android-studio-material-template 에서 도움을 받았다.

내가 수정한 소스는 https://github.com/susemi99/OverActionbar-navigation-drawer-sample

 

해야했던 일은

  • 액션바의 제목을 가운데로 이동
  • 내비게이션 아이콘 바꾸기
  • 내비게이션 아이콘에 뱃지 올리기
  • 액션바를 덮는 내비게이션 메뉴
  • 일정하지 않은 내비게이션 화면
  • 화면 높이보다 작은 내비게이션 화면
  • 이렇게 수정한 것을 여러 개의 액티비티에 적용하기

 

 

th_device-2015-05-26-174846

 

th_device-2015-05-26-174907

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar_actionbar"
            layout="@layout/toolbar_default"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>
    </LinearLayout>

    <mint.kr.testdrawer2.ScrimInsetsFrameLayout
        android:id="@+id/scrimInsetsFrameLayout"
        android:layout_width="241dp"
        android:layout_height="400dp"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:insetForeground="#4000">

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="mint.kr.testdrawer2.NavigationDrawerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"/>
    </mint.kr.testdrawer2.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>

 

package mint.kr.testdrawer2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class BaseActivity extends AppCompatActivity
{
  protected ImageButton btnActionBarMenu, btnActionbarBack;
  protected TextView textActionBarMessageCount;
  private NavigationDrawerFragment mNavigationDrawerFragment;
  private ViewGroup container;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    toolbar.setContentInsetsAbsolute(0, 0);
    TextView titleView = (TextView) findViewById(R.id.toolbar_title);
    titleView.setText(getTitle());
    btnActionBarMenu = (ImageButton) findViewById(R.id.btn_actionbar_menu);
    btnActionBarMenu.setVisibility(View.VISIBLE);
    btnActionBarMenu.setOnClickListener(clickListener);
    btnActionbarBack = (ImageButton) findViewById(R.id.btn_actionbar_back);
    btnActionbarBack.setOnClickListener(clickListener);
    textActionBarMessageCount = (TextView) findViewById(R.id.text_actionbar_message_count);
    textActionBarMessageCount.setVisibility(View.VISIBLE);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), toolbar);
    mNavigationDrawerFragment.setNavigationDrawerCallback(navigationDrawerCallbacks);

    container = (ViewGroup) findViewById(R.id.container);
  }

  protected void putContentView(int resId)
  {
    container.addView(getLayoutInflater().inflate(resId, null));
  }


  @Override
  public void onBackPressed()
  {
    if (mNavigationDrawerFragment.isDrawerOpen())
    {
      mNavigationDrawerFragment.closeDrawer();
    }
    else
    {
      super.onBackPressed();
    }
  }

  ////////////////////////////////////////
  // listener
  ///////////////////////////////////////
  private View.OnClickListener clickListener = new View.OnClickListener()
  {
    @Override
    public void onClick(View view)
    {
      if (view.getId() == btnActionBarMenu.getId())
      {
        mNavigationDrawerFragment.openDrawer();
      }
    }
  };

  private NavigationDrawerFragment.NavigationDrawerCallbacks navigationDrawerCallbacks = new NavigationDrawerFragment.NavigationDrawerCallbacks()
  {
    @Override
    public void onNavigationDrawerItemSelected(int position)
    {
      Toast.makeText(BaseActivity.this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
      if (position == 0)
      {
        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(intent);
      }
      else if (position == 1)
      {
        Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
        startActivity(intent);
      }
    }
  };
}

 

package mint.kr.testdrawer2;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationDrawerFragment extends Fragment
{
  private NavigationDrawerCallbacks mCallbacks;
  private ActionBarDrawerToggle mActionBarDrawerToggle;

  private DrawerLayout mDrawerLayout;
  private View mFragmentContainerView;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    view.findViewById(R.id.text_home).setOnClickListener(clickListener);
    view.findViewById(R.id.text_message).setOnClickListener(clickListener);
    return view;
  }

  public boolean isDrawerOpen()
  {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
  }

  /**
   * Users of this fragment must call this method to set up the navigation drawer interactions.
   *
   * @param fragmentId   The android:id of this fragment in its activity's layout.
   * @param drawerLayout The DrawerLayout containing this fragment's UI.
   * @param toolbar      The Toolbar of the activity.
   */
  public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar)
  {
    mFragmentContainerView = (View) getActivity().findViewById(fragmentId).getParent();
    mDrawerLayout = drawerLayout;

    mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
    mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
    mDrawerLayout.post(new Runnable()
    {
      @Override
      public void run()
      {
        mActionBarDrawerToggle.syncState();
      }
    });
    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
  }

  private void selectItem(int position)
  {
    if (mDrawerLayout != null)
    {
      mDrawerLayout.closeDrawer(mFragmentContainerView);
    }

    if (mCallbacks != null)
    {
      mCallbacks.onNavigationDrawerItemSelected(position);
    }
  }

  public void closeDrawer()
  {
    mDrawerLayout.closeDrawer(mFragmentContainerView);
  }

  public void openDrawer()
  {
    mDrawerLayout.openDrawer(mFragmentContainerView);
  }

  public void setNavigationDrawerCallback(NavigationDrawerCallbacks callback)
  {
    mCallbacks = callback;
  }

  ////////////////////////////////////////
  // listener
  ///////////////////////////////////////
  private View.OnClickListener clickListener = new View.OnClickListener()
  {
    @Override
    public void onClick(View view)
    {
      if (view.getId() == R.id.text_home)
      {
        selectItem(0);
      }
      else if (view.getId() == R.id.text_message)
      {
        selectItem(1);
      }
    }
  };

  ////////////////////////////////////////
  // interface
  ///////////////////////////////////////
  public interface NavigationDrawerCallbacks
  {
    void onNavigationDrawerItemSelected(int position);
  }
}

 

package mint.kr.testdrawer2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class SecondActivity extends BaseActivity
{

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

    TextView text1 = (TextView) findViewById(R.id.text_hello);
    text1.setText("aaaa");

    Button btn = (Button) findViewById(R.id.button);
    btn.setText("aaa");
    btn.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        Log.i("SecondActivity | onClick", "|" + "==================" + "|");
      }
    });
  }

}