Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:46:41+00:00 2026-06-10T20:46:41+00:00

I’ve been using the common Android Button with both icon (drawableTop) and text. It

  • 0

I’ve been using the common Android Button with both icon (drawableTop) and text. It works really poorly if you want to have a non-standard size button, so I decided to make a custom button with a LinearLayout having the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/ButtonHoloDark"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:gravity="center" 
   android:clickable="true" 
   android:focusable="true"
   android:orientation="vertical" >

   <ImageView
       android:id="@+id/buttonIcon"  
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:duplicateParentState="true" />

   <TextView
       android:id="@+id/buttonText"  
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:duplicateParentState="true"
       android:gravity="center"
       android:textColor="@color/white" />

</LinearLayout>

The layout is used by a custom class:

public class CustomIconButton extends LinearLayout {
    public CustomIconButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttributes(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.custom_icon_button, this, true);
}

...

But when I set an OnClickListener on my button in its parent layout it never gets called. I can only receive clicks if a set the listener to the ImageView and/or TextView. This leads to two possible effects when the button is clicked:

  1. The click is inside the ImageView or the TextView. The click is registered ok, but the buttons state drawable doesn’t change i.e. it doesn’t appear depressed.
  2. The click is inside the “empty area” of the button. The click is not registered, but the state drawable works ok.

Neither of these is feasible. I’ve played around with the following attributes on the LinearLayout or its children, but none really seem to have any effect whether true or false:

  • duplicateParentState
  • clickable
  • focusable

There doesn’t seem to be any reasonable way to get the LinearLayout parent receive clicks instead of its children. I’ve seen some possible solutions overriding dispatchTouchEvent or onInterceptTouchEvent on the custom component itself, but that really seems like a big mess if I have to start analyzing touch events to identify proper clicks.

So OnClickListener on a LinearLayout with children = no go?

EDIT:
This is how I currently insert the buttons into my Fragment’s main layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

...

<com.package.CustomIconButton
        android:id="@+id/someButton"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        mynamespace:buttonIcon="@drawable/someIcon"
        mynamespace:text="@string/str_someText" />

 ...

And the button is bound in the Fragment’s code as follows:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.main, container, false);

    View customButton = v.findViewById(R.id.goButton);     
    customButton.setOnClickListener(onClickListener);

    ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:46:43+00:00Added an answer on June 10, 2026 at 8:46 pm

    Are you sure you set the listener on the correct LinearLayout(as you have two, the parent and the one from the inflated layout). Some time ago I made a similar component and the listener worked with no problems(I don’t know what you have in those styles). The only difference was that I didn’t add a (not needed) LinearLayout, instead I used the merge tag:

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android" >
    
       <ImageView
           android:id="@+id/buttonIcon"  
           android:layout_width="fill_parent"
           android:layout_height="wrap_content" />
    
       <TextView
           android:id="@+id/buttonText"  
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"              
           android:textColor="@color/white" />
    
    </merge>
    

    and set the style on the parent LinearLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.