Search code examples
androidstringsettextbattery

setText can not be translated , how to string battery info to display correctly


hi i am trying to string the batteryinfo.setText, i have no idea how to do it. its working but information display is not accurate. help me to string correctly.

  public class Main extends Activity implements SensorEventListener {

            private TextView batteryInfo;



            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                PackageManager pm = getPackageManager();
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                batteryInfo=(TextView)findViewById(R.id.battinfo);

        his.registerReceiver(this.batteryInfoReceiver,  new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


         private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {

                    int  level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
                    int  temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
                    int  voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);


               //issues fixed for setText can not be translated 



batteryInfo.setText(String.format(getString(R.string.battery_display),level,temperature,voltage));
                    }
                };

            }

xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/battinfo"
        android:id="@+id/battinfo"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_below="@+id/imageViewCompass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />

string

//added battery display string

<string name="battinfo">Battery Status</string>
<string name="battery_display">BATT:  %1$d \n HEAT: %2$d °C\n  VOLT: %3$d \n</string>

Solution

  • Place the text you want to set in the TextView in strings.xml.

    Your string will be:

    <string name="battery_text" formatted="false">BATT:%d%% \n HEAT: %d &#8451; \n VOLT: %dmV</string>
    

    In onReceive() take the string from strings.xml

    batteryInfo.setText.(String.format(context.getString(R.string.battery_text),level,temperature,voltage));