SelectAll on EditText with Two-Way Data Binding












0















I have an Activity with a fragment... In this fragment I'm using two-way DataBinging in an EditText. This EditText is binded to a Double property of the object, and because of this, I had to implement an InverseMethod to convert String -> Double and Double -> String...



In my EditText I configured android:selectAllOnFocus="true", and I'm forcing it also on onCreateView method of the fragment: edQtd.selectAll()



The problem is, that when the fragment appears, the EditText has the focus, but the text is not selected, instead, the cursor is before the first number...



I wanted it to show with all the text selected...



Tried to instead of using the inverse method, just concatenate an empty String, but the result was the same...



From what I saw debbuging it, the binding class generated, sets the text after the fragments creation (after I manually called edQtd.selectAll()), removing the selection...



Any ideas how to solve it?



Edit:
For now I solved it adding a TextChangedListener to the EditText, where I select all the text only the first time the text is changed:



edQuantidade.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(getSelectAllEdQtdText()) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}
});









share|improve this question

























  • Did you try edQtd.selectAll() in onResume() of Fragment?

    – Khemraj
    Nov 20 '18 at 13:36











  • Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

    – Luan Träsel
    Nov 20 '18 at 13:47













  • You just want to select EditText text after your view opens?

    – Khemraj
    Nov 20 '18 at 13:48











  • Yes, I want to select all the text of the EditText right after open the view

    – Luan Träsel
    Nov 20 '18 at 13:51
















0















I have an Activity with a fragment... In this fragment I'm using two-way DataBinging in an EditText. This EditText is binded to a Double property of the object, and because of this, I had to implement an InverseMethod to convert String -> Double and Double -> String...



In my EditText I configured android:selectAllOnFocus="true", and I'm forcing it also on onCreateView method of the fragment: edQtd.selectAll()



The problem is, that when the fragment appears, the EditText has the focus, but the text is not selected, instead, the cursor is before the first number...



I wanted it to show with all the text selected...



Tried to instead of using the inverse method, just concatenate an empty String, but the result was the same...



From what I saw debbuging it, the binding class generated, sets the text after the fragments creation (after I manually called edQtd.selectAll()), removing the selection...



Any ideas how to solve it?



Edit:
For now I solved it adding a TextChangedListener to the EditText, where I select all the text only the first time the text is changed:



edQuantidade.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(getSelectAllEdQtdText()) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}
});









share|improve this question

























  • Did you try edQtd.selectAll() in onResume() of Fragment?

    – Khemraj
    Nov 20 '18 at 13:36











  • Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

    – Luan Träsel
    Nov 20 '18 at 13:47













  • You just want to select EditText text after your view opens?

    – Khemraj
    Nov 20 '18 at 13:48











  • Yes, I want to select all the text of the EditText right after open the view

    – Luan Träsel
    Nov 20 '18 at 13:51














0












0








0








I have an Activity with a fragment... In this fragment I'm using two-way DataBinging in an EditText. This EditText is binded to a Double property of the object, and because of this, I had to implement an InverseMethod to convert String -> Double and Double -> String...



In my EditText I configured android:selectAllOnFocus="true", and I'm forcing it also on onCreateView method of the fragment: edQtd.selectAll()



The problem is, that when the fragment appears, the EditText has the focus, but the text is not selected, instead, the cursor is before the first number...



I wanted it to show with all the text selected...



Tried to instead of using the inverse method, just concatenate an empty String, but the result was the same...



From what I saw debbuging it, the binding class generated, sets the text after the fragments creation (after I manually called edQtd.selectAll()), removing the selection...



Any ideas how to solve it?



Edit:
For now I solved it adding a TextChangedListener to the EditText, where I select all the text only the first time the text is changed:



edQuantidade.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(getSelectAllEdQtdText()) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}
});









share|improve this question
















I have an Activity with a fragment... In this fragment I'm using two-way DataBinging in an EditText. This EditText is binded to a Double property of the object, and because of this, I had to implement an InverseMethod to convert String -> Double and Double -> String...



In my EditText I configured android:selectAllOnFocus="true", and I'm forcing it also on onCreateView method of the fragment: edQtd.selectAll()



The problem is, that when the fragment appears, the EditText has the focus, but the text is not selected, instead, the cursor is before the first number...



I wanted it to show with all the text selected...



Tried to instead of using the inverse method, just concatenate an empty String, but the result was the same...



From what I saw debbuging it, the binding class generated, sets the text after the fragments creation (after I manually called edQtd.selectAll()), removing the selection...



Any ideas how to solve it?



Edit:
For now I solved it adding a TextChangedListener to the EditText, where I select all the text only the first time the text is changed:



edQuantidade.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(getSelectAllEdQtdText()) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}
});






android data-binding android-edittext selectall two-way-binding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:51







Luan Träsel

















asked Nov 20 '18 at 13:02









Luan TräselLuan Träsel

11




11













  • Did you try edQtd.selectAll() in onResume() of Fragment?

    – Khemraj
    Nov 20 '18 at 13:36











  • Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

    – Luan Träsel
    Nov 20 '18 at 13:47













  • You just want to select EditText text after your view opens?

    – Khemraj
    Nov 20 '18 at 13:48











  • Yes, I want to select all the text of the EditText right after open the view

    – Luan Träsel
    Nov 20 '18 at 13:51



















  • Did you try edQtd.selectAll() in onResume() of Fragment?

    – Khemraj
    Nov 20 '18 at 13:36











  • Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

    – Luan Träsel
    Nov 20 '18 at 13:47













  • You just want to select EditText text after your view opens?

    – Khemraj
    Nov 20 '18 at 13:48











  • Yes, I want to select all the text of the EditText right after open the view

    – Luan Träsel
    Nov 20 '18 at 13:51

















Did you try edQtd.selectAll() in onResume() of Fragment?

– Khemraj
Nov 20 '18 at 13:36





Did you try edQtd.selectAll() in onResume() of Fragment?

– Khemraj
Nov 20 '18 at 13:36













Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

– Luan Träsel
Nov 20 '18 at 13:47







Tried now, Khemraj, and the it didnt worked out. The code of binding class generated is runned after the OnResume method... I solved it by adding a TextChangedListener that selects all text only after the 1st change, but I didn'l like this solution...

– Luan Träsel
Nov 20 '18 at 13:47















You just want to select EditText text after your view opens?

– Khemraj
Nov 20 '18 at 13:48





You just want to select EditText text after your view opens?

– Khemraj
Nov 20 '18 at 13:48













Yes, I want to select all the text of the EditText right after open the view

– Luan Träsel
Nov 20 '18 at 13:51





Yes, I want to select all the text of the EditText right after open the view

– Luan Träsel
Nov 20 '18 at 13:51












1 Answer
1






active

oldest

votes


















0














Add following attributes on EditText in Layout.



<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>


And remove edQtd.selectAll() from code.



Edit



Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.



    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);





share|improve this answer


























  • I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

    – Luan Träsel
    Nov 20 '18 at 14:03











  • From where you get data to set layout?

    – Khemraj
    Nov 20 '18 at 15:12











  • I create and bind the object in onCreateView method

    – Luan Träsel
    Nov 20 '18 at 15:35











  • Test edited answer.

    – Khemraj
    Nov 20 '18 at 16:37











  • Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

    – Luan Träsel
    Nov 21 '18 at 12:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393598%2fselectall-on-edittext-with-two-way-data-binding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Add following attributes on EditText in Layout.



<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>


And remove edQtd.selectAll() from code.



Edit



Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.



    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);





share|improve this answer


























  • I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

    – Luan Träsel
    Nov 20 '18 at 14:03











  • From where you get data to set layout?

    – Khemraj
    Nov 20 '18 at 15:12











  • I create and bind the object in onCreateView method

    – Luan Träsel
    Nov 20 '18 at 15:35











  • Test edited answer.

    – Khemraj
    Nov 20 '18 at 16:37











  • Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

    – Luan Träsel
    Nov 21 '18 at 12:46
















0














Add following attributes on EditText in Layout.



<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>


And remove edQtd.selectAll() from code.



Edit



Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.



    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);





share|improve this answer


























  • I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

    – Luan Träsel
    Nov 20 '18 at 14:03











  • From where you get data to set layout?

    – Khemraj
    Nov 20 '18 at 15:12











  • I create and bind the object in onCreateView method

    – Luan Träsel
    Nov 20 '18 at 15:35











  • Test edited answer.

    – Khemraj
    Nov 20 '18 at 16:37











  • Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

    – Luan Träsel
    Nov 21 '18 at 12:46














0












0








0







Add following attributes on EditText in Layout.



<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>


And remove edQtd.selectAll() from code.



Edit



Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.



    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);





share|improve this answer















Add following attributes on EditText in Layout.



<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>


And remove edQtd.selectAll() from code.



Edit



Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.



    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 16:37

























answered Nov 20 '18 at 13:54









KhemrajKhemraj

14.1k44181




14.1k44181













  • I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

    – Luan Träsel
    Nov 20 '18 at 14:03











  • From where you get data to set layout?

    – Khemraj
    Nov 20 '18 at 15:12











  • I create and bind the object in onCreateView method

    – Luan Träsel
    Nov 20 '18 at 15:35











  • Test edited answer.

    – Khemraj
    Nov 20 '18 at 16:37











  • Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

    – Luan Träsel
    Nov 21 '18 at 12:46



















  • I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

    – Luan Träsel
    Nov 20 '18 at 14:03











  • From where you get data to set layout?

    – Khemraj
    Nov 20 '18 at 15:12











  • I create and bind the object in onCreateView method

    – Luan Träsel
    Nov 20 '18 at 15:35











  • Test edited answer.

    – Khemraj
    Nov 20 '18 at 16:37











  • Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

    – Luan Träsel
    Nov 21 '18 at 12:46

















I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

– Luan Träsel
Nov 20 '18 at 14:03





I tried and it didn't work, when the setText is executed by the databinding generated class, the selecion is removed...

– Luan Träsel
Nov 20 '18 at 14:03













From where you get data to set layout?

– Khemraj
Nov 20 '18 at 15:12





From where you get data to set layout?

– Khemraj
Nov 20 '18 at 15:12













I create and bind the object in onCreateView method

– Luan Träsel
Nov 20 '18 at 15:35





I create and bind the object in onCreateView method

– Luan Träsel
Nov 20 '18 at 15:35













Test edited answer.

– Khemraj
Nov 20 '18 at 16:37





Test edited answer.

– Khemraj
Nov 20 '18 at 16:37













Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

– Luan Träsel
Nov 21 '18 at 12:46





Now it worked, but I strill prefer the other solution I found which you don't have to wait the 500ms to get the text selected... I'll edit my question with this "solution" I found... Anyway, thanks

– Luan Träsel
Nov 21 '18 at 12:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393598%2fselectall-on-edittext-with-two-way-data-binding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents