Create an interface using Android ProgressBar and toast

Source: Internet
Author: User

This interface is displayed in the use of audiomanager.adjuststreamvolume (int streamtype, int direction, int flags), remember that the flags here are transmitted audiomanager.flag_ SHOW_UI will not show

Otherwise it just logically adjusts the sound of a stream, the user can not see the interface does not know the success or not.

Then I have a functional interface that is the same as this, but the function is not the same, well, I know that Android comes with the interface is not good-looking, but here is only a description of the principle and method, aesthetics is not discussed.

The 2 components of the core here are a textview and a ProgressBar. If you customize, you need to write a layout to wrap the 2 components and display them when you click a button.

Let's go back to the Android-powered interface, and when he clicks on a button, he bounces out and disappears automatically. This automatic disappears I feel very useful because the user only needs to adjust that one

Can see the interface changes on the line, do not need to always show, or to let users in the point of return? How troublesome, simple operation is important.

So I thought about the feature of the toast that comes with Android. Good! When the general direction is fixed, start writing code.

The logic is to let the user click on a button, pop up the interface above, and then disappear, here in the display of toast will also need to change the progress of the process bar.

The following code:

//Mr.////////////////////////into a toast and progress interface 
PB = new ProgressBar (context, NULL,
Android. R.attr.progressbarstylehorizontal);
Pb.setmax (Gameconfig.gamespeedmax);
Pb.setprogress (Gameconfig.gamespeeddefault);
Pb.setlayoutparams (New Linearlayout.layoutparams (
Extensionlayoutconfig.progressbarwidth, LinearLayout.LayoutParams.WRAP_CONTENT));
Toast = toast.maketext (context,
rfileidconvert.getstring ("Gamespeedbartitle"),
Toast.length_long);
Toast.setgravity (gravity.top, 0, 0);
LinearLayout Toastview = (linearlayout) Toast.getview ();
Toastview.setgravity (Gravity.center);
//Note that this 1 is necessary because his textview has been added at the time of creation, if you pass 0
//Then the above will show the text below the progress bar, if you need to, but I now need
The text is above the progress bar below so 1
Toastview.addview (PB, 1);

Here are the 2 interfaces for external calls:

    /**
* Set Progress
*/
public void setgamespeedbarprogress (int progress) {
Pb.setprogress (progress);
}
/**
* Show Toast
*/
public void Gamespeedbarshow () {
Toast.show ();
}

Out of the effect is this

found that compared with the original is not so atmospheric, so also need to adjust the spacing, this simple I will improve it tomorrow, probably the function is this.

Summary . Please do not underestimate such an interface, I have just started to feel very easy, can realize the problems found also a lot.

1. General layout I am used to the layout of the code, but encountered ProgressBar I can only no language. The default style in the code is circular, and the default is horizontal in the XML layout.

The <progressbar style= "? Android:attr/progressbarstylehorizontal" is set by default in XML.

Hehe, the difficulty comes out, this if you want to use the code to set up, tell you very sorry did not set style method. Some people will say that there is not a setscrollbarstyle (int style) method in ProgressBar.

Then I'll ask you to finish the test and talk. This method is invalid, see the description of the API. Then I looked up the source to find out the horizontal direction style using those settings interface,

We can look at what properties are set in the Progressbarstylehorizontal style for the view, and we find the values/under the RES directory under the framework Theme.xml file, search Progressbarstylehorizontal will find the following line:

<item name= "Progressbarstylehorizontal" > @android:style/widget.progressbar.horizontal</item>

The widget style for this theme is Widget.ProgressBar.Horizontal, we open the Style.xml file in the same directory, search for the style, you can find the following code:

<style name= "Widget.ProgressBar.Horizontal" >

<item name= "Android:indeterminateonly" >false</item>

<item name= "android:progressdrawable" > @android:d rawable/progress_horizontal</item>

<item name= "android:indeterminatedrawable" > @android:d Rawable/progress_indeterminate_horizontal</item >

<item name= "Android:minheight" >20dip</item>

<item name= "Android:maxheight" >20dip</item>

</style>

Well, see here to tell you that you can not use <item name= "Progressbarstylehorizontal" > @android:style/widget.progressbar.horizontal< /item>

and using

Android:indeterminateonly= "false"

android:progressdrawable= "@android:d rawable/progress_horizontal"

android:indeterminatedrawable= "@android:d rawable/progress_indeterminate_horizontal"

android:minheight= "20dip"

android:maxheight= "20dip"/>

You can also implement a horizontal progress bar. I know everyone will use that line instead of using so many lines. I know, I'm just telling you the principle.

OK, you know I like to use code layout, and then go code route is

Progressbar.setindeterminate (FALSE);

Progressbar.setprogressdrawable (Getresources (). Getdrawable (Android. R.drawable.progress_horizontal));

Progressbar.setindeterminatedrawable (Getresources (). Getdrawable (Android. R.drawable.progress_indeterminate_horizontal));

Progressbar.setminimumheight (20);

The result is that ProgressBar does turn into a bar, but does not appear as a progress bar, and we carefully contrast the differences between pure Java code and XML layout files, and we find

Android:indeterminateonly= "false" and progressbar.setindeterminate (false);

Not exactly the same as the layout file's properties have a only end but not in the code, we find that the API discovery does not setindeterminateonly such a method.

We open the source code of ProgressBar and find the. Setindeterminate (False) method.

This time we can see that indeterminate and indeterminateonly are not the same thing,

At this point, we should think that as long as we turn the value of indeterminateonly into false, we can make ProgressBar the style of the progress bar,

We looked up all the code and found that the corresponding public method was not provided to modify the value of the property.

In other words, we've been talking about this for a long time. It is not possible to create a horizontal bar style ProgressBar in the form of pure code.

But... Someone says you can use reflection to change the value of a private variable of a class. The content is abstract, this is still skipped, because I found it must know the variable name to reflect success,

But I found that some version of the variable name is not the same, the version of the Mindeterminateonly, the version of the Monlyindeterminate (2.2 version)

So this place I decisively gave up the pure code layout, using ProgressBar PB = new ProgressBar (context, null,android.  R.attr.progressbarstylehorizontal); It's just a matter of the first question.

2. When using toast, be aware that if you need that background box you must maketoast it. If you like clean, you can use the new Toast (context) and then set some properties.

If only want to toast the short-term display of the function, the interface is fully customizable, it is recommended to use the new Toast (context), finally we have to develop the habit of looking at the source oh, sometimes need to do their own, clothed AH

Create an interface using Android ProgressBar and toast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.