Kharl Mccatty
Kharl Mccatty

Reputation: 45

How to take integer from user in android,manipulate and place in another textbox

im fairly new to programming and I was trying to teach myself how to write apps for android devices. Im currently using the original user guide from the official android developers website. Im trying to take an integer input from the user and once a button is clicked the number would be multiplied by .1 *(a number taken from a second user edittext box). For example in the first textbox the user would input 100 and in the second textbox the user would input 10. The result in a third textbox would be 10/100*100, which would be 10. How would would I go about doing this and ensuring that the result is displayed in a third textbox.

Here is the code I have so far

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class FirstActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.discountpricecalculator.MESSAGE";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view) {
    //Do something in response to button

}

}

Upvotes: 0

Views: 2513

Answers (2)

britzl
britzl

Reputation: 10242

Here's a relatively complete example. You'll have to figure out the view XML for yourself.

public class YourActivity extends Activity {

    private EditText first;
    private EditText second;
    private TextView result;

    private Button button;


    public void onCreate(Bundle instanceState) {
        super.onCreate(instanceState);
        first = (EditText)findViewById(R.id.idoffirst);
        second = (EditText)findViewById(R.id.idofsecond);
        result = (TextView)findViewById(R.id.idofresult);
        button = (Button)findViewById(R.id.idofbutton);

        // limit input to numbers only (or use android:inputType="number" in XML)
        first.setInputType(InputType.TYPE_CLASS_NUMBER);
        second.setInputType(InputType.TYPE_CLASS_NUMBER);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // NOTE: You should deal with number format exceptions here (use try-catch)
                float firstValue = Float.parseFloat(first.getText());
                float secondValue = Float.parseFloat(second.getText());
                result.setText(Float.toString(firstValue * secondValue));
            }
        });
    }   
}

Upvotes: 1

karan
karan

Reputation: 8853

take edittext values using

EditText e1 = (EditText) findViewById(R.id.edittext1);
String myEditValue = e1.getText().toString();
int value = Integer.parseInt(myEditValue);  //similarly for second text view

now do your desired mathematical operation

value 3=value2*value1

similarly to set values

EditText e3 = (EditText) findViewById(R.id.edittext3);
e3.setText(Value3.toString());

Upvotes: 3

Related Questions