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
Related
I need to generate TextView inside a layout from a random, API generated, ArrayList. I cant seem to find a way in which they appear in one line, like a String, one after the other and also shift below once the line has reached the max layout width limit. I want each TextView to be sperate as I want to click them.
Would like to achieve something like this...
This is the current code but the line stops and I don't know how to shift it below. Currently I am using a relative layout as the base layout but it is not necessary.
for (int i = 0; i < abc.size(); i++) {
titleText = new TextView(this);
titleText.setId(i);
titleText.setText(abc.get(i));
relativeLayout.addView(titleText, i);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
layoutParams.addRule(RelativeLayout.RIGHT_OF, titleText.getId() - 1);
titleText.setLayoutParams(layoutParams);
titleText.setTextColor(getResources().getColor(R.color.teal_700));
tvArray.add(titleText);
}
Example
In your xml
<LinearLayout
android:id="#+id/my_ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
And write code in yourjava.class
LinearLayout my_ll = (LinearLayout) findViewById(R.id.my_ll);
for(int i=0;i<your_number_of_textviews;i++)
{
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
text.setText(""+i);
my_ll.addView(text);
}
I think this solution would have solved it. First before the loop retrieve somewhere the width of the screen by using -
Point screenSizePoint = new Point();
// this gives you the furthest point from 0,0 e.g. 1440x3168
getWindowManager().getDefaultDisplay().getSize(screenSizePoint);
int sum = 0;
Then you retrieve your abc list somewhere.
And then you always compute the width of the current TextView by using this
titleText.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int width = titleText.getMeasuredWidth();
sum += width;
if (sum >= screenSizePoint.x) {
// shift to next line
}
and sum it up with the width of text views placed before and if the width is bigger than the screen width you just begin on another line.
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
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 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.
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.