can i know pls why the input variable why it's giving me error always, thank you so much.
This is my code below:
public class MainActivity extends AppCompatActivity {
public TextView textView;
public EditText editText;
public Button btn;
public int input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
editText = findViewById(R.id.editText);
btn = findViewById(R.id.btn);
` input= Integer.parseInt(editText.getText().toString());`
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(input );
}
});
}
}
The error is happening as the String cant be cast to an integer. This could be because your editText is empty or that you have characters that are not integers in there (E.g. Letters). Therefor you should use try and catch to catch the error.
Currently you are trying to parse the input string from editText in onCreate() as soon as the view is created. This will try to parse the default text associated with the editText. I think what you want is to parse the input at the time of clicking button. In that case move the input parsing code inside onClick(). Also it is better to catch NumberFormatException when calling parseInt() if you cannot guarantee that the text entered in the EditText is always an integer.
#Override
public void onClick(View v) {
android.util.Log.i("MyActivity", "onClick: edit text = " + editText.getText());
input= Integer.parseInt(editText.getText().toString());
textView.setText(input );
}
I have two date fields (no, not the pickers) as fromDate and toDate. When I click on my Submit button, in the onResume(), I have validations in place for both date fields. When I enter an invalid value (not from syntax, locale, etc. point of view) for toDate and click Submit, I correctly see the toast. Then, I enter the correct the value and click Submit. The toast still appears ! In other words, the corrected date value is not being received.
I guess, I am missing the activity life-cycle w.r.t. toasts. (Each Toast is immediately followed by a return.) Can you please suggest what should be the correct flow to handle this error followed by correction ?
public class MainActivity extends Activity {
private Locale l ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set locale;
l = getResources().getConfiguration().locale;
}
#Override
protected void onResume() {
super.onResume();
addButtonListener();
}
private void addButtonListener() {
Button submitButton = (Button) findViewById(R.id.buttonSubmit);
submitButton.setOnClickListener(new OnClickListener() {
EditText fromDateText = (EditText) findViewById(R.id.fromDate);
EditText toDateText = (EditText) findViewById(R.id.toDate);
#Override
public void onClick(View v) {
String s;
if (fromDateText.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.err_fromDate_1, Toast.LENGTH_SHORT).show();
return;
}
if (toDateText.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.err_toDate_1, Toast.LENGTH_SHORT).show();
return;
}
}
});
}
}
Edited to add source code.
Make fromDateText and toDateText class variables and init them in onCreate.
I have one edittext. First time my edittext's settext is 1 and I want to clear this text in editttext click and input new keyboard values
I wrote code but not working
price_counter = (EditText) rootView
.findViewById(R.id.strada_price_counter);
price_counter.setText("1");
price_counter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
price_counter.setText("");
}
});
<EditText
android:id="#+id/strada_price_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/strada_buy_btn"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/strada_buy_btn"
android:background="#drawable/input_value"
android:gravity="center"
android:inputType="number">
</EditText>
this code not working if anyone knows solution please help me
You are not supposed to call setOnClickListener on EditText. Register click listener for a button and call settext for EditText on button click.
Update your code to the following:
price_counter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((EditText)v).setText("");
}
});
Try this code... This might be you are looking for
public class MainActivity extends Activity implements OnTouchListener
{
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
et.setText("1");
et.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
et.setText("");
}
return false;
}
}
Can anyone please tell why the if statement never gets executed even though I enter ran in the edit text field and press ok? When the user enters ran and presses the button ok, I want another button to become visible which enables him to stop the current activity. Basically I want to know why the if is skipped.
public class Reciever extends Activity{
protected static final String TAG = null;
private Button ok,stp;
private TextView tv;
private EditText ev;
private String s1,s2,s3,s4;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
s1="nar";
setContentView(R.layout.stop);
tv=(TextView) findViewById(R.id.textView1);
tv.setText(s1);
ev=(EditText) findViewById(R.id.editText1);
ok=(Button) findViewById(R.id.ok_button);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
s2="ran";
tv.setText(ev.getText().toString());
s3=ev.getText().toString();
if(s3==s2)//not going inside this loop
{
stp=(Button) findViewById(R.id.stopb);
stp.setVisibility(View.VISIBLE);
stp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
});
}
}
if(s3==s2)//no
just replace the above line with below
if(s3.equalsIgnoreCase(s2))//no
Use .equals instead of == to compare the value of two strings.
if (s2.equals(s3))
Using == tests for reference equality. Two strings can contain the same characters but have different references.
You can't compare text with ==, you need to use equals.
if(s3.equals(s2))
This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
Android setOnClickListener method - How does it work?
(5 answers)
Closed 1 year ago.
I'm trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile this code I get a force quit and ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined error.
public class Project extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText editText = (EditText)findViewById(R.id.editText1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
editText.setOnClickListener(this);
setContentView(R.layout.main);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText("");
}
}
Also you can use code below
editText.getText().clear();
First you need to call setContentView(R.layout.main) then all other initialization.
Please try below Code.
public class Trackfolio extends Activity implements OnClickListener {
/** Called when the activity is first created. */
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
editText.getText().clear(); //or you can use editText.setText("");
}
}
just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.
We can clear EditText data in two ways
First One setting EditText is empty like below line
editext.setText("");
Second one clearing EditText data like this
editText.getText().clear();
I suggest second way
Your code should be:
public class Project extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == editText) {
editText.setText("");
}
}
}
For Kotlin:
Create two extensions, one for EditText and one for TextView
EditText:
fun EditText.clear() { text.clear() }
TextView:
fun TextView.clear() { text = "" }
and use it like
myEditText.clear()
myTextView.clear()
public EditText editField;
public Button clear = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_layout);
this. editField = (EditText)findViewById(R.id.userName);
this.clear = (Button) findViewById(R.id.clear_button);
this.editField.setOnClickListener(this);
this.clear.setOnClickListener(this);
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.clear_button){
//setText will remove all text that is written by someone
editField.setText("");
}
}
Very Simple to clear editText values.when u click button then only follow 1 line code.
Inside button or anywhere u want.Only use this
editText.setText("");
package com.example.sampleproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class SampleProject extends Activity {
EditText mSearchpeople;
Button mCancel , msearchclose;
ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
mSearchpeople = (EditText)findViewById(R.id.editText1);
mCancel = (Button)findViewById(R.id.button2);
msearchclose = (Button)findViewById(R.id.button1);
mprofile = (ImageView)findViewById(R.id.imageView1);
mContact = (ImageView)findViewById(R.id.imageView2);
mcalender = (ImageView)findViewById(R.id.imageView3);
mConnection = (ImageView)findViewById(R.id.imageView4);
mGroup = (ImageView)findViewById(R.id.imageView5);
mFollowup = (ImageView)findViewById(R.id.imageView6);
msetting = (ImageView)findViewById(R.id.imageView7);
mAddacard = (ImageView)findViewById(R.id.imageView8);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mSearchpeople.clearFocus();
}
});
}
}
i don't know what mistakes i did while implementing the above solutions, bt they were unsuccessful for me
txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
txtDeck.setText("");
}
});
This works for me,
//To clear When Clear Button is Clicked
firstName = (EditText) findViewById(R.id.firstName);
clear = (Button) findViewById(R.id.clearsearchSubmit);
clear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.clearsearchSubmit);
firstName.setText("");
}
});
This will help to clear the wrong keywords that you have typed in so instead of pressing backspace again and again you can simply click the button to clear everything.It Worked For me. Hope It Helps
final EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
childItem.setHint(cellData);
childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
//Log.d("NNN", "Has focus " + hasFocus);
if (hasFocus) {
Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ctx.getApplicationContext(),
"loss the focus", Toast.LENGTH_SHORT).show();
}
return;
});
by setting Empty string you can clear your edittext
editext.setText("");
If the use of EditText is not mandatory, you can implement this behavior easily with the new material components:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_field"
app:endIconDrawable="#drawable/ic_close_black_24dp"
app:endIconMode="clear_text"
app:endIconTint="#color/black">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_value"
android:maxLines="1"
android:text="#{itemModel.value}" />
</com.google.android.material.textfield.TextInputLayout>
You only have to specify the drawable you want for the button that will clear the text and the action that it will execute. To clear the text, you can use iconMode="clear_text", but also "password_toggle" is available.
In XML you can write like:
<EditText
android:id="#+id/txtsearch"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#drawable/roundlayoutbutton1"
android:ems="10"
android:gravity="center"
android:inputType="text"
android:textAllCaps="true"
android:text="search_xxxx"
android:textColor="#color/colorPrimaryDark"
android:visibility="visible" />
and in java class you may have below one :
EditText searchHost;
OnCreate() you write:
searchHost=findViewById(R.id.txtsearch);
searchHost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(searchHost.getText().toString().equalsIgnoreCase("search_xxxx")){
searchHost.setText("");
Toast.makeText(getApplicationContext(),"Enter you text xxx...", Toast.LENGTH_LONG).show();
}
}
});
It works fine for me.
You can use the 'android:hint' attribute in your EditText also from code:
editText.setHint(CharSequence hint / int resid);
Then you don't need any onClickListener or similar. But consider that the hint value won't be passed. The editText will be stayed empty. In this case you can set your editText with your deflault value:
if(editText.getText().toString().equals("")) {
...use your default value instead of the editText... }
It's simple: declare the widget variables (editText, textView, button etc.) in class but initialize it in onCreate after setContentView.
The problem is when you try to access a widget of a layout first you have to declare the layout. Declaring the layout is setContentView.
And when you initialize the widget variable via findViewById you are accessing the id of the widget in the main layout in the setContentView.
I hope you get it!
I am not sure if your searching for this one
{
<EditText
.
.
android:hint="Please enter your name here">
}