Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get onClickListener() event on custom actionbar
    primarykey
    data
    text
    <p>I'm developing an application in which I have to get <code>onClick()</code> event on click of actionbar custom view. So far I'm able to achieve the following layout.</p> <p><img src="https://i.stack.imgur.com/hd8g5.png" alt="Custom Action Bar"></p> <p>Here is my code for achieving this:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); getActionBar().setCustomView(R.layout.custom_image_button); getActionBar().setDisplayOptions( ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Toast.makeText(getApplicationContext(), "Clicked on ActionBar", Toast.LENGTH_SHORT).show(); default: return super.onOptionsItemSelected(item); } } </code></pre> <p>Here is my custom_image_button layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;FrameLayout android:id="@+id/frame_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" &gt; &lt;TextView android:id="@+id/points" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:background="@drawable/points_yellow" android:gravity="center" android:paddingLeft="20dp" android:textColor="#887141" android:textIsSelectable="false" android:textSize="22sp" android:textStyle="bold" &gt; &lt;/TextView&gt; &lt;ImageView android:id="@+id/badge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|right" android:layout_marginRight="5dp" android:layout_marginTop="0dp" android:src="@drawable/badge_notification" &gt; &lt;/ImageView&gt; &lt;/FrameLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I was trying to have a click listener on the custom layout. For that I have tried the following code:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); getActionBar().setCustomView(R.layout.custom_image_button); getActionBar().setDisplayOptions( ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM); final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.custom_image_button, null); frameLayout = (FrameLayout) v.findViewById(R.id.frame_layout); frameLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Toast.makeText(getApplicationContext(), "Clicked on 1", Toast.LENGTH_SHORT).show(); return false; } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Toast.makeText(getApplicationContext(), "Clicked on ActionBar", Toast.LENGTH_SHORT).show(); default: return super.onOptionsItemSelected(item); } } </code></pre> <p>}</p> <p>But, I'm unable to get <code>onClick()</code> event on the custom image. What I'm doing wrong here, please guide.</p> <p>Any kind of help will be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload