I am new to android and I have used a TableLayout and I am adding two columns of data into it from a string array. But my second column goes out of screen. It seems that all entries of my second column start from where the longest entry in the first column ends. How to make both the columns fit into the screen with like 50% width for both columns?
My code goes like this..
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.findia1);
TableLayout tl = (TableLayout) findViewById(R.id.tablay1);
for (int i = 0; i < s1.length; i += 2) {
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView textview1 = new TextView(this);
textview1.setText(s1[i]);
tr1.addView(textview1);
TextView textview2 = new TextView(this);
textview2.setText(s1[i+1]);
tr1.addView(textview2);
tl.addView(tr1, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml code is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tablay1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</TableLayout>
</LinearLayout>
Give weight android:layout_weight="1" for the TextView
i guess i got the answer.. i used
tl.setColumnShrinkable(0, true);
tl.setColumnShrinkable(1, true);
this does the job!
Related
I guess my understanding of Android's layout model isn't good but I'm trying to create a tableLayout of 10x10 buttons and they keep getting out of the screen/device display.
Here's my layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
</TableLayout>
<!-- BUTTONS WILL BE HERE-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And my code is just creating rows and buttons :
public class MainActivity extends AppCompatActivity {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl = findViewById(R.id.table);
for(int i=0;i<10;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
for(int j=0;j<10;j++){
Button b = new Button(this);
b.setText(" ");
//b.setLayoutParams(params); //if I uncomment this, the buttons don't show ???
tr.addView(b);
}
tl.addView(tr);
}
}
}
Thanks for your help and if anyone can lead me to a good tutorial on how to make UIs with Android I'd be thankful.
From the Android Developer documentation:
The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container
This could be the reason why. You can use this method setShrinkAllColumns(true) to mark all columns as shrinkable or setColumnShrinkable (int columnIndex, true) to mark the indicated column shrinkable. You can also do it in the XML like this android:shrinkColumns="*".
More info on TableLayout HERE.
I want to dynamically add TableRow to above of TableLayout. I use blow code:
In my activity:
TableLayout table = (TableLayout)ChatActivity.this.findViewById(R.id.tblChats);
for(int i = 1; i < 10; ++i)
{
TableRow row = (TableRow)LayoutInflater.from(ChatActivity.this).inflate(R.layout.chat_row, null);
((TextView)row.findViewById(R.id.attrib_name)).setText("A");
((TextView)row.findViewById(R.id.attrib_value)).setText(i + "");
table.addView(row);
}
table.requestLayout();
chat_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/attrib_name"
android:textStyle="bold"/>
<TextView android:id="#+id/attrib_value"
android:gravity="right"
android:textStyle="normal"/>
</TableRow>
As you know these codes adds TableRow in the bottom of the TableLayout.
Is there any way to add it to the above of TableLayout?
yes you can add row as runtime in table layout.
use below code like chart.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tblChats"
android:layout_width="match_parent"
android:layout_height="match_parent"> </TableLayout>
and make activity like below ..
public class ChartActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
TableLayout table = findViewById(R.id.tblChats);
for (int i = 0; i < 5; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText("Name" + " " + i);
EditText editText = new EditText(this);
row.addView(textView);
table.addView(row, i);
}
}
}
According to Android Team's answer I realized I must change this line:
table.addView(row);
to this:
table.addView(row, 0);
I followed this piece of code written by Aby in another question: https://stackoverflow.com/a/22682248/5915747
The beginning of my text is being cut off on the left side of my screen. I'm missing quite a bit of info and not sure how to fix it, I have tried everything that worked for others but it has not worked for me.
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollviewtest"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.info.PopupActivity"
android:background="#f0f0f0"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/rel_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
android:background="#80000000"
android:layout_centerHorizontal="true">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
init method:
public void init() {
View popupview = getLayoutInflater().inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupview, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
popupWindow.showAtLocation(popupview, Gravity.CENTER, 0, 0);
popup_shown[0] = true;
TableLayout tableLayout = (TableLayout) popupview.findViewById(R.id.table_layout);
TableRow row0 = new TableRow(this);
TextView text0 = new TextView(this);
text0.setText("Test Header1");
row0.addView(text0);
TextView text1 = new TextView(this);
text1.setText("Test Header2");
row0.addView(text1);
TextView text2 = new TextView(this);
text2.setText("Teset Header3");
row0.addView(text2);
TextView text3 = new TextView(this);
text3.setText("Test Header4");
row0.addView(text3);
TextView text4 = new TextView(this);
text4.setText("Test Header5");
row0.addView(text4);
tableLayout.addView(row0);
for (int i = 0; i < 8; i++) {
TableRow tableRow = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setGravity(Gravity.CENTER);
tv0.setText("long test field 0");
tableRow.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.CENTER);
tv1.setText("long test field 1");
tableRow.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.CENTER);
tv2.setText("long test field 2");
tableRow.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.CENTER);
tv3.setText("long test field 3");
tableRow.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.CENTER);
tv4.setText("long test field 3");
tableRow.addView(tv4);
tableLayout.addView(tableRow);
}
}
The longer the strings are for the text views, the more it crops off, however it's a horizontal scrollview, so it should just start at the beginning of the screen and add length to the end, which is scrollable, right?
This is what it ends up looking like with the current strings in vertical/portrait orientation:
Vertical View
This is what it looks like in horizontal, it seems to fix since my screen is large enough for the data:
Horizontal View
If you have any questions, please ask!
Solved this by adding padding to the scrollview. It seems this is a common android bug.
I don't know will it work or not but try removing
android:layout_gravity="center"
line from Relative Layout
i want to add a TabeLayout to a LinearLayout programmatically.
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content">
</LinearLayout>
and this is how i add the table in my MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content);
String[] content = new String[] {"Hello", "World", "Test"};
float d = getResources().getDisplayMetrics().density;
int dp = (int)(3*d);
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableLayout table = new TableLayout(this);
table.setLayoutParams(tableParams);
for(int i = 0; i < content.length; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(rowParams);
int length = content[i].length();
TextView textView = new TextView(this);
textView.setLayoutParams(rowParams);
textView.setText(content[i]);
textView.setPadding(dp, 0, dp, 0);
row.setBackgroundColor(Color.BLUE);
row.addView(textView);
table.addView(row);
}
table.setBackgroundColor(Color.GRAY);
linearLayout.addView(table);
}
}
as you can see the tableParams are set to wrap_content. However, when i set the background color of the table i see, that the width of the table is as long as the display. Why is that so and how can i change that?
This is the output:
http://postimg.org/image/e29bsg9vv/
TableLayout deals with width a bit differently.
You need to let the TableLayout know that it can shrink the columns.
Try:
table.setShrinkAllColumns(true);
Take a look at the reference docs for TableLayout. In particular, notice it states the following:
The total width of the table is defined by its parent container.
Also, as a tip, you can replace:
table.setLayoutParams(tableParams);
linearLayout.addView(table);
with:
linearLayout.addView(table, tableParams);
I've seen non-scrollable ScrollView's a few times on stackoverflow.com, but none of the proposed solutions work for me. Ihope someone can help.
I have a ScrollView containing a TableLayout. The TableLayout is empty, but I programmatically insert rows and columns. I am guessing that I need to tell the ScrollView that the TableLayout has changed/updated, and ScrollView needs to recalculate if it needs to enable scrolling.
I tried to invalidate both the ScrollView and the TableLayout, I tried requestLayout(), but nothing seems to do anything. What am I missing?
Here is my layout XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="horizontal|vertical" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
And here is my Java code. Basicly, this code adds a row per player and 25 buttons next to each player. The buttons are there, but they are out of the screen boundaries.
// Get the TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int player = 0; player < players.length; player++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+player);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to add the player name
TextView labelTV = new TextView(this);
labelTV.setId(200+player);
labelTV.setText(players[player]);
//labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// numShots = 25
for (int shot = 0; shot < numShots; shot++)
{
Button shotButton = new Button(this);
shotButton.setId((player * 100) + shot);
shotButton.setText(Integer.toString(shot));
tr.addView(shotButton);
}
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Add the TableRow to the TableLayout
//};
}
I think you're needing a horizontal scroll? ScrollView only does vertical scrolling, you'll need to have a look at HorizontalScrollView.