DYNAMIC TABLE LAYOUT WITH DESIGNS AND PADDING IN ANDROID
ViewActvity.java
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;
import android.graphics.Typeface;
public class ViewActivity extends Activity {
TableLayout tl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
tl = (TableLayout) findViewById(R.id.maintable);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//Dynamic TextView and Property
TextView label_date = new TextView(this);
label_date.setId(20);
label_date.setText("ID");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tr_head.addView(label_date);
//Dynamic TextView and Property
TextView label_weight_kg = new TextView(this);
label_weight_kg.setId(21);
label_weight_kg.setText("Firstname");
label_weight_kg.setTextColor(Color.WHITE);
label_weight_kg.setPadding(5, 5, 5, 5);
tr_head.addView(label_weight_kg);
//Dynamic TextView and Property
TextView lname = new TextView(this);
lname.setId(21);
lname.setText("Last name");
lname.setTextColor(Color.WHITE);
lname.setPadding(5, 5, 5, 5);
tr_head.addView(lname);
//
tl.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//Using Sqlite Database to add Data
Integer count=0;
String query="select * from repos";
Cursor c = MainActivity.db.rawQuery(query,null);
while(c.moveToNext()){
String id=c.getString(0);
String fname=c.getString(1);
String sname=c.getString(2);
// Create the table row
TableRow tr = new TableRow(this);
if(count%2!=0)
{tr.setBackgroundColor(Color.GRAY);
}
else
{
tr.setBackgroundColor(Color.LTGRAY);
}
tr.setId(100+count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//
//Create two columns to add as table data
// Create a TextView to add date
TextView ids = new TextView(this);
ids.setId(200+count);
ids.setText(id);
ids.setPadding(2, 0, 5, 0);
ids.setTextColor(Color.WHITE);
tr.addView(ids);
//2nd
TextView fnames = new TextView(this);
fnames.setId(200+count);
fnames.setText(fname);
fnames.setTextColor(Color.WHITE);
tr.addView(fnames);
//3rd
TextView lnames = new TextView(this);
lnames.setId(200+count);
lnames.setText(sname);
lnames.setTextColor(Color.WHITE);
tr.addView(lnames);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view, menu);
return true;
}
private void showdisplaylong(String string) {
// TODO Auto-generated method stub
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
-----------------------------------------------------------------------------------
Activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TableLayout
android:stretchColumns="0,1"
android:id="@+id/maintable"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TableLayout>
</ScrollView>
</LinearLayout>
No comments:
Post a Comment