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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:52:00+00:00 2026-05-17T21:52:00+00:00

I have written a custom preference class which contains a simple progress bar in

  • 0

I have written a custom preference class which contains a simple progress bar in it to allow setting things like volume.
The preference worked great until I tried putting two of these preference in my preference screen.

When I did that the preference screen started to act wierdly » the two custom preferences switch locations constantly as i drag the seek bar.

Any one have any idea why this is happening?
Thanks

EDIT:
here is the code of the custom preference

import android.content.Context; import

android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Typeface; import android.preference.Preference; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView;

public class SeekBarPreference extends Preference implements        OnSeekBarChangeListener {


    private int mMaximum = 60;  private int mInterval = 1;  private int mMinimum = 1;

    private int mDefault = 50;  private TextView monitorBox;

    public SeekBarPreference(Context context)   {       super(context);     }

    public SeekBarPreference(Context context, AttributeSet attrs)   {       super(context, attrs);      initValues(context, attrs);     }

    public SeekBarPreference(Context context, AttributeSet attrs, int defStyle)     {       super(context, attrs, defStyle);        initValues(context, attrs);     }

    private void initValues(Context context, AttributeSet attrs)    {       TypedArray styledAttributes = context.obtainStyledAttributes(attrs,
                R.styleable.SeekBarPreference);         mMinimum = styledAttributes.getInt(
                R.styleable.SeekBarPreference_barMinValue, 1);      mMaximum = styledAttributes.getInt(
                R.styleable.SeekBarPreference_barMaxValue, 1);      mInterval = styledAttributes.getInt(
                R.styleable.SeekBarPreference_barInterval, 1);      mDefault = styledAttributes.getInt(
                R.styleable.SeekBarPreference_defaultValue, mMaximum);  }

    @Override   protected View onCreateView(ViewGroup parent)   {

        LinearLayout horizontalLayout = new LinearLayout(getContext());         horizontalLayout.setPadding(15, 5, 10, 5);      horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout verticalLayout = new LinearLayout(getContext());       verticalLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);        layoutParams.gravity = Gravity.LEFT;        layoutParams.weight = 0.5f;

        LinearLayout.LayoutParams settingTitleLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);        settingTitleLayoutParams.gravity = Gravity.LEFT;        settingTitleLayoutParams.weight =
1.0f;

        LinearLayout.LayoutParams seekbarLayoutParams = new LinearLayout.LayoutParams(
                80, LinearLayout.LayoutParams.WRAP_CONTENT);        seekbarLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;

        LinearLayout.LayoutParams selectedAmountLayoutParams = new LinearLayout.LayoutParams(
                30, LinearLayout.LayoutParams.WRAP_CONTENT);        selectedAmountLayoutParams.gravity = Gravity.CENTER;

        TextView titleTextView = new TextView(getContext());        titleTextView.setText(getTitle());      titleTextView.setTextSize(18);      titleTextView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);      titleTextView.setGravity(Gravity.LEFT);         titleTextView.setLayoutParams(settingTitleLayoutParams);

        TextView summeryTextView = new TextView(getContext());      summeryTextView.setText(getSummary());      summeryTextView.setTextSize(14);        summeryTextView.setTypeface(Typeface.SANS_SERIF);       summeryTextView.setGravity(Gravity.LEFT);       summeryTextView.setLayoutParams(settingTitleLayoutParams);

        SeekBar bar = new SeekBar(getContext());        bar.setMax(mMaximum);           bar.setProgress(mDefault);      bar.setPadding(10, 0, 10, 0);       bar.setLayoutParams(seekbarLayoutParams);       bar.setOnSeekBarChangeListener(this);

        this.monitorBox = new TextView(getContext());       this.monitorBox.setTextSize(12);        this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);       this.monitorBox.setLayoutParams(selectedAmountLayoutParams);        this.monitorBox.setPadding(2, 5, 0, 0);         this.monitorBox.setText(bar.getProgress()
+ "");

        verticalLayout.addView(titleTextView);      verticalLayout.addView(summeryTextView);        horizontalLayout.addView(verticalLayout, layoutParams);         horizontalLayout.addView(bar);      horizontalLayout.addView(this.monitorBox);      horizontalLayout.setId(android.R.id.widget_frame);

        return horizontalLayout;    }

    public void onProgressChanged(SeekBar seekBar, int progress,            boolean fromUser)   {

        progress = Math.round(((float) progress) / mInterval) * mInterval;      if (progress < mMinimum)        {           progress = mMinimum;        }

        if (!callChangeListener(progress))      {           seekBar.setProgress((int) this.mDefault);           return;         }

        seekBar.setProgress(progress);      this.mDefault = progress;       this.monitorBox.setText(progress + "");         updatePreference(progress);

        notifyChanged();    }

    public void onStartTrackingTouch(SeekBar seekBar)   {   }

    public void onStopTrackingTouch(SeekBar seekBar)    {   }

    @Override   protected Object onGetDefaultValue(TypedArray ta, int index)    {

        int dValue = (int) ta.getInt(index, 5);

        return validateValue(dValue);   }

    @Override   protected void onSetInitialValue(boolean restoreValue, Object defaultValue)     {       int temp = restoreValue ? getPersistedInt(5) : (Integer) defaultValue;

        if (!restoreValue)          persistInt(temp);

        this.mDefault = temp;   }

    private int validateValue(int value)    {

        if (value > mMaximum)           value = mMaximum;       else if (value < 0)             value = 0;      else if (value % mInterval != 0)            value = Math.round(((float) value) / mInterval) * mInterval;

        return value;   }

    private void updatePreference(int newValue)     {

        SharedPreferences.Editor editor = getEditor();      editor.putInt(getKey(), newValue);      editor.commit();    }

}

EDIT: I have found that creating a second class with the excat same code but different name an using one of each solves the problem = worst soulution ever. I do not want to do this. anyone have any idea?

  • 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-05-17T21:52:01+00:00Added an answer on May 17, 2026 at 9:52 pm

    Not a real solution but defining your layout as xml will lead to a better and faster program. You might solve your problem on the way. Needless to say that if your hacking something together its not wrong to do it programaticaly. That said…


    You could try taking the elements that flow left to right and placing them in a linear view and setting it horizontally then stacking those elements into a vertical linear view. It seems that floating the views side by side is causing the phone to make the bars jump back and forth.A classic css problem( I am interchanging float and gravity )

    I think that what is happening is when you update the seek bar the phone is updating the view and coming to different calculations for the positions of the floated views. If you lay everything out using vertical and horizontal linear views it should fix you problem and make your layouts much simpler.

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

Sidebar

Related Questions

I currently have a custom session handler class which simply builds on php's session
I have a custom class with a serialize method, and I want to be
I have a class library that I have written in C#.net. I want to
My site is completely custom, as such I like to know when I have
Over a few years, I have have written a few validation classes and have
this must be such a common scenario that there's been a lot written about
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've
I have been puzzling over a problem this morning with LinqToSQL. I'll try and
There is very strange assembly reference problem and loading problem I experience with my
I'm a bit confused about handling an array of objects in C++, as I

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.