Layout Problems with Buttons - android

I'm trying to add table rows programatically (see code following)
Each row contains some textfields and terminates with sometimes a Button
But the button is not aligned like textfields..(see image of output)
I would like to have the same height for textfield and Button
How can i do that ?
Many thanks for your support
Jcarlier
Brussels
public class MainActivity extends Activity {
............
TableLayout tl = (TableLayout) findViewById(R.id.table);
// heading
TableRow tr_head = new TableRow(this);
TextView label_club= new TextView(this);
label_club.setText("REMARQUE");
label_club.setGravity(Gravity.CENTER);
label_club.setTextColor(getResources().getColor(R.color.OrangeRed));
label_club.setBackground(getResources().getDrawable(R.drawable.border));
tr_head.addView(label_club);
// etc..
// iterate sqlite and show in table TableLayout
Integer icount=0;
Integer j =c.getColumnCount()-1;
if (c != null && c.getCount() > 0){
if (c.moveToFirst()) {
do {
TableRow tr = new TableRow(this);
tr.setBaselineAligned(false);
//tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for( int i=0;i<j+1;i++)
{
if ( (i == j) & ( c.getString(i).length() > 4) ) {
//map.put(icount, c.getString(i).toString());
Button btn=new Button(this);
btn.setId(icount);
//btn.setMaxWidth(10);
btn.setVisibility(View.VISIBLE);
btn.setText("Tableaux");
btn.setClickable(true);
//btn.setHeight(20);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// blabla
}
}
});
//btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
//btn.setLayoutParams (new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//btn.setGravity(Gravity.CENTER_HORIZONTAL);
// btn.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(btn); }
else {
TextView text=new TextView(this);
switch(c.getType(i))
{
case Cursor.FIELD_TYPE_INTEGER:
text.setText(String.valueOf(c.getInt(i)));
break;
case Cursor.FIELD_TYPE_STRING:
text.setText(c.getString(i));
break;
}
text.setPadding(0,0,10,0);
//text.setHeight(20);
//text.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(text);
}
}
// add row to table
tl.addView(tr);
icount++;
} while (c.moveToNext());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/layout"
android:scrollbars="horizontal|vertical"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent"
>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="2dp"
android:stretchColumns="*">
</TableLayout>
</HorizontalScrollView>
</ScrollView>

Related

Dynamically add TableRow to above of TableLayout

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);

add buttons to TableRow programatically and fill in screen

I can add TableRow and also some buttons in it programaticaly like below image:
but I want to figure something like this that button fill in screen:
and here is my codes:
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r3"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
and:
Button btn1;
private TableLayout buttonTableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int row = 0; row < 3; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 5; column++) {
Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, null);
String myName = new String(String.valueOf((row * 5) + column + 1));
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
and the xml(my_button):
<Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/newButton" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></Button>
please give me some advice to solve it.
Android TableRow class actually extends LinearLayout class, so you can use all of LinearLayout's features here.
In this case you can use android:layout_weight to equally stretch those buttons. Where you're adding those buttons just add the following piece of code.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;
button.setLayoutParams(p);
Or alternatively add these lines to your button.xml layout file:
android:layout_width="0dp"
android:layout_weight="1"
Docs say:
Equally weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

Create buttons programmatically goes new line

I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?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:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

linear layout inside linear layout is adding but is not visible

I'm having one empty Linear Layout in xml. And I'm adding some text view to it dynamically.
once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.
I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.
my xml is as follows:
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and my java file is as follows:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddTextViewsDynamically extends Activity implements
OnClickListener {
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_textview_dynamically);
button = (Button) findViewById(R.id.dyn_button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
}
Where am I going wrong?
Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using
text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
instead of
text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));
and you should see all your text views.
EDIT :
In your case this should do the trick;
xml :
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
<LinearLayout
android:id="#+id/dyn_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and code :
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Actually you are doing things right, I just removed padding and made orientation of all Linear Layouts to vertical. Modify this on xml file too and it will work :))) Its just you are adding a padding of 10 or 60 which is going of the view taken by the parent.
Modified Java Code:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
// layout2.setPadding(10, 10, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.VERTICAL);
// layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Try this one, then you will see that your code is OK.
<?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"
android:weightSum="100" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="80" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="20"
android:text="Add TextViews" />
</LinearLayout>

How to add multiple buttons to a row dynamically in Android

Does anybody know how to add multiple buttons to a table row dynamically in Android?
You can try this and see if it's what your are looking for.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<Button android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TableLayout android:id="#+id/tableLayout1"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>
Your Activity class:
public class mainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
Button b = (Button) findViewById( R.id.button1 );
b.setOnClickListener( this );
}
#Override
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById( R.id.tableLayout1 );
int buttonsInRow = 0;
int numRows = table.getChildCount();
TableRow row = null;
if( numRows > 0 ){
row = (TableRow) table.getChildAt( numRows - 1 );
buttonsInRow = row.getChildCount();
}
if( numRows == 0 || buttonsInRow == 3 ){
row = new TableRow( this );
table.addView( row );
buttonsInRow = 0;
}
if( buttonsInRow < 3 ){
Button b = new Button( this );
row.addView( b, 100, 50 );
}
}
}
Hope it helps.
Here layout is a TableLayout.If you want to add a row dynamically and buttons in that row can use the follwoing code
TableRow tr1=new TableRow(this);
Button tv=new Button(this);
tv.setText("");
tr1.addView(tv,250,30);
Button tv1=new Button(this);
tv1.setText("");
tr1.addView(tv1,100,30);
layout.addView(tr1);
If you already have row in the layout then just fetch the row and add buttons to the row
After searching for 30 sec I found http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically - this should help you, just change TextViews to Buttons.

Categories

Resources