I have one little problem...Here is my code..Is there a way to "distribute weights evenly" for those buttons what I made.. I tried to button[i].setWidth().. but when I turn around my phone it looks ugly.. so Is there away to distribute buttons width auto?
ViewGroup row1 = (ViewGroup)findViewById(R.id.TableRow02);
ViewGroup row2 = (ViewGroup)findViewById(R.id.TableRow04);
ViewGroup row3 = (ViewGroup)findViewById(R.id.TableRow06);
ViewGroup row4 = (ViewGroup)findViewById(R.id.TableRow08);
ViewGroup row5 = (ViewGroup)findViewById(R.id.TableRow10);
Button button[] = new Button[36];
for(int i=1;i<36;i++)
{
button[i] = new Button(this);
if(i==32||i==33||i==34||i==35){button[i].setVisibility(-1);}
button[i].setText("700€");
button[i].setTextSize(10);
button[i].setWidth(20);
// Insert buttons in rows
if(i<8){row1.addView(button[i]);}
else if(i<15){row2.addView(button[i]);}
else if(i<22){row3.addView(button[i]);}
else if(i<29){row4.addView(button[i]);}
else if(i<36){row5.addView(button[i]);}
}
Tactically, put the buttons in a LinearLayout and set android:layout_weight="1" for each of them.
Strategically, design a decent UI, one that does not involve a row of 36 buttons.
As CommonsWare said, instead of setting the width, you should consider to set the weight parameter of the buttons in order to achieve a flexible layout.
If you want to achieve this programmatically (i.e. in code and not in the XML layout), you can use the button's setLayoutParams method. I haven't tested it, but something like this should work:
// outside of loop
LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0
);
....
// enter loop
....
button[i].setLayoutParams(p);
In this example, 1.0 represents the weight. The other two parameters represent layout_width and layout_height parameter.
But seriously, I can't imagine that a layout with 36 buttons is very user friendly :-)
Related
I've been trying to write some dynamically generated layout code for a simple app I came up with. I want to display a vertical row of cards, each on containing an undefined number of vertically aligned text boxes.
I wrote the code to generate these and populate the text, but it doesn't appear to be working and I can't figure it out for the life of me.
I'm new to Android Studio, and Java is still relatively fresh to me as well, so I could well be missing something quite obvious here.
I've tried using a few different types of View in A. Studio, and so far most work by themselves, but none can be contained within a card which would be ideal for me. Dynamically creating and editing properties of textViews works fine, but once I include the card view they no longer appear using the exact same code.
//Define Params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left,top,right,bottom);
//Add a card for each ingredient
for (Ingredient ing : ingredients)
{
CardView card = new CardView(this);
CardView.LayoutParams cardParams = new CardView.LayoutParams(CardView.LayoutParams.WRAP_CONTENT, 200);
card.setLayoutParams(cardParams);
card.setRadius(15);
card.setPadding(25,25,25,25);
card.setElevation(10);
card.setMaxCardElevation(30);
card.setBackgroundColor(Color.DKGRAY);
//Make a grid for each card, text on the left, image on the right
LinearLayout linearLayoutInCard = new LinearLayout(card.getContext());
LinearLayout.LayoutParams layoutParamsInCard = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutInCard.setLayoutParams(layoutParamsInCard);
card.addView(linearLayoutInCard);
for(int x = 0; x < 3; x++)
{
TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setPadding(left, top, right, bottom);
textView.setTextSize(15);
textView.setElevation(11);
textView.setTextColor(Color.WHITE);
linearLayoutInCard.addView(textView);
switch (x)
{
case 0:
textView.setText(ing.name);
break;
case 1:
textView.setText(ing.price);
break;
case 2:
//textView.setText(ing.calories);
break;
}
}
I'm expecting a vertical row of cards with text boxes vertically aligned withing them, each with their own content (this whole script will only make one card for now, but that's a data driven thing) yet when I run the application, I get nothing but a blank screen.
Before venturing much further...this could be an example of the XY Problem.
Maybe have a look at the RecyclerView option?
RecyclerViews are entirely designed to manage the UI look and responsiveness of changing/scrolling data sets.
They can be a bit "what the heck" at first...but once writing them a few times, your UI look and code base is a lot more efficient and clean.
RecyclerView example Youtube
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.
I have a linearlayout on xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
And I add dynamic 100 textviews inside it by code below:
llayout = (LinearLayout) findViewById(R.id.rootlayout);
for (int i = 0; i < 100; i++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
int randomInt = new Random().nextInt(100) +1 ;
tv.setText(""+randomInt);
llayout.addView(tv);
}
The result is 100 textviews added and display vertically. This is not as my expect. I want these textviews display with random position inside the layout look like the image below:
How to do it? Thank you!
LinearLayout is for layouting its children linearly (as the name suggests).
As there is no RandomLayout, you can use a RelativeLayout with random left and top layout margins, or an AbsoluteLayout and set random x and y.
Edit: Avoid overlapping texts
Random positions can of course lead to overlapping and it would be up to you to adjust the positions or ignore positions too similar to previous ones. Or you might actually compare the bounding boxes (left, top, width, height) of the view you're about to add to all other views in the container and if there is any overlapping, find another place for it.
You can use tv.setX(position) and tv.setY(position) for show view on specific position
I have a custom view that extends from FrameLayout. I'd like one of the children to have a size equal to the 20% of the total size as shown in the image.
How can I achieve the imageview to always have this size even if the custom view changes its size in runtime?
Lets say you have your view:
FrameLayout view = (FrameLayout)findViewById(R.id.customView);
Then you can get the layoutParams of the view calculate 20% of it, and then create a new view which will be 20% of the view.
For example:
ImageView img = new ImageView(this);
int imgSize = view.getLayoutParams().width * 0.2; //20 % of the width
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(imgSize, imgSize);
//params.gravity = Gravity.TOP;//How to set gravity programmatically
img.setLayoutParams(params);
//Set img attirubutes like src etc.
view.addView(img)
Also because you are using a FrameLayout it might be better to create an instance of a LayoutParam first and the set it in img.setLayoutParams(params), because iam sure you want to change the layout_gravity of the different views, which you can do in the params vairable
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