Display one row in the view from ListView - android

I had a ListView created programmatically, and I need to display only ONE visible row between the red borders not multiple items, and scroll to get the others.
This my function
public void fillFormules(List<Category> objectsTextToShow)
{
LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j <objectsTextToShow.size() ; j++) {
TextView textCat = new TextView(getActivity());
textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
textCat.setTextSize(28);
textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
textCat.setTextColor(colorUtils.TITLE);
textCat.setGravity(Gravity.CENTER);
relativeLayout.addView(textCat);
textCat.setBackgroundColor(colorUtils.backgroundColor);
LinearLayout parentLayout = new LinearLayout(getActivity());
parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
List<Item> items = new ArrayList<Item>();
ListView listView = new ListView(getActivity());
for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++) {
items.add(objectsTextToShow.get(j).getItems().get(i));
}
listView.setAdapter(new ScrollAdapter(getActivity(), items));
parentLayout.addView(listView);
layoutItemDetail.addView(parentLayout);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
I use this xml to inflate the view, even I give match parent fot the weight and the height but it doesn't work :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="25dp"
android:text="TextView" />
This what I must have in the end but I don't know how to do it :/

You can set a fix height to your Linearlayout used to display every item of the list. And put also the same height to your listView
LinearLayout parentLayout = new LinearLayout(getActivity());
parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, yourHeight, (float) 1.0));
And in your LinearLayout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="yourHeight"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="25dp"
android:text="TextView" />

Related

How to make Android LinearLayout to clip its children content?

I have a LinearLayout (horizontal) with several child TextViews inside. I have set fixed layout_width for both the LinearLayout and all the TextViews. When the total width of the children exceeds the width of the layout, it automatically shrink the children, making them narrower, which is not what I want. I want the layout to clip the children content and keep their width.
How can I achieve that?
For example: Let's say my LinearLayout is 200dp wide, and there are 4 TextView each 60dp wide. I want it to show the first three TextViews, and 1/3 of the 4th (60 + 60 + 60 + 20 = 200)
Thanks.
Use android:weightSum in LinearLayout
And use android:layout_width="0dp" and android:layout_weight="1" in TextView
Try this.
<?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="horizontal"
android:weightSum="10">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello"/>
</LinearLayout>
Also
You can change android:layout_width="match_parent" to android:layout_width="wrap_content" in TextView .
Edit
And you can use in your java code .
You can change the width or weight by yourself .
private LinearLayout mLinearLayout;
private int viewsCount = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
for (int i = 0; i < viewsCount; i++) {
TextView textView = new TextView(this);
if(i == viewsCount-1){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
} else {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3);
}
mLinearLayout.addView(textView);
}
}

Android table align tidy with frozen header

I am trying to create a table with a frozen header. I can achieve that by having a tablelayout which encompasses a scrollview wrapping another tablelayout but my header alignment is out.
Heres the XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/table_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</TableLayout>
</ScrollView>
</TableLayout>
and heres the code
public class PrepositionRulesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
init();
}
public void init(){
createTableHeader();
createTableBody();
// alignRows();
}
private Collection<Preposition> load(Context context) {
DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context);
return dataSource.get();
}
private void createTableHeader() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header);
tableLayout.addView(createHeaderRow(), 0);
}
private void createTableBody() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body);
int row = 0;
TableRow tableRow = createHeaderRow();
tableLayout.addView(tableRow, row++);
for (Preposition preposition : load(this)) {
tableLayout.addView(createBodyRow(preposition), row++);
}
}
private TableRow createHeaderRow() {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(getString(R.string.preposition)));
row.addView(createCell(Kasus.Akkusativ.name()));
row.addView(createCell(Kasus.Dativ.name()));
row.addView(createCell(Kasus.Genitiv.name()));
return row;
}
private TableRow createBodyRow(Preposition preposition) {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(preposition.getPreposition()));
row.addView(createCell(preposition.contains(Kasus.Akkusativ)));
row.addView(createCell(preposition.contains(Kasus.Dativ)));
row.addView(createCell(preposition.contains(Kasus.Genitiv)));
return row;
}
private void alignRows() {
TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header);
TableRow headerRow = (TableRow)headerLayout.getChildAt(0);
TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body);
TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0);
headerLayout.setLayoutParams(bodyLayout.getLayoutParams());
for(int i = 0; i < bodyRow.getChildCount(); i++) {
headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams());
}
}
private View createCell(boolean isEnabled) {
if (isEnabled) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_done_black_24dp);
return view;
}
return new Space(this);
}
private View createCell(String text) {
TextView view = new TextView(this);
view.setPadding(15, 0, 15, 5);
view.setTypeface(Typeface.DEFAULT_BOLD);
view.setText(text);
return view;
}
}
I thought I could achieve the alignment by adding a copy of the header row to the body table and than copying its layout to the row added to the header table but that did not work.
Any suggestions would be greatly appreciated
Image showing misalignment:
It would be hard to control the alignment in a tablelayout, because a column's width will always depend and will be affected by another column's width of another row in the same table.
In order to create a table with tidy alignment, I would suggest you use a LinearLayout (for the header and row) and listview to show to items (row).
Example row.xml for both table's header and list's row:
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="beginning|middle|end">
<TextView
android:id="#+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="4dp"
android:text="#string/model"
android:textStyle="bold"/>
<TextView
android:id="#+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="#string/color"
android:textStyle="bold"/>
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/status"
android:textStyle="bold"/>
</LinearLayout>
This is how you apply it in your activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<include layout="#layout/row"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Lastly, you inflate the same row.xml for each of your list item in your listview's adapter. Below here is the screenshot of the table created by the above xmls. Kindly note that I have removed the divider and set the textstyle to normal programmatically when showing each row item in the table.

Setting layout weight to dynamically added view is not working in Android

I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column

Android TableLayout isn't displaying rows

I am having problems getting my TableLayout to display my rows.
I'm propagating the rows programmatically via this method:
private void buildStatesTable() {
Log.d(SelectStatesPanel.class.getCanonicalName(), "Entering Build States Table");
ArrayList<String> states = new ArrayList<String>();
Random rand = new Random();
for(int i = 0; i < 10; i++){
if(i < goodStates.length){
states.add(goodStates[i]);
}else{
states.add(badStates[rand.nextInt(badStates.length)]);
}
}
Collections.shuffle(states);
TableLayout tl = (TableLayout) findViewById(R.id.statesTable);
final int NUM_PER_ROW = 2;
for(int i = 0; i < (states.size() / NUM_PER_ROW); i++){
TableRow tr = new TableRow(getContext());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for(int j = 0; j < NUM_PER_ROW && (i*NUM_PER_ROW + j) < states.size(); j++){
TextView lblState = new TextView(getContext());
lblState.setText(states.get(i*NUM_PER_ROW + j));
lblState.setTextColor(Color.BLACK);
lblState.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
lblState.setClickable(true);
lblState.setOnClickListener(this);
tr.addView(lblState);
//Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding State: " + states.get(i*NUM_PER_ROW + j));
Log.d(SelectStatesPanel.class.getCanonicalName(), "\t\t\t (" + i + ", " + j + ")");
}
// Add the TableRow to the TableLayout
tl.addView(tr);
Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding Row");
}
}
And this is my XML (table layout is at the bottom):
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Good choice! There's hope for you yet."
android:textSize="16dip"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:id="#+id/txtLex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Lexington"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtTampa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Tampa"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtSacramento"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Sacramento"
android:textSize="14dip"
android:gravity="center"
/>
<TextView
android:id="#+id/txtHiltonhead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_margin="5dip"
android:text="Hiltonhead"
android:textSize="14dip"
android:gravity="center"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dip"
android:gravity="center"
android:src="#drawable/usa"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dip"
android:id="#+id/statesTable"
android:stretchColumns="0,1">
</TableLayout>
</LinearLayout>
Why isn't my table layout's showing content?
I can't see specifically what is going wrong in your code, but here is a similar implementation in my own code:
public class EconFragment extends Fragment {
private TableLayout questionContainer;
static int pos = 0;
private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
"hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("Econ", "onCreateView");
return inflater.inflate(R.layout.econfragment, container, false);
}
public void onResume() {
super.onResume();
LayoutInflater inflater = (LayoutInflater) getActivity().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer);
//these lines are my first attempt to try and get the questions to repopulate after returning
//from pressing back - as of yet, they don't repopulate the screen:
//View econFragment = inflater.inflate(R.layout.econfragment, null);
//container.addView(econFragment);
int leftMargin=5;
int topMargin=5;
int rightMargin=5;
int bottomMargin=5;
while (pos < 10) {
View question = inflater.inflate(R.layout.question, null);
question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.questionTextView);
title.setText(titles[pos]);
Button charts = (Button) question.findViewById(R.id.chartsButton);
charts.setId(pos);
charts.setOnClickListener(chartsListener);
TableRow tr = (TableRow) question;
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
questionContainer.addView(tr);
pos++;
}
Log.d("Econ", "onResume");
}
And the source of questionContainer, econfragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/questionContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
As you can see, a Question.class object is created as a View object in the EconFragment fragment. question object is used to findViewById for each needed portion.
Comment out the whole row code and replace it with a two liner textview row. Does that work? Then the array from which you build your row views is maybe the problem.
Best regards.
I discovered the problem on my own.
The problem was this:
lblState.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Specifying TableRow.LayoutParams fixed the problem.

Order rows in TableLayout

I have TableLayout in my xml.
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/jobs_table"
android:gravity="top"
android:stretchColumns="*"
android:shrinkColumns="*"/>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/monday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tuesday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wednesday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thursday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/friday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/saturday"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sunday"/>
</TableRow>
Then, I add rows with buttons in Activity.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jobs);
tableLayout = (TableLayout) findViewById(R.id.jobs_table);
// Make some buttons here.
TableRow row = new TableRow(this);
tableLayout.addView(row, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
When I run application, row with TextViews from XML are on the bottom and Buttons from Java code are on the top. How can I reverse them(Buttons are on the button and row with TextViews are on the top)?
I think this version of addView does what you need (with index = 0)
public void addView (View child, int
index)
Adds a child view.
If no layout parameters are already
set on the child, the default
parameters for this ViewGroup are set
on the child. Parameters
child the child view to add
index the
position at which to add the child
So, I did it as:
TextView[] days = new TextView[7];
for(int i = 0; i < 7; i++) {
days[i] = new TextView(context);
days[i].setGravity(Gravity.CENTER);
row.addView(days[i]);
}
days[0].setText(R.string.monday);
days[1].setText(R.string.tuesday);
days[2].setText(R.string.wednesday);
days[3].setText(R.string.thursday);
days[4].setText(R.string.friday);
days[5].setText(R.string.saturday);
days[6].setText(R.string.sunday);
rowsWithButtons.add(row);

Categories

Resources