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);
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 have seen that this can be done for ListView where you can just grab the string-array values from strings.xml and populate the ListView dynamically depending on the number of listed items.
But I was wondering if the same feat can be done using Radio Buttons and Radio Button Group?
I've come across this piece of code
RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;
for(int i=0;i<3;i++){
RadioButton radioButton = new RadioButton(this);
radioButton.setText("new"+i);
radioButton.setId("rbtn"+i);
rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rgp.addView(radioButton, rprms);
}
And was wondering how I can modify the radioButton.setId("rbtn"+i) to grab string-array values from the strings.xml file instead.
Use String[] foo_array = getResources().getStringArray(R.array.foo_array); to read array from strings.xml. Then loop through the obtained array.
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);
I have built an app that dynamically adds TextViews and ImageViews into the TableLayout. I am facing one small issue, I can't get ImageView to be displayed inline with TextView fields. In my fragment I get the results back from a server and display it in the layout.
I have tried several ways such as setting the gravity of TextView to Center vertical like this
I tried limiting the width of the text fields but it won't work.
firstRow.setGravity(Gravity.CENTER_VERTICAL);
This is how my rows are populated:
businessNameRow.addView(businessNameField);
I am adding sets dynamically, each set consists of 7 TextView rows and 1 ImageView in each row as follows:
TableRow firstRow = new TableRow(getActivity().getApplicationContext());
TableRow secondRow = new TableRow(getActivity().getApplicationContext());
TableRow thirdRow = new TableRow(getActivity().getApplicationContext());
TableRow fourthRow = new TableRow(getActivity().getApplicationContext());
TableRow fifthRow = new TableRow(getActivity().getApplicationContext());
TableRow sixthRow = new TableRow(getActivity().getApplicationContext());
TableRow imageRow = new TableRow(getActivity().getApplicationContext());
TableRow seventhRow = new TableRow(getActivity().getApplicationContext());
Then I just add the row to the table
final TableLayout tl = (TableLayout)root.findViewById(R.id.tableResultsGPS);
tl.addView(firstRow); //And so on
My TableLayout is placed within ScrollView so I can scroll through list of results.
Attaching image of what I want to achieve and what it looks like now.
Seems like what you are trying to achieve is more suitable with a ListView implementation.
The general idea:
Create a list item with the layout of an individual item (your textview and image)
Create a new class that models your each item
Using a list view, bind the data to the ListView.
There are many tutorials out there. Try this
Side Note: Good suggestion for layout choice would be relative layout for your ListView item. One of my personal favorite because of its flexiblity.
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);