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);
Related
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);
}
}
I am fetching data from database. Based on the value, I'm dynamically creating radio buttons. When I try to add radio group to that radio buttons, radio group is not setting up. Bcoz of that I can select many radio buttons at once.
attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values
are Small, Medium and Large.
Here is the complete code for RadioButton.
RadioGroup radioGroup = new RadioGroup(mMain);
LinearLayout[] attr_layout = new LinearLayout[optionsList.size()];
attr_layout[i] = new LinearLayout(mMain);
attr_layout[i].setOrientation(LinearLayout.HORIZONTAL);
int attr_size = attributes.size();
for (int k = 0; k < attr_size; k++)
{
String price = String.format(Locale.ENGLISH, AppConstants.DECIMAL_POINTS, Float.parseFloat(attributes.get(k).getAttr_price()));
String name_price = attributes.get(k).getAttr_name()
+" ("+ mMain.getString(R.string.currency_code)
+" "+ price +")";
if(!multiSelect.equals("1")) // This multiselect value 1 and 0 is coming from database.
//Based on these value, app will display checkbox or radio button
{
final RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(k));
radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(radioButton, "Museo_Slab.otf");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f);
//lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(params);
attr_layout[i].addView(radioButton, lp);`
Add radioButtons into radio group then add into layouts.
Use the code
radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)
instead of
attr_layout[i].addView(radioButton, lp);`
Radio group allows to check only one button at once belonging to it (if you are trying to check all buttons of a radio group).
radioGroup.addView(radioButton, lp); instead of attr_layout[i].addView(radioButton, lp); and then put attr_layout[i].addView(radioGroup); after adding all radioButtons, i.e. outside the for loop.
So what I want to achieve is to change the position of my buttons. this is my sample picture:
Note:
My buttons have background drawable, for example sake, I just uploaded without the background images on buttons.
Default
When shuffled:
So this is the code I'm working on:
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
// Shuffle the collection
Collections.shuffle(objects);
List<Button> buttons = new ArrayList<Button>();
buttons.add((Button) findViewById(R.id.bObject1Stage2_1));
buttons.add((Button) findViewById(R.id.bObject2Stage2_1));
buttons.add((Button) findViewById(R.id.bObject3Stage2_1));
buttons.add((Button) findViewById(R.id.bObject4Stage2_1));
buttons.add((Button) findViewById(R.id.bObject5Stage2_1));
Collections.shuffle(buttons);
for (int i = 0; i < objects.size(); i++) {
//buttons.get(i).setText(objects.get(i).toString());
buttons.get(i);
}
But it's not changing the position of buttons. What am I missing in here? Any help is truly appreciated. Thanks.
Use RelativeLayout.LayoutParams to switch values between buttons You wish to move and apply them using setLayoutParams method
This will allow You to switch position of 2 buttons. To shuffle them You have to prepare some algorithm to make number of switches.
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
This will work only if these button are not relative to each other.
If for example b2 was relative to b1 so it is below it You would have to set b1 to be below b1 using addRule method and removeRule on b2 so it is not relative to b1 any more.
for example You have in xml
<Button
android:id="#+id/bObject2Stage2_1"
/*...*/
android:layout_toRightOf="#+id/bObject1Stage2_1" />
to set this rule You would write
params.addRule(RelativeLayout.RIGHT_OF, R.id.bObject1Stage2_1);
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.
My current code is:
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String b = json_data.getString("examnames");
JSONObject getexamnamesobject = new JSONObject(b);
getexamnames=getexamnamesobject.getString("types");
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
createView(tr,tv1,getexamnames);
t1.addView(tr);
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
//adjust the porperties of the textView
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//t.setTextColor(Color.DKGRAY);
//t.setBackgroundColor(Color.CYAN);
t.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
//tr.setBackgroundColor(Color.BLACK);
tr.addView(t); // add TextView to row.
}
My output for above code is, I'm getting textviews vertically like this:
English
Maths
Science
What must be the change in the above coding to get the horizontal output like this:
English Maths Science
Please help me. This very important for me
Thanks in advance
Set setStretchAllColumns() and setShrinkAllColumns() to the table layout. and make it orientation "horizontal" for the table layout.
You are creating a TableRow and adding it to your TableLayout once for each item in jArray. Your createView method adds one string to that row: the value for the "types" key on the json object from the corresponding item in jArray (getexamnamesobject.getString("types")).
You might have to just loop through and collect all the "types" strings with a StringBuilder and put them in a single TableRow.
Your new LayoutParams should be new TableRow.LayoutParams. Views with "naked" LayoutParams could not be stacked one after another.