How to Multiply Two EditText Values and Display into Third EditText

Posted by

Hello all and welcome to easytechsolutions.info i hope you all are good and enjoy android programming.So in today’s post we are going to play around EditText and make a simple app in which there is listview in which we are going to multiply two EditText value and display it into third one so without further due lets get started !

Below are the steps you need to do:-

Step 1:- Open up your android studio and make new project select blank activity and you are good to go.

Step 2:- Open up activity_main.xml and copy paste below code in it:-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.listviewedittextoutputexample.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        android:columnWidth="90dp"
        android:numColumns="3"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center">
    </ListView>

    <Button
        android:id="@+id/multiply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ff00"
        android:text="Multiply"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        android:fontFamily="sans-serif"
        android:textStyle="bold"/>

</RelativeLayout>

In this step we define listview and also a button to perform calculation.

Step 3:- Make a new layout file inside res/layout/list_grid.xml and copy paste below code in it:-

list_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginRight="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#DCC6E0"
        android:inputType="number"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:fontFamily="sans-serif"
        android:hint="Quantity"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginLeft="8dp"
            android:fontFamily="sans-serif-condensed"
            android:textColor="#000"/>


        <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#DCC6E0"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:hint="Price"
        android:inputType="number"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"/>

        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="
            android:fontFamily="sans-serif-condensed"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:textColor="#000"/>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#87D37C"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:hint="Total"
        android:inputType="number"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"/>
    </LinearLayout>
</LinearLayout>

In above step we define our layout for one particular field so that when we populate listview through adapter class it maintain and display every listview field exactly in same proportion.

Step 4:- Make a new java class inside package and copy paste below code in it:-

CustomAdapter.java

package com.listviewedittextoutputexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

/**
 * Created by AJAY THAKUR on 9/16/2017.
 */

public class CustomAdapter extends BaseAdapter {

    private Context context;
    private String[] result;

    public CustomAdapter(Context context, String[] result) {
        this.context = context;
        this.result = result;
    }

    @Override
    public int getCount() {
        return 15 ;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 500;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null){

            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView =layoutInflater.inflate(R.layout.list_grid,parent,false);
            viewHolder.edit1 = (EditText) convertView.findViewById(R.id.editText1);
            viewHolder.edit2 = (EditText) convertView.findViewById(R.id.editText2);
            viewHolder.edit3 = (EditText) convertView.findViewById(R.id.editText3);

            convertView.setTag(viewHolder);
        }
        return convertView;
    }
}

In above step we make a custom class to populate listview as we want .

Step 5:- Open up MainActivity.java and copy paste below code in it:-

MainActivity.java

package com.listviewedittextoutputexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import static android.R.attr.button;

public class MainActivity extends AppCompatActivity {

    String[] result = {""};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Defining Variables:-
        EditText editText = (EditText) findViewById(R.id.editText1);
        EditText editText1 = (EditText) findViewById(R.id.editText2);
        EditText editText2 = (EditText) findViewById(R.id.editText3);
        Button button = (Button) findViewById(R.id.multiply);

        //Defining GridView Variable and FindViewById:-
        final ListView list = (ListView) findViewById(R.id.listView);

        //Setting Adapter instance and passing 2 params in it(Context,array name):-
        CustomAdapter adapter = new CustomAdapter(getApplicationContext(), result);
        list.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < list.getCount(); i++) {
                    int a = 0;
                    EditText edit1 = (EditText) getViewByPosition(i, list).findViewById(R.id.editText1);
                    EditText edit2 = (EditText) getViewByPosition(i, list).findViewById(R.id.editText2);
                    EditText edit3 = (EditText) getViewByPosition(i, list).findViewById(R.id.editText3);
                    if (!edit1.getText().toString().equals("") && !edit2.getText().toString().equals("")) {
                        a = Integer.parseInt(String.valueOf(edit1.getText())) * Integer.parseInt(String.valueOf(edit2.getText().toString()));
                    }
                    edit3.setText(String.valueOf(a));
                }
            }
        });

    }

    public View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }
}

So in this final step we just make ArrayAdapter and instance of CustomAdapter class to populate custom listview now save all and run you will get to see listview in which every row contains 3 EditText enter any digits in first two and hit button you will get to see the multiply value in third EditText.

So that’s it for now hope you will now know how to retain and display value in EditText.
Our Facebook Page URL:-https://www.facebook.com/easytechsolutions.info/

Leave a Reply

Your email address will not be published. Required fields are marked *