How to make out of function EditText Android - android

I need help about EditText in android.
In my app i have 2 editText fields. The firs is mandatory to make some calculations, the second is not mandatory it is avalaible only if checkbox is checked. My problem is that i cant make calculations if i don't put some value in secont EditText field.
How can i disable it (out of function) the second field, and to be enabled only if checkbox is checked ?

You can disable your EditText by calling edittext.setEnabled(false) (enable it by calling the same method with true) and you can get the status of your checkbox by calling checkbox.isChecked(). Just combine the two to achieve what you want.

Related

How to make a text field that can be entered when the user clicks it but otherwise is disabled?

I think I'm missing something very obvious here.
All I want is a field where users can enter a parameter that affects the app, for example -100
I want the field to be visible on the screen, displaying the current value of the parameter, but not with a cursor in it or underlined in red or anything. Simply visible, but if the user wants to change the parameter, they can click in the box and change the value.
Many thanks for your help!
You can try to add both TextView and EditText in your layout, then show and hide based on your need.
Just show EditText by default and add TextWatcher on it, when enter is pressed or saved, then show your TextView with value from EditText. Then write onClickListener for TextView to show EditText again.
Hope that makes sense.

Enable/Disable EditText in Android

I am new to Android and I want to ask from you people. that I have a Android Activity named as Account Page, It shows the user his/her registration data. The Activity contains editable fields (EditText).
I want that initially, all The EditTexts are disabled and get enabled when I click on the Edit TextView (Button?). When the fields are enabled, the Edit TextView should read 'Apply'. When I click on 'Apply', then data should be modified, and the fields and Button restored to the initial state.
I am using this implementation using SharedPreferences in Android.
Kindly tell me how I can achieve this.
For disabling edittext: How to disable edittext in android
disable edittexts in onCreate() method or disable it using XML attribute.
In onClick method of Edit textview, enable all edittext and change text of textview to Apply.
In onClick method of Apply textview, first check if the text in textview is "Apply" and then update the date and set text back to Edit.
You can achieve this by mimicking the two 'states' of your form using two methods : init() and edit(). In the method init(), disable all fields that you want to disable and set the text of your Button/TextView to 'Edit'.
In the method edit(), enable all the above fields and set the text of your Button/TextView to 'Apply'.
All you need now is to correctly place calls to these methods. I suggest you place calls to init() when your Activity is created and after the user hits Apply.
Add a call to edit() whenever you set the text of your Button to 'Edit'.
I would love if you understood what I'm suggesting and coded it yourself.

Use android:EditText as label

I want to set some text in EditText type fields. I want to do this while loading the activity. And then I don't want user to make any changes to it. Can I set EditText type boxes to non-editable mode? Does there exists any other element which could solve my purpose?
A TextView sounds like what you want; you can call setText() on them.
For EditText itself you could call this version of setText(), and set the buffer type to NORMAL instead of EDITABLE.
there are two options.
setEditable and setEnable. set false in Oncreate.
but in setEditable user can edit text by long click -> paste option.
If you are not taking any input from user in the EditText and it is just used to show some text that is set programmatically at onCreate, then you can use TextView instead. It is more suited for your requirement.
Here is a comparison between EditText and TextView that might be helpful.
Is there any particular reason for you to use a non-editable EditText? In case you absolutely have to use EditText then setting it as non-editable in your code after you set the value will suffice. You can do it in a few different ways, one of them would be setting input type as null in code.
yourEditText.setInputType(InputType.TYPE_NULL);
You can do android:enabled="false" in layout of that activity.
or
editText.setEnabled(false);

Keep a part of edittext uneditable

In my app I have a couple of checkboxes, that when checked combine the first part of an edittext. In the same edittext I would like to allow the user to append some text, while disallowing to delete the built text.
This is how it looks
[This part is build from checkbox combination, and can change in real time][This part is user defined]
Now is there a way to not allow the user to modify the first part of the edittext, but still allow the app to change this text?
You can certainly create a TextWatcher object and add it to your edittext. In your textwatcher, you can store the constant text information in an instance variable. Then, you can fill in the onTextChanged() and afterTextChanged() methods to create the type of behavior you are looking for.
For example, you can check the cursor position (using editText.getSelected()) to see if the user tried to change some of the text that shouldn't be changed-- if they do, then have some code to handle the case.
I know this isn't the best answer, but I don't yet have privilege to make a comment. Hope this helps!

Type in EditText with buttons

So I hid the soft keyboard because I only want "1" and "0" to be the buttons to type with and a delete button. I have two EditText boxes. How do I go about letting the user click on the EditBox and be able to have those Buttons only type in that box? and same with the delete. I only need to know how to make it recognize that you have chosen that box. Take whats there and add "0" to the end of the string.
Thanks
If all you are looking for is to get the EditText that is selected, then use the hasFocus() method on each EditText. The method returns a Boolean value depending on whether or not it has focus (has been selected by the user). Then insert your text into the EditText that has focus.
If this does not help, I've done something similar to this before and can offer you a different solution.

Categories

Resources