Monday, July 28, 2014

Display formated number in decimal, octal and hexadecimal using String.format

How to display formated number in decimal, octal and hexadecimal using String.format, with %d, %o, %x and %X.


In layout file, add android:inputType="number" to EditText to limit the user input to number only.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidstringformat.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
    <Button
        android:id="@+id/buttonprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Print - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


MainActivity.java
package com.example.androidstringformat;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
 
 EditText edit1;
 TextView textOut;
 Button buttonPrint;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edit1 = (EditText)findViewById(R.id.edit1);
     textOut = (TextView)findViewById(R.id.printout);
     buttonPrint = (Button)findViewById(R.id.buttonprint);
     
     buttonPrint.setOnClickListener(onClickListener);
    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    String strIn = edit1.getText().toString();

    if(!strIn.equals("")){
     //convert String to integer
     int inInt = Integer.parseInt(strIn);

     textOut.setText(
      String.format("parse to Integer\n(Decimal): %d\n", inInt) +
      String.format("(Octal): %o\n", inInt) +
      String.format("(hexadecimal): %x\n(HEXADECIMAL): %X\n", inInt, inInt));
    }
    
   }};

}




Next: Accept negative number in EditText

No comments:

Post a Comment