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 + "!");
}
});
Related
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();
I'm working with an app that uses Android Intent class to make calls.
I can successfully create a call to a number so that is working.
What I want now is to show the last outgoing call number to show in a TextView.
Also I made that TextView to be clickable so by a click I can redial the number.
I'm using CallLog.Calls.getLastOutgoingCall(getApplicationContext()); to get the last called number.
This works only once in my application.
I start the application, enter a number and it makes a call. The first outgoing called number I set in the TextView. After that I enter a second number which is successfully set in the TextView but when I click to redial the app calls the first number!
Why is that?
My last outgoing number is the second.
Why is it calling the first number?
Also if I restart the app then it redials the second outgoing number.
Here is my code:
public class MainActivity extends ActionBarActivity {
Button btnCall;
TextView number;
EditText calledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCall = (Button) findViewById(R.id.button);
number = (TextView) findViewById(R.id.textView2);
calledNumber = (EditText) findViewById(R.id.editText);
//Gets the last outgoing number from the call log
final String lastCalledNumber = CallLog.Calls.getLastOutgoingCall(getApplicationContext());
btnCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TheNumber = calledNumber.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + TheNumber));
startActivity(callIntent);
number.setText(TheNumber);
}
});
//redial number in TextView by click
number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:" + lastCalledNumber));
startActivity(call);
}
});
}
}
Per the activity lifecycle, onCreate() is only called once and does not run each time your app appears - this would mean that your getLastOutgoingCall() would be correct the first time but wouldn't necessarily work the second time.
If you'd like to run something every time the activity appears, you should move it to onResume() - this ensures it will always be up to date:
String lastCalledNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Same except without the `getLastOutgoingCall()`
}
#Override
public void onResume() {
lastCalledNumber = CallLog.Calls.getLastOutgoingCall(this);
}
I am learning how to use strings and onlclick in java. I have written a programme below which shuffle three names and then outputs them into three buttons.
When I click on Paul, I want the message to be displayed in message box. Since Paul will be in a button each time. I am puzzled on how to attach my message to Paul.
Paul moves around due to the use of array. I understand this is a tough question, but I also know, there are some very clever ppl out there who love a challenge.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generate(View view) {
String [] names = new String[3];
names[0] = "Bob";
names[1] = "Paul";
names[2] = "Mike";
Button btn_a = (Button) findViewById(R.id.a);
Button btn_b = (Button) findViewById(R.id.b);
Button btn_c = (Button) findViewById(R.id.c);
TextView message = (TextView)findViewById(R.id.message);
Arrays.asList(names);
Collections.shuffle(Arrays.asList(names));
btn_a.setText(names[0]);
btn_b.setText(names[1]);
btn_c.setText(names[2]);
}
public void a1(View view) {
}
public void b1(View view) {
}
public void c1(View view) {
}
}
This is a trick practical implementation in Java where a single listener is used for multiple buttons, rather than one listener for each button, so that each button's content determines what happens, not each button's listener. Helps for dynamic button grids (i.e. an 8x8 chessboard) to not define 64 listeners and code them all.
I don't have an Android IDE on hand, so this is pseudo-code, but you should be able to get the gist from this.
//Create a Universal Listener for all our buttons
OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText().toString(); //get the button's name
if(text.equals("Paul")) {
//do anything for Paul ONLY in here
}
}
});
btn_a.setOnClickListener(listener); //give all the buttons the same listener, but only Paul's listener will do anything when you click on it
btn_b.setOnClickListener(listener);
btn_c.setOnClickListener(listener);
Using info from: http://developer.android.com/reference/android/widget/Button.html and https://stackoverflow.com/a/5620816/2958086
I am new to android development using JAVA, and I'm having an issue with a simple app I created. (Please don't laugh at it!)
All it's supposed to do is display a number in an editable textview; The three buttons on this main activity are a +, -, and reset. Super simple, right? I can't tell what I did wrong, but everytime I run the app to test, and then click on any of the buttons, it exits the app and goes back to the android home screen. Not sure what I did wrong... but here's the code I have so far:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_Plus;
Button btn_Minus;
Button btn_Reset;
final EditText sCount= (EditText) findViewById(R.id.txtCount);;
btn_Plus=(Button)findViewById(R.id.btnPlus);
btn_Minus=(Button)findViewById(R.id.btnMinus);
btn_Reset=(Button)findViewById(R.id.btnReset);
//One way I tried to work it
btn_Plus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//gets the text in the textview, makes it a string,
//converts it to an int, calculates, then puts it
//back.
String iCounter = sCount.getText().toString();
int iCount = Integer.parseInt(iCounter);
iCount += 1;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
//The second way I tried -- neither way works.
bM.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = Integer.parseInt(sCount.getText().toString());
iCount -= 1;
if(iCount >0)
iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
bR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
}
}
Thanks so much!
Use a TextView instead, unless you plan to allow the user to directly enter the number (which it seems like you don't). Rather than do text manipulation to get the number, why don't you just store the number as a member variable, update that when a button is pressed, and change the text accordingly? This is how I would write it:
public class Main extends Activity {
int count = 0;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txtCount);
findViewById(R.id.btnPlus).setOnClickListener(this);
findViewById(R.id.btnMinus).setOnClickListener(this);
findViewById(R.id.btnReset).setOnClickListener(this);
}
private void updateText() {
String text = Integer.toString(count);
textView.setText(text);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnPlus:
count++;
break;
case R.id.btnMinus:
count--;
break;
case R.id.btnReset:
count = 0;
break;
}
updateText();
}
}
I am splitting a string and putting the result in edittexts using a loop so that the user can edit the data.thereafter he can save all the data in
the edittexts by just pressing the final button.Problem is i don't know how to get each value from the edittext when he pressses the save button.this is my code:
EditText etstringone,etstringtwo;
Button btn_save;
btnsave=new Button(this);
String mystring="somevalue";
String del="\\|";
String[] splitResult = mystring.split(del);
for (String e : splitResult)
{
etstringone=new EditText(this);
etstringtwo=new EditText(this);
etstringone.setText(splitResult[0]);
etstringtwo.setText(splitResult[1]);
mylayout.addView(etstringone);
mylayout.addView(etstringtwo);
}
mylayout.addView(btnsave);
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
how do i go about this?thanks.
NB:emphasis on using a single button to get all the data,i managed to do a scenario for apppending a new save button each time the
loop runs but its not neat.
The edit texts are dynamically created, but you can still access the object. Either cache a reference to the edit texts or walk the child-views of your "mylayout".
EDIT
From your code, you declare
EditText etstringone,etstringtwo;
You instantiate them
etstringone=new EditText(this);
etstringtwo=new EditText(this);
Now I don't know the scope of this (are they local vars, class/member vars?) if class vars, you can reference them in the
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
body. eg
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String e1 = etstringone..getText().toString();
}
});