4 Replies - 2034 Views - Last Post: 03 August 2015 - 10:56 PM

#1 syuen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 15

how to make toast show calculate result.

Posted 02 August 2015 - 03:49 AM

i just done making a calculator .and the calculater result show as textview . so i wanted to put the textview on the toast .

	
    }
	    Toast toast=Toast.makeText(getApplicationContext(),"Result",Toast.LENGTH_SHORT);
	    toast.show();
	  }


Is This A Good Question/Topic? 0
  • +

Replies To: how to make toast show calculate result.

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: how to make toast show calculate result.

Posted 02 August 2015 - 07:30 AM

Pass the result you get from your calculation to the second parameter of method makeText .
Was This Post Helpful? 0
  • +
  • -

#3 DevMichaelSmith   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 26-July 15

Re: how to make toast show calculate result.

Posted 02 August 2015 - 06:59 PM

Are you asking how to make a Toast show the value of a TextView?

MainActivity.java
package teach.googler8898.mikesmith.toastertest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void NewButtonclick(View v){
        //Find the TextView by id and assign it to a reference
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        //Use TextView inside your toast
        Toast toast=Toast.makeText(getApplicationContext(),tv1.getText(),Toast.LENGTH_SHORT);
        toast.show();

    }
}

activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:id="@+id/textView1"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onclick="NewButtonclick" />

</RelativeLayout>

Was This Post Helpful? 0
  • +
  • -

#4 syuen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 15

Re: how to make toast show calculate result.

Posted 02 August 2015 - 08:15 PM

View PostDevMichaelSmith, on 02 August 2015 - 06:59 PM, said:

Are you asking how to make a Toast show the value of a TextView?


Hi . but what i get from your code is a blank toast show out.

this is my calculator code .

import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.onclickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Calculator extends Activity implements onclickListener 
{
	  EditText input1;
	  EditText input2;
	  
	  Button jia;
	  Button jian;
	  Button cheng;
	  Button chu;
	  Button clear;
	  
	  TextView tvResult;
	  
	  String oper = "";
	  
	  public void onCreate(Bundle savedInstanceState) 
	  {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_calculator);

	    input1 = (EditText) findViewById(R.id.etNum1);
	    input2 = (EditText) findViewById(R.id.etNum2);
	    
	    jia = (Button) findViewById(R.id.btnAdd);
	    jian = (Button) findViewById(R.id.btnSub);
	    cheng = (Button) findViewById(R.id.btnMult);
	    chu = (Button) findViewById(R.id.btnDiv);
	    clear = (Button) findViewById(R.id.button1);
	    
	    tvResult = (TextView) findViewById(R.id.tvResult);
	    
	    jia.setonclickListener(this);
	    jian.setonclickListener(this);
	    cheng.setonclickListener(this);
	    chu.setonclickListener(this);
	    clear.setonclickListener(new clear());
	  }
	  
	  public void onclick(View v) 
	  {
	    float num1 = 0;
	    float num2 = 0;
	    float result = 0;
	    
	    if (TextUtils.isEmpty(input1.getText().toString())
	        || TextUtils.isEmpty(input2.getText().toString())) 
	    {
	      return;
	    }
	    num1 = Float.parseFloat(input1.getText().toString());
	    num2 = Float.parseFloat(input2.getText().toString());

	    switch (v.getId()) 
	    {
	    case R.id.btnAdd:
	      oper = "+";
	      result = num1 + num2;
	      break;
	    case R.id.btnSub:
	      oper = "-";
	      result = num1 - num2;
	      break;
	    case R.id.btnMult:
	      oper = "*";
	      result = num1 * num2;
	      break;
	    case R.id.btnDiv:
	      oper = "/";
	      result = num1 / num2;
	      break;
	    default:
	      break;
	    }
	    Toast toast=Toast.makeText(getApplicationContext(),tvResult.getText(),Toast.LENGTH_LONG);
	    toast.show();
	    }
	  class clear implements onclickListener
	  {
		public void onclick(View v) 
		{
			  input1.getText().clear();
			  input2.getText().clear();
	  }
	  }
}

Was This Post Helpful? 0
  • +
  • -

#5 DevMichaelSmith   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 26-July 15

Re: how to make toast show calculate result.

Posted 03 August 2015 - 10:56 PM

I meant to add tvResult.getText().toString() that may be the problem. You can print the value of the TextView as a test just using System.out.println(tvResult.getText().toString());
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1