Android-display webpage loading progress with custom title bars

Source: Internet
Author: User

This time, we were working on lephone adaptation. The test group submitted a bug: the text in the title bar was not completely displayed for a long time. In fact, this was not a bug, this problem has not appeared on other machines in the past, but it is said that the lephone platform is not very beautiful, because Lenovo modified the native title bar UI. The modification process encountered a difficult problem. The progress of the title bar that comes with the system can always reach 100% and then gradually recede, but each time I finally reach 100%, the Section is not shown completely, after trying to use a thread program to die and stick to the master, the effect was the same. Later, my colleague reminded me to use an animation. This is indeed the case. I guess this is also true for the system. After the Progress reaches 100%, it will be okay to change its transparency with an animation.

Effect: The title bar displays the webpage title and scrolls, And the webpage loading progress is displayed with a progress bar (the modified lephone includes a return button after the title bar is customized, and the title text and progress bar are not very nice in the frame layout ).

1. First define a relativelayout layout file broser_custom_title.xml (the class alwaysmarqueetextview overwrites textview to achieve a running horse lamp effect, which can be found online

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.android.CustomTitleTest.AlwaysMarqueeTextView
android:id="@+id/tvtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:focusableInTouchMode="true"
android:singleLine="true" android:ellipsize="marquee"
android:focusable="false" android:marqueeRepeatLimit="marquee_forever"
android:layout_centerVertical="true"/>

<ProgressBar android:id="@+id/pb"
android:layout_width="fill_parent" android:layout_height="2sp"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone" android:layout_alignParentBottom="true"
></ProgressBar>
</RelativeLayout>

2. inherit from webchromeclient, and rewrite the onprogresschanged and onreceivedtitle events (use the animation to fade away after the progress bar is loaded)

Public class mywebchromeclient extends webchromeclient {

Private activity;
Private progressbar Pb;
Private textview tvtitle;
Public mywebchromeclient (activity ){
This. Activity = activity;
}
Animation animation;
@ Override
Public void onprogresschanged (webview view, int newprogress ){
PB = (progressbar) activity. findviewbyid (R. Id. Pb );
PB. setmax (100 );
If (newprogress <100 ){
If (PB. getvisibility () = view. Gone)
PB. setvisibility (view. Visible );
PB. setprogress (newprogress );
} Else {
PB. setprogress (100 );
Animation = animationutils. loadanimation (activity, R. anim. animation );
// Run the animation Animation
PB. startanimation (animation );
// Sets the visibility of the spinner to invisible.
PB. setvisibility (view. Invisible );
}

Super. onprogresschanged (view, newprogress );
}

@ Override
Public void onreceivedtitle (webview view, String title ){
Tvtitle = (textview) activity. findviewbyid (R. Id. tvtitle );
Tvtitle. settext (title );
Super. onreceivedtitle (view, title );
}

}

3. animation style Res/anim/animation. xml of the progress bar


<?xml version="1.0" encoding="utf-8"?>  

<set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="700"/>
</set>

4. Set the custom title bar with codes

    private WebView browser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.broser_custom_title);
browser = (WebView) findViewById(R.id.my_browser);
// currentWebView=browser;
browser.setWebChromeClient(new MyWebChromeClient(Main.this));
browser.loadUrl("http://www.163.com");
}
Related Article

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.