showtimeTable = new TableLayout(this);
showtimeTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for(int i=0;i<object.dateList.size();i++){
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Create testview to be the row-content. */
mytext = new TextView(this);
mytext.setText(object.dateList.get(i).getTextDate());
mytext.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
mytext.setTextAppearance(this, R.style.detaileventDate);
tr.addView(mytext);
time = new TextView(this);
time.setText(object.dateList.get(i).getTime());
time.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
time.setTextAppearance(this, R.style.detaileventTime);
tr.addView(time);
/* Add row to TableLayout. */
showtimeTable.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
layout.addView(showtimeTable);
I have this table all setup, but the two textfields are hugging each other. What I want to achieve is the appearance of two columns and have the time textfield on the right side.
I tried changing the margins in their respective Styles but that doesnt affect it.
I think you should use padding here. It will make padding of the Text view inside the table row. I have tried to do it with your code and just added some test lines:
mytext.setPadding(20, 3, 20, 3);
time.setPadding(20, 3, 20, 3);
It worked fine. Moreover, you can use "Gravity" for your purposes. See and example here: http://developer.android.com/resources/tutorials/views/hello-tablelayout.html
Table layout does not have concept of columns but you can add multiple views in table row by defining different width of those views.
Following code will specify size of column depending on specified colSpan value.
int COL_COUNT=2;
int mScreenWidthInDp = getScreenWidthInDip(activityContext);
private View getColumnItem(String colText, int colSpan) {
View view = mActivity.getLayoutInflater().inflate(R.layout.table_column_view, null);
TextView textView = (TextView) view.findViewById(R.id.tablet_title);
textView.setText(colText);
int columnWidth = (mScreenWidthInDp / COL_COUNT) * span;
TableRow.LayoutParams params = new TableRow.LayoutParams(columnWidth, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
params.span = colSpan;
view.setLayoutParams(params);
return view;
}
public static int getScreenWidthInDip(Activity context) {
if (mScreenWidthInDp == 0) {
WindowManager wm = context.getWindowManager();
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int screenWidth_in_pixel = dm.widthPixels;
float screenWidth_in_dip = screenWidth_in_pixel / dm.density;
mScreenWidthInDp = (int) screenWidth_in_dip;
}
return mScreenWidthInDp;
}
you can set layout weight parameter for the table row. This will arrange the childrens accordingly.
mytext.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT,1f));
The final parameter is the layout weight
Related
TableLayout tl = new TableLayout(ActivityAddFlashcard.this);
TableRow tittleRow = new TableRow(ActivityAddFlashcard.this);
TableRow fRow = new TableRow(ActivityAddFlashcard.this);
TableRow.LayoutParams params = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.addView(getTextView("کلمه"));
fRow.addView(getTextView("معنی"));
TextView tv = new TextView(ActivityAddFlashcard.this);
tv.setLayoutParams(new TableRow.LayoutParams(tl.getWidth() // <---- //return 0
, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("dksvlfsnfmmmmnv");
tittleRow.addView(tv);
tittleRow.setGravity(Gravity.CENTER);
tl.addView(tittleRow);
fRow.addView(new TextView(ActivityAddFlashcard.this));
tl.addView(fRow);
TableLayout.LayoutParams params2 = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.setGravity(Gravity.CENTER);
fRow.setLayoutParams(params);
tl.setLayoutParams(params2);
tl.setGravity(Gravity.CENTER);
this is my table layout, i want to set the width of the tittle row equal to width of table.
but getWidth return 0 .
Your view can change its width time to time during layouting process. Probably you're looking for MATCH_PARENT solution:
tv.setLayoutParams(
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
)
);
You may be interested to setup MATCH_PARENT as layoutParameter for tittleRow as well.
Im trying to create two table layouts through activity..
I already have one table layout but how to set through activity?
I know to do it through xml but want to do it programatically..
Please Help
Check this answer and another example here
Just like in xml, you will create a TableLayout, provide params and add rows with your own UI in it.
Take one linear layout(or relative layout) in in your xml get it reference by findViewById() in onCreate() method of your activity.after that create table dynamically and add it to the linear layout.I create a method to do so . ex-
LinearLayout linear= (LinearLayout ) findViewById(R.id.linear);
//call method to add the tablelayout.
linear.addView(createtable(3,5));
private TableLayout createtable(int requiredcolumn, int requiredrow) {
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1f);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
//for border
rowParams.setMargins(2, 2, 2, 2);
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(Color.WHITE);
for (int row = 0; row < requiredrow; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < requiredcolumn; column++) {
Random color = new Random();
int randomColor = Color.argb(255, color.nextInt(256),
color.nextInt(256), color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}
I'm trying to create a view that would take data from an adapter and distribute it next to each other in rows. The view should rely on its width to decide when to put elements in next row. Please have a look on the ilustration, of what I'm trying to achieve.
http://i43.tinypic.com/2w327bq.png
I've found some great code here: Android - multi-line linear layout It generates linear layouts dynamically (puts horizontal layouts inside of an vertical layout). However, I would like to keep all this data in a single view and set an adapter on it. I would be grateful for any suggestions on how to achieve this.
Code snippet from quoted thread:
/**
* Copyright 2011 Sherif
* Updated by Karim Varela to handle LinearLayouts with other views on either side.
* #param linearLayout
* #param views : The views to wrap within LinearLayout
* #param context
* #param extraView : An extra view that may be to the right or left of your LinearLayout.
* #author Karim Varela
**/
private void populateViews(LinearLayout linearLayout, View[] views, Context context, View extraView)
{
extraView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// kv : May need to replace 'getSherlockActivity()' with 'this' or 'getActivity()'
Display display = getSherlockActivity().getWindowManager().getDefaultDisplay();
linearLayout.removeAllViews();
int maxWidth = display.getWidth() - extraView.getMeasuredWidth() - 20;
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.length; i++)
{
LinearLayout LL = new LinearLayout(context);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views[i].measure(0, 0);
params = new LinearLayout.LayoutParams(views[i].getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
LL.addView(views[i], params);
LL.measure(0, 0);
widthSoFar += views[i].getMeasuredWidth();
if (widthSoFar >= maxWidth)
{
linearLayout.addView(newLL);
newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
}
else
{
newLL.addView(LL);
}
}
linearLayout.addView(newLL);
}
First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);
I have to display a grid of pictures taken from the database. Depending on the screen size of the user, I want to show more or less pictures.
1) How to change the number of pictures shown dynamically according to the number of pics from the database?
1a) Which layout would be adequate?
2) If there are more pictures than that can fit on a single screen, obviously it has to be scrolled. How do I define a page wise scrolling instead of scrolling little by little, meaning after each scrolling, next page will have all new members (just like we scroll Applications in Android)
At the moment, I have a TabHost layout for the main Activity and a LinearLayout for the grid type display activity.
I am using API version 10, so GridLayout is not available.
Any suggestions would be very helpful. Thanks in advance.
1) How to change the number of pictures shown dynamically according to the number of pics from the database?
As far as this part is concerned use a for loop based on the number of pics available in your database, the only thing you need is to know the number of elements present in your database, which you then use as I have used numberOfElements here
for(int i = 1; i <= numberOfElements ; i++) {
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
// Text View
TextView tv = new TextView(this);
tv.setText("FAULT "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lHor.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setId(i);
cb.setTag("CheckBox");
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
checkBoxes.add(cb);
lHor.addView(cb);
l.addView(lHor);
}
You might already know how to get the number of elements in your database, if not I used this
// Method 3: Getting total number of entries present in the database
public int getTotalNumberOfEntries() {
String[] columns = new String[]{ KEY_ROWID, KEY_TYPE, KEY_DATE};
Cursor c = myDataBase.query(DATABASE_TABLE, columns, null, null, null, null, null);
int count = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
count++;
}
c.close();
return count;
}
========== EDIT ================
You can call this method in your onCreate() method of your activity
private void setDynamicContentViewOfThisPage() {
// Defining the Scroll View and the LinearLayout
ScrollView sv = new ScrollView(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
// You will need to collect data from the previous Intent :-)
TextView introduction = new TextView(this);
introduction.setText("Set Text Here");
introduction.setGravity(Gravity.LEFT);
introduction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(introduction);
Button instructionsButton = new Button(this);
instructionsButton.setTag("Some Button");
instructionsButton.setId(987654321); // Random ID set to avoid conflicts :-D
instructionsButton.setText("Click to read all the instructions");
instructionsButton.setGravity(Gravity.CENTER);
instructionsButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(instructionsButton);
instructionsButton.setOnClickListener(this);
// Creates a line
TableLayout tl1 = new TableLayout(this);
View v1 = new View(this);
v1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v1.setBackgroundColor(Color.rgb(51, 51, 51));
tl1.addView(v1);
l.addView(tl1);
// Version 2 (Creating Different Layouts)
for(int i = 1; i <= 3 ; i++) {
// Creates a line
TableLayout tl2 = new TableLayout(this);
View v2 = new View(this);
v2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v2.setBackgroundColor(Color.rgb(51, 51, 51));
tl2.addView(v2);
l.addView(tl2);
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
LinearLayout lVer1 = new LinearLayout(this);
lVer1.setOrientation(LinearLayout.VERTICAL);
lVer1.setGravity(Gravity.RIGHT);
lVer1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
// Text View
TextView tv = new TextView(this);
tv.setText("TV "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
// tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer1.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
lVer1.addView(cb);
lHor.addView(lVer1);
LinearLayout lVer = new LinearLayout(this);
lVer.setOrientation(LinearLayout.VERTICAL);
lVer.setGravity(Gravity.RIGHT);
lVer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
Button showsomeOtherButton = new Button(this);
showsomeOtherButton.setTag("showSomeButton");
showsomeOtherButton.setId(i);
showsomeOtherButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
showsomeOtherButton.setText("View Image");
// showsomeOtherButton.setEnabled(false);
lVer.addView(showsomeOtherButton);
Button someOtherDataButton = new Button(this);
someOtherDataButton.setId(i);
someOtherDataButton.setTag("someOtherButton");
someOtherDataButton.setText("Do this action " + i);
// someOtherDataButton.setEnabled(false);
someOtherDataButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer.addView(someOtherDataButton);
showsomeOtherButton.setOnClickListener(this);
someOtherDataButton.setOnClickListener(this);
lHor.addView(lVer);
l.addView(lHor);
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
}
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
Button nextPageButton = new Button(this);
nextPageButton.setTag("goToNExtPageButton");
nextPageButton.setId(98765432);
nextPageButton.setText("Go To Next Page");
nextPageButton.setGravity(Gravity.CENTER);
//nextPageButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity=Gravity.CENTER;
nextPageButton.setLayoutParams(layoutParams);
l.addView(nextPageButton);
// Set the content View to this
this.setContentView(sv);
}
}
I solved the problem by using a LinearLayout. I dynamically added views to it using:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
TextView ChildView = new TextView(getApplicationContext());
ChildView.setText("I am the child");
parentLayout.addView(ChildView);
I loop upto the available view as suggested by Sumit.
I had to enclose the whole activity's layout into a ScrollView and it made up for the invisible children in the dynamic layout