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?
Related
I have trying to make custom layout manager for my recyclerview in android. by the name CustomLinearLayoutManager. and trying to calling constructor and class of this into my home fragment. but failed. and getting some errors.
CustomLinearLayoutManager class
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager (Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
private static final float MILLISECONDS_PER_INCH = 200f;
#Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return CustomLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
#Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
Home Fragment
CustomLinearLayoutManager customLinearLayoutManager = new CustomLinearLayoutManager(getContext());
customLinearLayoutManager.smoothScrollToPosition();
recyclerViewHeaderSlider = view.findViewById(R.id.bannerSlider);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerViewHeaderSlider);
recyclerViewHeaderSlider.setHasFixedSize(true);
recyclerViewHeaderSlider.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
headerSliderAdapter.setOnClick(this);
recyclerViewHeaderSlider.setAdapter(headerSliderAdapter);
Errors which i am facing..
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
Error: Constructor 'CustomLinearLayoutManager(android.content.Context, int, boolean)' is never used
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
Error: Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used
In Home Fragment:
customLinearLayoutManager.smoothScrollToPosition();
Error: smoothScrollToPosition() in CustomLinearLayoutManager cannot be
applied to:
Expected Parameters: Actual Arguments:
recyclerView: RecyclerView
state: State
position: int
Firs, if you defined those constructors and you are not using them then this:
Error: Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used
This is not an error in all. This is an warning since it is never used.
Second, if you call a method, please make sure that the parameters are added properly else then this message is displayed.
Error: smoothScrollToPosition() in CustomLinearLayoutManager cannot be applied to: Expected Parameters: Actual Arguments:
Then you should call this method smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) by adding:
First parameter : an object as RecyclerView
Second parameter: an enum as RecyclerView.State
Third parameter : a position as integer
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);
}
}
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.
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
I cannot access the method marked****(setScrollViewListener) from class DetectHere .even at outside the class (ie from the method onCreate) DetectHere the method setScrollViewListener is valid..here i have created a class SSScrollView to know when the scroll bar reaches the end of scroll..kindly help.
package com.example.mee.layoutcreate;
import ...
public class MainActivity extends ActionBarActivity {
public interface ScrollViewListener {
void onScrollChanged(SSScrollView scrollView, int x, int y, int oldx, int oldy);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SSScrollView scroll=new SSScrollView(this);
public class DetectHere implements ScrollViewListener {
scroll.setScrollViewListener(this); ********************* cannot access this method
#Override
public void onScrollChanged(SSScrollView scrollView, int x,
int y, int oldx, int oldy) {
return;
}
}
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the class SSScrollView
package com.example.mee.layoutcreate;
import...
public class SSScrollView extends ScrollView {
private MainActivity.LDObservableScrollViewListener scrollViewListener = null;
public SSScrollView(Context context) {
super(context);
}
public SSScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SSScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(MainActivity.LDObservableScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
#Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
The problem I guess is that listener requires a Context as parameter. Since DetectHere is not extended from Activity, you cannot set it inside. You should create constructor as below:
private Context ctx;
public DetectHere (Context ctx){
this.ctx = ctx;
{
and then set listener:
scroll.setScrollViewListener(ctx);
But actually in your case DetectHere is a listener itself. So in your Activity you need to set it as:
scroll.setScrollViewListener(new DetectHere());
and implement this class methods, the IDE will hint you to do this.
I hope this is what you're asking and it will help you.