How can I make a cursor like the calculator app on my phone?


I am a learner of Android App Development.

Recently, I have taken a calculator app project, and everything is complete I want it to have a cursor like the calculator on my phone, and it will blink. I tried in my project, but I couldn't. Now I want it to look like my phone

How can I do this

This is my phone's calculator and cursor, which is what I want to do in my app. enter image description here

And this is the project I made that doesn't have a cursor. enter image description herestrong text

And this is my xml design

fragment_calculator.xml

<?xml version="1.0" encoding="utf-8"?>
   <androidx.constraintlayout.widget.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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        tools:context=".view.fragment.CalculatorFragment">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/Display"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_margin="2dp"
        android:background="@drawable/ed_bg"
        android:textSize="40sp"
        android:textCursorDrawable="@drawable/cursor_color"
        android:gravity="end"
        android:textColor="@color/black"
        android:cursorVisible="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:inputType="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#37F6D1FA"
        android:columnCount="4"
        android:padding="6dp"
        android:paddingBottom="20dp"
        android:rowCount="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">


        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:backgroundTint="#000000"
            android:gravity="center"
            android:text="AC"
            android:textAllCaps="false"
            android:textColor="#FF9800"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:backgroundTint="#000000"
            android:gravity="center"
            android:text=""
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_back"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:backgroundTint="#000000"
            android:gravity="center"
            android:text="←"
            android:textAllCaps="false"
            android:textSize="24sp"
            android:textStyle="bold">


        </androidx.appcompat.widget.AppCompatButton>

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_div"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="/"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="7"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="8"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="9"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_mul"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="×"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="4"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="5"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="6"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_sub"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="-"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="1"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="2"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="3"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="+"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_percent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="%"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="0"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_dot"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="."
            android:textAllCaps="false"
            android:textSize="24sp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_equal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_margin="6dp"
            android:background="@drawable/btn_bg"
            android:gravity="center"
            android:text="="
            android:textAllCaps="false"
            android:textSize="24sp" />

    </GridLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

And this is my Java code

CalculatorFragment.java

package com.dreemit.toolsmaster.view.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dreemit.toolsmaster.R;

public class CalculatorFragment extends Fragment {

//TextView tvDisplay;

EditText Display;
private  StringBuilder currentInprt =new StringBuilder();

public CalculatorFragment (){


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_calculator, container, false);


    Display=view.findViewById(R.id.Display);
    Display.setSelection(Display.getText().length());
    Display.setCursorVisible(true);
   // Display.setFocusableInTouchMode(true);
    Display.setText("0");

    Toast.makeText(getContext(), "Simple Calculator", Toast.LENGTH_SHORT).show();

    int [] allButtons = {
            R.id.btn_0,R.id.btn_1,R.id.btn_2,R.id.btn_3,R.id.btn_4,
            R.id.btn_5,R.id.btn_6,R.id.btn_7,R.id.btn_8,R.id.btn_9

    }; // array of int........

    for (int id : allButtons){

        //.....every number of btn click listener if user click any number of button it add in [ currentInput] and show in tvDisplay
        Button button =view.findViewById(id);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentInprt.append(button.getText());
                Display.setText(currentInprt.toString());



            }
        });

    }//.....end of loop

    //......Clear button action
    view.findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            currentInprt.setLength(0);
            Display.setText("0");

        }
    });//....End..of..Clear button


    //......Equal button action
    view.findViewById(R.id.btn_equal).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
               double result= eval(currentInprt.toString());
               Display.setText(String.valueOf(result));
                currentInprt=new StringBuilder(String.valueOf(result));

            }catch (Exception e){

                Display.setText("Error");

            }

        }
    });
    //......Clear button end





    //....Add  button action...
        view.findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                currentInprt.append("+");
                Display.setText(currentInprt.toString());


            }
        });//....Add button end


        //......Sub button action
view.findViewById(R.id.btn_sub).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        currentInprt.append("-");
        Display.setText(currentInprt.toString());

    }
});//....Sub button end.....

  //.....multiplay button action
view.findViewById(R.id.btn_mul).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        currentInprt.append("*");
        Display.setText(currentInprt.toString());


    }
});//....multiplay button end

//...Button divide action
view.findViewById(R.id.btn_div).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        currentInprt.append("/");
        Display.setText(currentInprt.toString());

    }

});//...End button divide

return view;


}//....last of oneCreate.......


//..Simple.... eval function from here ...........
public static double eval(final String str) {
        return new Object() {
            int pos = -1, ch;

            void nextChar() {
                ch = (++pos < str.length()) ? str.charAt(pos) : -1;
            }

            boolean eat(int charToEat) {
                while (ch == ' ') nextChar();
                if (ch == charToEat) {
                    nextChar();
                    return true;
                }
                return false;
            }

            double parse() {
                nextChar();
                double x = parseExpression();
                if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
                return x;
            }

            double parseExpression() {
                double x = parseTerm();
                for (;;) {
                    if      (eat('+')) x += parseTerm(); // addition
                    else if (eat('-')) x -= parseTerm(); // subtraction
                    else return x;
                }
            }

            double parseTerm() {
                double x = parseFactor();
                for (;;) {
                    if      (eat('*')) x *= parseFactor(); // multiplication
                    else if (eat('/')) x /= parseFactor(); // division
                    else return x;
                }
            }

            double parseFactor() {
                if (eat('+')) return parseFactor(); // unary plus
                if (eat('-')) return -parseFactor(); // unary minus

                double x;
                int startPos = this.pos;
                if (eat('(')) { // parentheses
                    x = parseExpression();
                    eat(')');
                } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
                    while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
                    x = Double.parseDouble(str.substring(startPos, this.pos));
                } else {
                    throw new RuntimeException("Unexpected: " + (char)ch);
                }

                return x;
            }
        }.parse();
    }




}//....last
2
Jul 11 at 5:16 PM
User AvatarMd Kaium Hossain
#java#android#android-studio

No answer found for this question yet.