I have a GridLayout and inside it someTextView. I have used android:layout_column in xml and it is working. Now my question is that how can we achieve this through java coding. I have seen some examples but they are like as
tv.setLayoutParams(new TableRow.LayoutParams(3));
What Should I do for GridLayout?
I can't say it is that method but it is substitute and giving what I want .
I have set TextView in 4th column and 1st row by this code.
tv.setLayoutParams(new GridLayout.LayoutParams(GridLayout.spec(0),GridLayout.spec(3)));
Waiting if any other answer is available.
Related
Although this looks a lot repeated question but first time for me. I searched all over and could not get the result and ended up posting here.
I am creating a table dynamically of which the TableLayout part is written in xml part.
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="#+id/tl_componentA">
</TableLayout>
</RelativeLayout>
I created an object for the table
TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
Now I tried to add a row dynamically from here onwards as
createTableRowColumn(tableLayoutA);
Functions Related are
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
I did all this and nothing showed me in the emulater. But when I gave same structure in xml mannually then thing was working well.
For this reason i tried something to figure out
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
In toast it was showing me 0. I could not get why, although I have fixed the size for tableLayoutA in the xml itself.
A table in real life needs at least one row and one column.
You didn't see the table, because you only created a row, but, there were no column.
You need to add a column to the row for something to be visible.
Try this :
TextView label_date = new TextView(this);
label_date.setText("DATE");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tableRow.addView(label_date);// add the column to the table row here
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
You can replace the textView with whatever the value you want it to be.
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Here what happens is, you say, add a view into tableLayout and you tell which view it is, and you also say the width and height of the view should wrap it's contents.
Also, specifying 40,70 is not a great idea, what happens when you have varying screen sizes. Furthermore, use a library to handle dynamic view addition and removal. You don't need to reinvent the wheel and save a lot of time and effort. For table views, https://github.com/EsotericSoftware/tablelayout might be a good one (not sure). Another question is, is table view what you are looking for? Make sure you are not mistaking recyclerView for table views, I say this because I don't understand your use case.
Useful link : https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/
I want to develop the Messaging application like native Application. The Problem is when i add buttons after adding two or three buttons depending on name size will become like this
My Code is as below
RelativeLayout tr = (RelativeLayout) findViewById(R.id.contacts_div);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(MainActivity.this);
b.setText(name);
b.setId(id);
tr.addView(b,params);
if(prev_id!=0)
{
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
}
I want if the enough space is not available the it will add on Next Line.
i try many posts in stack overflow but fail to solve this problem
Thanks in advance
You should use a gridview for this.
try setting your button position through yout code once the text is set then you can see if the total width of your buttons is too big. Then you'll know when to place a button on the next line.
And as #Snicolas said use a GridView, the placement will be easier.
I hope this will help you.
You should use FlowLayout. It's not standart android component, so you should find implementation you like. E.g. this one is nice.
I want to make an android app that have a huge text content. imagine that i want to make a dictionary app. so what should i do. what method should i choose. should i make lots of textviews and activities or there is a solution?
I'm newbie in android programming...please help me! I tried by making a lot of textviews and activity manually but it's time-consuming...
Take a look at http://developer.android.com/guide/topics/ui/layout/listview.html you can import words for example from a databases and put them into the listview then dynamically create activities.
If you want TextViews you can always make them by code.
You declare a xml with a scrollView on top and inside a linearlayout.
On the class you do (or something close to that):
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
for(String text: textList){
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) layout).addView(valueTV);
}
Hope it helps
i have create my own View and ViewGroup. The ViewGroup is a LinearLayout that includes a dynamic TableView. I add two rows, but only one is displayed. The first Row take the full height.
The Destination on Run is this Output:
But i need this:
Perfectly, the cells are square and the Bottom is unused.
Like this:
I have uploaded the stylized code
I hope someone can help me.
Sry, i cant include the images directly because: "We're sorry, but as a spam prevention mechanism, new users aren't allowed to post images. Earn more than 10 reputation to post images."
And the image Urls i have included as text because: "We're sorry, but as a spam prevention mechanism, new users can only post a maximum of two hyperlinks. Earn more than 10 reputation to post more hyperlinks."
Thank you
I dont know more about creating table by code but same problem i feel while creating the table from XMl file. In that time i have solved the problem by using height-width of linear layout with fill parent option.
Or U can Use Relative Layout instead of the Linear Layout and try it. May be it helps u.
Try setting the weights this way.
float rowWeight = 1/noOfColumns;
TableRow.LayoutParams mCellLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, rowWeight );
mCellLayoutParams.setMargins(5, 5, 5, 5);
I am trying to display a SpreadSheet in the Android application with auot adjustable columns and each columns should surrounded by lines.I used the Table Layout the data are displayed in the Table format but i dont know how to surround each column with lines, auto adjustment.If anyone knows it please help me.
You can set a background color for the TableLayout and give your TableRows a margin:
<TableLayout android:background="#000000">
<TableRow android:background="#ffffff" android:layout_margin="3dip">
<!-- etc. -->
I open-sourced an elementary spreadsheet I wrote here:
https://github.com/dennis-sheil/android-spreadsheet
One elementary features it does not have yet:
You can load Microsoft Excel pre-2007 (.xls) files, but not Excel 2007/2010 (.xlsx) files. This is the feature I have been stuck on implementing for a while. There is a codebase (POI) out there to do this, but there are complexities to implementing it.
I have found this article with a Spreadsheet layout example. Maybe it can be helpful to someone too. But it is just an example, still need to update it for general purpose.
http://www.codeofaninja.com/2013/08/android-scroll-table-fixed-header-column.html
Use TableLayout.LayoutParams or TableRow.LayoutParams. They inherit ViewGroup.MarginLayoutParams, which you seem to need.
A sample code with TableRow.LayoutParams could be:
// you can also init values for width, height and weight here
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.setMargins(LEFT_MARGIN, TOP_MARGIN, RIGHT_MARGIN, BOTTOM_MARGIN);
TextView textView = new TextView(this);
textView.setText("I'm in the table");
TableRow row = new TableRow();
row.addView(textView, params);
The same principle could be applied with TableLayout.LayoutParams, when you add to the table layout.