Insert linearlayout in tablerow - android

I want to add linearlayout in table row and that linear layout will hold some buttons and textview.How I can achieve that.I am using this but it does not display anything
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(0, 0, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tr.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Please Help me.

Try adding row params like this
TableRow row = new TableRow(this);
row.addView(linearLayout, new TableRow.LayoutParams(1));
Add relativeLayout instead of linearlayout and set params like this for textview
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.BELOW);
tv.setLayoutParams(rel_lp);

For a start you need to close your curly braces at right place. While Setting onClickListener, you are not closing the curly brace and not even putting a semicolon there. So it seems you are making a textView in the listener. The following line
checkbox.setOnClickListener(new View.OnClickListener() {
should be like this
checkbox.setOnClickListener(new View.OnClickListener() {
protected void onClick(View v){
//your listener code here.
}
});
And when the layout is so complex, you should use XML files instead of Dynamically coding, unless you have to.

Related

How to change position Textview get using context

TableRow tr1 = new TableRow(context);
TextView hbar = new TextView(context);
tr1.addView(hbar, 0);
tl.addView(tr1,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I need to change the possition of textview.
hbar.setPadding(10, 0, 0, 0); not worked. and also tried with set layoutparams. any suggestions ??
set the layoutperams to the TableRow with LayoutParams.MATCH_PARENT, and then try it once.
TableRow tr1 = new TableRow(context);
TextView hbar = new TextView(context);
tr1.addView(hbar, 0);
tl.addView(tr1,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tl.setPadding(10,0,0,0);
here views are creating dynamically, we unable to change the positions in onCreate method, try to override the onWindowFocusChange() and then do what you want.
try this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//your textview.
tl.setPadding(10,0,0,0);
}

How do I prevent programatically generated buttons from center aligning in Android?

I have a set of buttons that generate based on the number of items pulled from the SQLite database. The buttons generate and work fine but they center align and evenly space themselves out. Meaning if there is one: the button is centered; If there is two: they space evenly from the center; etc.
What I need is for them to stay left align and allow me to use margins to space them out. I know how to set the margins using the parameters but can't figure out why they are defaulting centered in the TableLayout row.
Again the code works and responds perfectly aside from the alignment. I've having similar issues on other activities and I'm sure it's related.
I've included some code that may show what I'm doing:
imgTitleTable = (TableLayout) findViewById(R.id.imagesTableLayout);
int i = 0;
while (i < imgTitle.length) {
if (i % 6 == 0) {
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
imgTitleTable.addView(tr);
imgtr = new TableRow(this);
imgtr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imgTitleTable.addView(imgtr);
}
// add images
ImageButton imgButton = new ImageButton(this);
imgButton.setImageResource(R.drawable.images_sample);
imgButton.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
imgButton.setBackground(null);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent theIntent = new Intent(getApplicationContext(),
Viewer.class);
startActivity(theIntent);
}
});
// add img title
TextView title = new TextView(this);
title.setText(imgTitle[i]);
title.setId(i);
title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
title.setGravity(Gravity.CENTER);
title.setWidth(imgButton.getDrawable().getIntrinsicWidth());
title.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent theIntent = new Intent(getApplicationContext(),
Viewer.class);
startActivity(theIntent);
}
});
tr.addView(imgButton);
imgtr.addView(title);
i++;
}
Try
TableRow.LayoutParams params = new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.LEFT;
imgButton.setLayoutParams(params);

How to add views dynamically to a RelativeLayout already declared in the xml layout?

I declared a RelativeLayout in a xml layout file. Now I want to add Views from code to the existing Layout. I added a Button dynamically to this existing layout as below through code:
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
Now I need to add another Button to the right of the already added Button. I am not able to find the way in which I can add the new one to the right of the previously added button.
Add the rule RelativeLayout.RIGHT_OF for the second added Button LayoutParams:
// first Button
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
may be this can help you, try it.
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout tl=new TableLayout(this);
rLayout.addView(tl);
TableRow tr1=new TableRow(this);
tl.addView(tr1);
Button btn1 = new Button(this);
btn1.setText("Hello");
btn1.setLayoutParams(lprams);
btn1.setId(1);
tr1.addView(btn1);
TextView tv1 = new TextView(this);
tv1.setWidth(40);
tv1.setHeight(LayoutParams.WRAP_CONTENT);
tr1.addView(tv1);
Button btn2 = new Button(this);
btn2.setText("World");
btn2.setLayoutParams(lprams);
btn2.setId(2);
tr1.addView(btn2);
Create another button:
Button tv2 = new Button(this);
tv2.setText("World");
tv2.setLayoutParams(lprams);
tv2.setId(2);
Add add it into your RelativeLayout:
rLayout.addView(tv2);

How to center ListView footer horizontally

I've added a footer to my ListView like so:
...{
listView.addFooterView(getButtonFooter(context));
...}
private View getButtonFooter(Context context) {
Button button = new Button(context);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
return button;
}
I'm trying to figure out how I can now center the button horizontally.
EDIT: oh, maybe like this? button.setGravity(Gravity.CENTER_HORIZONTAL);
EDIT: lol, nope. That didn't seem to work. Guess that just center's the text inside the button...
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
layout.addView(button);
return layout;

Create TableLayout programmatically

I'm trying to create a TableLayout programatically. It just won't work. The same layout in an xml file works though. This is what I have:
public class MyTable extends TableLayout
{
public MyTable(Context context) {
super(context);
setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(context);
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button b = new Button(getContext());
b.setText("hello");
b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(b);
addView(row)
}
}
...
// In main activity:
MyTable table = new MyTable(this);
mainLayout.addView(table);
When I run this, I don't get a crash, but nothing appears. If I get rid of the TableRow instance, at least the button does appear as a direct child of the TableLayout. What am I doing wrong?
Just to make the answer more clear:
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 tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// assuming the parent view is a LinearLayout
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);// TableLayout is the parent view
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);// TableRow is the parent view
tableRow.addView(textView);
Explanation
When you call setLayoutParams, you are supposed to pass the LayoutParams of the parent view
It turns out I needed to specify TableRowLayout, TableLayout etc for the layout params, otherwise the table just won't show!
For me, to get mine I had to call addContentView().
A good solution is to inflate layout files for each instance of row you want to create. See this post : How to duplicate Views to populate lists and tables?
Your problem is at this line:
b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
You need to change LayoutParams to TableRow.LayoutParams:
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

Categories

Resources