Looping through specific widgets - java

Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.

What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.

original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}

Related

aligning dynamic buttons in gridlayout

I am trying to align dynamic buttons in Grid layout.
Problem:
However, if text is more than one line the formation of buttons in row
is not as needed.
String item[] = new String[]{"item1", "item2", "item3 yes",
"item4","item5"};
Button[] myButton = new Button[item.length];
GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
scrViewButLay.setColumnCount(4);
scrViewButLay.setRowCount(10);
// GridLayout.LayoutParams leftMarginParams = new GridLayout.LayoutParams(
// );
for (int index = 0; index < item.length ; index++) {
myButton[index] = new Button(this); //initialize the button here
myButton[index].setText(item[index]);
myButton[index].setHeight(100);
myButton[index].setWidth(100);
myButton[index].setTag(index);
scrViewButLay.addView(myButton[index]);
}
}
`
I recommend to use an Adapter and then customize however you want
check out this example
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76
I think using a recyclerView with GridLayoutManager should help you out.
This post https://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/ should help you out.
You only need to apply a layout_gravity as the default seems to be to align the text to be the same height (this is why it only happens with the item 3 with two lines).
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.setGravity(Gravity.BOTTOM);

Creating textfields after hitting a button (Eclipse/Android)

I am trying to learn android development in Eclipse and I am stuck. I created a button with using this.
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="bla"
android:layout_gravity="center"
android:textSize="15dp"
android:id="#+id/bla"
/>
And my listener is the following.
button1=(Button) findViewById(R.id.bla);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
What I want is the following. When I click my button I want 3 different textfields to be seen. After I enter some values to that textfields, I want to get back to the initial screen which contains button1. How can I do that? Also with which method I can store the values that are written to this textfields?
Edit:All answers were helpful and I upvoted them but I accepted Diego's answer because of the clarity
If you want the EditText appear after you click a Button you can include all of them in the same XML layout file, set the EditText to INVISIBLE (android:visibility="invisible") and change it to VISIBLE in the OnClick listener.
To create EditText call this on OnButton Click
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout2);
EditText tv;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < 5; i++) {
tv = new EditText(this);
tv.setText("Dynamic TextView" + i);
tv.setId(i + 5);
ll.addView(tv);
}
And get inserted values from EditText check this
String[] strings = new String[allEds.size()];
for (int i = 0; i < allEds.size(); i++) {
strings[i] = allEds.get(i).getText().toString();
}
Initially create 3 edit text's in your xml file and place them as invisible like android:visibility="invisible" for each and every edit text,
now in our java file when button is clicked visible them. like buttonid.setVisibility(View.VISIBLE);

How To Solve Editext in More Numbers in a Layout And Get Values From That?

See this picture:
I am designing a sudoku in android, have done the layout like this, so 81 Editext is there. I want to get all the values in the Editext so its difficult to declare and mange 81 views in activity. Can you suggest me a method which is effective for processing my problem?
Create edittexts dynamically.
In every iteration you are rewriting the ed variable, so when loop is finished ed only points to the last EditText instance you created.
You should store all references to all EditTexts:
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
allEds.add(ed);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
Now allEds list hold references to all EditTexts, so you can iterate it and get all the data.
getting data
String[] strings = new String[](allEds.size());
for(int i=0; i < allEds.size(); i++){
string[i] = allEds.get(i).getText().toString();
}
NOTE:
This is not results your design there may be some changes.but follow these procedure to get exact results. I think you must you use two for loops to get your edittexts in a gridview.
I would suggest you to use array of Edittext's like this
EditText text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99;
EditText[] fields={text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99};
//Give your edittext ids in ids array like this
int ids[]={R.id.text1,R.id.text2,R.id.text3.....R.id,text99};
int values[9][9]={};
for(int i=0;i<fiends.length;i++)
{
fields[i]=(EditText)findViewById(ids[i]);
}
To get values use the same way take an array and save those values in array like this.
use for loop and get the values from the textfields and save it in the 2d array as i have declared in values array. I suppose you know how to code the rest of the thing you really want to know please comment, I'll post the rest.thank u.. I hope this will help you.
You can try out this way:
public static ArrayList<EditText> m_arr_edit= = new ArrayList<EditText>();
RelativeLayout p_rl_layout=new RelativeLayout(m_context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout m_ll_timelayout = new LinearLayout(m_context);
m_ll_timelayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < p_intervals; i++)
{
EditText m_edit = new EditText(m_context);
m_edit.setLayoutParams(new LinearLayout.LayoutParams(90, LayoutParams.WRAP_CONTENT));
m_edit.setText("Set Text");
m_edit.setId(i);
m_arr_edit.add(m_btn);
m_ll_timelayout.addView(m_btn);
}
layoutParams.addRule(RelativeLayout.BELOW);
p_rl_layout.addView(m_ll_timelayout, layoutParams);
I hope it will help you.

how can I add buttons in array in android

I am new user of android and making slide puzzle game and using eclipse.I made buttons in xml file.In java file I made an array of button type.Now my problem is how I add my buttons in array which I made in the xml file in table layout...
Each button should have a unique ID and you can use findViewById(R.id.ButtonX) to get the buttons. Say you have 5 buttons:
Button[] buttons = new Button[5];
buttons[0] = (Button)findViewById(R.id.Button0);
buttons[1] = (Button)findViewById(R.id.Button1);
buttons[2] = (Button)findViewById(R.id.Button2);
buttons[3] = (Button)findViewById(R.id.Button3);
buttons[4] = (Button)findViewById(R.id.Button4);
Of course the IDs have to match those in the XML. You could also use an ArrayList<Button> if you need to add or remove buttons from this array. Do note that adding or removing from the array will not add or remove them from the activity.
Create an activity. In Eclipse there should be 2 lower tabs with one letting you graphically edit the layout. Then all you have to do is link the Java buttons with the xml buttons.
You can try something like this. Assuming bi,b2 b3 are the button names in the XML
ArrayList<Button> bl = new ArrayList<Button>();
bl.add(new Button("b1"));
bl.add(new Button("b2"));
bl.add(new Button("b3"));
I understood that: You want to create dynamic button(You create button in java code not xml). Try that way:
Button btn1 = new Button(this);
Button btn2 = new Button(this);
Button btn3 = new Button(this);
//your table name : tbl
//you must create TableRow and add button to anyone row.And add row to table
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
row1.addView(btn1);
row2.addView(btn2);
row2.addView(btn3);
tbl.addView(row1);
tbl.addView(row2);

OnClickListener and Table Layout

I have an Activity with two layouts, both implemented in R.layout.main. The first one is a Relative Layout with the app's main screen, and the other is a Table Layout, holding a kind of Preferences Screen. Normally, the first one is set to visible, and the second one to gone. By clicking a button I make the Relative Layout gone, and the Table Layout visible.
And here starts my problem, I wanted to set a OnClickListener to that Table Layout (which is actually an array of buttons).
I tried something like:
final TableLayout table = (TableLayout)findViewById(R.id.tab);
table.setOnClickListener(new OnClickListener(){
public void onClick(View arg){
Button clickedButton = (Button)arg;
String t = (String) clickedButton.getTag();
Toast toast = Toast.makeText(getApplicationContext(),t,Toast.LENGTH_SHORT);
toast.show();
}
});
Obviously, it doesn't work.
I'm quite new to Android programming, and I've been looking for a suitable solution for the whole day without any results.
It couldn't work because you are first trying to cast a TableLayout to a button...
if your TableLayout is only containing buttons you could do something like:
TableLayout yourRootLayout = findView....
int count = yourRootLayout.getChildCount();
for(int i = 0; i < count; i++){
View v = yourRootLayout.getChildAt(i);
if(v instanceof TableRow){
TableRow row = (TableRow)v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++){
View v2 = row.getChildAt(r);
if (v2 instanceof Button){
Button b = (Button)v2;
b.setOnClickListener(this);
}
}
}
}
and let your activity implement OnClickListener. Just copy your Existing onClick into Activity itself...

Categories