com.jaygoo.widget.RangeSeekBar : Android Seekbar show progress value along the seekbar - android

I use as library "com.jaygoo.widget.RangeSeekBar" to get a Range Seek Bar.
Here's my following code XML :
<com.jaygoo.widget.RangeSeekBar
android:id="#+id/seekBarPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:rsb_min="1"
app:rsb_max="5000"
app:rsb_gravity="center"
app:rsb_indicator_background_color="#color/white"
app:rsb_indicator_show_mode="alwaysShow"
app:rsb_indicator_text_color="#color/darkGrey"
app:rsb_indicator_text_size="10sp"
app:rsb_mode="range"
app:rsb_progress_color="#color/honey"
app:rsb_thumb_drawable="#drawable/circle"/>
This RangeSeekBar used to specify the price range, I would like to know How can I add the "$" symbol at the indicator in my seekrangeBar as the follwonig picture :
I add the following kotlin code :
seekBarPrice.leftSeekBar.setIndicatorText("$1")
seekBarPrice.rightSeekBar.setIndicatorText("$1")
seekBarPrice.setRange(1F,5000F)
seekBarPrice.setOnRangeChangedListener(object: OnRangeChangedListener {
override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
}
override fun onRangeChanged(
view: RangeSeekBar?,
leftValue: Float,
rightValue: Float,
isFromUser: Boolean
) {
Log.d("tag", "Value: $leftValue")
seekBarPrice.leftSeekBar.setIndicatorText("$".plus(leftValue.toInt()))
seekBarPrice.rightSeekBar.setIndicatorText("$".plus(rightValue.toInt()))
}
override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
}
})
And my problem is solved

In your library to put % sign this way so you will change as per your requirement:
seekBarPrice.setIndicatorTextStringFormat("$%s%")
I hope it'll help you...!

I think that's the right way
rangePrice.setOnRangeChangedListener(new OnRangeChangedListener() {
#Override
public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser)
{
}
#Override
public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {
}
#Override
public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
view.setIndicatorTextDecimalFormat("$%s%");
}
});

Related

How to navigate from preference screen to a fragment?

I am building an app where there is a settings screen which I build with Andreoid Jetpack Preference library. I want to travel from the preference screen into a more detailed fragment in order to personalize email. But I don know how to handle the click on the preference, I have managed to import the method provided from the library but I dont know how to implement it as there is no information available. I have the function onPreferenceClick but i dont know how to build its logic. It shall return false when it is not clicked and true when it is.
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (onPreferenceClick(preferenceScreen.getPreference(1))) {
findNavController().navigate(R.id.editMailFragment)
}
override fun onPreferenceClick(preference: Preference?): Boolean {
preference?.setOnPreferenceClickListener {
}
}
return
}
If you using PreferenceFragmentCompat, you can override onCreatePreferences to handle click on preference:
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.your_preference);
Preference preferenceMap = findPreference("your_preference_key");
preferenceMap.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
findNavController().navigate(R.id.editMailFragment)
return true;
}
});
}
I have solved it, here is the code if someone has the same trouble.
val showValueListener = Preference.OnPreferenceClickListener {
findNavController().navigate(R.id.editMailFragment)
true
}
findPreference<Preference>("email")?.onPreferenceClickListener = showValueListener

Override method for anonymous class in Kotlin for Android

My code is written in Java and I'm refactoring it to Kotlin. I have this:
Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar) {
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
dismiss();
dialog = null;
setLockedDialog(false);
return false;
}
};
I'm not able to write it using Kotlin. This is my approach:
dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar) {
override fun dispatchTouchEvent(ev: MotionEvent) {
// TODO Implement function
}
}
My Android Studio shows me:
1- Type mismatch. Required: Boolean. Found: Int (under android.R.style.Theme_Translucent_NoTitleBar).
2- Type mismatch. Required: DialogInterface.OnCancelListener! Found: () - > Unit surrounding the overrided function.
Any suggestions?
Change the code to this:
dialog = object: Dialog(this, android.R.style.Theme_Translucent_NoTitleBar) {
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
// TODO Implement function
}
}
The reason it's complaining is because 2 things. First, you need to create an object to override a function of Dialog. Second, the dispachTouchEvent wasn't returning a Boolean but unit.

How to use this library in Kotlin?

colorPicker.setColorSelectionListener(new SimpleColorSelectionListener() {
#Override
public void onColorSelected(int color) {
// Do whatever you want with the color
imageView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
});
I am trying to use this color picker library which is in Java. I am not able to convert the code into Kotlin. How can I do so?
Will probably look something like
colorPicker.seColorSelectionListener(object : SimpleColorSelectionListener() {
override fun onColorSelected(color:Int) {
imageView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
});
you can simply copy paste the code and Android studio will convert. For convenience you can use it like.
colorPicker.setColorSelectionListener(object : SimpleColorSelectionListener() {
override fun onColorSelected(color: Int) {
// Do whatever you want with the color
}
})

How to type text on a SearchView using espresso

TypeText doesn't seem to work with SearchView.
onView(withId(R.id.yt_search_box))
.perform(typeText("how is the weather?"));
gives the error:
Error performing 'type text(how is the weather?)' on view 'with id:../yt_search_box'
For anyone that bump into this problem too, the solution is to write a ViewAction for the type SearchView, since the typeText only supports TextEditView
Here my solution:
public static ViewAction typeSearchViewText(final String text){
return new ViewAction(){
#Override
public Matcher<View> getConstraints() {
//Ensure that only apply if it is a SearchView and if it is visible.
return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
}
#Override
public String getDescription() {
return "Change view text";
}
#Override
public void perform(UiController uiController, View view) {
((SearchView) view).setQuery(text,false);
}
};
}
#MiguelSlv answer above, converted to kotlin
fun typeSearchViewText(text: String): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "Change view text"
}
override fun getConstraints(): Matcher<View> {
return allOf(isDisplayed(), isAssignableFrom(SearchView::class.java))
}
override fun perform(uiController: UiController?, view: View?) {
(view as SearchView).setQuery(text, false)
}
}
}
This works for me:
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
Based on César Noreña respose I manage to insert text in the search field.
First I clicked on my view and then typeText on the android search view id.
onView(withId(R.id.my_own_search_menu_id)).perform(click());
onView(withId(R.id.search_src_text)).perform(typeText("how is the weather?"))
Also the X button of SearchView has ID search_close_btn
onView(withId(R.id.search_close_btn)).perform(click()); // first time erase the content
onView(withId(R.id.search_close_btn)).perform(click()); // second time close the SearchView
you can use Resource#getSystem to get the View
Resources.getSystem().getIdentifier("search_src_text",
"id", "android")
onView(withId(Resources.getSystem().getIdentifier("search_src_text",
"id", "android"))).perform(clearText(),typeText("enter the text"))
.perform(pressKey(KeyEvent.KEYCODE_ENTER))

Automating number picker in android using espresso

How to automate number picker using espresso. I want to set specific time in the timePicker using espresso.
To match a View by its class name you can simply use:
onView(withClassName(Matchers.equalTo(TimePicker.class.getName())));
Once you have the ViewInteraction object you can set a value on it defining and using a ViewAction as following:
public static ViewAction setTime(final int hour, final int minute) {
return new ViewAction() {
#Override
public void perform(UiController uiController, View view) {
TimePicker tp = (TimePicker) view;
tp.setCurrentHour(hour);
tp.setCurrentMinute(minute)
}
#Override
public String getDescription() {
return "Set the passed time into the TimePicker";
}
#Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(TimePicker.class);
}
};
}
Match the view and then perform the action:
ViewInteraction numPicker = onView(withClassName(Matchers.equalTo(NumberPicker.class.getName())));
numPicker.perform(setNumber(1));
Create a ViewAction to set the number:
public static ViewAction setNumber(final int num) {
return new ViewAction() {
#Override
public void perform(UiController uiController, View view) {
NumberPicker np = (NumberPicker) view;
np.setValue(num);
}
#Override
public String getDescription() {
return "Set the passed number into the NumberPicker";
}
#Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(NumberPicker.class);
}
};
}
There is a problem with accepted answer : it does not fire on change event. So (if you need it) you can't test if your view react to this on change event.
The following code (kotlin) is not cool anyway but i think it's the only way.
fun setValue(value: Int): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "set the value of a " + NumberPicker::class.java.name
}
override fun getConstraints(): Matcher<View> {
return ViewMatchers.isAssignableFrom(NumberPicker::class.java)
}
// the only way to fire onChange event is to call this private method
override fun perform(uiController: UiController?, view: View?) {
val numberPicker = view as NumberPicker
val setValueMethod = NumberPicker::class.java.getDeclaredMethod(
"setValueInternal",
Int::class.java,
Boolean::class.java
)
setValueMethod.isAccessible = true
setValueMethod.invoke(numberPicker, value, true)
}
}
}
For those looking at this question later (like me) this might be helpful: DateTimePickerTest makes use of PickerActions. PickerActions allows code for date pickers like this (Java):
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month + 1, day));
Or for time pickers (Kotlin):
onView(withClassName(Matchers.equalTo(TimePicker::class.java.name))).perform(PickerActions.setTime(0, 10))
I used the built-in TimePickerDialog.
(Kotlin)
fun setAlarmTime(){
val calendar = Calendar.getInstance()
onView(isAssignableFrom(TimePicker::class.java)).perform(
PickerActions.setTime(
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE) + 2
)
)
onView(withText("OK")).perform(click())
}
Firstly, you should open the TimePickerDialog, then use this code. The time will be the current time + 2 minute. After the setup, it clicks on the OK button.
I have found a solution to the problem, that Luigi Massa Gallerano's solution does not trigger the change listener.
Additionally to his method you need to add a wrapping method, that swipes up and down one time each. This triggers the changeListener, though the former value of course, is lost.
fun setNumberPickerValue(viewInteraction: ViewInteraction, value: Int) {
viewInteraction.perform(setValue(value))
viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_CENTER, GeneralLocation.BOTTOM_CENTER, Press.FINGER))
SystemClock.sleep(50)
viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER))
SystemClock.sleep(50)
}
The example given is using NumberPicker and Kotlin, but in principle, it's the same.

Categories

Resources