Monday, July 28, 2014

Accept negative number in EditText

In last exercise "Display formated number in decimal, octal and hexadecimal using String.format", EditText defined in XML with android:inputType="number". It will not accept minus sign, so the input number is always positive.

To accept minus sign to enter negative number, change it to android:inputType="numberSigned".


<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="numberSigned" />
    <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>


3 comments:

  1. Thanks for creating this informative post. I got solution of my problem from this post.

    ReplyDelete
  2. thanks man, and what about binary?

    ReplyDelete