Stuck with Android studio - java

I am currently building an app where the user enters their name and when you press the button it should say "Welcome, (whatever name the user entered) in a Toast.
I feel like I have everything write but it's not working. It lets me type in my name but when I click the button nothing happens. What am I doing wrong?
Here's my code
public class MainActivity extends AppCompatActivity {
private EditText input;
private Button click;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText5);
click = (Button) findViewById(R.id.outputBTN);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), input.getText(). "Welcome ", Toast.LENGTH_SHORT).show();
}
});
}
}

You have to use the toString(); method because input.getText() returns you an Editable Object!
With the toString() you can convert your Object into a String.
See getText() method: https://developer.android.com/reference/android/widget/EditText.html
Solution:
Toast.makeText(getApplicationContext(),"Welcome," + input.getText().toString(), Toast.LENGTH.SHORT).show();

Related

How to fix "non-static vs static" issue in onCreate() as an input receiver

So I have made a simple app that currently has an EditText input with a button that, onClick, takes user to a specified website + the user input at the end.
However, my EditText input doesn't seem to "connect" with my declared String and therefore no input value gets detected, taking the user straight to the default website as the button is pressed.
I have tried this code:
rns = et.getText().toString();
Where "et" is the declared name of the EditText input, and "rns" is the declared name of the String to read the input.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText et;
String rns; /* Declarations here */
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.regNum); /* regNum is android id */
rns = EditText.getText().toString(); /* This is the line in question,
with a red underscore on "getText() due to being referenced from a static context */
b = (Button) findViewById(R.id.regBut); /* regBut is android id */
b.setOnClickListener(MainActivity.this);
}
public void onClick(View v){
if (v == b){
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse
("https://www.vegvesen.no/kjoretoy/Kjop+og+salg/Kjøretøyopplysninger?
registreringsnummer=" + rns));
startActivity(browserIntent);
}
}
I expected the EditText input field "et" (id = regNum) to detect the user input, but it doesn't at all. One thing I hadn't considered was it not detecting numbers, but I checked and it didn't detect letters alone either.
(Test results were all with the aforementioned code:
rns = et.getText().toString();
)
Your code should have been:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText et;
String rns; /* Declarations here */
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.regNum); /* regNum is android id */
b = (Button) findViewById(R.id.regBut); /* regBut is android id */
b.setOnClickListener(MainActivity.this);
}
public void onClick(View v){
if (v == b){
/* Get the string from the edittext on button press */
rns = et.getText().toString();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse
("https://www.vegvesen.no/kjoretoy/Kjop+og+salg/Kjøretøyopplysninger?registreringsnummer=" + rns));
startActivity(browserIntent);
}
}
You retrieve your EditText via et = (EditText) findViewById(R.id.regNum);, so you should retrieve it using the et variable you just declared:
et.getText().toString()
You are accessing the method directly from class name, (not on the instance of your object) and calling getText() from it, to which in my recollection is not a static method.
Also have you tried checking against the view id on your onClick callback? v == b is not always true in this case.
Try
public void onClick(View v){
if (v.getId() == R.id.regBut){
...
}
}
You can also check out JakeWharton's ButterKnife for injecting views from xml to your code. So you won't have to do findViewById() and you can annotate #OnClick to your respective methods.

How can I use a double between two classes?

I want to build a currency calculator.
There is a plain text and a button.
if somebody scribe a number in the plain text and press the button a dialog will be shown.
BUT the number of the plaintext is everytime 2.1311!
here is my code
//this is the Main Activity
public class MainActivity extends AppCompatActivity {
public void PesoInEuro (View view){
EditText Peso = findViewById(R.id.EuroBetrag);
String amountPeso = Peso.getText().toString();
double amountPesodouble = Double.parseDouble(amountPeso);
double amountEurodouble = amountPesodouble * 46.85;
String amountEuro = String.valueOf(amountEurodouble);
Button buttonOne = (Button) findViewById(R.id.PesoEuro);
buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog(){
DiaPesoEuro exampleDialog = new DiaPesoEuro();
exampleDialog.show(getSupportFragmentManager(), "example Dialog");
}
First of all, it's a good practice to find your views in the OnCreate method.
Secondly, you have read and calculate your values when the user hit the button.
In the code above, you got the data in the PesoInEuro, and when the user presses the button, it shows the retrieved data and your calculation is based on them.

I Would Like To Have A Single Button Display A Different Line Of Text Each Consecutive Time It's Pressed in Android Studio

I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}

How can I make this Variable by UserInput work? android

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.okay);
mEdit = (EditText)findViewById(R.id.name);
final TextView questionOne =(TextView)findViewById(R.id.questionOne);
final String name = mEdit.getText().toString(); //
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
questionOne.setText("Tell me your lucky number, " + name + "!");
} });
}
}
So, my goal is to let the user write his name and then print it out with the setText method. In order to do this I declared the variable "name". But when I run the app and enter my name, it just prints out "Tell me your lucky number, !". So the variable name is missing completely. Can someone tell me what I did wrong with the variable, please?
Thank you in advance!
First of all, you over-wrote the button's one click listener. You were logging the EditText content just fine at one point.
Anyways, this gets the text immediately when the View is loaded. (And unless you put default text into that field, it is an empty string)
final String name = mEdit.getText().toString();
And it is final, so that variable can never even change values.
You need to "react" to the button event. So, do that.
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = mEdit.getText().toString();
questionOne.setText("Tell me your lucky number, " + name + "!");
}
});

Android button with multiple click simple

i am new in android programming, and i am working on my first application, so i just want to know how every time when i click same button it does some action, for example if i have a button called ( next ) and i want to click on it and an image will appear, this one i did it, but i want to click on the same button and show another image view in the same activity.
i have tried some code but with no results
so please if anyone can post a code that explain how i can do it.
Here is a quick example of code...
Rather than show a random image, it will show a random String. All you need to do is just modify it to show images instead.
public class MainActivity extends Activity {
private String[] names = {"Joe", "Mark", "Amanda", "Kelly", "Michael", "Jenny"};
private Button button;
private TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get our views
name = (TextView) findViewById(R.id.name);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
next(); // show random name
}
});
}
// Display random name on button click
private void next() {
name.setText(names[rand()]);
}
// Pick a random number from 0 to names.length
private int rand() {
return new Random().nextInt(names.length);
}
}

Categories

Resources