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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:16:11+00:00 2026-06-11T04:16:11+00:00

I would like to create a widget with the following layout: <LinearLayout orientation=horizontal> <CheckBox

  • 0

I would like to create a widget with the following layout:

<LinearLayout orientation="horizontal">
<CheckBox />
<EditText />
<SeekBar />
</LinearLayout>

When the seekbar gets moved it should check the checkbox and update the editbox with the current seekbar value.

I am using something similar over and over again in an application and getting tired of re-implementing the same thing.

What is the best way to encapsulate all this into its own class for reuse?


public class SeekChoice extends LinearLayout {



    public SeekChoice(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        LinearLayout ll = (LinearLayout) inflater.inflate(R.id.widget_item, null);
        this.addView(ll);
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:id="@+id/widget_item"
    >


    <CheckBox
        android:id="@+id/checkBox_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item Name" />



    <EditText
        android:id="@+id/editText_item_count"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:ems="10" >

    </EditText>


    <SeekBar
        android:id="@+id/seekBar_item_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

Then in my layout I put:

<com.example.lobsternav.widget.SeekChoice
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>    

The exception is as follows:

09-09 20:23:08.759: E/AndroidRuntime(21888): FATAL EXCEPTION: main
09-09 20:23:08.759: E/AndroidRuntime(21888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lobsternav/com.example.lobsternav.MarkActivity}: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.os.Looper.loop(Looper.java:137)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.reflect.Method.invokeNative(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.reflect.Method.invoke(Method.java:511)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at dalvik.system.NativeStart.main(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createView(LayoutInflater.java:596)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Activity.setContentView(Activity.java:1867)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.example.lobsternav.MarkActivity.onCreate(MarkActivity.java:85)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Activity.performCreate(Activity.java:5008)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 20:23:08.759: E/AndroidRuntime(21888):    ... 11 more
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.Class.getConstructor(Class.java:431)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createView(LayoutInflater.java:561)
09-09 20:23:08.759: E/AndroidRuntime(21888):    ... 24 more

craziness… It throws an exception if I dont have the following constructor defined:

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


}

I guess all the constructors from LinearLayout need to be defined and need to call the super constructor?

  • 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-11T04:16:13+00:00Added an answer on June 11, 2026 at 4:16 am

    Finally have this working.

    My Java Code:

    package com.example.lobsternav.widget;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.example.lobsternav.R;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.SeekBar;
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    public  class SeekChoice extends LinearLayout implements OnSeekBarChangeListener, OnCheckedChangeListener{
    
        CheckBox checkbox = null;
        EditText editBox =null;
        SeekBar seekbar = null;
    
        boolean isChecked = false;
        int curValue = 0;
        List <String> mappings = new ArrayList<String>();
        int startRange = 0;
        int endRange = 50;
        int defaultValue = 0;
    
        private void init(Context context)
        {
            this.setOrientation(LinearLayout.HORIZONTAL);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    
            LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.item_widget, null);
            layout.setOrientation(LinearLayout.HORIZONTAL);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    
    
            checkbox = (CheckBox)layout.findViewById(R.id.checkBox_item_name);
            checkbox.setOnCheckedChangeListener(this);
            //this.addView(checkbox);
    
            editBox = (EditText)layout.findViewById(R.id.editText_item_count);
            editBox.setEnabled(false);
            editBox.setText(getMappedValue(defaultValue));
    
            int ems = (new Integer(endRange)).toString().length()+1;
            editBox.setEms(ems);
            //this.addView(editBox);
    
            seekbar = (SeekBar)layout.findViewById(R.id.seekBar_item_count);
            seekbar.setMax(endRange);
            seekbar.setProgress(defaultValue);
            //seekbar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            seekbar.setOnSeekBarChangeListener(this);
            //this.addView(seekbar);    
            this.addView(layout);
    
        }
    
    
        public void readAttributes(Context context, AttributeSet attrs)
        {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekChoice);
            this.startRange = a.getInteger(R.styleable.SeekChoice_startRange, 0);
            this.endRange = a.getInteger(R.styleable.SeekChoice_endRange, 100);
            this.defaultValue = a.getInteger(R.styleable.SeekChoice_defaultValue, 0);
            this.isChecked = a.getBoolean(R.styleable.SeekChoice_isChecked, false);
            a.recycle();
        }
    
        public SeekChoice(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            readAttributes( context,  attrs);
            init(context);
    
        }
    
        public SeekChoice(Context context, AttributeSet attrs) {
            super(context, attrs);
            readAttributes( context,  attrs);
            init(context);
    
        }
        public SeekChoice(Context context) {
            super(context);
            init(context);
        }
    
        public SeekChoice(Context context,int startRange, int endRange, int defaultValue) {
            super(context);
            init(context);
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
        {
            this.editBox.setText("" + getMappedValue(progress));
    
            if (progress > 0)
            {
                this.checkbox.setChecked(true);
                isChecked = true;
            }
    
        }
    
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
    
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
    
    
    
    
        public int getStartRange() {
            return startRange;
        }
    
    
        public void setStartRange(int startRange) {
            this.startRange = startRange;
        }
    
    
        public int getEndRange() {
            return endRange;
        }
    
    
        public void setEndRange(int endRange) {
            this.endRange = endRange;
        }
    
    
        public int getDefaultValue() {
            return defaultValue;
        }
    
    
        public void setDefaultValue(int defaultValue) {
            this.defaultValue = defaultValue;
        }
    
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
    
            this.isChecked = isChecked;
    
            if (isChecked == false)
            {
                this.seekbar.setProgress(0);
            }
    
            this.editBox.setText("" + getMappedValue(0));
        }
    
    
    
        public String getMappedValue(int current)
        {
            if (current < this.getMappings().size())
            {
                return this.getMappings().get(current);
            }
    
    
    
            return "" + current;
    
    
        }
    
        public List<String> getMappings() {
            return mappings;
        }
    
    
        public void setMappings(List<String> mappings) {
            this.mappings = mappings;
        }
    
        public void addMappings(String mapping) {
            this.mappings.add(mapping);
        }
        public boolean isChecked() {
            return isChecked;
        }
    
    
        public String getCurrentValue()
        {
            return this.editBox.getText().toString();
        }
        public void setChecked(boolean isChecked) {
            this.isChecked = isChecked;
        }
    
    
    }
    

    The layout I inflate in the constructor:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:id="@layout/item_widget">
    
       <CheckBox
            android:id="@+id/checkBox_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Item Name" />
    
    
    
        <EditText
            android:id="@+id/editText_item_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:ems="10" >
    
        </EditText>
    
    
        <SeekBar
            android:id="@+id/seekBar_item_count"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
    

    /res/values/attr.xml (for passing custom attributes to the widget):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
           <declare-styleable name="SeekChoice">
                <attr name="startRange" format="integer"/>
                <attr name="endRange" format="integer"/>
                <attr name="defaultValue" format="integer"/>                
                <attr name="isChecked" format="boolean"/>       
    
    
    
    
           </declare-styleable>
    
    </resources> 
    

    The Layout file where I include the widget:

        <RelativeLayout android:layout_width="fill_parent"
            android:layout_height="match_parent" 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:custom="http://schemas.android.com/apk/res/com.example.lobsternav">
    .....
    .....
    
    <com.example.lobsternav.widget.SeekChoice
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    custom:defaultValue="10"
    />
    
    .....
    .....
    
    </RelativeLayout>
    

    Things that wasted my time so Ill share with everyone:

    1). Make sure to add all the constructors of the class you are overriding

    2). The namespace (in my example it is “xmlns:custom=”http://schemas.android.com/apk/res/com.example.lobsternav”) must be the package name where your R resides and not the package where the widget resides.

    3). in the attr.xml file the tag declare-styleable does not autocomplete in the eclipse xml editor so it does not look like a valid tag – but IT IS. I read 2 other posts saying to use this but the android eclipse plug in was not showing this as a valid tag under resources so I almost didnt try using the tag.

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

Sidebar

Related Questions

I would like to create a widget so that my visitors can display it
I would like to create a widget for use in Django Forms. There is
I would like to create WP sidebar widget similar to this one : As
I would like to create a seekbar for a Mono for Android app that
I would like to create a pending intent for my app widget view that
I would like to create a ListView widget which allowed scrolling horizontally within a
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a json object to send as a post array,

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.