Last active
July 17, 2020 00:43
-
-
Save emmaguy/81f87b3ceb0721aa509e to your computer and use it in GitHub Desktop.
SeekBarWithIntervals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SeekBarWithIntervals extends LinearLayout { | |
private LinearLayout mLinearLayout = null; | |
private SeekBar mSeekBar = null; | |
private int WidthMeasureSpec = 0; | |
private int HeightMeasureSpec = 0; | |
public SeekBarWithIntervals(Context context, AttributeSet attributeSet) { | |
super(context, attributeSet); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
getActivity().getLayoutInflater().inflate(R.layout.seekbar_with_intervals, this); | |
} | |
private Activity getActivity() { | |
return (Activity) getContext(); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
super.onLayout(changed, l, t, r, b); | |
if (changed) { | |
// We've changed the intervals layout, we need to refresh. | |
mLinearLayout.measure(WidthMeasureSpec, HeightMeasureSpec); | |
mLinearLayout.layout(mLinearLayout.getLeft(), mLinearLayout.getTop(), mLinearLayout.getRight(), mLinearLayout.getBottom()); | |
} | |
} | |
protected synchronized void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { | |
WidthMeasureSpec = widthMeasureSpec; | |
HeightMeasureSpec = heightMeasureSpec; | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
public int getProgress() { | |
return getSeekBar().getProgress(); | |
} | |
public void setProgress(int progress) { | |
getSeekBar().setProgress(progress); | |
} | |
public void setIntervals(List<String> intervals) { | |
displayIntervals(intervals); | |
getSeekBar().setMax(intervals.size() - 1); | |
} | |
private void displayIntervals(List<String> intervals) { | |
if (getLinearLayout().getChildCount() == 0) { | |
for (String interval : intervals) { | |
TextView textViewInterval = createInterval(interval); | |
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | |
param.weight = 1; | |
getLinearLayout().addView(textViewInterval, param); | |
} | |
} | |
} | |
private TextView createInterval(String interval) { | |
View layout = LayoutInflater.from(getContext()).inflate(R.layout.seekbar_with_intervals_labels, null); | |
TextView textView = (TextView) layout.findViewById(R.id.textViewInterval); | |
textView.setText(interval); | |
return textView; | |
} | |
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onSeekBarChangeListener) { | |
getSeekBar().setOnSeekBarChangeListener(onSeekBarChangeListener); | |
} | |
private LinearLayout getLinearLayout() { | |
if (mLinearLayout == null) { | |
mLinearLayout = (LinearLayout) findViewById(R.id.intervals); | |
} | |
return mLinearLayout; | |
} | |
private SeekBar getSeekBar() { | |
if (mSeekBar == null) { | |
mSeekBar = (SeekBar) findViewById(R.id.seekbar); | |
} | |
return mSeekBar; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<LinearLayout | |
android:id="@+id/intervals" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"/> | |
<SeekBar | |
android:id="@+id/seekbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/intervals"/> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/textViewInterval" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment