Search code examples
androidandroid-actionbarandroid-actionbar-compatandroid-actionbaractivityandroid-actionmode

How to make click on ActionBar Logo in android


I tried to do something on Click on icon in ActionBar but do nothing.I also like to do open drawer_layout on click on a icon in action bar. How to make it clickable and handle the event of it`s pressing?

public class MainActivity extends AppCompatActivity {
ViewPager viewPager,viewPager01;
CustomSwipAdapter swip_adapter;
CustomSwipAdapter01 swip_adapter01;

String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        getSupportActionBar().setLogo(R.drawable.sample_01);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("");
    }
    catch (Exception e) {
    }
    viewPager = (ViewPager)findViewById(R.id.viewpager);
    swip_adapter =new CustomSwipAdapter(this);
    viewPager.setAdapter(swip_adapter);

/*    viewPager01 = (ViewPager)findViewById(R.id.viewpager01);
    swip_adapter01 =new CustomSwipAdapter01(this);
    viewPager01.setAdapter(swip_adapter01);*/

    menu = new String[]{"Home","E-Gift Voucher"};
    dLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    dList = (ListView)findViewById(R.id.left_drawer);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);

    dList.setAdapter(adapter);
    dList.setSelector(android.R.color.holo_blue_dark);

    dList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dLayout.closeDrawers();
            Bundle args = new Bundle();
            args.putString("Menu", menu[position]);
            Fragment detail = new DetailFragment();
            detail.setArguments(args);
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
        }
    });
}
@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);
    MenuInflater inf = getMenuInflater();
    inf.inflate(R.menu.main_activity_action,menu);
    return super.onCreateOptionsMenu(menu);*/

    MenuInflater inf = getMenuInflater();
    inf.inflate(R.menu.main_activity_action,menu);
    return true;//super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // 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 (item.getItemId()){
         case android.R.id.home:
             Toast.makeText(MainActivity.this,"Click on Logo",Toast.LENGTH_SHORT).show();
             return  true;
         default:
             return super.onOptionsItemSelected(item);
     }
    }
}

Solution

  • layout xml

    <RelativeLayout
        android:id="@+id/rl_main_search_layout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent_semi"
        tools:context="com.worldofmoms.views.fragments.search_and_explore.SearchFragment">
    
        <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar_search"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="?attr/colorPrimary"
              android:minHeight="?attr/actionBarSize"
              app:theme="@style/ActionBar_Light"/>
    
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar_search"
            android:background="@android:color/white"
            android:visibility="gone"
            />
    
    
    </RelativeLayout>
    

    Add in your activity onCreate()

           mToolbar = (Toolbar) view.findViewById(R.id.toolbar_search);
           setSupportActionBar(mToolbar);
           getSupportActionBar().setDisplayShowHomeEnabled(true);
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    and Add

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();
          if(id==android.R.id.home){
            // home button from toolbar clicked
            }
        }