So I'm in a basic part of my application I'm wanting to make. I've never gotten this error before, and I don't know what's going on. My .setText is throwing an error saying "setText cannot be resolved or is not a field" I've looked around and haven't been able to find my problem. I believe I'm doing it correctly. If anyone could help me out that'd be great!
MainActivity.java:
public class MainActivity extends Activity {
final TextView loading_Text = (TextView)findViewById(R.id.textView4);
final EditText name_Edit = (EditText)findViewById(R.id.editText1);
//String Values
String Age="";
String Name = name_Edit.getText().toString();
//Int Values
int Gender = 0; //1 male | 2 female
int Group = 0; //Different groups for ages and genders
int save_Info = 0; //save info to phone
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button male_Button= (Button)findViewById(R.id.button1);
Button female_Button = (Button)findViewById(R.id.button2);
male_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Gender++;//Adds one to show this user is a male.
loading_Text.setText=(Name);
}
});
}
I saw two problems:
First:
loading_Text.setText=(Name);
Should be
loading_Text.setText("The text you want to set");
You'll need to take a look at the API document to see how to call the method.
Second:
Move these part:
final TextView loading_Text = (TextView)findViewById(R.id.textView4);
final EditText name_Edit = (EditText)findViewById(R.id.editText1);
//String Values
String Age="";
String Name = name_Edit.getText().toString();
inside your onCreate, like this:
public class MainActivity extends Activity {
TextView loading_Text;
EditText name_Edit;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading_Text = (TextView)findViewById(R.id.textView4);
name_Edit = (EditText)findViewById(R.id.editText1);
Or you'll get NullPointerException.
This is because you were trying to reach the View's property before the view is being initialized. View will be initialized after setContentView, and what you were intend to do was findViewById from R.layout.activity_main before it had been loaded.
Similarly, you'll need to move this call of method:
String Name = name_Edit.getText().toString();
somewhere after setContentView.
setText is a function. So you would need to pass name as a argument.
like loading_Text.setText(Name);
Change
loading_Text.setText=(Name);
to this:
loading_Text.setText(Name);
Also, if you don't see anything in the textview, it is because you are getting the edittext's text before you even create your views, I use an on edittext listener like this to refresh the String when the edit text is changed:
name_Edit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Name = name_Edit.getText().toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
I hope this works for you :)
Related
I want to be able to store the contents of the editText input, then be able to display it in a listview.
I have connected a RFID device that is set in emulator mode. This mode basically also you to scan an RFID tag and the RFID number gets populated where ever the mouse cursor is. In this case it is at the editText input. The lenght of the RFID number is 10, since the RFID number as 10 digits. Once the RFID number is detected I then want to display it on the listview and scan another tag and add that to the listview also.
In my case whenever I code sees the display method the app crashes and I dont know why. Can someone explain to me why this is happening?
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();```
MainActivity code:
public class MainActivity extends AppCompatActivity {
EditText etRfidNo;
TextView textView;
private Set<String> epc = new HashSet<>();
ArrayAdapter<String> contactAdapter;
String single_epc;
Button scan;
ListView listView;
boolean set = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
etRfidNo = (EditText) findViewById(R.id.etRfidNo);
scan = (Button) findViewById(R.id.scan);
TextView textV = (TextView)findViewById(R.id.textView);
etRfidNo.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//TextView textV = (TextView)findViewById(R.id.textView);
//textV.setText(s); //set text for text view
single_epc = String.valueOf(s);
if(s.length() == 10)
{
Date currentDate = new Date();
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
public void display() {
contactAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<>(epc));
listView.setAdapter(contactAdapter);
}
}
listView = (ListView) findViewById(R.id.listviewID);
Forgot to add this.
I create a table with a switch and a edit text fields. What I want to happen is to set the switch to enable when I entry a certain text in the edit text field that is on the same tablerow So How to I set the switch in says row 8 to enable if I enter text in edit text row 8.
for (int i = 0; i< dbarray_id.size(); i++ )
{
CODE IS HERE TO CREATE A TABLEROW
//Now add a switch to the row
Switch switch1 = new Switch(getActivity());
switch1.setId(i);
switch1.setTag(i);
switch1.setSwitchMinWidth(50);
switch1.setEnabled(false)
//Add a edittext field to the row
final EditText txtaccesscode = new EditText(getActivity());
txtaccesscode.setId(i);
txtaccesscode.setTag(i);
txtaccesscode.setHint("CODE?");
txtaccesscode.setInputType(TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtaccesscode.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(
....etc... for after change ...
repeat for the number of rows in the database.
So when I enter text into any row the corresponding switch is enabled.
Thanks
Encapsulate both EditText and Swith references in a class, then when textWatcher fires both references will be the expected.
public static class SwitchToggleListener {
public SwitchToggleListener(final Switch switchView, final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
//Do nothing
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
//Your own condition here
switchView.setEnabled(s.toString().equals("ok"));
}
#Override
public void afterTextChanged(final Editable s) {
//Do nothing
}
});
}
}
Then in your loop do this:
new SwitchToggleListener(switch1, editText1);
new SwitchToggleListener(switch2, editText2);
new SwitchToggleListener(switch3, editText3);
If you have too many rows maybe this is inneficient, there are other ways to do this, let me know if that is the case.
The switch need to be final or a field.
final Switch switch = new Switch(getActivity());
Then you can access it on your beforeTextChanged listener, in there you change like always.
txtaccesscode.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(){
switch.setEnable(true);
}
}
);
After the loop reaching the end you can't access the variable anymore but it still exist. And on the next iteration of the loop the switch you create even having the same name is a different switch, because it's a new scope.
I'm new to programming and Android. I'm making my best attempt at a simple app and I'm stuck!
I have two editTexts (set to accept numbers only) and a button. The idea is to display the sum of the two user inputs when the button is pressed. However, my app stops working and force closes when I click the button in the emulator.
Any help would be greatly appreciated.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended = (EditText) findViewById(R.id.editTextReccomended);
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
prevailingSetting.setText(r+p);
}
});
}
I've looked at several similar questions on here but I haven't been able to find anything that I can implement further than what I have already done. But I am probably looking in the wrong places.
Thanks!
You can't use setText() with an Integer since it expects a CharSequence, you have to use
prevailingSetting.setText(String.valueOf(r+p));
or just
prevailingSetting.setText(""+(r+p));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended =(EditText)findViewById(R.id.editTextReccomended);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
int s = r+p;
prevailingSetting.setText(Integer.toString(s));
}
});
Ok ! if this is your problem then first of all convert your editText value to string and after that convert it into integer like !
String str1=editTextPrevailing.getText().toString();
String str2=editTextRecommended.getText().toString();
int p = Integer.parseInt(str1);
int r = Integer.parseInt(str2);
int s = r+p;
prevailingSetting.setText(""+s);
I am having a problem with setError() on EditText. When an activity is opened, it checks if certain fields are empty and sets error message on them if true. However, the exclamation mark icon is only displayed in case I write some text in field and then delete it. If I lose focus on that field, the icon will disappear again. Both fields Naam and Telefonnumer have this validation.
I use Android 2.2.2 SDK and the application is run on Nexus 7 with latest updates.
I have Util class:
public class Util {
private static String TAG = "Util Class";
public static boolean editTextIsEmpty(EditText edittext) {
if (edittext.getText().toString().trim().length() < 1)
return true;
else
return false;
}
public void editTextListener(final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (editTextIsEmpty(editText) && editText.isEnabled())
editText.setError("Nodig");
else
editText.setError(null);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editTextIsEmpty(editText) && editText.isEnabled())
editText.setError("Nodig");
else
editText.setError(null);
}
});
}
}
and then I have method validateInput() in my activity:
public class DeliveryActivity extends BaseActivity {
private ImageButton btnSetDate;
private Button btnToSummary;
private Button btnSearchAddress;
private EditText txtPostcode;
private EditText txtHouseNumber;
private EditText txtHouseNumberSuffix;
private EditText txtStreet;
private EditText txtCity;
private EditText txtDeliveryDate;
private EditText txtName;
private EditText txtPhone;
private EditText txtEmail;
private EditText txtRemark;
private TextView lblExtraDeliveryInfo;
private Spinner spinnerDelivery;
private Spinner spinnerDeliveryPeriod;
private Spinner spinnerContact;
private Spinner spinnerDeliveryAddress;
private Spinner spinnerExtraDeliveryInfo;
private RelativeLayout rlDeliveryAddressDetails;
private DevRestHelper additionalDeliveryInfo;
private DevRestHelper searchClientAddress;
private Util util = new Util();
private int year;
private int month;
private int day;
public static final int DIALOG_DATEPICKER = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery);
initControls();
validateInput();
}
private void initControls() {
btnSetDate = (ImageButton) findViewById(R.id.activity_delivery_btnCalendar);
btnToSummary = (Button) findViewById(R.id.activity_delivery_btnSummary);
btnSearchAddress = (Button) findViewById(R.id.activity_delivery_btnSearchAddress);
spinnerDelivery = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryMethod);
spinnerDeliveryPeriod = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryPeriod);
spinnerContact = (Spinner) findViewById(R.id.activity_delivery_spinnerContactperson);
spinnerDeliveryAddress = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryAddress);
spinnerExtraDeliveryInfo = (Spinner) findViewById(R.id.activity_delivery_spinnerExtraDeliveryInformation);
txtPostcode = (EditText) findViewById(R.id.activity_delivery_txtPostcode);
txtHouseNumber = (EditText) findViewById(R.id.activity_delivery_txtHousenumber);
txtHouseNumberSuffix = (EditText) findViewById(R.id.activity_delivery_txtHousenumberSuffix);
txtStreet = (EditText) findViewById(R.id.activity_delivery_txtStreet);
txtCity = (EditText) findViewById(R.id.activity_delivery_txtCity);
txtDeliveryDate = (EditText) findViewById(R.id.activity_delivery_txtDeliveryDate);
txtName = (EditText) findViewById(R.id.activity_delivery_txtName);
txtPhone = (EditText) findViewById(R.id.activity_delivery_txtPhone);
txtEmail = (EditText) findViewById(R.id.activity_delivery_txtEmail);
txtRemark = (EditText) findViewById(R.id.activity_delivery_txtRemark);
lblExtraDeliveryInfo = (TextView) findViewById(R.id.activity_delivery_lblExtraDetailInformation);
rlDeliveryAddressDetails = (RelativeLayout) findViewById(R.id.activity_delivery_rlDeliveryAddressDetails);
}
private void validateInput() {
util.editTextListener(txtPostcode);
util.editTextListener(txtHouseNumber);
util.editTextListener(txtDeliveryDate);
}
}
Let me just say that code work on BlueStacks emulator.
There is a known bug with setError on Jelly Bean_MR1 (4.2 and 4.2.1). I am however assuming that the Nexus 7 you are testing with is running one of those versions of Android. See here: http://code.google.com/p/android/issues/detail?id=40417
The error will be shown while you have focus on that EditText field, but when you lose focus, the error icon is not visible to notify the user of the problem.
Before you set Error on any view or edit text, just call the
yourEditText.requestFocus();
yourEditText.setError("Your Error Message");
then set Error. it will solve your problem. Atleast mine did.
try this
new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (editTextIsEmpty(editText) && editText.isEnabled())
editText.setError("Nodig");
else
editText.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// nothing here
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// nothing here
}
}
You can use following code:
May it will be helpful to you:
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
However, you can set a Spanned and a custom error icon using the overloaded setError(CharSequence, Drawable).
You can easily create a Spanned from HTML using fromHtml().
For Example:
yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));
This is the only you need to get expected setError behaviour on the TextView
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
Is it possible to initialize all UI elements of certain type (like all TextViews or all LineraLayouts or ...) in a some kind of loop?
I have many layouts with a lot of the elements of the same type and it's really painful to do it all just by typing.
You can use RoboGuice .It doesn't use loops, but helps you to Inject your View, Resource, System Service, or any other object in to your code.
RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google's own Guice library.
To give you an idea, take a look at this simple example of a typical Android activity:
class AndroidWay extends Activity {
TextView name;
ImageView thumbnail;
LocationManager loc;
Drawable icon;
String myName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (TextView) findViewById(R.id.name);
thumbnail = (ImageView) findViewById(R.id.thumbnail);
loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
icon = getResources().getDrawable(R.drawable.icon);
myName = getString(R.string.app_name);
name.setText( "Hello, " + myName );
}
}
This example is 19 lines of code. If you're trying to read through onCreate(), you have to skip over 5 lines of boilerplate initialization to find the only one that really matters: name.setText(). And complex activities can end up with a lot more of this sort of initialization code.
Compare this to the same app, written using RoboGuice:
class RoboWay extends RoboActivity {
#InjectView(R.id.name) TextView name;
#InjectView(R.id.thumbnail) ImageView thumbnail;
#InjectResource(R.drawable.icon) Drawable icon;
#InjectResource(R.string.app_name) String myName;
#Inject LocationManager loc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name.setText( "Hello, " + myName );
}
}
In this example, onCreate() is much easier to take in at a glance. All the platform boilerplate is stripped away and you're left with just your own app's business logic. Do you need a SystemService? Inject one. Do you need a View or Resource? Inject those, too, and RoboGuice will take care of the details.
RoboGuice's goal is to make your code be about your app, rather than be about all the initialization and lifecycle code you typically have to maintain in Android.
This text is from here
I have/had done something similar. Just for your reference, here's the code:
public class AbcActivity extends Activity
{
protected boolean changesPending;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
setViews(); //this method is created and called to take care of the buttons and edittext fields, and can probably hold a number of other fields/widgets as well
}
/** Take care of the Buttons and EditTexts here*/
private void setViews()
{
EditText userEdit = (EditText)findViewById(R.id.editText1);
EditText passwordEdit = (EditText)findViewById(R.id.editText2);
Button loginButton = (Button)findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
login(); // some random method
}
});
Button cancelButton = (Button)findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
cancel(); //another random method
}
});
userEdit.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
changesPending = true;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
});
passwordEdit.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
changesPending = true;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
});
}
}
Hope this helps.
If you are trying to handle a large number of Views it may be worthwhile handling creation of these Views at runtime, attaching them to the relevant container. For example:
ViewGroup container = (ViewGroup) findViewById(R.id.container);
for(int i = 0; i < NUM_TEXT_VIEWS; i++){
TextView tv = new TextView(this); // where 'this' is your Activity
tv.setText("This is TextView " + i);
container.addView(tv);
}
Properties set in your xml file for a View usually have a corresponding Java method call.