How to achieve this android Discrete SeekBar design? [duplicate] - android

This question already has answers here:
Discrete SeekBar
(2 answers)
Closed 5 years ago.
i achieved tooltip design using this link as reference, now i want to achieved seekbar interval as vertical line and not as dots with numbers as like above the image.

Check this code it may help you
public class DWRulerSeekbar extends RelativeLayout implements SeekBar.OnSeekBarChangeListener {
private int seekbarMinValue = 0;
private int seekbarMaxValue = 50;
private SeekBar seekBar;
private Context context;
private OnDWSeekBarListener listener;
private LayoutParams lp;
public interface OnDWSeekBarListener {
public void onStopSeekbarValue(double value);
}
public DWRulerSeekbar(Context context) {
super(context);
this.context = context;
init();
}
public DWRulerSeekbar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public DWRulerSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
public DWRulerSeekbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
init();
}
private void init() {
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (getHeight() > 0) {
addCompoenet();
getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
}
private void addCompoenet() {
removeAllViews();
LineRulerView lineRulerView = new LineRulerView(context);
LayoutParams rulerLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getHeight() / 3 * 2);
rulerLayoutParams.setMargins(0, 5, 0, 0);
rulerLayoutParams.addRule(CENTER_IN_PARENT);
lineRulerView.setLayoutParams(rulerLayoutParams);
lineRulerView.setMinMaxValue(4, 21);
lineRulerView.setBackgroundColor(Color.TRANSPARENT);
addView(lineRulerView);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int densityDpi = (int)(metrics.density * 160f);
seekBar = new SeekBar(context);
lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
seekBar.setThumb(getResources().getDrawable(R.drawable.green_round_circlebg));
seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar));
seekBar.setProgress(0);
seekBar.setVisibility(View.VISIBLE);
seekBar.setBackgroundColor(Color.TRANSPARENT);
seekBar.setLayoutParams(lp);
seekBar.setMax(15);
seekBar.setOnSeekBarChangeListener(this);
addView(seekBar);
}
public DWRulerSeekbar setDWRulerSeekbarListener(OnDWSeekBarListener listener) {
this.listener = listener;
return this;
}
public DWRulerSeekbar setMinMaxValue(int seekbarMinValue, int seekbarMaxValue) {
this.seekbarMinValue = seekbarMinValue;
this.seekbarMaxValue = seekbarMaxValue;
addCompoenet();
return this;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (listener != null) {
listener.onStopSeekbarValue(seekbarMinValue + progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
and in xml you have to add this
<com.xelpmoc.zommodity.customseekbar.DWRulerSeekbar
android:id="#+id/dwRulerSeekbar"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="75dp">
</com.xelpmoc.zommodity.customseekbar.DWRulerSeekbar>
To get the values from seekbar add this in activity
private void initDWSeekbar() {
seekbar_ruler = (DWRulerSeekbar) findViewById(R.id.dwRulerSeekbar);
seekbar_ruler
.setMinMaxValue((int) MIN_VALUE, (int) MAX_VALUE)
.setDWRulerSeekbarListener(new DWRulerSeekbar.OnDWSeekBarListener() {
#Override
public void onStopSeekbarValue(double value) {
timeslorval=value;
}
});
}
seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:height="5dp" android:gravity="center"
android:id="#android:id/background"
android:drawable="#drawable/seek_bar_background"/>
<item android:height="5dp" android:gravity="center"
android:id="#android:id/progress"
android:drawable="#drawable/seek_bar_progress" />
</layer-list>
and green_round_circlebg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:width="20dp" android:height="20dp">
<shape android:shape="oval" >
<solid android:color="#2CC285"></solid>
<stroke android:color="#000000" android:width="1dp"></stroke>
</shape>
</item>
</layer-list>

Related

How to start an AnimationDrawable from class extending View

I'm making a custom View that displays a spinner to show that the camera is focusing.
public class ApertureAnimation extends View
{
private final static String TAG = "ApertureAnimation";
private AnimationDrawable animationDrawable;
public ApertureAnimation(Context context)
{
super(context);
init(null, 0);
}
public ApertureAnimation(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs, 0);
}
public ApertureAnimation(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle)
{
animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.apperature_focus, null);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int contentWidth = getWidth() - paddingLeft - paddingRight;
int contentHeight = getHeight() - paddingTop - paddingBottom;
if(animationDrawable != null)
{
animationDrawable.setBounds(paddingLeft, paddingTop, paddingLeft + contentWidth, paddingTop + contentHeight);
animationDrawable.start();
animationDrawable.draw(canvas);
}
}
public void start()
{
if(animationDrawable != null)
animationDrawable.start();
}
public void stop()
{
if(animationDrawable != null)
animationDrawable.stop();
}
}
apperature_focus.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/apperature_0" android:duration="200" />
<item android:drawable="#drawable/apperature_30" android:duration="200" />
<item android:drawable="#drawable/apperature_60" android:duration="200" />
</animation-list>
The issue I'm having is that only the first frame shows when I call start(). I am able to get the animation to work if I use an ImageView outside of a class extending View, like this https://developer.android.com/guide/topics/graphics/drawable-animation, but when I try to bring that code into this custom UI component it doesn't work.

Android Studio: Create Object in custom SwipeButton with attributes from XML

public class SwipeButton extends RelativeLayout {
private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
private ViewGroup background;
private Drawable disabledDrawable;
private Drawable enabledDrawable;
private OnStateChangeListener onStateChangeListener;
private OnActiveListener onActiveListener;
private static final int ENABLED = 0;
private static final int DISABLED = 1;
private int collapsedWidth;
private int collapsedHeight;
private LinearLayout layer;
private boolean trailEnabled = false;
private boolean hasActivationState;
public SwipeButton(Context context) {
super(context);
init(context, null, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1, -1);
}
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, -1);
}
#TargetApi(21)
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr, int
defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
public boolean isActive() {
return active;
}
public void setText(String text) {
centerText.setText(text);
}
public void setBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setSlidingButtonBackground(Drawable drawable) {
background.setBackground(drawable);
}
public void setDisabledDrawable(Drawable drawable) {
disabledDrawable = drawable;
if (!active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setButtonBackground(Drawable buttonBackground) {
if (buttonBackground != null) {
swipeButtonInner.setBackground(buttonBackground);
}
}
public void setEnabledDrawable(Drawable drawable) {
enabledDrawable = drawable;
if (active) {
swipeButtonInner.setImageDrawable(drawable);
}
}
public void setOnStateChangeListener(OnStateChangeListener
onStateChangeListener) {
this.onStateChangeListener = onStateChangeListener;
}
public void setOnActiveListener(OnActiveListener onActiveListener) {
this.onActiveListener = onActiveListener;
}
public void setInnerTextPadding(int left, int top, int right, int bottom) {
centerText.setPadding(left, top, right, bottom);
}
public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
swipeButtonInner.setPadding(left, top, right, bottom);
}
public void setHasActivationState(boolean hasActivationState) {
this.hasActivationState = hasActivationState;
}
My XML:`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/AnswerRelativeLayout"
android:layout_height="70dp"
android:orientation="vertical"
tools:context="com.example.dynamicobj.FragmentMain">
<com.example.dynamicobj.SwipeButton
android:id="#+id/test_btn"
android:layout_width="match_parent"
android:layout_height="70dp"
app:button_background="#drawable/shape_button"
app:button_image_disabled="#drawable/ic_launcher_background"
app:button_image_height="60dp"
app:button_image_width="100dp"
app:has_activate_state="true"
app:initial_state="disabled"
app:inner_text="Termine buchen"
app:inner_text_background="#drawable/shape_rounded"
app:inner_text_bottom_padding="18dp"
app:inner_text_right_padding="200dp"
app:inner_text_color="#android:color/black"
app:inner_text_size="16sp"
app:inner_text_top_padding="18dp" />
</LinearLayout>`
F
ragmentMain:
public class FragmentMain extends Fragment {
Context context;
View rootView;
public FragmentMain() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout layout = (LinearLayout)
rootView.findViewById(R.id.AnswerRelativeLayout);
SwipeButton button = new SwipeButton(getContext());
layout.addView(button);
return rootView;
}
}
So I got this custom SwipeButton class from Git (com.ebanx.swipebtn.SwipeButton). Now I want to create an Object from SwipeButton with a layout looking like the one from the xml file. Is there any method, where I just can give the new button the prefinished layout without having to use all these Methods in the SwipeButton class? I am going to create the buttons dynamically later on, but all being the same layout. Help pls?
you forgot to add LayoutParams to your button
use below code before adding button into layout
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
and set your LinearLayout height wrap_content not 70dp

seekbar with preview, like youtube

I need some component or github link to show a seekbar (or progress bar) of a video, and show a thumbnail of the video at the time set (set by the seekbar)
(like youtube)
Thanks a lot!
Ok, I Figured out myself, and I uploaded to:
https://github.com/CorradiSebastian/SeekBarPreview
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sebastiancorradi.seekbar.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coded By Sebastian Corradi SebastianCorradi#gmail.com"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textColor="#AAAAAA"
android:textSize="10dp"
app:layout_constraintTop_toTopOf="parent" />
<com.sebastiancorradi.seekbar.components.SeekBarPreview
android:id="#+id/seekBarPreviewSDCard"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.constraint.ConstraintLayout>
.JAVA:
public class SeekBarPreview extends LinearLayout {
private SeekBar mSeekBar;
private ImageView mPreview;
private View rootView;
private int totalDuration;
private int currentPosition;
private MediaMetadataRetriever retriever;
public SeekBarPreview(Context context) {
super(context);
init();
}
public SeekBarPreview(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public SeekBarPreview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rootView = inflater.inflate(R.layout.seekbarpreview_layout, this, true);
mPreview = rootView.findViewById(R.id.imageView);
mSeekBar = rootView.findViewById(R.id.seekBar);
retriever = new MediaMetadataRetriever();
mPreview.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
public void setUrl(String url){
retriever.setDataSource(url, new HashMap<String, String>());
String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
if (duration != null) {
totalDuration = Integer.valueOf(duration);//milisecs
} else {
totalDuration = 0;
//or throw an exception
}
}
public void setUri(Uri uri){
retriever.setDataSource(getContext(),uri);
String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
if (duration != null) {
totalDuration = Integer.valueOf(duration);//milisecs
} else {
totalDuration = 0;
//or throw an exception
}
}
public void update(int i) {
Long newPosition = totalDuration * i * 10L;
Bitmap bitmap = retriever.getFrameAtTime(newPosition);
mPreview.setImageBitmap(bitmap);
int x = mSeekBar.getThumb().getBounds().centerX();
int newPos = x - (i * mPreview.getWidth() / 100);
mPreview.setX(newPos);
}
public void initialize(){
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
update(i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mSeekBar.setProgress(1);
}
}
And in the parent you just have to add, MXL:
<com.sebastiancorradi.seekbar.components.SeekBarPreview
android:id="#+id/seekBarPreviewSDCard"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
JAVA:
SeekBarPreview seekBarPreviewSDCard = findViewById(R.id.seekBarPreviewSDCard);
String path = "android.resource://" + getPackageName() + "/" + R.raw.bunny;
seekBarPreviewSDCard.setUri(Uri.parse(path));
seekBarPreviewSDCard.initialize();
I think you should use this GitHub library for preview images when you seek SeekBar
https://github.com/rubensousa/PreviewSeekBar
Hope this help you...if you need any help you can ask

ListView item background hell

Because checkbox isnt an option for my project I want the checkable to have a background when checked.Supporting from 2.3 i havent manage to solve this problem yet.
Selection is correct but what i see at screen isnt.Random color at random row..
Fist i have this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/abs__list_selector_holo_light" />
<item android:state_checked="true" android:drawable="#drawable/abs__list_selector_holo_light" />
<item android:state_selected="true" android:drawable="#drawable/abs__list_selector_holo_light" />
<item android:drawable="#drawable/abs__list_selector_holo_light" />
</selector>
-
public class CheckableRelativeLayout extends RelativeLayout implements
Checkable {
private boolean isChecked;
private List<Checkable> checkableViews;
private static final int[] CheckedStateSet = {
R.attr.state_checked
};
public CheckableRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}
public CheckableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}
public CheckableRelativeLayout(Context context, int checkableId) {
super(context);
initialise(null);
}
/*
* #see android.widget.Checkable#isChecked()
*/
public boolean isChecked() {
return isChecked;
}
/*
* #see android.widget.Checkable#setChecked(boolean)
*/
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}
/*
* #see android.widget.Checkable#toggle()
*/
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
#Override
public boolean performClick() {
toggle();
return super.performClick();
}
/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}
/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}
}
What i get is this
Here's a clean example on making a Checkable View:
import android.R;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.LinearLayout;
public class ActivatedLinearLayout extends LinearLayout implements Checkable{
public static final int[] CHECKED_STATE = {R.attr.state_checked};
private boolean mChecked;
public ActivatedLinearLayout(Context context) {
super(context);
}
public ActivatedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ActivatedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setChecked(boolean b) {
mChecked = b;
refreshDrawableState();
}
#Override
public boolean isChecked() {
return mChecked;
}
#Override
public void toggle() {
mChecked = !mChecked;
refreshDrawableState();
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
int[] states = super.onCreateDrawableState(extraSpace + 1);
if (mChecked){
mergeDrawableStates(states, CHECKED_STATE);
}
return states;
}
}
And selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checked"/>
<item android:drawable="#drawable/unchecked"/>
</selector>

HorizontalScrollView can host only one direct child?

I'm basicly going on this site: http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
But i'm having this problem:
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child "
on this line:
addView(internalWrapper);
Here is myHorizontalScrollView class:
public class MyHorizontalScrollView extends HorizontalScrollView {
int scrollToViewPos = 0;
public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyHorizontalScrollView(Context context) {
super(context);
init(context);
}
void init(Context context) {
// remove the fading as the HSV looks better without it
setHorizontalFadingEdgeEnabled(false);
setVerticalFadingEdgeEnabled(false);
}
public void initViews(final MyHorizontalScrollView me, View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
LinearLayout internalWrapper = new LinearLayout(getContext());
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
final int w = me.getMeasuredWidth();
final int h = me.getMeasuredHeight();
sizeCallback.onGlobalLayout();
int[] dims = new int[2];
scrollToViewPos = 0;
for (int i = 0; i < children.length; i++) {
sizeCallback.getViewSize(i, w, h, dims);
internalWrapper.addView(children[i], dims[0], dims[1]);
if (i < scrollToViewIdx) {
scrollToViewPos += dims[0];
}
}
new Handler().post(new Runnable() {
public void run() {
me.scrollBy(scrollToViewPos, 0);
}
});
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// Allow touch events.
return true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Allow touch events.
return true;
}
public interface SizeCallback {
public void onGlobalLayout();
public void getViewSize(int idx, int w, int h, int[] dims);
}
}
MyHorizontalScrollView in xml:
<?xml version="1.0" encoding="utf-8"?>
<com.yahya.LeftSlideMenu.MyHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0px"
android:fadingEdge="none"
android:fadingEdgeLength="0px"
android:padding="0px"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/top"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0px"
android:background="#ffffffff"
android:orientation="horizontal"
android:padding="0px" >
</LinearLayout>
</com.yahya.LeftSlideMenu.MyHorizontalScrollView>
You are missing addView(internalWrapper); that's why is not visible.

Categories

Resources