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 8328275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:23:05+00:00 2026-06-09T01:23:05+00:00

I try to write my own CheckBox using RelativeLayout with TextView and FrameLayout (with

  • 0

I try to write my own CheckBox using RelativeLayout with TextView and FrameLayout (with selector on background) inside.

I have setDuplicateParentStateEnabled(true) for FrameLayout that it take checkable status from parent, but why make RelativeLayout checkable I don’t know.

public class MyCheckbox extends RelativeLayout implements OnClickListener, Checkable
{
private static final int ID_CHECKBOX = -1234411;
private static final int ID_TEXTVIEW = -1234412;
private static final int ID_PARENT = -1234413;

private TextView textView;
private FrameLayout checkBox;
private boolean isChecked;

public MyCheckbox(Context context)
{
    this(context, null);
}

public MyCheckbox(Context context, AttributeSet attrs)
{
    super(context, attrs);

    LayoutParams lp;

    setId(ID_PARENT);
    setOnClickListener(this);

    // checkBox
    checkBox = new FrameLayout(context);
    checkBox.setId(ID_CHECKBOX);
    checkBox.setDuplicateParentStateEnabled(true);

    // textView
    textView = new TextView(context);
    textView.setId(ID_TEXTVIEW);
    textView.setOnClickListener(this);


    boolean checkboxToRight = false;

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaptrixCheckbox);

    for (int i = 0; i < a.getIndexCount(); i++)
    {
        int attr = a.getIndex(i);
        switch (attr)
        {
            case R.styleable.MyCheckbox_checkboxToRight:
                checkboxToRight = a.getBoolean(attr, false);
                break;

            case R.styleable.MyCheckbox_checkMark:
                checkBox.setBackgroundDrawable(a.getDrawable(attr));
                break;

            case R.styleable.MyCheckbox_text:
                textView.setText(a.getText(attr));
                break;
        }
    }
    a.recycle();

    if (checkboxToRight)
    {
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(CENTER_VERTICAL);
        lp.setMargins(margins, 0, margins, 0);
        addView(checkBox, lp);

        lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(CENTER_VERTICAL);
        lp.addRule(LEFT_OF, ID_CHECKBOX);
        addView(textView, lp);
    }
    else
    {
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(CENTER_VERTICAL);
        lp.setMargins(margins, 0, margins, 0);
        addView(checkBox, lp);

        lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(CENTER_VERTICAL);
        lp.addRule(RIGHT_OF, ID_CHECKBOX);
        addView(textView, lp);
    }

    refreshDrawableState();
}

@Override
public void onClick(View v)
{
    int id = v.getId();

    if (id == ID_PARENT || id == ID_TEXTVIEW) toggle();
}

public boolean isChecked()
{
    return isChecked;
}

public void setChecked(boolean chacked)
{
    isChecked = chacked;
    refreshDrawableState();
}

@Override
public void toggle()
{
    isChecked = !isChecked;
    refreshDrawableState();
}
}
  • 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-09T01:23:08+00:00Added an answer on June 9, 2026 at 1:23 am

    I just need override method:

    private static final int[] STATE_CHECKABLE = {android.R.attr.state_checked};
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace)
    {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked) mergeDrawableStates(drawableState, STATE_CHECKABLE);
        return drawableState;
    }
    

    Now all works.

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

Sidebar

Related Questions

try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a macro in clojure that sets up a namespace and
Users on my site have a page they can write their own html to.
i try to write my own control.But my control' html has problem: Look like
Hello i try to write my own htoi method for convertion hexadecimal values to
I am using a modified version of the TaskCloud example to try and read/write
for example, I try to write my own vector , so I just write
I try to write a Facebook app. This app will communicate with a rfid
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}

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.