Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onFocusChangeListener not working

Tags:

android

I wrote some code for an android calculator application. What I wanted in that is :-

If an EditText field lose focus, all output fields should get updated like it happens in excel sheet, which updated all connected values in a sheet if we update any value in a cell.

But my code did not do anything. If I don't use try catch block, the app is collapsing. It shows that there is an error in code, but I could not trace it.

Please help if there is better way to do this money denomination totaling calculator.

Layout of my calculator

Java code is:-

package mahalaxmi.mahalaxmi;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by SIV on 10-03-2018.
 */

public class Fragment_four extends Fragment {


    private static EditText notes2000, notes500, notes200, notes100, notes50, notes20, notes10, notes5, notes2, notes1;
    private static TextView value2000, value500, value200, value100, value50, value20, value10, value5, value2, value1;
    private static TextView errorText41, quantityTotal, valueTotal;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout, container, false);


        notes2000 = (EditText) view.findViewById(R.id.qty2000);
        notes500 = (EditText) view.findViewById(R.id.qty500);
        notes200 = (EditText) view.findViewById(R.id.qty200);
        notes100 = (EditText) view.findViewById(R.id.qty100);
        notes50 = (EditText) view.findViewById(R.id.qty50);
        notes20 = (EditText) view.findViewById(R.id.qty20);
        notes10 = (EditText) view.findViewById(R.id.qty10);
        notes5 = (EditText) view.findViewById(R.id.qty5);
        notes2 = (EditText) view.findViewById(R.id.qty2);
        notes1 = (EditText) view.findViewById(R.id.qty1);


        value2000 = (TextView) view.findViewById(R.id.result2000);
        value500 = (TextView) view.findViewById(R.id.result500);
        value200 = (TextView) view.findViewById(R.id.result200);
        value100 = (TextView) view.findViewById(R.id.result100);
        value50 = (TextView) view.findViewById(R.id.result50);
        value20 = (TextView) view.findViewById(R.id.result20);
        value10 = (TextView) view.findViewById(R.id.result10);
        value5 = (TextView) view.findViewById(R.id.result5);
        value2 = (TextView) view.findViewById(R.id.result2);
        value1 = (TextView) view.findViewById(R.id.result1);
        quantityTotal = (TextView) view.findViewById(R.id.qtyTotal);
        valueTotal = (TextView) view.findViewById(R.id.resultTotal);
        errorText41 = (TextView) view.findViewById(R.id.errorText4);




        View.OnFocusChangeListener listener;

        listener = new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                short val2000=0, val500=0, val200=0, val100=0, val50=0, val20=0, val10=0, val5=0, val2=0, val1=0;
                int totalQty, totalValue;
                int tval2000=0, tval500=0, tval200=0, tval100=0, tval50=0, tval20=0, tval10=0, tval5=0, tval2=0, tval1=0;
                if(!hasFocus){
                    try {

                        //extracting the entered values from EditText field
                        val2000 = Short.parseShort(value2000.getText().toString());
                        val500 = Short.parseShort(value500.getText().toString());
                        val200 = Short.parseShort(value200.getText().toString());
                        val100 = Short.parseShort(value100.getText().toString());
                        val50 = Short.parseShort(value50.getText().toString());
                        val20 = Short.parseShort(value20.getText().toString());
                        val10 = Short.parseShort(value10.getText().toString());
                        val5 = Short.parseShort(value5.getText().toString());
                        val2 = Short.parseShort(value2.getText().toString());
                        val1 = Short.parseShort(value1.getText().toString());

                        //calculating the output values
                        tval2000 = value(notes2000, val2000);
                        tval500 = value(notes500, val500);
                        tval200 = value(notes200, val200);
                        tval100 = value(notes100, val100);
                        tval50 = value(notes50, val50);
                        tval20 = value(notes20, val20);
                        tval10 = value(notes10, val10);
                        tval5 = value(notes5, val5);
                        tval2 = value(notes2, val2);
                        tval1 = value(notes1, val1);

                        //setting the output value

                        value2000.setText(tval2000 + "");
                        value500.setText(tval500 + "");
                        value200.setText(tval200 + "");
                        value100.setText(tval100 + "");
                        value50.setText(tval50 + "");
                        value20.setText(tval20 + "");
                        value10.setText(tval10 + "");
                        value5.setText(tval5 + "");
                        value2.setText(tval2 + "");
                        value1.setText(tval1 + "");

                        totalQty = val2000 + val500 + val200 + val100 + val50 + val20 + val10 + val5 + val2 + val1;
                        totalValue = tval2000 + tval500 + tval200 + tval100 + tval50 + tval20 + tval10 + tval5 + tval2 + tval1;

                        quantityTotal.setText(totalQty + "");
                        valueTotal.setText(totalValue + "");

                    }

                    catch (Exception e){};



                }
      }
        };



        notes2000.setOnFocusChangeListener(listener);
        notes500.setOnFocusChangeListener(listener);
        notes200.setOnFocusChangeListener(listener);
        notes100.setOnFocusChangeListener(listener);
        notes50.setOnFocusChangeListener(listener);
        notes20.setOnFocusChangeListener(listener);
        notes10.setOnFocusChangeListener(listener);
        notes5.setOnFocusChangeListener(listener);
        notes2.setOnFocusChangeListener(listener);
        notes1.setOnFocusChangeListener(listener);



        return view;

    }

    private void clearQty(EditText e) {
        e.setText("");
    }

    private int value(int e, int s) {

        return e * s;
    }


}

Layout file is :-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <TableLayout
        android:layout_width="351dp"
        android:layout_height="506dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lableDenom"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:text="Denom"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />

            <TextView
                android:id="@+id/lableQty"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:paddingLeft="10dp"
                android:text="Qty"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold|italic"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <TextView
                android:id="@+id/lableValue"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:text="Value"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lable2000"
                android:layout_width="67dp"
                android:layout_height="35dp"
                android:background="@color/Rs_2000"
                android:paddingLeft="5dp"
                android:text="2000"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <EditText
                android:id="@+id/qty2000"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:ems="10"
                android:inputType="number"
                android:maxLength="4"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/result2000"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:background="@color/Rs_2000"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

        ....
        and so on
        ....

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lableTotal"
                android:layout_width="67dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:paddingLeft="5dp"
                android:text="Total"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <TextView
                android:id="@+id/qtyTotal"
                android:layout_width="100dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:ems="10"
                android:inputType="number"
                android:maxLength="4"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/resultTotal"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

    </TableLayout>

    <TextView
        android:id="@+id/errorText4"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="42dp"
        tools:layout_editor_absoluteY="460dp" />

</android.support.constraint.ConstraintLayout>
like image 704
Siva Prasad Avatar asked Oct 17 '22 21:10

Siva Prasad


2 Answers

You have assigned same listener - View.OnFocusChangeListener() for all the edit text. The listener function will be triggered on each EditText item click. So, when the focus changed for the first item, listener will be called and you are trying to read all the EditText values and convert them to Short.

In this case all other EditTexts doesn't have value and this will trigger NumberFormatException.

Please see the below implementation, which will give an idea on implementing same listener on multiple views.

int val2000=0, val500=0, val200=0, val100=0, val50=0, val20=0, val10=0, val5=0, val2=0, val1=0;
int totalQty, totalValue;
int tval2000=0, tval500=0, tval200=0, tval100=0, tval50=0, tval20=0, tval10=0, tval5=0, tval2=0,tval1=0;

View.OnFocusChangeListener listener;
    listener = new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {

            if(!hasFocus){
                try {

                    switch(v.getId()) {
                        case R.id.qty2000:
                            val2000 = Integer.parseInt(notes2000.getText().toString());
                            tval2000 = value(2000, val2000);
                            value2000.setText(tval2000 + "");
                            break;

                        case R.id.qty500 :
                            val500 = Integer.parseInt(notes500.getText().toString());;
                            tval500 = value(500, val500);
                            value500.setText(tval500 + "");
                            break;

                        case R.id.qty200:
                            val200 = Integer.parseInt(notes200.getText().toString());
                            tval200 = value(200, val200);
                            value200.setText(tval200 + "");
                            break;


                        case R.id.qty100:
                            val100 = Integer.parseInt(notes100.getText().toString());
                            tval100 = value(100, val100);
                            value100.setText(tval100 + "");
                            break;

                        case R.id.qty50:
                            val50 = Integer.parseInt(notes50.getText().toString());
                            tval50 = value(50, val50);
                            value50.setText(tval50 + "");
                            break;

                        case R.id.qty20:
                            val20 = Integer.parseInt(notes20.getText().toString());
                            tval20 = value(20, val20);
                            value20.setText(tval20 + "");
                            break;

                        case R.id.qty10:
                            val2000 = Integer.parseInt(notes10.getText().toString());
                            tval10 = value(10, val10);
                            value10.setText(tval10 + "");
                            break;


                        case R.id.qty5:
                            val5 = Integer.parseInt(notes5.getText().toString());
                            tval5 = value(5, val5);
                            value5.setText(tval5 + "");
                            break;


                        case R.id.qty2:
                            val2 = Integer.parseInt(notes2.getText().toString());
                            tval2 = value(2, val2);
                            value2.setText(tval2 + "");
                            break;

                        case R.id.qty1 :
                            val1 = Integer.parseInt(notes1.getText().toString());
                            tval1 = value(1, val1);
                            value1.setText(tval1 + "");
                            break;

                        default:
                            //do the default behaviour.
                            break;
                    }
                }

                catch (Exception e){
                    e.printStackTrace();
                }

                finally {

                    totalQty = val2000 + val500 + val200 + val100 + val50 + val20 + val10 + val5 + val2 + val1;
                    totalValue = tval2000 + tval500 + tval200 + tval100 + tval50 + tval20 + tval10 + tval5 + tval2 + tval1;

                    quantityTotal.setText(totalQty + "");
                    valueTotal.setText(totalValue + "");
                }
            }
        }
    };
like image 161
Sudhin Philip Avatar answered Oct 20 '22 04:10

Sudhin Philip


Please create separate listeners for each view or write switch case for each view Inside the listener

switch(view.getId){
    case: R.id.your_view_id:
    break;
}
like image 27
Nibin Salim Avatar answered Oct 20 '22 05:10

Nibin Salim