Saturday, August 17, 2013

Re: [android-developers] Need help setting table column width

package com.example.tabletest;

import java.util.StringTokenizer;

import com.example.tabletest.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
setTable();
return true;
}

private void setTable()
{
TableLayout tl1 = (TableLayout)findViewById(R.id.tablelayout1);
tl1.removeAllViews();
tl1.setPadding(10, 20, 10, 0);
tl1.setColumnShrinkable(0, true);



//tl1.setStretchAllColumns(true);
//tl1.setShrinkAllColumns(true);

TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);

TextView title = new TextView(this);
title.setText("This is a test table");
title.setBackgroundColor(0x55443300);

//title.setHeight(20);
title.setGravity(Gravity.CENTER_HORIZONTAL);

rowTitle.addView(title);
tl1.addView(rowTitle);



int result_str_size = 5;
for ( int i=0 ; i< result_str_size; i++)
{
TableRow tr = new TableRow(this);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT);
String result_str="Hello | World | this is | fun";
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setMinimumHeight(20);

tr.setPadding(10, 2, 10, 2);
TextView id = new TextView(this);
//id.setWidth(1);
//tr.addView(id);

//id = new TextView(this);


id.setBackgroundColor(0x55779900);
id.setLayoutParams(trparams);
id.setGravity(Gravity.CENTER_HORIZONTAL);


id.setText(""+(i+1)+"");
tr.addView(id);
StringTokenizer st = new StringTokenizer(result_str,"|");

int back_color=0xFFFF9900;

while (st.hasMoreElements())
{
back_color+=0x40;
TextView name = new TextView(this);
name.setLayoutParams(trparams);

name.setBackgroundColor(back_color);

String token = st.nextToken();
name.setText(token);

tr.addView(name);
}

tl1.addView(tr);
}

TableRow rowbutton = new TableRow(this);
rowbutton.setGravity(Gravity.CENTER_HORIZONTAL);

Button button = new Button(this);

button.setText("OK");
button.setClickable(true);

button.setBackgroundColor(0x55443300);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_SHORT).show();
}
});


//title.setHeight(20);
button.setGravity(Gravity.CENTER_HORIZONTAL);

rowbutton.addView(button);
tl1.addView(rowbutton);

}

}
Hello TreKing

   Thanks for the reply.
   Please find attached java file.

   I have attached my project in the first mail. Hence I didn't include the source files in subsequent mails.

   Here I have attached two files. MainActivity.java and TableTest.tar. MainActivity.java is there in the project file also.

thanks
SAlil



On Sunday, August 18, 2013 2:56:07 AM UTC+5:30, TreKing wrote:

On Fri, Aug 16, 2013 at 8:05 PM, Salil <sal...@gmail.com> wrote:
  I tried the solutions that you specified. When set the shrinkable to first column the first column disappeared. ( as in the attachment ).

Please show the full chunk of code you're using to create your table.
 
This is my first project in Android and hence my knowledge about the internals of UI elements are not that good. Hence I am not sure where to hit for solving the problem. That is why I am dependent on the community. Sorry if the question asked is a silly one.

That's fine, your question is not silly, I'm just trying to get you to share the relevant pieces of information that will help me or someone else point you in the right direction.

You need to clearly show what you have done and what results you have obtained so we can determine the source of your error.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate