#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button = (Button) findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
input1 = editTextView1.getText().toString();
input2 = editTextView2.getText().toString();
First initialize the editText just like you initialize the Button and TextView then you are able to get the text from editText
you must have initialize.
(EditText, input1 and input2).
in Activity :
EditText editTextView1;
EditText editTextView2;
String input1;
String input2;
in onCreate :
editTextView1 = (EditText) findViewById(yourEditText1_Id);
editTextView2 = (EditText) findViewById(yourEditText2_Id);
Here is a little code that might help point you in the right direction. Post your code and I'll make the code fit your needs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView3);
EditText editText1 = (EditText) findViewById(R.id.editTextView1);
EditText editText2 = (EditText) findViewById(R.id.editTextView2);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
input1 = editText1.getText().toString();
input2 = editText2.getText().toString();
break;
}
}
Just going to make some of the answers above a bit clearer. An Activity is like a Class in a regular Java program. That means that it has fields (variables) and methods (functions). You can't access a field from within a method unless you declare in within the body of the class, like this:
public class MainActivity extends Activity implements OnClickListener{
EditText input1;
protected void onCreate(Bundle savedInstanceState){
// normal onCreate stuff
input1 = (EditText) findViewById(R.id.input1);
// this should be the id in your layout.xml file
}
public String getText(){
String s = input1.getText().toString().trim();
return s;
}
}
Note the use of the trim() method, which will remove any extra whitespace the user may have added at the end of their input.
Related
I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
I have 4 fields and 2 buttons, one of the buttons is reset, I want that when I click on the button it clears all the fields
A simple example:
public class MyActivity extends AppCompatActivity{
private EditText editText1, editText2, editText3, editText4;
private Button btn_reset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
//javaVariable = (Cast type) findViewById(R.id.IdFromXML);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here's the actions to do when btn_reset is clicked
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
}
});
}
}
Create an onClickListener to that reset button
Button resetButton = (Button) findViewById(R.id.reset);
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg1) {
textView1.setText(null);//use editText if fields are EditText
textView2.setText(null);
textView3.setText(null);
textView4.setText(null);
}
});
you can use this way on AutoCompleteTextView, EditText and TextView.
I want to create a simple program by put data in edit text to display in text view in the same activity.it is look like below image.
How can I do this? Thanks.
Try this one
EditText editText = (EditText) findViewById(R.id.edittext);
TextView textView = (TextView) findViewById(R.id.textview);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(View view){
String text = editText.getText().toString();
textView.setText(text);
});
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView yourTextview = (TextView)findViewById(R.id.yourTextviewId);
EditText yourEditText = (EditText)findViewById(R.id.yourTextviewId);
Buton yourOkButton = (Button)findViewById(R.id.yourOkButtonId);
yourOkButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
yourTextview.setText(yourEditText.getText().toString());
}
});
}
Hope it helps
In Kotlin you can done like this :
yourButtonID.setOnClickListener
{
val textView = findViewById<View>(R.id.yourTextViewID) as TextView
textView.setText(yourEditTextID.text.toString())
yourTextViewID.text!!.clear()
}
I have been coding a simple android Java program, so far I included an ImageView, button and an EditText in the activity_main.xml. However, when I run it, with this MainActivity java class:
public class MainActivity extends Activity {
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
is not working and it returns a fatal error I cannot resolve. It says:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo, java.lang.NullPointerException
How can I fix it? Thanks a lot!
You should move these initializations to onCreate :
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
i.e :
private Button start;
private EditText nameField;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
nameField = (EditText) findViewById(R.id.nameField);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
When you call findViewById(R.id.start) in the member initialization expression, the Activity instance is not initialized yet, which causes the NullPointerException.
I am a beginner, I'm trying to build a calculator.
I have a problem.
I write the answer like this:
ans += Double.parseDouble(etResult.getText().toString());
outTest.setText("The answer is " + ans);
This is in an OnClickListenter.
The problem is that I have to communicate with the double ans, which I defined in the beginning of my project, if I want to communicate with the double I need him to be final, but if he is final I cant change him in the OnClickListener.
Can anyone help me and tell me how can I do this? thanks.
Use the usual trick: instead of double ans = 0 declare final double[] ans = {0} and then you'll be able to use ans[0] += ... inside the handler code.
You can writ your code as follows
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Double dbl;
Button btn;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dbl = Double.parseDouble(et.getText().toString());
Log.e("double", "" + dbl);
}
});
}
}
Try it
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Double dbl;
Button btn;
EditText et1, et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dbl = Double.parseDouble(et1.getText().toString());
et2.setText(dbl.toString());
}
});
}
}