I am trying to build an app which will need vertical sliders so I am buidling a custom SeekBar. I have managed to get a seekbar on screen which is correctly shown vertically but I can't find how to set the height dynamically so that it shows properly- at the moment it is sort of squished into one row of the Linear Layout.
I am very familiar with C# but Android is new to me so this may well be a basic error but I am getting so frustrated!
Here is my custom class:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Lightslides001
{
public class BCSeekBar : SeekBar
{
protected BCSeekBar(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public BCSeekBar(Context context)
: base(context)
{
}
public BCSeekBar(Context context, IAttributeSet attrs)
: base(context, attrs)
{
}
public BCSeekBar(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
{
}
private int _min = 0;
public int Min { get { return _min; } set { _min = value; } }
public override int Progress
{
get
{
return base.Progress + _min;
}
set
{
base.Progress = value;
}
}
public override int Max
{
get
{
return base.Max + _min;
}
set
{
base.Max = value + _min;
}
}
public override void Draw(Android.Graphics.Canvas canvas)
{
canvas.Rotate(90);
canvas.Translate(0, -this.Width);
base.OnDraw(canvas);
}
public object GetInputData()
{
return (Progress + _min).ToString(CultureInfo.InvariantCulture);
}
protected void Measure(int widthMeasureSpec, int heightMeasureSpec)
{
base.Measure(heightMeasureSpec, widthMeasureSpec);
SetMeasuredDimension(MeasuredHeight, MeasuredWidth);
}
public void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(h, w, oldh, oldw);
}
public bool OnTouchEvent(MotionEvent evt)
{
if (!Enabled)
return false;
switch (evt.Action)
{
case MotionEventActions.Down:
case MotionEventActions.Move:
case MotionEventActions.Up:
{
Progress = ((int)(Max * evt.YPrecision / Height));
OnSizeChanged(Width, Height, 0, 0);
}
break;
case MotionEventActions.Cancel:
break;
}
return true;
}
}
}
And here is my activity:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.PM;
namespace Lightslides001
{
[Activity(Label = "Lightslides001",
MainLauncher = true,
Icon = "@drawable/icon",
ScreenOrientation=ScreenOrientation.Landscape)]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
var seekbar = new BCSeekBar(this);
seekbar.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.FillParent);
var ll = FindViewById<LinearLayout>(Resource.Id.layout1);
ll.AddView(seekbar);
}
}
}
I really hope someone can help, thanks!