I want to add a new row below previous ones. I mean I have a UI containing 2 text boxes and 1 submit button. When I fill the text box and press submit, the content of text box is added below the previous one. Is this done with listview or tableview?
Please give suggestions to me and how to implement that technique. And after item is added in listview and when I click on any of list position, it performs different-2 activity. And I am new in Android.
TextView exampleTextView = (TextView) findViewById(R.id.field1);
exampleTextView.setText("wedwqe");
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow tr = (TableRow) findViewById(R.id.row);
tr.addView(exampleTextView);
// table.removeView(tr);
table.addView(tr);
And the XML file is:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="#DDDDDD"
android:stretchColumns="1" >
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row">
<TextView android:id="#+id/field1"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textStyle="bold"
android:textSize="18dip"
/>
<TextView android:id="#+id/field2"
android:padding="3dip"
android:text=" "
android:textSize="18dip"
android:gravity="right"
/>
</TableRow>
</TableLayout>
You can use addView(View child, int index) that you can find in here
I hope this helps
Related
I really need help
I have a ResultSet as a result of select query from MySQL DB. how can I display it in table layout?? I have setup a layout for tableview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/some_hint" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<TableLayout
android:id="#+id/producttablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:scrollbars="horizontal|vertical"
android:showDividers="none|beginning|middle|end" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:paddingTop="#dimen/activity_horizontal_margin" >
<TextView
android:id="#+id/ttt1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ttt2"
android:text="Column 12"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to be able to add ResultSet data into it. Something like:
super.onCreate(savedInstanceState);
String s = null;
setContentView(R.layout.activity_list_all_products);
// Show the Up button in the action bar.
setupActionBar();
context333=this;
TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
try
{
connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
preparedStatement2 = connect2.prepareStatement("select article_code,article_desc from products limit 4");
resultSet2=preparedStatement2.executeQuery();
resultSet2.beforeFirst();
while (resultSet2.next()) {
how to add next record here to the table layout??
}
Please help
You can create a new Views (it seems you are populating your table rows with TextViews) and populate them with the results from your query as you wish. Then you can create a new TableRow with a constructor (see the documentation).
Add the views to to table row instnace using:
tableRowInstance.addView(viewInstance);
and then add the tableRow to the tableLayout the same way:
myTable.addView(tableRowInstance)
TableLayouts and TableRows are both subclasses of View. You should do some research on ViewGroup, View, and how they are structured. This link provides nice visualizations.
I want to create a multirow Button list. Something like this :
but I want to do it dynamically(in code). Is there a way to tell layout to do this automatically? Or i have to do this myself usingRelativeLayout.LayoutParams. I can do this by code but I should control so many things and I was wondering if there is another easier way to do this. For example tell layout to add elements in the next row when the current one is full!
You could also do this with LinearLayout and make all the buttons the same size using weight.
As for your question:
i can do this by code but i should control so many things and i was
wondering if there is another easier way to do this. for example tell
layout to add elements in the next row when the current one is full!
This is potentially possible if you measure the screen width and height and use the Functions in in the View class to figure out the specifics of that particular view and its children.
Alternative
But as mentioned in the comments, there are other views that you can use to solve your problem like GridView.
You can also use a table layout ,
Create first row of tabllayout in xml like 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="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="#+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I created this xml for four three texts and on button in a single row
refer to the table in onCreate
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
add rows like
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// Here value is the number of rows you want in table
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
for this i took help from here
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
and
its xml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
after some more google search i finally found the best way to do this. it's so clean and simple, using Adapters and grids.
thanks for all the answers
here is a Tutorial: Creating a Custom Adapter for Gridview(ButtonAdapter)
I have a LinearLayout with a TextView and TableLayout embedded in it. here is the format of my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txtHeader"
android:layout_width="match_parent"
android:text="Header" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
</LinearLayout>
I am adding rows dynamically to the table. Everytime, I get an update, I need to clear the table and write new information (number of rows are variables). So, I am trying to do clear the table as a first step whenever I have to update.
ll = (LinearLayout)findViewById(R.id.wlayout);
tb = (TableLayout)findViewById(R.id.wtable);
private void on Update(){
tb.removeAllViewsInLayout();
Create table dynamically..
}
But it is removing my text header also. I have also tried ll.removeView(tb) where ll is the LinearLayout. Can any one please help me. I am new to Android. Thank you.
You have not given id to TableLayout tag.Please give id and then try to clear it.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+#id/wtable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
And then try this..
tb = (TableLayout)findViewById(R.id.wtable);
tb.removeAllViewsInLayout();
In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.
How do I do I create a table in android?
I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.
Is there any other solution to this?
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.
}
These will create 10 rows and 1 textview in each row in taken table layout.
take one table layout in your xml file like below :
<TableLayout
android:id="#+id/tblayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
Use ListView with Custom adapter , create five textview's in listview item as columns..
create listitem like this
row.xml
<?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">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Its not necessary to user TableLayout.
you can user Custom ListView And set header of ListView as your Column name
To create custom listview see this example
I have a layout where I put i rows dynamically in a tablelayout that is inside a scrollview. Everything is working smoothly except that I don't can get the checkbox text to wordwrap (See xml below). The text is also set at runtime. I have searched the internet and has made a lot of trying to set different properties with no luck so far.
Is this task possible to achieve?
<?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:layout_marginBottom="15dip">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
CheckBox extends TextView, so I assume you can wrap its text like they solve the issue in this post :
Android TextView Text not getting wrapped
If you cannot get it to work like that, you can maybe also try, using match_parent in the checkbox.
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
hope it helps.
use table row inside the Linear layout instead of table layout..
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
</LinearLayout>
I have made dynamic layouts with checkboxes in the past and used this approach:
// Create new view to add layout to
sv = new ScrollView(this);
// Create new layout to add check boxes to
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add layout to view
sv.addView(ll);
// Create 6 check boxes
int itemSize = 6;
for(int j=0; j<itemSize; j++){
CheckBox cb = new CheckBox(this);
cb.setText("number " + j);
ll.addView(cb);
}
// Finalize view
this.setContentView(sv);
Is this what you are looking for?