Lpc_dark Lpc_dark - 5 months ago 16
Java Question

EditText Problems when generated programmatically

Ive been trying to create a text editor that has words suggestions turned on. This is what i have been doing hoping to get it to work but haven't had any such luck so far

EditText test = (EditText) new EditText(Page.this);
test.setRawInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
test.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
test.setRawInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);
params.setMargins(5, 0, 5, 0);

test.setLayoutParams(params);
test.setText(text);
test.setId(id);
holder.addView(test);


holder is the container. The Edittext has text loaded in from that database but I don't think that really matters. Everything is working properly. The EditText loads and the database stuff is fine which is why i didn't post that section. The only problem i am having is that autocorrect or any form of auto complete isn't working. I have some EditText that is loaded from the xml and they seem to work fine. I can't load these from xml because depending on the input the user gives additional EditText are generate so i have to do it Programmatically. Are there any suggestions as to why this is not working.

Yes i have looked over the documentation and i didn't seems to find an answer there as well because the flags i have added are not doing the job.

Intention
Programmatically Add a Edittext which has Autocomplete enabled

Problem:
Edittext is generated but does not show any text support aid as autocomplete or correct

Thank you in advance StackOverFlow nation

Answer

Alright i found out what is needed to be done your best bet is to use something called a layout inflator.

this is because there is a bunch of things set for a Edittext when done in xml. Create this(below) for your xml file. Note you can only have one Root elements. So you will need a new file for each template.

notice i have it with an id as well

use this(below) to insert programmatically

EditText Stest = (EditText) getLayoutInflater().inflate(R.layout.section_header, null);



<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textAutoCorrect|textCapWords"
    android:id="@+id/section" >
</EditText>

this way it will insert and have all the default plus whatever additions you have created. In truth there is lack of support for doing things code base because most uses the XML.

this way you can still us code for dynamic insertion but use xml to define how the inserted object/view will behave

Comments