Android Studio: getting an input from user - android

I'm new to Android Studio. I would like to read input from user from a dynamically created elements. I've already managed to create the elements. Now I do not know at which place should I place the reading element. Can someone give me a MWE here ? I do not know also if I should use getText(this) or editText(this) or something else.
public void click(View view) {
String col1;
String col2;
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
TextView c = new TextView(this);
EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
for (int x = 0; x < 5; x++) {
col1 = "(" + x + ")"+Integer.toBinaryString(x);
col2 = "1";
TableRow newRow = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
EditText editText1 = new EditText(this);
TextView column3 = new TextView(this);
column1.setText(col1);
column1.setText(col1);
column2.setText(col2);
newRow.addView(column1);
newRow.addView(editText1);
newRow.addView(column3);
tl.addView(newRow, new TableLayout.LayoutParams());
}
}

Related

textviews in table.i want user input on those textviews on a submit button click

I want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.

how can i getText() From edittext programmatically created

i create two edittext programmatically and i need to get text from him
this is my code for create edittext
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
//First EditText
edText = new EditText(getActivity());
edText.setId(0);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setBackgroundResource(R.drawable.edittext);
edText.setPadding(5, 5, 5, 5);
linear=(LinearLayout)rootView.findViewById(R.id.edittextpanel);
linear.addView(edText ,layoutParams);
nb++;
//Second EditText
edText = new EditText(getActivity());
edText.setId(1);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setPadding(5, 5, 5, 5);
edText.setBackgroundResource(R.drawable.edittext);
linear.addView(edText ,layoutParams);
Help please i'm blocked
edText = new EditText(getActivity());
edText.setId(id);
String stringAnswer = edText.getText().toString();
If you want to create and reference more than one edit text you'll have to create an EditText array
List<EditText> allEDs = new ArrayList<EditText>();
Then create the Edit Texts
edText = new EditText(getActivity());
edText.setId(id);
Push them into the array
allEDs.add(edText);
Create a string array same size as the EditText array
String[] ETResults = new String[allEDs.size()];
Loop through the EditText array and put converted results into String array
for(int i=0; i < allEDs.size(); i++){
ETResults[i] = allEDs.get(i).getText().toString();
}
And then assign the desired string to whatever you want
String stringAnswer = ETResults[i];
try {
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
for(int i = 0; i <viewGroup.getChildCount(); i++){
if(viewGroup.getChildAt(i) instanceof EditText) {
EditText view = (EditText)viewGroup.getChildAt(i);
Log.i("val " , " val " + ((EditText) viewGroup.getChildAt(i)).getText() + " view "+ view.getText() );
}
}
}catch (Exception ex){
Log.e("Error in Bind layout", ex.toString());
}
Maintain a reference to both EditTexts. Put the following in the main body of your Fragment to make them globally accessible..
EditText edText1;
EditText edText2;
Then in the method you've shown you can use...
// Do some stuff
edText1 = new EditText(getActivity());
// Do some more stuff
edText2 = new EditText(getActivity());
...
Later when you need to get the text you can use...
String text1 = edText1.getText().toString();
String text2 = edText2.getText().toString();
Alternatively you can find them later in your Fragment using their ids...
LinearLayout linear = (LinearLayout) findViewById(R.id.edittextpanel);
EditText et = (EditText) linear.findViewById(0);
String text1 = et.getText().toString();
et = (EditText) linear.findViewById(1);
String text2 = et.getText().toString();

Creating TableLayout From Class

Icreate this post, because i am new at this, and i need a little help. I am doing a little exercise about a application you that with some values you can make a tablelayout from class. But after i push the button i get the error "The specified child already has a parent. You must call removeView() on the child's parent first"
public class TabelFlat extends Activity{
//deklarasi variabel
String jumlah,jasa,jangkawaktu;
int jum = 0, jsa = 0, jkwaktu = 0;
int a = 0, b = 0, c = 0, d = 0, i = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
//mengambil value yg diimport dari class lain
if (extras != null) {
jumlah = extras.getString("jumlah");
jasa = extras.getString("jasa");
jangkawaktu = extras.getString("jangkawaktu");
}
//konversi string menjadi int
jum = Integer.parseInt(jumlah);
jsa = Integer.parseInt(jasa);
jkwaktu = Integer.parseInt(jangkawaktu);
//membuat tabel
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
MainLayout.setStretchAllColumns(true);
//Baris Judul
TableRow row1 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("Bulan");
TextView text2 = new TextView(this);
text2.setText("Total Angsuran");
TextView text3 = new TextView(this);
text2.setText("Angsuran");
TextView text4 = new TextView(this);
text2.setText("Bunga");
TextView text5 = new TextView(this);
text2.setText("Sisa Saldo");
text2.setGravity(android.view.Gravity.RIGHT);
//memasukkan nilai text ke row
row1.addView(text1);
row1.addView(text2);
row1.addView(text3);
row1.addView(text4);
row1.addView(text5);
//menambah row ke tabel
MainLayout.addView(row1);
//kalkulasi
TextView tangsuran = new TextView(this);
a = (jum * 120/100);
tangsuran.setText(""+String.valueOf(a));
TextView angsuran = new TextView(this);
b = jum / jkwaktu;
angsuran.setText(""+String.valueOf(b));
TextView bunga = new TextView(this);
c = jum * 20 / 100 / jkwaktu;
bunga.setText(""+String.valueOf(c));
//loop
d = jum;
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
barisloop.addView(tangsuran);
barisloop.addView(angsuran);
barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}
setContentView(MainLayout);
}
}
here you add the tangsuran, angsuran and bunga text view to new row, but they are already added to another one.You must create new objects for the rows in the loop.
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
//barisloop.addView(tangsuran);
//barisloop.addView(angsuran); CREATE NEW TEXTVIEWS
//barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}

How to get the dynamically created form values in android?

I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..

Android - Dynamic Table Rows NullPointerException

I am making an android application, which creates a dynamic interface, according to a string read from your preferences:
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String Favs = SP.getString("Favs", "None");
String[] Genveje;
if (!Favs.equals("None"))
{
Genveje = Favs.split(";");
tr = new TableRow[Genveje.length];
for (int i = 0; i < Genveje.length; i++)
{
String BtnName = Genveje[i].split("#")[0];
String BtnPath = Genveje[i].split("#")[1];
Button btn = new Button(this);
btn.setText(BtnName);
btn.setTag(BtnPath);
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
tr[i].addView(btn);
.........
}
The problem is: when i try to do ANYTHING with tr[i] i get a NullPointerException, and i have no clue why.
Anyone with a suggestion?
tr[i]=new TableRow(this);
tr[i].addView(btn);

Categories

Resources