APK won't compile after using setText in text field - android

Modifying an APK to try to make something slightly more user friendly. There is a login screen with username and password and a server portal field. The portal field now never needs to change. So I want to set a default text so the user does not have to type in the full portal URL
Currently looks like this
<EditText android:textSize="15.0dip"
android:textColor="#color/black"
android:textColorHint="#color/white"
android:id="#id/et_sever_url"
android:background="#drawable/selector_login_fields"
android:paddingLeft="20.0dip"
android:paddingRight="20.0dip"
android:focusable="true"
android:nextFocusUp="#id/et_password"
android:nextFocusDown="#id/bt_submit"
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:layout_marginLeft="25.0dip"
android:layout_marginTop="20.0dip"
android:layout_marginRight="25.0dip"
android:text="#string/serverurl"
android:hint="#string/serverurl"
android:maxLines="1" android:lines="1"
android:layout_below="#id/et_password"
android:layout_centerHorizontal="true"
android:inputType="textUri"
android:textCursorDrawable="#null"
android:fontFamily="sans-serif" />
The text and the hint just put the server url in the field but it still has to be manually typed in. I tried to use android:setText="#string/serverurl" but then the apk fails to compile again.

There is no edittext properties like this : android:setText="#string/serverurl"
All you want to do set default string to your editext then in xml just use below property.
android:text="#string/serverurl"
Also remove the hint property you really don't need that because you are seting default text so hint won't be shown.
Also second why to do this in java code like below.
EditText edtServerUrl =findViewById(R.id.et_sever_url);
edtServerUrl.setText(R.string.serverurl)
Put above code inside onCreate() method of activity.

Related

Is secured entry possible for api.ai intents?

I am trying to do agent training using api.ai.
I wanted to read a secret key from user expression. Is it possible to allow the user text entry in secured format (Like we enter passwords - the big black dots)?
You can use an EditText with the inputType set to textPassword like this:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"/>

Android Float.parseFloat results in a fatal error due to decimal point

I'm using the following code to parse a float from a string:
EditText CurrPLC = (EditText) findViewById(R.id.CurrPLC);
String s5 = CurrPLC.getText().toString();
float F5 = Float.parseFloat(s5);
This works great until I try to write a negative value into the EditText. Because the "negative" symbol is on the same button as the decimal point, I write a decimal first and then the app crashes because a string of "." cannot be parsed into a float value. The code is executed on any text change in the EditText
Here's the EditText code from the layout file:
<EditText
android:id="#+id/CurrPLC"
android:layout_width="100dp"
android:layout_height="30dp"
android:padding="2dp"
android:hint="eg. 3277"
android:gravity="center_horizontal"
android:layout_gravity="right"
android:textSize="13sp"
android:digits="-0123456789."
android:inputType="numberSigned|numberDecimal"
android:maxLength="10"
android:background="#drawable/plc_edit_text_holo_light"
android:layout_marginRight="19dp"
android:layout_marginEnd="19dp"
android:layout_alignBaseline="#+id/CurrPLCLabel"
android:layout_alignBottom="#+id/CurrPLCLabel"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
/>
Is there something I've done wrong, or is this simply down to handling this before the error occurs?
Thanks
Based on your comment of using a TextWatcher which would attempt to parse the float on each key press, the problem is not with negative numbers.
parseFloat() will work fine with negative numbers. The problem is with an empty string or -, -., or . most likely.
So you should not parse on each key press and instead only do it wherever you need it (I'm not sure of your full use case so I can't comment on the appropriate time and place to parse the float). Otherwise you need to add a try/catch for java.lang.NumberFormatException wherever you are currently parsing the float.

Android - How to set numeric EditText to a specified number of digits

I want an EditText to contain exactly 8 digits (like bank accounts). Of course in addition to :
android:inputType="number"
Is there an option in XML or do I have to manage this programmatically ?
Programmatically.
Also, you can save time by using existing library EditText with validation. You can use a regex type with following pattern [0-9]{8}.
I think there is no way just in xml.
You have to validate it programmatically.
#Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
This is my phone edit text:
<EditText
style="#style/ObligatoryEditTextStyle"
android:id="#+id/etxtClientPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:gravity="left"
android:textColor="#color/hub_blue_normal"
android:visibility="visible"
android:selectAllOnFocus="true"
android:digits="0123456789"
android:maxLength="8"/>
Tell me if I helped you and good programming!!
Try the following line in your EditText:
android:maxLength="8"
Change input type in decimal, then set maxLength="8".
android:inputType="decimal"
android:maxLength="8"

Show past emails in android EditText while entering emails?

I have an EditText like this:
<EditText
android:id="#+id/txt_login_username"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_above="#+id/pengala_logo"
android:layout_alignLeft="#+id/txt_login_pwd"
android:ems="10"
android:hint="Please enter Email"
android:inputType="textAutoComplete"
android:textColorHint="#ffffff"
android:textSize="20sp" />
I want to show all email ids as suggestion when a user starts typing in the EditText. Similar to this
For that use Autocomplete TextView.
When user enter userName in Autocomplete TextView and press login you need to store username in sharedpreferences. For sharedpreferences see This tutorial.
Select entred values from sharedpreferences and display it in your Autocomplete TextView when user comes again.
Use this library for achieving this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<jp.yokomark.widget.account.autocomp.AccountAutoCompleteEditText
android:id="#+id/any"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_account_type_any"
app:accountType="any"/>
AccountAutoCompleteEditText has a custom attribute of accountType. This attribute accepts any of the following value.
Value Meaning
any Show the candidate account of all type.
email Show the candidate account using email address.
phoneNumber Show the candidate account using phone number.
By this you can easily use it

How to get the text typed in an EditText for android using Python?

I used the xml below to design the layout of my android app. I used the EditText field to get an input from the user.
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:tag="Tag Me"
android:inputType="text">
<requestFocus></requestFocus>
</EditText>
How do I get the text typed in that EditText and print it using Python?
You can't use Python, Android programming is done in Java. You can get the text from the EditText as follows (in Java):
EditText et = (EditText)findViewById( R.id.editText1 );
String s = et.getText().toString();
If you're asking how to send that text to a webserver that uses Python, that's quite a bit more complicated.

Categories

Resources