Add a dynamic tablelayout in a relativelayout above a existing button - android

i'm trying to add a tablelayout in a relativelayout, but i'm havind some problems to put this tablelayout above a existing button, i can only but it below it.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:stretchColumns="0,1"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Button" />
the lp.addRule(RelativeLayout.ABOVE, idx); isn't working
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the size of the screen to set the size of the buttons according
// to the level
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int level = 3;
int width = size.x - (size.x / level);
int btsize = width / level;
// Get the main Layout
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.main);
//
TableLayout tLayout = new TableLayout(this);
int id = 0;
for (int i = 0; i < level; i++) {
// Create the TableRow
TableRow tRow = new TableRow(this);
for (int j = 0; j < level; j++) {
id++;
Button bt = new Button(this);
bt.setId(id);
bt.setWidth(btsize);
bt.setHeight(btsize);
bt.setMinimumWidth(0);
bt.setMinimumHeight(0);
tRow.addView(bt); // add button into the row
}
tLayout.addView(tRow);// add row into the table
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int idx = findViewById(R.id.button1).getId();
lp.addRule(RelativeLayout.ABOVE, idx);
rLayout.addView(tLayout,lp);// add table into the relativelayout
}

you can try this...
put this code in your oncreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relay = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams layoutprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout table=new TableLayout(this);
relay.addView(table);

Related

Android: layout created programmatically doesnt appear?

I think this question has been asked alot but I havent been able to find an answer through all similar posts. There's no error, but when the app runs nothing appears. So here is my code in my activity.
public class MainGameActivity extends AppCompatActivity {
int width = 2;
int height = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
RelativeLayout mainLayout = (RelativeLayout) View.inflate(this, R.layout.activity_main_game, null);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
Table gameTable = new Table(width, height);
ArrayList<TableRow> tableRowArray = new ArrayList<TableRow>();
for (int i = 0; i < height; i++) {
tableRowArray.add(new TableRow(this));
tableRowArray.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < width; j++) {
Button button = new Button(this);
button.setText(i + "." + j);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRowArray.get(i).addView(button);
}
tableLayout.addView(tableRowArray.get(i));
}
mainLayout.addView(tableLayout);
setContentView(mainLayout);
}
}
and here is my XML, there's basically nothing:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.khangbuin.flyingdiscscanner.MainGameActivity">
</RelativeLayout>
Thank you in advance for answering.

how can i get Values from dynamically created andorid TableRow?

How can i get the data from the Table Row on the button Click Event from the selected Radio Button.
How i can set the Radio Group in a Radio button also when i write
rg.addView(rb); My Android Application is Closed Unfortunately.
My XML File code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dynamictablelayout.Round_Trip_Search_Result" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:id="#+id/tl"
android:stretchColumns="*" >
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tl"
android:id="#+id/ll"
android:orientation="vertical" >
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll"
android:id="#+id/tl1"
android:stretchColumns="*" >
</TableLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/ll1"
android:orientation="vertical" >
</LinearLayout>
my activity file
TextView tv;
RadioGroup rg, rg1;
RadioButton rb;
TableRow tr;
int cnt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_round__trip__search__result);
String[] str = { "Airindia", "Ai-101", "3.00", "2000", "Go Air",
"GA-101", "4.00", "2500" };
String[] str1 = { "Air India", "Ai-100", "5.00", "2500",
"Etihad Airways", "EI-200", "12.00", "3500" };
TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableLayout tll = (TableLayout) findViewById(R.id.tl1);
LinearLayout l1 = (LinearLayout) findViewById(R.id.ll);
LinearLayout l11 = (LinearLayout) findViewById(R.id.ll1);
int k = 0;
int tr_id = 100;
for (int i = 0; i < str.length; i += 4) {
tr = new TableRow(this);
tr.setId(tr_id);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
tv = new TextView(this);
tv.setText(str[k]);
tr.setId(tr_id);
tr.addView(tv);
k++;
}
rg = new RadioGroup(this);
rb = new RadioButton(this);
//rg.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr_id++;
}
TextView rtn = new TextView(this);
rtn.setText("Flight from XXX to YYY");
l1.addView(rtn);
int h = 0;
for (int i = 0; i < str1.length; i += 4) {
tr = new TableRow(this);
tr.setId(tr_id);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
tv = new TextView(this);
tv.setText(str1[h]);
tr.setId(tr_id);
tr.addView(tv);
h++;
}
rg1 = new RadioGroup(this);
rb = new RadioButton(this);
// rg1.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
tll.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr_id++;
}
Button b = new Button(this);
b.setText("book");
l11.addView(b);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Click",
Toast.LENGTH_SHORT).show();
int id = rb.getId();
Toast.makeText(getApplicationContext(), "ID =>" + id,
Toast.LENGTH_SHORT).show();
}
});
LogCat Error Occurs when i remove the line rg.addView(rb);:
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3249)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3194)
03-20 10:25:32.085: E/AndroidRuntime(6235): at android.view.ViewGroup.addView(ViewGroup.java:3170)
03-20 10:25:32.085: E/AndroidRuntime(6235): at com.example.dynamictablelayout.Round_Trip_Search_Result.onCreate(Round_Trip_Search_Result.java:61)
rg1.addView(rb);
rb.setId(tr_id);
tr.addView(rb);
In first and third lines above you are adding parents to the same element "rb" without removing its parent. A child(rb) cannot have two parents. You must call removeView() on the child's parent first.

Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview

I am trying to scroll item to the middle position of the screen after selecting it.I have used Horizontalscrollview and LinearLayout inside it to add item. I show in the XML
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updown"
android:layout_weight="0.8"
android:fadingEdgeLength="0dp" >
<LinearLayout
android:id="#+id/horizontalbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tlbackground"
android:baselineAligned="true"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Class
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout l1 = (LinearLayout)findViewById(R.id.horizontalbar);
for(int i = 0; i < 20; i++)
{
Button b = new Button(this);
b.setText("t"+i);
ll.addView(b);
}
}
}
I want something like in figure
before click
after click
How can I Scroll that selected item to the middle in HorizontalScrollview?
After long try , I have got it with this
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int scrollX = (b.getLeft() - (screenWidth / 2)) + (b.getWidth() / 2);
hsl.smoothScrollTo(scrollX, 0);
}
});
ll.addView(b);
}
This is how your onCreate should look like:
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.horizontalbar);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int center = (width - b.getWidth())/2;
hsl.scrollTo(b.getLeft() - center, b.getTop());
}
});
ll.addView(b);
}
The only thing i didn't discover is why i need to substract additional 200px (in my case). Probably some padding or something else. Instead of 200, you could discover how many dips is it and call function:
public static int convertDipToPixels(Context c, float dips) {
return (int) (dips * c.getResources().getDisplayMetrics().density + 0.5f);
}

Add multiple views in ScrollView?

I am working on an Android App. I have some problem..
Here is my app :
The pink rectangle is a ScrollView. I want to add TextView to the same ScrollView. I know I need a LinearLayout and add my Views inside this layout. After I need to add the LinearLayout to my ScrollView. Here is my ScrollView class with my methods setClassicLabel to implement TextViewand setClassicButton to implement Button in my ScrollView.
Here is my ScrollView.java :
public class MyScrollView extends ScrollView {
private JSONParser jParser;
private JSONObject contenuScrollView;
private Context context;
#SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
super(context);
this.context = context;
}
public void onCreate(Bundle savedInstanceState) throws JSONException {
}
/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, LinearLayout ll) throws JSONException {
if (listLabel.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listLabel.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
TextView tv = new TextView(context);
tv.setText(listLabel.getJSONObject(i).getString("text"));
tv.setTextSize(listLabel.getJSONObject(i).getInt("police_size"));
tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
ll.addView(tv, params);
}
this.addView(ll, paramsll);
}
}
#SuppressWarnings("deprecation")
public void setClassicButton(JSONArray listButton, LinearLayout ll) throws JSONException {
if (listButton.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listButton.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("position_y"));
int dest = listButton.getJSONObject(i).getInt("dest");
MyButton myButton = new MyButton(context, dest);
RoundRectShape rect = new RoundRectShape(
new float[] {50,50, 50,50, 50,50, 50,50},
null,
null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(Color.parseColor(listButton.getJSONObject(i).getString("color")));
myButton.setBackgroundDrawable(bg);
myButton.setTextColor(Color.parseColor(listButton.getJSONObject(i).getString("colortext")));
//BitmapDrawable bdra = new BitmapDrawable(downloadBitmap(listButton.getJSONObject(i).getString("http")));
myButton.setText(listButton.getJSONObject(i).getString("text"));
myButton.getBackground().setAlpha(30);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
MyButton button = (MyButton)view;
Intent intent = new Intent(context, ClassicView.class);
Content content = new Content(button.getDestination());
intent.putExtra("CONTENT", content);
context.startActivity(intent);
}
});
ll.addView(myButton, params);
}
this.addView(ll, paramsll);
}
}
}
MyScrollView heritates from ScrollView. The problem is the next :
When I create my ScrollView with the method setClassicScrollView, i call my methods setClassicLabel and setClassicButton to implements theses two views in my ScrollView. But i can only set Button or Label. I can't set one label and one button. I must have a problem who comes from a layout.. I am on it for two days
Here is my method setClassicScrollView :
public void setClassicScrollView(JSONArray listScrollView, RelativeLayout rl) throws JSONException {
if (listScrollView.length() > 0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
for (int i = 0; i < listScrollView.length(); i++) {
MyScrollView myScrollView = new MyScrollView(this, listScrollView);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("position_y"));
myScrollView.setBackgroundColor(Color.RED);
myScrollView.setLayoutParams(params);
myScrollView.setHorizontalScrollBarEnabled(true);
myScrollView.getMaxScrollAmount();
myScrollView.getBackground().setAlpha(50);
//myScrollView.setClassicLabel(listScrollView.getJSONObject(i).getJSONArray("Label"), ll);
myScrollView.setClassicButton(listScrollView.getJSONObject(i).getJSONArray("Button"), ll);
rl.addView(myScrollView);
}
}
}
Someone has an idea of how I can set TextViews and Buttons in the same ScrollView ?
Here's the layout for your scroll view.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
Here.. In the scroll view, I have one linear layout as one child and after that I have other views inside the linear layout.

How to create a full screen TableLayout programmatically (table height collapses unintendedly)

The following TableLayout matches exactly my requirements (it fills its parent and columns are stretched evenly):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7434a6" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#836492" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#103456" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#958453" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#756aa9" />
</TableRow>
</TableLayout>
However, as I want to have the number of rows being configurable I started to create my layout programmatically:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT, 1f);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}
Up to a certain point this works not bad as well. Only issue I have is, that my table collapses vertically:
What is wrong here? Thought my code is doing the same as my XML. I searched and tried quite a lot but didn't get the right idea.
Try this..
LinearLayout.LayoutParams tableParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams rowParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
LinearLayout.LayoutParams itemParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1f);
LinearLayout tableLayout = new LinearLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setOrientation(LinearLayout.VERTICAL);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
LinearLayout tableRow = new LinearLayout(context);
tableRow.setOrientation(LinearLayout.HORIZONTAL);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
OR In your rowParams change TableRow to TableLayout and try.
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT, 1f);
TableLayout.LayoutParams rowParams =
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; 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);
}

Categories

Resources