0

how do i render the value of "percentage" in my circular progress? this code basically calculates wifi signal strength and shows it in textview, but i would also like to show it in circular progress bar.

Main Activity

public static int getWifiStrengthPercentage(Context context) {
    try {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        int rssi = wifiManager.getConnectionInfo().getRssi();
        int level = WifiManager.calculateSignalLevel(rssi, 10);
        int percentage = (int) ((level / 10.0) * 100);
        tv.setText(percentage + " %");
        return percentage;
    } catch (Exception e) {
        return 0;
    }
}
}

xml

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:indeterminateDrawable="@drawable/progress"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_centerVertical="true"
    android:layout_marginLeft="18dp" />

prgress.xml

 <?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="900" >

<shape
    android:innerRadiusRatio="2.3"
    android:shape="ring"
    android:thickness="3.8sp" >
    <solid android:color="#F3E2A9" />
</shape>

</rotate>

1 Answers1

0

To show wifi signal strength in the circular progress bar, Just set it in Progress Bar and take one TextView in the middle of Circular Progress bar.

XML :

<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:indeterminateDrawable="@drawable/progress"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_centerVertical="true"
android:layout_marginLeft="18dp" />

<TextView
android:id="@+id/tvprogress"
android:layout_width="wrap_content"  
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">

In Code:

public static int getWifiStrengthPercentage(Context context) {
      try {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        int rssi = wifiManager.getConnectionInfo().getRssi();
        int level = WifiManager.calculateSignalLevel(rssi, 10);
        int percentage = (int) ((level / 10.0) * 100);
        progressBar1.setProgress(percentage);
        tvprogress.setText(String.valueof(percentage) + " %");
        return percentage;
    } catch (Exception e) {
        return 0;
    }
}

Hope it will help you.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • does it in the same layout which inflated in the activty where you have set your view? – KishuDroid Jan 13 '16 at 06:38
  • this may be because your progress is getting updated. – KishuDroid Jan 13 '16 at 07:34
  • i have an idea, ill update my code in the question, i removed the progress and instead, ill set progress images in my imageview which would look like a circular progress according to the %, i have prepared 10 images already! – user5779349 Jan 13 '16 at 07:36
  • now can you tell me how to detect percentage and set image in my imageview according to the %? i have 11 images ready – user5779349 Jan 13 '16 at 07:37
  • You are already detect percentage, just put if - else if - elsecondition like if(Integer.parseInt(strpercentage) >0 && Integer.parseInt(strpercentage) <10 ){ i.setImageResource(R.drawable.yourimage); } – KishuDroid Jan 13 '16 at 07:40
  • please give me a snippet and ill accept the answer :) – user5779349 Jan 13 '16 at 07:42
  • if(Integer.parseInt(strpercentage) >0 && Integer.parseInt(strpercentage) <26){ i.setImageResource(R.drawable.yourimage); }else if(Integer.parseInt(strpercentage) >25 && Integer.parseInt(strpercentage) <51){ i.setImageResource(R.drawable.yourimage); }... This is the how you can put your images. – KishuDroid Jan 13 '16 at 10:28