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.
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();
}
}
This is the simple code I applied.Please explain why such behavior is taking place.Oncreate is not updating then why on click of button it can be called again and again
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gaggan();
}
public void gaggan(){
Button bt =(Button) findViewById(R.id.button4);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText e2= (EditText) findViewById(R.id.editText);
EditText e3= (EditText) findViewById(R.id.editText3);
int num1=Integer.parseInt(e2.getText().toString());
int num2=Integer.parseInt(e3.getText().toString());
int sum=num1+num2;
Toast.makeText(MainActivity.this,Integer.toString(sum), Toast.LENGTH_SHORT).show();
}
bt.setOnClickListener will register your click-listener (new View.OnClickListener() {..} on the button and thus, whenever the button is clicked, the onClick() method of your click-listener is executed.
The Android docs have a more detailed explanation
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()
}
#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.
Displaying two random numbers and counting scores for odd and even turns. When one of it reaches to 100, the application stops.
This is my java file of application
public class Board_Play1 extends Activity {
int d=0,a=0,b=0,turn=2;
Random random = new Random();
EditText diceno = (EditText) findViewById(R.id.editText1);
EditText p1 = (EditText) findViewById(R.id.editText2);
EditText p2 = (EditText) findViewById(R.id.editText3);
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
diceno.setText(String.valueOf(d));
p1.setText(String.valueOf(a));
p2.setText(String.valueOf(b));
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}
else
{
d=random.nextInt(6)+1;
diceno.setText(String.valueOf(d));
}
if(turn%2==0)
a+=d;
else
b+=d;
if(a>100)
a-=d;
if(b>100)
b-=d;
p1.setText(String.valueOf(a));
p2.setText(String.valueOf(b));
turn++;
}
a=0;b=0;
}
}
It doesn't open and give an error saying Unfortunately your app has stopped. Why is this happening? What can I change?
Move all this inside onCreate after setContentView
EditText diceno;
EditText p1;
EditText p2 ;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
diceno = (EditText) findViewById(R.id.editText1);
p1 = (EditText) findViewById(R.id.editText2);
p2 = (EditText) findViewById(R.id.editText3);
findViewById looks for a view in the current view hierarchy. You ned to set the content of the layout to the activity first and then initialize vies by using findViewById.
Also there is no need to re- initialize editText in button onClick. Get rid of the below in the same
EditText diceno = (EditText) findViewById(R.id.editText1);
Also move this
Button button = (Button) findViewById(R.id.button1);
before the while loop. no need to initialize everytime