I am building an Android application and I want to create a progress bar very much like what is seen at the bottom of the MIUI File Explorer
There is no completely obvious solution as far as I can tell.
So far what I can think of is
Layering many progress bars with transparent backgrounds on top of one another
Extend the Progress bar class to take multiple values and rewrite the onDraw method
I think the first option would be far too slow and inefficient but the second option might be overly complex but is definitely the better solution.
Is there any better way of going about this?
you should make a custom view for it. Example:
final Paint blue = new Paint();
blue.setColor(Color.BLUE);
View progressBar = new View(getActivity()){
protected void onDraw(android.graphics.Canvas canvas) {
canvas.drawRect(0,0,getWidth()*progressPecentFirst,getHeight(),blue);
blue.setColor(Color.RED);
canvas.drawRect(0,0,getWidth()*progressPecentSecond,getHeight(),blue);
//Repeat this
};
};
parentLayout.addView(progressBar);
One option would be to not make it a traditional progress bar at all. In one of my personal apps I needed a display similar to a circular progress bar that had segments of varying colours.
Might I suggest you start by drawing a filled rectangle from 0B to 14.9GB in the colour for music, from 939MB to 14.9GB in the colour for Videos, from 3.52GB to 14.9GB in the colour for pictures, and so on.
You can then just draw the rounded ends in the background colour as a mask.
This would be pretty quick since you're only drawing graphics primitives and you can extend it as much as you want.
The only slight drawback would be that you'd have to implement much of the logic yourself, but that's a small price to pay in my opinion.
For someone else who can help, I leave an example on my github based on #Zelleriation answer. The progress bar.
This adapts to what I needed, feel free to modify it to yours.
<io.ekrlaz.segmentedprogressbar.view.SegmentProgressBar
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="10dp"
app:cornerRadius="16dp"
/>
And in your activity.
val binding = ActivityMainBinding.inflate(layoutInflater)
binding.progress.setMax(100F)
binding.progress.setPrimaryProgress(40F)
binding.progress.setSecondaryProgress(10F)
binding.progress.setThirdProgress(40F)
Link: https://github.com/EKRLAZ/segmented-progressbar
Here is the segmented progress bar based on the #Zelleriation answer. The progress bar.
SegmentedProgressBar progressBar = (SegmentedProgressBar) findViewById(R.id.segmentedProgressBar);
progressBar.setProgressColor(Color.BLACK);
progressBar.publishProgress(0.5f);
<com.muneikh.SegmentedProgressBar
android:id="#+id/segmentedProgressBar"
android:layout_width="match_parent" />
https://github.com/muneikh/SegmentedProgressBar
Dear Please see the build in example API Demo of android SKD 2.2 in api demo go to view -> progress bar -> incremental then you see the solution of you problem.
Related
I want to create a dynamic intermittent progress bar (as illustrated in the image below), that will change its intervals depending on the user's choice, in Android Studio.
The idea is that the user will choose how many times he wishes to do a behavior, and then the bar will fragment accordingly. Then each time they do the behavior, the bar will color a step increment, as shown in the image below.
Intermittent Progress Bar
I am looking for some general guidance on how to do it, since I am new to this.
I have thought of 3 ways to do this:
Have a ton of png. drawables or vectors for each case, and use one accordingly in an Image View. (seems kind of stupid to me)
Create as many views as are the intervals, and then change the view colors accordingly (in this case there will be a problem with the dynamic part of it i.e. interval variability)
Customize somehow a horizontal ProgressBar to do this automatically.
I have searched the internet for the third way which is the most elegant to me, but cant find an answer.
Thank you for your time.
This is actually trivial to obtain using the current ProgressBar APIs.
Depending on the number of tasks required and number of tasks done, you can update a ProgressBar using setMax and setProgress
progressBar.setMax(totalTasks);
progressBar.setProgress(tasksDone);
This will cover a fraction tasksDone / totalTasks in the progressBar.
Edit: Based on the scenario highlighted in the comments, you can simply use multiple views in a LinearLayout.
You can use a LinearLayout with the background of the partition color you want.
<LinearLayout
....
android:orientation="horizontal"
android:background="#color/partition"
....
/>
And then simply add the child views programmatically with equal weights (and a horizontal margin):-
for(task in tasks) {
val progressBar = View(this)
progressBar.marginEnd = gapRequired
// customize your view here: Set background, shape etc.
// Set the width to 0 and weight to 1f
val params = LinearLayout.LayoutParams(0, height, 1f)
linearLayout.addView(view, index, params)
}
Later to modify an individual view (fragment of progress bar):-
val view = linearLayout.getChildAt(index)
// Modify the view as needed
To remove a view from the layout:-
linearLayout.removeViewAt(index)
Or if you have the view referenced:-
linearLayout.removeView(view)
You can also remove all views (if you need to reset the entire progress bar for some reason) using linearLayout.removeAllViews()
You might want to use RecyclerView with an adapter if you are expecting a lot of these fragments in your progressBar.
I have a RecyclerView with the implementation of ItemTouchHelper. I am dragging and sorting the items in RecyclerView using ItemTouchHelper.
Also I am performing different actions on different direction of swipe. If user goes from left to right (Swipe) I just Deactivate status of item. If he perform right to left swipe I activate the relevant item.
What I want:
But now here comes the change, What I want is that I need to show some icons after I perform right or left swipe. And By clicking on those icons/buttons I want to perform action then.
Problem:
Problem is as I told you I have implementation of ItemTouchHelper, then how I am doing to perform what I wanted (as described above)
How to solve this? I have seen many libraries but they have limitations, also as the app has very much things going and there are some more implementations on RecyclerView used in our app, so I really do not want to risk the rest of implementations for the sake of this..
UPDATE 1:
To clear my question more I am going to add on more. Right now My implementation is something like this. But this is not what I want. I want to show swipe menu for any type of swipe I mean either it is left or right I want to show some icons on left and right side of item
You can take iOS swipe menu as an example (see here). I want exactly same behavior in android.
You're asking the impossible: suggest a library that doesn't exist (as you have already found out).
You're asking people to also find a solution for a problem you only briefly describe. You're not providing any code whatsoever, nor any specific issue you're facing when trying to come up with a solution; you're essentially expecting magic to happen.
How can you make this better?
Provide something like: "I'm attempting to implement a swipe solution for a recycler view's row; when I try to do YYYY is happening instead. Hhere's my piece of code where I do , calculated like this , am I missing something?"
Anyway...
You claim to be using an ItemTouchHelper already, so, if you look at how a very basic one works, you'll notice that the helper will ultimately draw on the canvas directly via:
#Override
public void onChildDraw(
final Canvas canvas,
final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder,
final float deltaX,
final float deltaY,
final int actionState,
final boolean isCurrentlyActive) {
This is derived from the platform ItemTouchHelper.SimpleCallback. So...
If you want to implement your solutions, you'll need to implement it there to do things like stop dragging, draw the content (icons), etc.
It's not a trivial solution and getting the whole thing right is difficult; more so if you introduce "some more implementations on RecyclerView".
I hope this answer points you in the right direction, and that your next related question is more about what you've tried (and failed) to do, than a "please do this for me" scenario. (If your intentions weren't those, please apologize, but you spent 5 minutes typing this question and it would take hours/days to implement this, so set your expectations this time).
Finally, when I wrote this, I realized most people wanted iOS swipe behavior; unfortunately, said behavior is not pre-implemented (like on iOS) on Android and you have to deal with it yourself; it's not impossible nor the most difficult task on earth to do on Android, but will give you a few headaches if you're hacking RecyclerView too much.
UPDATE
You've realized now, that ItemTouchHelper has a problem. It draws on the Canvas directly, so it has no knowledge of a Layout, View, Margin, etc. These are all things that live outside the realm of the touch helper. The helper is given a canvas, and drawing capabilities and that's it.
Where does this canvas size come from?
Well, it's calculated during the layout/measure pass(es) for the RecyclerView and its views.
How do I then stop dragging "at exactly the center of the screen" then?
Pass the values to your Helper; remember that the responsibility of calculating where the middle of the screen is, is not in the TouchHelper's contract; but your Activity/Fragment does know this. Provide the information the Touch Helper needs to perform the things you want it to perform.
Revert "back" to the original position, means knowledge of what the initial state was, etc. All this information is known by the RecyclerView and beyond, not the TouchHelper.
So you'd need to measure your layouts, save some values, pass them to the touchhelper so it can operate, etc.
As you can see, the full picture starts to become more and more complicated. My advice to you is:
Try to push this feature OUT as "the platform doesn't do it it will take time, it's not worth" (the worthiness and discoverability of swipe actions is dubious at best, but it's an ubiquitous action nowadays so you may have to do it regardless).
If you really have to do this, abstract things as much as you can, create all the classes/interfaces you think you can need, even if you end up with a "MiddleScreenCalculatorDelegate" kind of thing. It will be easier to fix later, but at least have each component do a very small subset of things.
Lastly, your item touch helper will have to calculate how much offset (delta) has the view been swiped already and stop when it reaches a known threshold.
Good luck :)
p.s.: I suggest you post a different question with specific issue(s) to get better help, this answer is very unhelpful as it is. :)
It's there a way to make a vertical slider on Android?, for example, I want the action bar to be dragged all the way from top to bottom, to show a fragment, pretty much like the notification bar on Android.
Thank you very much
There is always a way ;), but it would take some custom code to do it. I don't really have to much time to write something out for you, but I did find something that may be useful to you. These guys open sourced a slide from bottom to top view like is used in Google Play Music. You can take a look and maybe modify it to slide from top to bottom.
https://github.com/umano/AndroidSlidingUpPanel
Good Luck
There used to be a slidingdrawer view as part of the framework but it's now deprecated. There is however a nice implementation of something similar on github that you can take a look at here:
https://github.com/umano/AndroidSlidingUpPanel
Umano's Drawer is dated and limited. You should use a more up-to-date library like https://github.com/drxeno02/androidprojects-book2-slidingdrawer.
This library has no limitations such as only having two children. You can have as many children as you want, and you can set offsets and design your drawer to look anyway you want.
I have a vertical LinearLayout which has two another vertical layouts inside. The first one is always shown, and the second one depends on some event. I do want to animate that second layout when shown. How can I do it smoothly and nice? Thank you so much,
You should check out this library on Github. It may help. It is an animation library for listviews.
ListViewAnimations
A very simple, yet elegant way to get your layouts animated is by using the android:animateLayoutChanges="true" on its parent. This will likely take a lot of work in getting a nice animation out of your hands, unless you want to customize it.
With KitKat, Google introduced the android.transtion API, which may also be worth looking at.
You can create your own animation and you apply that animation to your layout when you need(In your case at the time of shown your layout)
yourLayout.startAnimation(yourAnimation);
I want to develop an android app, but I'm still not that great at using xml or the eclipse wysiwyg editor to make scalable and robust interfaces!
Below is a quick concept I drew up on word of what my main game screen will consist of:
I'm currently thinking a vertical linear frame first, with a frame that doesn't change for my important stats etc., then a frame in the middle which is switched by the buttons, but I'm stuck on how to implement this.
For the centre button screen, the map screen, I am hoping to add an image of a map which can be moved around, and somehow attach invisible button objects to points in this picture, maybe with an array of squares or something?
I understand this is a pretty open question and might be difficult to answer, but if you think you can help in anyway please do.
Thanks!
There is a log of possibilities, but you can use a LinearLayout with android:orientation="vertical" and include your stats in top. For the changing screens with buttons you can use a FragmentTabHost and create different Fragment for each of your views.
Check this example with a implementation.
Regarding the map, I think the easiest option is to use Google Maps Android API v2. Here is a example to use the maps v2 and a example of how to add markers to the map.