So, I create table view in XML to which the rows are added dynamically using the following java code:
private void fillTable(TableLayout tableLayout, String[] items) {
for (int i = 0; i < items.length; i++) {
TextView itemText = new TextView(FillingActivity.this);
itemText.setText(items[i]);
TableRow row = new TableRow(FillingActivity.this);
row.addView(itemText);
if(i % 2 == 1)
row.setBackgroundColor(color.LightGreen);
tableLayout.addView(row);
}
}
The code for a single tablelayout is the following:
<TableLayout
android:id="#+id/dinnertableviewing"
android:layout_width="346dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="210dp" >
</TableLayout>
But then when add many items to that table it overlaps with the space for the next tables
here is a picture that shows it:
Don't use AbsoluteLayout. The difficulties you're running into when an AbsoluteLayout contains content that can resize or when you run on differently sized screens (e.g. just about every Android device in the wild) are exactly why AbsoluteLayout is deprecated.
If you're trying to position elements vertically down the screen as they fit, use a LinearLayout with android:orientation="vertical".
Related
I am struggling at one thing. Let's say I have linear layout with set height and width is match_parent. I will have a set number of views 1 to 6 and I don't know at runtime how much I will receive from server. The problem is, how can I sort them in a layout so they scale their width accordingly to number of views present ? If there are more than 3 views I need to put them in two lines.
I was thinking about using layout weight, but can't think about solution that can put them to two lines. Any ideas ?
You have to dynamically add views to linear layout.
First create container layout in xml.
<LinearLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Then check,
if(list.size()<=3)
{
Then assign weight to container. i.e weight=list.size
for(int i=0;i<size;i++)
{
TextView textview = new TextView(this);
textview.setText(brandName);
textview.setWeight(1f);
container.addView(textview);
}
}
else
{
int totalRows= (list.size/3)+(list.size%3);
int count=0;
for(int i=0;i<totalRows;i++)
{
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
for(int j=count;j<count+3;j++)
{
count++;
TextView textview = new TextView(this);
textview.setText(brandName);
newLL.addView(textview);
}
container.addView(newLL);
}
}
You have to do something like this.This is not actual code.
You should use listview in your rooted LinearLayout and design single list item in an other xml layout , create a custom adapter for your single list item and set the adapter to your list view . this answer will help you how to create custome adapter
In my Project , I have 80 TextViews.
I should set their text from 1 to 80 once project runs , and they dont need to be changed in future.
Except TxtViews , I have some other things in my Layout, the TextViews are under ImagesViews. actually I have 80 imagesViews and under them are 80 TextViews. I want to set text of textViews from 1 to 80 dynamically.
I know I can do it in my layout.xml ,
but its really time consuming.
is there any way to do that by code?
for example with a for cycle or something like that?
Create a ViewGroup suitable for your needs in the layout, for example:
<LinearLayout
android:id="#+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Then you create you TextView instances programatically, and add them to the LinearLayout, like this:
LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
for(int i = 0; i < 80; i++) {
TextView textView = new TextView(getContext());
textView.setText("text" + i);
layout.addView(textView);
}
Optionally, you can add tags or whatever to locate them again. Alternatively just iterate over the layouts subviews.
If you know that 80 Textview fixed then you should take listview for that.
Listview Benefit
Memory management automatically
Listview manage indexing
If they share the same layout, except for the text, and could be displayed as a list, you could use an ArrayAdapter and pass the values from code.
http://www.mkyong.com/android/android-listview-example/
Checkout the below example,
public class MainActivity extends Activity {
LinearLayout linearLayout ;
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollViewActivityMain);
}
private void populateTextViews() {
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//add all textViews here
for(int i=0; i < 80; i++){
TextView myTextView = new TextView(this);
myTextView.setText("My TextView "+i);
myTextView.setGravity(Gravity.CENTER);
linearLayout.addView(myTextView);
}
scrollView.addView(linearLayout);
}
}
Don't forget to put that scrollView in your xml.
Let me know if it works for you...
If your TextViews are declared on the xml, wrap them on another view so you can reference it on the java code later, then simply use a for.
Something like:
View view = findViewById(R.id.your_wrapper);
for(int i=0; i<((ViewGroup)view).getChildCount(); i++) {
View nChild = ((ViewGroup)view).getChildAt(i);
TextView tv = (TextView) nChild;
tv.setText(String.valueOf(i + 1));
}
If not, you can simply create them dynamically inside your java code, and append them to a layout like LinearLayout.
Example:
xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear"
/>
Java code
LinearLayout ll = (LinearLayout) findViewById(R.id.linear);
for (int i = 1; i <= 80; i++) {
TextView tv = new TextView(this); // Assuming you're inside an Activity.
int count = ll.getChildCount();
tv.setText(String.valueOf(i));
ll.addView(tv, count, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
EDIT: But truly, you should use RecyclerView or ListView for that if your values are not going to change.
You can read more about RecyclerView here, and on ListView here.
Second edit: From what you're saying on your comments, you REALLY should be using ListView instead of your current design. The solutions above and from the other answers won't work at all for your problem.
I want to nest a TableLayout inside a RelativeLayout and later dynamically edit the TableLayout in my Java Code.
My XML-File looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_load_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoadDateActivity" >
<!-- few buttons and textviews -->
<TableLayout
android:id="#+id/activity_load_date_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/button" >
</TableLayout>
</RelativeLayout>
Java Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_date);
//Do something with my Buttons and TextViews(this works fine)
tblLayout = (TableLayout) findViewById(R.id.activity_load_date_table_layout);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.button_calc) {
for (int i = 0; i < listOfEntries.size(); i++) {
Entry temp = listOfEntries.get(i);
if (temp.getDate().getTime() >= startDate.getTime()
&& temp.getDate().getTime() <= endDate.getTime()) {
TableRow tr = new TableRow(this);
TextView comm = new TextView(this);
comm.setText(listOfEntries.get(i).getComment());
TextView val = new TextView(this);
val.setText(String.valueOf(listOfEntries.get(i).getValue()));
LayoutParams params = new LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1f);
tr.setLayoutParams(params);
tr.addView(comm);
tr.addView(val);
tblLayout.addView(tr);
}
}
tblLayout.invalidate(); //Shouldn't this redraw the entire TableLayout and therefore adding my TableRows? This is not working.
}
}
Through various tests with TextViews and Toasts I have gathered that the tblLayout should be filled and the TableRows are added to the Layout, the only thing that is not working is the "repainting" of my Layout. How do I achieve that?
Edit:
Apparently the thing that made this not work was actually the LayoutParams given to the TableRow, once I commented those out I atleast got it printed to the screen. They are however not where I expect them to be.
I expected them to be below the buttons, instead they are in the top left corner on top of the buttons. This leads me to believe that the TableLayout is actually the same size as the RelativeLayout but is layered above the RelativeLayout. The error should therefor lie in my XML-File. What height do I need to give my TableLayout to make this work the way I expect?
Edit2:
I needed to add the android:layout_below attribute to my TableLayout, works as a charm now!
You need to call the method "requestLayout()"
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
i am designing an app for android tablets. I have created a layout file which contains the application bar, next is some buttons. In the middle of the screen i want to have a horizontal scroll view inside which i need to show some images.
The number of images inside the scroll view depends upon the return data from the url. For this i am maintaining an array list. According to the size of the array i need to create the image views withing the scroll view.
I have placed all other buttons, textviews in the layout file and i need to make the above said view alone through coding, how to do this.
If the array size is 19, then the list of images within scroll view to be shown in the following order only
1 4 7 10 13 16 19
2 5 8 11 14 17
3 6 9 12 15 18
In iPad iBook apps library page, the books will be listed out in this way.
how to do this....
not sure but can try approach like this
1- Create/inflate a horizontal scrollview ..
2- make for loop running i= 0 to x
where x= (totalCount/3)+(totalCount%3>0?1:0)
3- Create a Linear layout with orientation vertical
4- create one more loop form j=0 to 3 or (i+1)*3+j< totalCount
5- add your element layout in Linear layout
6 after the inner loop closed add Linear layout in horizontal scroll-view
loop termination condition like the value of x may not be exact please check them
For making item clickable
1- take any view from element layout like in you case image-view is good option
2- creates a class in you activity or better to extend you activity with clickListner.
3- while creating the imageView for each element set this listener to all
4- Set the data object or index with element with image-view in tad using SetTag
5- in Onclick function you will get image-view as argument and use getTag to get that data of attached with clicked element
Thanks to Dheeresh Singh
Following is my main.xml file
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
i have my Horizontal scroll view in my main.xml file
public class LayoutActivity extends Activity
//implements OnClickListener
{
ViewGroup layout;
LinearLayout lr;
int x;
int total = 4;
int count = 0;
LinearLayout lay;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
count = 0;
layout = (ViewGroup)findViewById(R.id.linear_1);
x = (total/3)+(total%3 > 0?1:0);;
for(int i = 0; i < x; i++)
{
lr = new LinearLayout(this);
lr.setOrientation(LinearLayout.VERTICAL);
layout.addView(lr);
for(int j = 0; j < 3; j++ )
{
count++;
final View child = getLayoutInflater().inflate(R.layout.inflateview, null);
lay = (LinearLayout)child.findViewById(R.id.threeByThree_tableRow1_1_Layout1);
lay.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "selected id is "+child.getId(), Toast.LENGTH_SHORT).show();
}
});
lr.addView(child);
child.setId(count);
if(i == total)
{
break;
}
}
}
}
}
in the above code i have lr is the LinearLayout to display it in vertical order and View child is the data which i need to show in both vertical and horizontal order.
Thanks a lot Dheeresh Singh
How to create a shelf like view in android that show several book in any row? Also, it should have horizontal and vertical features like the moon+reader app has.
I can write a shelf view that moves horizontally but it doesn't fully work. I used a xml file for view items that included image, text and button. I wrote a class that extends AdapterView to create a customized ListView that I called "shelf view". Unfortunately, my program show one row and I can't use it for several row.
Last Updated: Now, I can detect a new way for create shelf-view better than the previous solution. I described it in CodeProject
By the Way, In this application I used two classes:
HorizontalListView Class that extends the AdapterView. It downloaded from GitHub
Quaere library use almost same as Linq2Object in .Net. You can download here.
Apr 22 '12:
There are some ways to implement shelf view that it have two features(horizontal & vertical scroll). I try to write a program that can run dynamically. This sample App have a XML file and a showShelfView java class.
So you can see my App:
main XML file: First, Add following code in main.XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/sclView">
<TableLayout
android:id="#+id/tblLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp">
</TableLayout>
</ScrollView>
showShelfView Class: Inner TableLayout add several HorizontalScroll equals with number of rows. Also inner any TableRow add Image.
Don't forget set a shelf image for Row's background:
public class showShelfView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int numRow = 4;
int numCol = 8;
TableLayout tblLayout = (TableLayout) findViewById(R.id.tblLayout);
for(int i = 0; i < numRow; i++) {
HorizontalScrollView HSV = new HorizontalScrollView(this);
HSV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow tblRow = new TableRow(this);
tblRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tblRow.setBackgroundResource(R.drawable.bookshelf);
for(int j = 0; j < numCol; j++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.book1);
TextView textView = new TextView(this);
textView.setText("Java Tester");
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tblRow.addView(imageView,j);
}
HSV.addView(tblRow);
tblLayout.addView(HSV, i);
}
}
}