TableLayout - TextView and ImageView inline - java

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.

Related

Looping through specific widgets

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);
}
}

Adding more than one LinearLayout into scrollview using while loop

I want to add more than one LinearLayout into my ScrollView, the number of LinearLayout is base on how many data selected from the MySQL database. However, it seems can't add more than one LinearLayout into the ScrollView.
The reason is maybe this: The specified child already has a parent. You must call removeView() on the child's parent first. But I am not sure about the real reason. How can I solve this problem? Here is my coding in Android Studio:
String query = "select * from restaurant";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
LinearLayout data_big_layout, detail_data_layout;
TextView name, type_area, price_txt;
ImageView restaurant_img;
LinearLayout.LayoutParams restaurant_img_params, data_big_params;
while(rs.next()){
data_big_layout = new LinearLayout(this);
data_big_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
data_big_params.setMargins(0,15,0,0);
data_big_layout.setLayoutParams(data_big_params);
data_big_layout.setOrientation(LinearLayout.HORIZONTAL);
data_big_layout.setWeightSum(20);
data_big_layout.setBackgroundColor(Color.WHITE);
data_big_layout.setTag(rs.getInt(1));
detail_data_layout = new LinearLayout(this);
lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,8.0f);
detail_data_layout.setLayoutParams(lparams);
detail_data_layout.setOrientation(LinearLayout.VERTICAL);
detail_data_layout.setPadding(50,0,0,0);
lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
name = new TextView(this);
name.setLayoutParams(lparams);
name.setText(rs.getString(2));
name.setTextSize(24);
name.setTypeface(Typeface.DEFAULT_BOLD);
type_area = new TextView(this);
type_area.setLayoutParams(lparams);
type_area.setText(rs.getString(5)+"/"+rs.getString(6));
type_area.setTextSize(18);
price_txt = new TextView(this);
price_txt.setLayoutParams(lparams);
price_txt.setText(rs.getString(7));
price_txt.setTextSize(18);
detail_data_layout.addView(name);
detail_data_layout.addView(type_area);
detail_data_layout.addView(price_txt);
data_big_layout.addView(detail_data_layout);
restaurant_img = new ImageView(this);
restaurant_img_params = new LinearLayout.LayoutParams(200,
300,12.0f);
restaurant_img.setImageResource(R.drawable.test_restaurant);
restaurant_img.setPadding(0,0,50,0);
data_big_layout.addView(restaurant_img, restaurant_img_params);
Toast.makeText(getBaseContext(),"ID: "+rs.getInt(1), Toast.LENGTH_LONG).show();
data_scroll_view.addView(data_big_layout); //Cannot add data_big_layout in the next loop
z = "Search successful";
}
isSuccess = true;
stmt.close();
rs.close();
con.close();
The code data_scroll_view.addView(data_big_layout); only can run in the first time of the while loop. I selected two rows of data but the output in programme only can show one LinearLayout. How can I solve it? Thanks all.
scroll view can only contain one child
Scroll view may have only one direct child placed within it. Google doc
therefore, inside your loop, you should gather your views into a single vertical LinearLayout then add it to the ScrollView outside of the loop.
Referring to https://developer.android.com/reference/android/widget/ScrollView
ScrollView is "A view group that allows the view hierarchy placed
within it to be scrolled. Scroll view may have only one direct child
placed within it.
To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout".
So instead of trying to add LinearLayouts to the ScrollView, put one LinearLayout as the ScrollView's child and add any views/layouts to that LinearLayout.

LayoutParams.MATCH_PARENT does not work but integer does

I am generating editTexts in a TableRow and the editTexts width is wrapping the content by default. I tried to set their width to match_parent like this:
TableRow tr = new TableRow(getActivity());
EditText et = new EditText(getActivity());
LayoutParams lparams = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
et.setLayoutParams(lparams);
It did not change anything, but i can set their width if i replace LayoutParams.MATCH_PARENT with an integer. Could you please point me in the right direction to see why i cannot set their width to MATCH_PARENT?
I accidentally found out, that if i am not using a TableRow, but placing the editTexts into the TableLayout its width will automaticall match the parent's width.

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);

How to fix my TableLayout and TableRow into specific positions dynamically?

I'm having problems setting up my TableRow with some TextViews in TableLayout dynamically. I have two pictures, one is my current situation shown, and the other is my mockup, expected situation (the goal which I need to achieve). I do not have any XML layouts; All of these are created programmatically, which is also something I need to achieve.
The snapshots shown are for a High Score screen, where I get a list of players with high scores, and display them altogether in a TableLayout. I'm just having trouble with the row/column positions.
Here's my code:
public void onResume() {
super.onResume();
//=============================================
TextView number = new TextView(this);
number.setText("1");
TextView place = new TextView(this);
place.setText("4th");
TextView testScore = new TextView(this);
testScore.setText("113489");
table = new TableLayout(this);
//rows = new Stack<TableRow>();
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row);
this.setContentView(table);
}
If anyone knows how I should change my code from the Current Situation to the mockup Expected Situation, I gladly appreciate it. If I'm doing something wrong, please post a comment. Thanks in advance.
Set the layout weight of each textview to 1 and the layout width to 0dp. That will cause them to split the available space equally. You could use layout gravity left, center and right as appropriate to further ensure maximum separation.
I just found a good link about TableLayout and TableRow, creating specific positions dynamically. Link is here. Of course, the code here is just the barebones of it, used to demostrate a simple position layout.
Here's my code and the given picture of the result:
public void onResume() {
super.onResume();
//=============================================
dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 1, this.getResources().getDisplayMetrics());
if (dip <= 0)
dip = 1;
TextView number = new TextView(this);
TextView place = new TextView(this);
TextView testScore = new TextView(this);
number.setText("1");
place.setText("4th");
testScore.setText("113489");
number.setWidth(50 * dip);
place.setWidth(75 * dip);
testScore.setWidth(150 * dip);
number.setPadding(20*dip, 0, 0, 0);
table = new TableLayout(this);
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
this.setContentView(table);
}
I'm actually astonished that there's a method for obtaining the DIP unit. Should've searched for that in the first place. :P

Categories