I have a edittext field for users input. user can enter values up to ten digit number.when clicking on the button, i need to show the log [ mathematics log ] value of that number in double precision.
That i showned below.
If i input xxxxxxxxxx [ consider this as number ]
The output must look like this yy.yy.
package com.example.logvalue;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
final int noo = Integer.parseInt(edit.getText().toString());
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(Main.this, String.format("%.2f", Math.log10(noo)) , Toast.LENGTH_LONG).show();
}
});
}
}
Use the String.format("%.2f", Math.log10(number)) syntax.
The String class has this format function exactly for these.
I assume you want logarithm with base of 10.
Explanation: String.format is the Java-equavalient of the traditional printf from C++. The % symbol means there is something to be fetched from the argument list, the point means that we want to specify the number of decimals, and hence we put a 2 after that point.
Hope I helped.
Related
I'm fairly new to Android Studio. I've tried to create an app that calculates how much you can spend everyday based on you income. My problem is that it doesn't display any decimals. Heres the code:
package com.example.amazi.howmuchmoney;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1,et2;
TextView tvResult;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
tvResult=(TextView) findViewById(R.id.textView3);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int num1=Integer.parseInt(et1.getText().toString());
int num2=Integer.parseInt(et2.getText().toString());
int result=num1/num2;
tvResult.setText("Each day you can spend "+result);
}
});
}
}
Mathematical operations performed on integers are exact, therefore;
(int) 3 / (int) 2 = (int) 1
Using double as your data type will fix your problem, although;
Integer.parseInt
Will still return an integer value, try;
Double.parseDouble
So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes
I used samples from similar code to make this, unfortunately I'm not to sure what I did wrong.
The purpose of this app is to output text entered in a field to a TextView, where I changed the color, when a button is pressed.
package edu.wmich.project3
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class Main extends Activity {
String txtResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button text =(Button) findViewById(R.id.btnColor0);
final TextView result = ((TextView) findViewById(R.id.txtResult));
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtResult= getText(R.id.txtField0).toString();
result.setText(txtResult);
}
});
}
}
txtResult = (findViewById(R.id.txtField0)).toString();
will solve...
the problem is that you're using the method getText() from the Activity which has nothing to do with the TextField you're dealing with.
...what is the type of view of R.id.txtField0? I guess with this you can take it from here.
I am trying to make a basic conversion tool that i will expand on later. But when i take the information from the EditText and covert it to Integer then try to multiply it, my application crashes on the button click.
My Java code is as follows, New member to this site, but have been using it for help for weeks now, So thanks you have all helped out so much already
package com.example.mayconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Feetyards extends Activity{
EditText feet;
Button convertFeet;
int formula;
TextView displayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.feet_yards);
displayText =(TextView) findViewById(R.id.displayText);
formula = 3;
convertFeet = (Button) findViewById(R.id.buttonFeet);
final EditText feet = (EditText) findViewById(R.id.editTextFeet);
convertFeet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String data = feet.getText().toString();
final int dataint = Integer.parseInt(data);
displayText.setText(dataint + formula);
}
});
}
}
It always helps to post your LogCat errors, but it looks like you have two problems:
You are accidentally creating two variables with the same name feet:
This creates a local feet variable (not what you want)
final EditText feet = (EditText) findViewById(R.id.editTextFeet);
Use this instead to initialize your class variable feet:
feet = (EditText) findViewById(R.id.editTextFeet);
You need to convert your "formula" back into a String:
displayText.setText((dataint + formula) + "");
When you use setText(Int), this tried to load a String value stored in your resources, while setText(String) displays the String passed to the function.
replace this line
displayText.setText(dataint + formula);
with
displayText.setText((dataint + formula)+"");
ok if you didnt see my previous question I asked how 2 Command button to import text to textview from edittext using Scanner? Here is what I have done:
I keep geting this error
"Syntax error on token
"setOnClickListener",
VariableDeclaratorId expected after
this token"
what am I missing or doing wrong?
package test.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.Scanner;
import android.R.layout;
public class test extends Activity {
Scanner what = (new Scanner(System.in));
private int addbtn;
Button btn = (Button) findViewById(addbtn);
btn.setOnClickListener = (new OnClickListener() {
public void onClick(View v) {
int txtbox;
EditText txt = (EditText) findViewById(txtbox);
int tv1;
TextView txt1 = (TextView) findViewById(tv1);
txt.setText( txt.getText().toString() );}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
You're missing a closing brace } on the new OnClickListener block.
Also you shouldn't be attempting Button btn = (Button) findViewById(addbtn); before the onCreate(...) method has called setContentView(...).
On top of that, addbtn isn't a valid resource id.
Use findViewById() method before setContentView(...).
On top of which your closing brackets look messed up. What's matching the ( before the new onClickListener?