ImageView Click

How Can I implement on click on Imageview? here is the code I copy and used:

`using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Views.Accessibility;
using SupportToolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using System.Collections.Generic;
using SupportFragment = Android.Support.V4.App.Fragment;

namespace DrawerLayout_V7_Tutorial
{
//MainLauncher = true,
[Activity (Label = "OnlyTick", Icon = "@drawable/icon", Theme="@style/MyTheme")]
public class MainActivity : ActionBarActivity
{
private SupportToolbar mToolbar;
private MyActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mLeftDrawer;
private ListView mRightDrawer;
private ArrayAdapter mLeftAdapter;
private ArrayAdapter mRightAdapter;
private List mLeftDataSet;
private List mRightDataSet;
private FrameLayout mFragmentContainer;
private SupportFragment mCurrentFragment;
private Fragment1 mFragment1;
private Fragment2 mFragment2;
private Fragment3 mFragment3;
private Stack mStackFragments;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);
        mRightDrawer = FindViewById<ListView>(Resource.Id.right_drawer);
        mFragmentContainer = FindViewById<FrameLayout>(Resource.Id.fragmentContainer);

        mFragment1 = new Fragment1();
        mFragment2 = new Fragment2();
        mFragment3 = new Fragment3();

        mStackFragments = new Stack<SupportFragment>();

        var trans = SupportFragmentManager.BeginTransaction();
        trans.Add(Resource.Id.fragmentContainer, mFragment3, "Search");
        trans.Hide(mFragment3);

        trans.Add(Resource.Id.fragmentContainer, mFragment2, "Featured");
        trans.Hide(mFragment2);

        trans.Add(Resource.Id.fragmentContainer, mFragment1, "All Category");
        trans.Commit();

        mCurrentFragment = mFragment1;

        mLeftDrawer.Tag = 0;
        mRightDrawer.Tag = 1;

        SetSupportActionBar(mToolbar);

        mLeftDataSet = new List<string>();
        mLeftDataSet.Add ("List1");
        mLeftDataSet.Add ("List2");
        mLeftDataSet.Add("List2");
        mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataSet);
        mLeftDrawer.Adapter = mLeftAdapter;

        mRightDataSet = new List<string>();
        mRightDataSet.Add ("Right Item 1");
        mRightDataSet.Add ("Right Item 2");
        mRightAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mRightDataSet);
        mRightDrawer.Adapter = mRightAdapter;

        mDrawerToggle = new MyActionBarDrawerToggle(
            this,                           //Host Activity
            mDrawerLayout,                  //DrawerLayout
            Resource.String.openDrawer,     //Opened Message
            Resource.String.closeDrawer     //Closed Message
        );

        mDrawerLayout.SetDrawerListener(mDrawerToggle);
        SupportActionBar.SetHomeButtonEnabled(true);
        SupportActionBar.SetDisplayShowTitleEnabled(true);
        mDrawerToggle.SyncState();

        if (bundle != null)
        {
            if (bundle.GetString("DrawerState") == "Opened")
            {
                SupportActionBar.SetTitle(Resource.String.openDrawer);
            }

            else
            {
                SupportActionBar.SetTitle(Resource.String.closeDrawer);
            }
        }

        else
        {
            //This is the first the time the activity is ran
            SupportActionBar.SetTitle(Resource.String.closeDrawer);
        }

      //ImageView imgBtnCars = FindViewById<ImageView>(Resource.Id.imgcar);
      //  imgBtnCars.SetOnClickListener (this);// .SetOnClickListener(this);

    }
    //public void OnClick(View V)
    //{
    //    Console.WriteLine("testing");

    //}
    public override bool OnOptionsItemSelected (IMenuItem item)
    {       
        switch (item.ItemId)
        {

        case Android.Resource.Id.Home:
            //The hamburger icon was clicked which means the drawer toggle will handle the event
            //all we need to do is ensure the right drawer is closed so the don't overlap
            mDrawerLayout.CloseDrawer (mRightDrawer);
            mDrawerToggle.OnOptionsItemSelected(item);
            return true;

        case Resource.Id.action_help:
            if (mDrawerLayout.IsDrawerOpen(mRightDrawer))
            {
                //Right Drawer is already open, close it
                mDrawerLayout.CloseDrawer(mRightDrawer);
            }

            else
            {
                //Right Drawer is closed, open it and just in case close left drawer
                mDrawerLayout.OpenDrawer (mRightDrawer);
                mDrawerLayout.CloseDrawer (mLeftDrawer);
            }

            return true;

        case Resource.Id.action_fragment1:              
            ShowFragment(mFragment1);
            return true;

        case Resource.Id.action_fragment2:
            ShowFragment(mFragment2);
            return true;

        case Resource.Id.action_fragment3:
            ShowFragment(mFragment3);
            return true;

        default:
            return base.OnOptionsItemSelected (item);
        }
    }

    private void ShowFragment (SupportFragment fragment)
    {
        if (fragment.IsVisible){
            return;
        }

        var trans = SupportFragmentManager.BeginTransaction();

        trans.SetCustomAnimations(Resource.Animation.slide_in, Resource.Animation.slide_out, Resource.Animation.slide_in, Resource.Animation.slide_out);

        fragment.View.BringToFront();
        mCurrentFragment.View.BringToFront();

        trans.Hide(mCurrentFragment);
        trans.Show(fragment);

        trans.AddToBackStack(null);
        mStackFragments.Push(mCurrentFragment);
        trans.Commit();

        mCurrentFragment = fragment;
    }

    public override void OnBackPressed ()
    {

        if (SupportFragmentManager.BackStackEntryCount > 0)
        {
            SupportFragmentManager.PopBackStack();
            mCurrentFragment = mStackFragments.Pop();
        }

        else
        {
            base.OnBackPressed();
        }               
    }

    public override bool OnCreateOptionsMenu (IMenu menu)
    {
        MenuInflater.Inflate (Resource.Menu.action_menu, menu);
        return base.OnCreateOptionsMenu (menu);
    }

    protected override void OnSaveInstanceState (Bundle outState)
    {
        if (mDrawerLayout.IsDrawerOpen((int)GravityFlags.Left))
        {
            outState.PutString("DrawerState", "Opened");
        }

        else
        {
            outState.PutString("DrawerState", "Closed");
        }

        base.OnSaveInstanceState (outState);
    }

    protected override void OnPostCreate (Bundle savedInstanceState)
    {
        base.OnPostCreate (savedInstanceState);
        mDrawerToggle.SyncState();
    }

    public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
    {
        base.OnConfigurationChanged (newConfig);
        mDrawerToggle.OnConfigurationChanged(newConfig);
    }
}

}

`

Best Answer

Answers

Sign In or Register to comment.