SeekBar.java has error - java

I have this SeekBar.java :
package android.widget;
import android.content.Context;
import android.util.AttributeSet;
public class SeekBar extends AbsSeekBar {
public interface OnSeekBarChangeListener {
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
void onStartTrackingTouch(SeekBar seekBar);
void onStopTrackingTouch(SeekBar seekBar);
private OnSeekBarChangeListener mOnSeekBarChangeListener;
public SeekBar(Context context) {
this(context, null);
}
public SeekBar(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.seekBarStyle);
}
public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
void onProgressRefresh(float scale, boolean fromUser, int progress) {
super.onProgressRefresh(scale, fromUser, progress);
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser);
}
}
public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
mOnSeekBarChangeListener = l;
}
#Override
void onStartTrackingTouch() {
super.onStartTrackingTouch();
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStartTrackingTouch(this);
}
}
#Override
void onStopTrackingTouch() {
super.onStopTrackingTouch();
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStopTrackingTouch(this);
}
}
#Override
public CharSequence getAccessibilityClassName() {
return SeekBar.class.getName();
}
}
The Override lines in onProgressRefresh, onStartTrackingTouch, onStopTrackingTouch give this error :
Method does not override from its super class.
And this problem causes another error in my code, how can I fix this? Thanks.

SeekBar extends AbsSeekBar
AbsSeekBar doesn't have a method named onProgressRefresh yet you are using the override annotation.
The onProgressRefresh is in OnSeekBarChangeListener interface.
I am guessing you forgot to write implents.
SeekBar extends AbsSeekBar implements OnSeekBarChangeListener

That error means that its super class does not have those methods(onProgressRefresh, onStartTrackingTouch, onStopTrackingTouch). If you want to override those methods, you shall check the super class. Or you just remove #Override

Related

Cannot override protected method in superclass

I'd like to override two methods of TextView (of which AppCompatEditText is a subclass) with the following signatures:
protected void setSpan_internal(Object span, int start, int end, int flags) {
((Editable) mText).setSpan(span, start, end, flags);
}
protected void setCursorPosition_internal(int start, int end) {
Selection.setSelection(((Editable) mText), start, end);
}
No matter what I do, they don't appear like overridable methods in the CTRL+O dialog in Android Studio, and if I do manually the IDE linter complains that "Method is never used".
That is my code:
public class MaxLengthEditText extends AppCompatEditText {
public MaxLengthEditText(Context context) {
super(context);
}
public MaxLengthEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MaxLengthEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
protected void setSpan_internal(Object span, int start, int end, int flags) {
final int textLength = getText().length();
getText().setSpan(span, start, Math.min(end, textLength), flags);
}
protected void setCursorPosition_internal(int start, int end) {
final int textLength = getText().length();
Selection.setSelection(getText(), Math.min(start, textLength), Math.min(end, textLength));
}
}
What am I doing wrong here?

Android Extended Button detect onClick

I have extended the Button class and not implemented onClick in extended button. And I am using the extended button every where and setting the onClick listener in each activity.
How can I detect the button is clicked in the Extended class.
public class ButtonExtended extends Button {
public ButtonExtended(Context context) {
super(context);
}
public ButtonExtended(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
//onClick(){ here i want to detect the click }
}
In this ButtonExtended class, I want to detect, if the button is clicked just for the logging purpose. How can I achieve this?
You can override performClick() behavior to get the clicked state.
public class ButtonExtended extends AppCompatButton{
public ButtonExtended(Context context) {
super(context);
}
public ButtonExtended(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean performClick() {
Log.d("Button","performClick");
return super.performClick();
}
}
performClick() will get called first before the the listener. Do not omit return super.performClick() in method performClick() this consume the event and your listener will not get called.

Android view onClick event not passed to self

I'm extending the EditText class in android to incorporate additional functionality one of which is to display a dialog when clicked. I want the behaviour to be portable and hence self contained.
However setting onClickListener to itself (this) as parameter has no effect and the function onClick(View) is never called.
public class TimePickerEditText extends EditText implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
private Calendar today;
private TimePickerDialog timePickerDialog;
public TimePickerEditText(Context context) {
super(context);
postInstantiateSetup();
}
public TimePickerEditText(Context context, AttributeSet attrs) {
super(context, attrs);
postInstantiateSetup();
}
public TimePickerEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
postInstantiateSetup();
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
postInstantiateSetup();
}
public void postInstantiateSetup()
{
setOnClickListener(this);
today = Calendar.getInstance();
onTimeSet(null,today.get(Calendar.HOUR_OF_DAY),today.get(Calendar.MINUTE));
}
#Override
public void onClick(View view) {
if(timePickerDialog == null) {
timePickerDialog = new TimePickerDialog(getContext(), this, 20, 0, true);
}
timePickerDialog.show();
}
#Override
public void onTimeSet(TimePicker timePicker, int hours, int minutes) {
String hoursString = ""+hours;
if(hours<10)
hoursString="0"+hoursString;
String minutesString = ""+minutes;
if(minutes<10)
minutesString="0"+minutesString;
this.setText(hoursString+":"+minutesString);
}
}

Implement interface using custom ScrollView class - Android

I need to know when the user scroll up or down. I managed to achieve that but now I am stuck getting the result back to my main class where I need it. Specifically I don't know how to pass the results to an interface I created.
Here is the error I get:
Attempt to invoke interface method 'void
com.app.android.interfaces.ScrollDirection.Down(int)' on a
null object reference
And here is my custom ScrollView:
public class CustomScrollView extends ScrollView {
private ScrollDirection scrolldirection;
public CustomScrollView(Context context) {
super(context);
scrolldirection = (ScrollDirection) context;
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onScrollChanged(int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
super.onScrollChanged(scrollX, scrollY, oldScrollX, oldScrollY);
if(scrollY<oldScrollY){
scrolldirection.Down(1);
}else{
scrolldirection.Down(-1);
}
}
public interface ScrollDirection{
public void Down(int direction);
}
}
you need to add this line scrolldirection = (ScrollDirection) context; inside every constructor
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
scrolldirection = (ScrollDirection) context;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
scrolldirection = (ScrollDirection) context;
}
To allow Android Studio to interact with your view, at a minimum you must provide a constructor that takes a Context and an AttributeSet object as parameters
Docs link
Update : The recent issue was the implementation of CustomScrollView inside Fragment but Fragment do not have their context. To implement this ,make parent Activity implements the ScrollDirection and make some function in Fragment and call them from Activity's Down function.

Multiple instances of a view object within an android activity

I have two custom view objects that are created within an activity like so.
public class Statistics extends Activity {
GraphWindow graph1;
GraphWindow graph2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
graph1 = (GraphWindow) findViewById(R.id.graph1);
graph2 = (GraphWindow) findViewById(R.id.graph2);
...
}
However they seem to be acting as one instance, so a public method to graph1 will also be executed on graph 2. Do I need to initiate each graph view as a new instance somehow? Where would I do this?
EDIT
Here is the (condensed) GraphWindow Class:
public class GraphWindow extends View {
//draw data
public ArrayList<DataPoint> data = new ArrayList<DataPoint>();
//set height
public int graphHeight = 0;
public int indexStart = 0;
public int indexFinish = 0;
public boolean isTouched = false;
public boolean isDraggable = false;
public GraphWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public GraphWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GraphWindow(Context context) {
super(context);
}
public void setGraphHeight(int graphHeight) {
this.graphHeight = graphHeight;
}
public void isDraggable(boolean isDraggable) {
this.isDraggable = isDraggable;
}
public void panBox(MotionEvent event) {
rectX = (int)event.getX();
rectW = this.getWidth()/5 + rectX;
this.postInvalidate();
}
public void clearData() {
this.data.clear();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
}
}
In particular the clear data method will operate on both graph1 and graph2.

Categories