java - How do i show complete calcualtion in textview? -


how show complete calcualtion in textview using parenthesis? example: 2+3-((4/2)*9) making calculator app uses parenthesis , remember calculations history , should display math operation in textview.

    here code:       package com.example.calculater;      import android.app.activity;     import android.os.bundle;     import android.view.menu;     import android.view.view;     import android.view.view.onclicklistener;     import android.widget.button;     import android.widget.textview;      public class mainactivity extends activity implements onclicklistener {          textview textdisplay;         @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);             textdisplay = (textview) findviewbyid(r.id.edittext1);              button b1 =  (button) findviewbyid(r.id.btn1);             button b2 =  (button) findviewbyid(r.id.btn2);             button b3 =  (button) findviewbyid(r.id.btn3);             button b4 =  (button) findviewbyid(r.id.btn4);             button b5 =  (button) findviewbyid(r.id.btn5);             button b6 =  (button) findviewbyid(r.id.btn6);             button b7 =  (button) findviewbyid(r.id.btn7);             button b8 =  (button) findviewbyid(r.id.btn8);             button b9 =  (button) findviewbyid(r.id.btn9);             button b0 =  (button) findviewbyid(r.id.btn0);             button multiply1 =  (button) findviewbyid(r.id.multiply);             button divide1 =  (button) findviewbyid(r.id.divide);             button plus1 =  (button) findviewbyid(r.id.plus);             button minus1 =  (button) findviewbyid(r.id.minus);             button equal1 =  (button) findviewbyid(r.id.equal);             button clear1 =  (button) findviewbyid(r.id.clear);             button back1 =  (button) findviewbyid(r.id.backspace);             button dot1 =  (button) findviewbyid(r.id.decimal);             button plusminus1 =  (button) findviewbyid(r.id.plusminus);             button history1=  (button) findviewbyid(r.id.history);             button open1 =  (button) findviewbyid(r.id.open);             button close1 =  (button) findviewbyid(r.id.close);              b1.setonclicklistener(this);             b2.setonclicklistener(this);             b3.setonclicklistener(this);             b4.setonclicklistener(this);             b5.setonclicklistener(this);             b6.setonclicklistener(this);             b7.setonclicklistener(this);             b8.setonclicklistener(this);             b9.setonclicklistener(this);             b0.setonclicklistener(this);             multiply1.setonclicklistener(this);             divide1.setonclicklistener(this);             plus1.setonclicklistener(this);             minus1.setonclicklistener(this);             equal1.setonclicklistener(this);             clear1.setonclicklistener(this);             back1.setonclicklistener(this);             dot1.setonclicklistener(this);             plusminus1.setonclicklistener(this);             history1.setonclicklistener(this);             open1.setonclicklistener(this);             close1.setonclicklistener(this);     }         @override         public boolean oncreateoptionsmenu(menu menu) {             // inflate menu; adds items action bar if present.             getmenuinflater().inflate(r.menu.main, menu);             return true;         }          int clear_flag = 0;         string sign_flag = "";         double total = 0.0;         int last_button = 0;          public void shownum (string number){             if(clear_flag==1){                 textdisplay.settext("");                 clear_flag=0;             }             else if(textdisplay.gettext()=="0"){                 textdisplay.settext("");             }             textdisplay.settext(textdisplay.gettext() + number);         }           public void showsign(string sign){             if(last_button==r.id.plus || last_button==r.id.minus || last_button==r.id.multiply              || last_button==r.id.divide){              }             else{                 clear_flag = 1;//set flag                 double newnumber = double.parsedouble(textdisplay.gettext().tostring());                 if(sign_flag == "" || sign_flag == "="){                     total = newnumber;                     textdisplay.settext(total.tostring());                   }                 else if(sign_flag == "+"){                     total = total + newnumber;                     textdisplay.settext(total.tostring());                  }                 else if(sign_flag == "-"){                     total = total - newnumber;                     textdisplay.settext(total.tostring());                 }                 else if(sign_flag == "*"){                     total = total*newnumber;                     textdisplay.settext(total.tostring());                 }                 else if(sign_flag == "/"){                     total = total/newnumber;                     textdisplay.settext(total.tostring());                   }}             sign_flag = sign;             }          @override         public void onclick(view v) {             if(v.getid() == r.id.btn0){                 shownum ("0");             }             else if(v.getid() == r.id.btn1){                 shownum ("1");             }             else if(v.getid() == r.id.btn2){                 shownum ("2");             }             else if(v.getid() == r.id.btn3){                 shownum ("3");             }             else if(v.getid() == r.id.btn4){                 shownum ("4");             }             else if(v.getid() == r.id.btn5){                 shownum ("5");             }             else if(v.getid() == r.id.btn6){                 shownum ("6");             }             else if(v.getid() == r.id.btn7){                 shownum ("7");             }             else if(v.getid() == r.id.btn8){                 shownum ("8");             }             else if(v.getid() == r.id.btn9){                 shownum ("9");             }             else if(v.getid() == r.id.clear){                 textdisplay.settext("");////originally 0 ""                 total = 0.0;                 sign_flag = "";             }             else if(v.getid() == r.id.decimal){                 if(clear_flag==1){                     textdisplay.settext("");                     clear_flag = 0;                 }                 if(textdisplay.gettext().tostring().indexof(".")<0){                     textdisplay.settext(textdisplay.gettext() + ".");                     }}             else if(v.getid() == r.id.backspace){                 if(textdisplay.gettext().tostring().length()>0){                     int start = 0;                     int end = textdisplay.gettext().tostring().length()-1;                     string newtext = textdisplay.gettext().tostring().substring(start,end);                     textdisplay.settext(newtext);             }}             else if(v.getid() == r.id.plus){                 showsign("+");              }             else if(v.getid() == r.id.minus){                 showsign("-");              }             else if(v.getid() == r.id.multiply){                 showsign("*");              }             else if(v.getid() == r.id.divide){                 showsign("/");              }             else if(v.getid() == r.id.equal){                 double newnumber = double.parsedouble(textdisplay.gettext().tostring());                 if(sign_flag == "+"){                     total = total+newnumber;                     textdisplay.settext(total.tostring());                 }                 else if(sign_flag == "-"){                     total = total-newnumber;                     textdisplay.settext(total.tostring());                 }                 else if(sign_flag == "*"){                     total = total*newnumber;                     textdisplay.settext(total.tostring());                 }                 else if(sign_flag == "/"){                     total = total/newnumber;                     textdisplay.settext(total.tostring());                 }                 sign_flag = "=";     }       //when minus pressed before number input, applications closes.                  else if (v.getid() == r.id.plusminus){                 string number = textdisplay.gettext().tostring();                 if(number == null) return;  //exits function                 if(number.equals("")) return;  //exits function                // textdisplay.settext("please enter number first press +/-");                 double newnumber = double.parsedouble(number);                 total = newnumber * (-1);                 textdisplay.settext(total.tostring());                  }                  else if (v.getid()== r.id.open){                   }                 else if (v.getid()== r.id.close){                  }              last_button = v.getid();     }}      here xml code:     <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <edittext         android:id="@+id/edittext1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginright="5pt"         android:layout_weight="0.58"         android:inputtype="numberdecimal" >      </edittext>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <button             android:id="@+id/clear"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="clear" />           <button             android:id="@+id/divide"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="/" />             <button             android:id="@+id/multiply"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="*" />          <button             android:id="@+id/backspace"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="back" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <button             android:id="@+id/btn7"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="7" />          <button             android:id="@+id/btn8"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="8" />          <button             android:id="@+id/btn9"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="9" />          <button             android:id="@+id/minus"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="-" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <button             android:id="@+id/btn4"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="4" />          <button             android:id="@+id/btn5"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="5" />          <button             android:id="@+id/btn6"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="6" />          <button             android:id="@+id/plus"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="+" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <button             android:id="@+id/btn1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="1"               />          <button             android:id="@+id/btn2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="2" />          <button             android:id="@+id/btn3"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="3" />          <button             android:id="@+id/plusminus"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="+/-" />      </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <button             android:id="@+id/btn0"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="0" />          <button             android:id="@+id/decimal"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="." />          <button             android:id="@+id/open"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="( " />          <button             android:id="@+id/close"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text=")" />      </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0.58" >          <linearlayout             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:layout_weight="0.20" >              <linearlayout                 android:layout_width="wrap_content"                 android:layout_height="match_parent"                 android:layout_weight="0.04"                 android:orientation="vertical" >                  <button                     android:id="@+id/history"                     android:layout_width="252dp"                     android:layout_height="wrap_content"                     android:text="history" />                  <button                     android:id="@+id/equal"                     android:layout_width="248dp"                     android:layout_height="wrap_content"                     android:text="@string/_" />              </linearlayout>          </linearlayout>      </linearlayout>  </linearlayout> 

what codemagic said, store each in variable , maybe make 1 final string variable hold string.format. maybe

string finalstring = string.format("this result %s , %s" ,          variablename, variablename);  

then

textdisplay.settext(finalstring); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -