I am having issues in TableLayout, width issues. I tried searching a lot, but all I get is solution using xml. I am doing it programmatically. Here is what I am doing
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow tableTitle = new TableRow(this);
tableTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow name = new TableRow(this);
TableRow password = new TableRow(this);
TableRow button = new TableRow(this);
TextView empty = new TextView(this);
TextView title = new TextView(this);
title.setText("Hello table layout");
title.setGravity(Gravity.CENTER_HORIZONTAL);
TextView txtUname = new TextView(this);
txtUname.setText("Username");
TextView txtPass = new TextView(this);
txtPass.setText("Password");
EditText uname = new EditText(this);
EditText pass = new EditText(this);
Button login = new Button(this);
login.setText("Login");
name.addView(txtUname);
name.addView(uname);
password.addView(txtPass);
password.addView(pass);
button.addView(empty);
button.addView(login);
table.addView(name);
table.addView(password);
table.addView(button);
table.setColumnShrinkable(0, true);
setContentView(table);
}
I want to shrink first column. What should I add in this code?
Anyone help!
You should consider moving your to layout xml file. This would make your life way easier.
To make only first column shrinkable replace setShrinkAllColumns(true)
with setColumnShrinkable(0, true).
Related
I'm having a problem with LinearLayout where I want to display a TableLayout after a TextView. I've tried modifying the LayoutParams however I can't seem to make it wrap (which I'm assuming I want it to do).
This is simplified version:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
//This is displayed
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
//This is not displayed - but if I remove the above view it will display...
TableLayout tl = new TableLayout(this);
ll.addView(tl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
By specifiying Orientation as Vertical it worked for me:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
TableLayout tl = new TableLayout(this);
ll.addView(tl);
I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment
I have this code which generates a GUI programmatically
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table = new TableLayout(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
int count = 0;
for (int r = 0; r < 3; ++r) {
TableRow row = new TableRow(this);
for (int c = 0; c < 3; ++c) {
count++;
final Button btn = new Button(this);
btn.setText("");
btn.setId(count);
btn.setOnClickListener(this);
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
TableRow erow = new TableRow(this);
Button btn = new Button(this);
btn.setText("Reset");
btn.setId(10);
btn.setOnClickListener(this);
erow.addView(btn, cellLp);
table.addView(erow, rowLp);
setContentView(table);
}
In the listener, I can change the text of the button I click this way
#Override
public void onClick(View v) {
Button btn = (Button)v;
btn.setText("text");
}
But, how, can I interact with other buttons? I have tried with something like btn = (Button)findViewById(7); but it doesn't work.
I also tried with an array of buttons but still doesn't work. Any tips?
findViewById() is mainly for views created by inflating XML layout files.
Though you can call setId() for views, in this case you should keep simply references to the programmatically created views, i.e.
private Button button1;
and then just use those fields.
Try to use Tag instead id for your buttons:
for (...) {
count++;
final Button btn = new Button(this);
...
btn.setTag(count);
btn.setOnClickListener(this);
...
}
And in listener call (Integer)v.getTag()
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(text.getText());
// textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
How can i print this code when it adds the view to the left of the TableLayout
Set the Textview Gravity to the Right...
textview.setGravity(Gravity.RIGHT);
I've been searching on this around here but I cant seem to find the right source/answer for this.
I want to pull data from my SQLite DB and display it in the layout (with TextViews), according to the number of elements I have in the DB.
How do I add TextViews to the layout "on the fly"?
hardcoding textviews is very simple but I have no idea how to it dynamically.
thanks for any help!
here is my class:
package android.GUI;
public class Shifts extends Activity implements OnClickListener {
String dbTime;
DBAdapter DB = new DBAdapter(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
LinearLayout layout = (LinearLayout) findViewById(R.id.shifts);
TextView text = new TextView(this);
DB.open();
Cursor c = DB.getAllShifts();
if (c.moveToFirst()) {
do {
} while (c.moveToNext());
layout.addView(text);
text.setText(showDB(c));
}
DB.close();
}
public String showDB(Cursor c) {
dbTime = c.getString(1) + "\n" + c.getString(2);
return dbTime;
}
You have used
DBAdapter DB = new DBAdapter(this);
The line before Activity is initiated. So this keyword is causing is null pointer exception.Please write this line on the method onCreate() before db.open();
Adding dynamically:
TextView tv = new TextView(this);
tv.setText("TaskID");
TextView tv1 = new TextView(this);
task = new EditText(this);
task.setMaxWidth(100);
task.setMinHeight(100);
tv1.setText("Task");
id = new EditText(this);
id.setMaxHeight(10);
TextView tv2 = new TextView(this);
name = new EditText(this);
name.setMaxHeight(1);
tv2.setText("Name");
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(l);
l.addView(tv);
l.addView(id);
l.addView(tv1);
l.addView(task);
l.addView(tv2);
You can use ListView and SimpleCursorAdapter, which automatically creates views for your DB records. http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
Or if you still want to manage it by yourself you can use following:
// In onCreate method.
// E.g. your topmost view is LinearLayour with id 'layout'.
LinearLayout layout = findViewById(R.id.layout);
TextView text = new TextView(this);
text.setText("Hello, I'm dynamically added text view");
layout.addView(text).
What you are looking for is a ListView. To populate data from the db into the list, use a SimpleCursorAdapter