How to make Progressbar fit the screen?

Asked

Viewed 365 times

0

I’m developing an app for my site and finally manage to add a ProgressBar that accompanies the page loading, then manage to put a style and now just missing that it really starts from the beginning of the screen (like Chrome) the blue bar starts from one side to the other, and mine cuts a little the beginning and the end. Follows the layout:

main.xml

<ProgressBar
    android:id="@+id/progressBar"
    android:minHeight="2dip"
    android:maxHeight="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_alignParentTop="true" />
<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/progressBar" />

xml style.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
    <item name="android:minHeight">0dip</item>
    <item name="android:maxHeight">0dip</item>
</style>

custom_progress_bar_horizontal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background">
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="#ffffffff"
                android:centerColor="#ffdddddd"
                android:centerY="0.50"
                android:endColor="#ffffffff"
                android:angle="270" />
        </shape>
    </item>
    <item
        android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners
                    android:radius="0dip" />
                <gradient
                    android:startColor="#770e75af"
                    android:endColor="#771997e1"
                    android:angle="90" />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="#212121"
                    android:endColor="#000000"
                    android:angle="90" />
            </shape>
        </clip>
    </item>
</layer-list>

Mainactivity.java

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private WebView webView;
    @SuppressLint("SetJavaScriptEnabled")

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);

        progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.custom_progress_bar_horizontal));

        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClientDemo());
        webView.setWebChromeClient(new WebChromeClientDemo());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.techpositivo.com.br");

    }

    private class WebViewClientDemo extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
    }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }
    }

    private class WebChromeClientDemo extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}

Thank you very much, I hope you help me, because I’m starting in programming. ;)

  • Welcome to Stack Overflow! For the community to help you, it’s important to explain your problem in detail. I suggest I read the articles: Tour and how to ask a question. Could post the java file in which you make progress?

  • @Thiagoluizdomacoski Hello, thank you very much for the welcome. I’m using Stack Overflow for Android, until then I can not post images (prints) so I can explain in detail. And how to put the java file? Because I would post everything, only that the code was no longer displayed after 'custom_progresso_bar_horizontal.xml.

  • Click Edit! So you can add more codes! I realized there was nothing else! But it’s fixed! as you can see!

  • @Thiagoluizdomacoski Ready friend, edited and was added the java file in which I am using.

  • @Techpositivo whenever you add code, select it all and click the button {} so it gets formatted.

  • 1

    @Diegofelipe OK, sorry for the mistakes. I’m learning now, and thank you so much for the tip!

  • @Thiagoluizdomacoski Friend, after many attempts, get Solver the problem. How do I share in the right way so other people with the same doubt can see?

  • Click on the "Answer Your Question" button. After answering accept it as the correct answer.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.