How to make a TextView editable at the click of a button? - android

I have an app that displays certain content from a database and lets users edit it. I have an activity that displays those details in some textviews. Now, there is an "EDIT" button at the bottom of the screen. When this button is clicked, I want to make the textview's editable (which are initially read-only) so that the database can be updated. Also, I want the "EDIT" button to turn into a "SAVE" button. I understand I can just use another activity for this, but is there any way to do this while in the same activity?

EditText is a TextView, what it can do TextView can do better
so suppose you have a TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="i am a text"
android:textColor="#243b03" />
and you want it to be editable by a click of a button; in your onclick add this
TextView.setCursorVisible(true);
TextView.setFocusableInTouchMode(true);
TextView.setInputType(InputType.TYPE_CLASS_TEXT);
TextView.requestFocus(); //to trigger the soft input
they are not static, but i made it static so you know which view object's methods are those. About your Button after the text has been inputted then you change the text to "done" or whatever, Button extends from TextView so treat it as if it was-research on TextWatcher

You'd be better off to switch the TextView to an EditText and toggle it with editText.setEnabled(boolean) to get the effect you want

Making a textview editable has its limitations, such as not being able to modify the data already in it (Check this answer)
You can just use an EditText and use android:inputType="none" in xml or use <your_editText>.setEnabled(false) in your activity/fragment to make it read-only. After the user clicks EDIT, you can make the editText editable by putting the below statements inside the button's onClick method.
<your_editText>.setEnabled(true)
<your_editText>.requestFocus();
Regarding making the EDIT button turn into SAVE, you can put a counter inside your onClick method which will keep track of the order of clicks. After the first click you can change the button text using
<your_button>.setText("SAVE").
On the next click you can execute whatever statements you're using to save data.

Related

Kotlin recyclerView nextFocusDown

I need to make nextFocusDown on items in recyclerView. Every item has the same id, but to make nextFocusDown I need to have specific id. How shoudl I do this in recycerView to make focus on next edittext in list?
You can put focus on your edit text programmatically. Get the event when the user presses enter button or some button that tells you the user is done editing the first edit text. Then you can programmatically put focus on next item in recyclerview using following code:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();

How can I make this Custom Control and Android

I want to make a custom control in Android.
Actually, I want to extends a EditText with one Button and one TextView. (Like below picture)
My Custom Control Picture
I have not problem with listener. I want just make a custom control that contains 3 controls. (EditText, Button and TextView)
How can I do it?
Create a Linear layout with orientation = horizontal .
Then create an edittext and a button in it .
In java code ,set onClicklistener on button and then get the value of edittext by [edittext].getText().toString() ;
Control means you want to perform any action (a similar action i understand from your explaination) on click of any of the three?
If so, have a specific method which will be called on click of each of the items and alter them according to your choice.
Is this what you meant by user control?

How to change the focus from edittext view

I have a one textview two edittext fields. am using first for the taking input text from user .
for the Second view I made the focusable set to false and I wrote a onClickListener which pops up(Date Picker) so that user can set the date.
Scenario:
I first I click on the first edit text and I start typing some text when am done I click on the second edittext and a I get a popup(Dialog) I select the date and click set on the dialog.
My Problem here is : After setting the Date, focus is still in the first field.
What I have tried.
-- While implementing the onClickListener for the second edittext , I tried to change the focus to the textView.
textview.requestFocus();
but it din't work.
Is there a way I can change it ?
if you don't want to enter date manually.. than make that Edittext to Enable false.
editText.setEnabled(false);
editText.setFocusable(false);
editText.setInputType(InputType.TYPE_NULL);
and just set the text on edittext from datepicker dialog..

EditText in listview losing control

I have added two textview(id, name), two edittext and two buttons(edit,save) in every row of listview, When edit button is clicked both edittext appears in that row(visibility is set to gone in XML) now all I want to do is allow user to add some contents in edittext and on pressing save button those contents will be reflected in textview and edittext will be disappeared again.
On edit buttons click view two edittext are appearing as expected but as soon as I type in any of the edittext they are getting refreshed. In debug mode I have found out that as soon as I am typing in Edittext getView() method called again & again and that's the reason behind refresh.
I have implemented empty onclicklistner on edittext so that when it get touched and typed it will not call getView but thats not working and I know that's a stupid attempt to solve problem.
Please tell what to do so that when I click or type in edittext it will not call getView().
You have to off focus from xml of edittext and button

How to make a ListView act as an EditText

I am developing an application for Android, and for that I am trying to make a ListView act in such a way, that when a user presses an empty entry, he can start typing text directly into that empty entry, and that when the user touches any other part of the screen, it is saved. Is there a way to do this? I was thinking of using onClick somehow, but I have no concrete approach.
Here is one basic, general approach:
For each row of the ListView, create a layout that has a visible TextView and a EditText with the visibility set to gone.
Use an onClickListener for each row to swap the visibilities of the TextView and EditText (respectively, gone and visible) when the row is selected.
Track the active row for clicks to another row or background.
When the active row changes, set the value of the EditText to the TextView and return them to their original visible states.
Other approach that could be taken is using the View.OnFocusChangeListener and the TextWatcher you can get more detail about them here http://developer.android.com/reference/packages.html

Categories

Resources