I'am trying to populate 2 LinearLayout to achieve something like this:
This is a simple test and the code it's not fully implemented to do what i want.
The problem is after the first pool the linkedList it's empty, and the size before the pool was 2.
My Layout to populate each column space.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/latest_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/latest_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#drawable/card_w_shadow"
android:onClick="onTopicsPreviewClick"
android:orientation="vertical"
android:paddingBottom="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/latest_preview_title"
style="#style/CardTitle_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="4dp"
android:background="#color/C_Moodle_Orange" />
<LinearLayout
android:id="#+id/latest_description_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="4dp" >
<TextView
android:id="#+id/latest_preview_description"
style="#style/CardText_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:ellipsize="end"
android:maxLines="4"
android:text="Lorem ipsum dolor sit amet" />
</LinearLayout>
</LinearLayout>
My fragment:
public class FragLatest extends Fragment {
#SuppressWarnings("unchecked")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getActivity().getApplicationContext();
ManContents content = new ManContents(context);
// The Linked List, in this example is size =2 and it contains
// Mancontents objects
LinkedList<ManLatest> latestList = (LinkedList<ManLatest>) new ManDataStore(
context).getData("Latest");
// The Vertical LinearLayout
LinearLayout outerLayout = new LinearLayout(context);
outerLayout.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
outerLayout.setOrientation(LinearLayout.VERTICAL);
outerLayout.addView(setLayoutTitle(context));
// Get from resource the number of cards per line
int cardsPerLine = getResources().getInteger(
R.integer.latest_item_per_line);
// Calculate the number of rows to build
int lines = setRows(latestList, cardsPerLine);
for (int i = 0; i < lines; i++) {
// The Horizontal Linear Layout
LinearLayout innerLayout = new LinearLayout(context);
innerLayout.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < cardsPerLine; j++) {
ManLatest latest = latestList.poll();
Long topicId = Long.parseLong(latest.getTopicId());
String courseId = latest.getCourseId();
MoodleCourseContent[] courseContent = content
.getContent(courseId);
MoodleCourseContent topic = content.getTopic(topicId,
courseContent);
View view = inflater.inflate(R.layout.frag_latest, null);
view.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1f));
TextView title = (TextView) view
.findViewById(R.id.latest_preview_title);
title.setText(topic.getName());
TextView description = (TextView) view
.findViewById(R.id.latest_preview_description);
description.setText(topic.getSummary());
innerLayout.addView(view);
}
outerLayout.addView(innerLayout);
}
return outerLayout;
}
/**
* #param latest
* #param cardsPerLine
* #return
*/
private int setRows(LinkedList<ManLatest> latest, int cardsPerLine) {
int colums;
if (latest.size() > cardsPerLine) {
Double count = Math.ceil(latest.size() / 2.0);
colums = count.intValue();
} else {
colums = 1;
}
return colums;
}
/**
* #param context
* #return
*/
private TextView setLayoutTitle(Context context) {
TextView large_title = new TextView(context);
large_title.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
large_title.setText("Latest Contents");
large_title.setTextAppearance(context, R.style.CardsLayoutTitle);
// large_title.setTypeface(null, Typeface.ITALIC);
large_title.setPadding(30, 10, 0, 10);
return large_title;
}
Related
I am creating an app that would display data from my SQLite database, though I have been successful in displaying the data in Custom ListViews. But I am wondering how would I be able to display data in a Table like form, I can display the first line of data though I don't know where to go from here.
This is my ActivityMain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gin.customlistview.MainActivity">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVED MESSAGES"
android:textSize="20dp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<TableRow
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_margin="1dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Phone Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Username"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Account Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
android:layout_column="2"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<TextView
android:id="#+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:background="#FFFFFF"
android:gravity="center"
android:layout_margin="1dp" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="#+id/txtAccountNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center" />
</TableRow>
</TableLayout>
</LinearLayout>
This is my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
checkPermission();
showData();
}
public static void showData() {
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
Thanks in advance for any help and suggestions! :D
try something like this
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
Once You have Created the tableLayout Create a table row layout table_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/title"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
inside your activity iterate through the values retrieved from the sql and add it to the table
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
data.moveToFirst();
do {
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView name = (TextView) tableRow.findViewById(R.id.name);
TextView title = (TextView) tableRow.findViewById(R.id.title);
name.setText(data.getString(1));
title.setText(data.getString(2));
tableLayout.addView(tableRow);
} while (data.moveToNext());
data.close();
}
Use below code , rows will be created dynamically according to the data , result will be something similar to this
LinearLayout table = (LinearLayout) findViewById(R.id.table);
ArrayList<Tabledata> datalist = new ArrayList<>();
datalist = db.getyourSQLITEdata();
// this method for table title
createtitle(table);
for(int i=0;i<datalist.size();i++){
// this method is creating table rows and filling data
Createtable(table,i);
}
}
table title
// your table title here (if you want title for your table you can use this)
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void createtitle(LinearLayout main){
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
// your table title here
String[] title={"title1","title2","title3","title4","title5","title6","title7"};
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(title[i]);
col1.setTextSize(13);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
table
// your data table is created here
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void Createtable(LinearLayout main,int pos) {
Tabledata data = datalist.get(pos);
// this is where you get data from list and show it in table
String[] yourdata = {data.getdata1(),data.getdata2(),data.getdata3(),data.getdata4(),data.getdata5(),data.getdata6(),data.getdata7()};
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(yourdata[i]);
col1.setTextSize(10);
col1.setLines(1);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
Xml code
<LinearLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
</LinearLayout>
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
//table layout
tableLayout = (TableLayout) findViewById(R.id.table);
checkPermission();
showData();
}
public void showData() {
TextView phn_no,user_name,account_no;
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
phn_no=new TextView(this);
user_name=new TextView(this);
account_no=new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
params.setMargins(1,1,1,1);
params.gravity= Gravity.CENTER;
phn_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
phn_no.setTextColor(Color.parseColor("#000"));
phn_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
phn_no.setLayoutParams(params);
phn_no.setText(data.getString(1));
user_name.setBackgroundColor(Color.parseColor("#FFFFFF"));
user_name.setTextColor(Color.parseColor("#000"));
user_name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
user_name.setLayoutParams(params);
user_name.setText(data.getString(2));
account_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
account_no.setTextColor(Color.parseColor("#000"));
account_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
account_no.setLayoutParams(params);
account_no.setText(data.getString(3));
TableRow row = new TableRow(this);
row.addView(phn_no);
row.addView(user_name);
row.addView(account_no);
tableLayout.addView(row);
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
}
You can use this
You can use a custom listview or Recyclerview which is more or less like a listview, it's just an advanced listview to be precise. You can use this documentation to understand recyclerviews https://developer.android.com/training/material/lists-cards.html
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.
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" />
I am trying this for 2 days,I am trying to make UIs for the view having two columns in first row,then in second row having three columns,and in third again two columns and so on..And also I have to implement dynamic data(means num of rows/columns are dynamic).
I don't have any idea to implement this type of view.I have also try stragged grid view but this is for dynamic views..but in this I have static 2,3,2,3... columns for 1,2,3,4..rows.Please help me.Any tutorial will be most helpful.
Finally I have solved this problem.I have tried with Raghunandan and user3278416's suggestion.And did all the stuff in runtime except Table layout.Here is the xml file:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
>
And the the two layouts one is for small layout(the second row) and one is for large one(first row):
<?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" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_offerrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
The small one:
<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_homesmallrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
Now the Java Code:
int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;
int j = 0;
while (Show.size()>j) {
try {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow.LayoutParams param = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
LinearLayout layout1 = new LinearLayout(getApplicationContext());
TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);
question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));
View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));
View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));
View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));
View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
if (gett) {
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
// trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
layout1.addView(question);
layout1.addView(question1);
tr.addView(layout1);
tablee.addView(tr);
gett = false;
j = j+2;
}
else
{
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
layout1.addView(question_small);
layout1.addView(question1_small);
layout1.addView(question2_small);
tr.addView(layout1);
tablee.addView(tr);
gett = true;
j = j+3;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here Show is the arraylist of hashmap type and tablee is the tablelayout object which I have made in Xml file.
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.