I need my screen to have a couple of text views defined in the activity layout. Below those views I want to set a table whose number of rows and columns will depend upon the user's input (dynamic).
This code is in the oncreate of the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
But this code is taking the whole screen and I am unable to have those text views which I have defined in the activity layout.
Here's my activity_home_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
If you only want your TableLayout to below your TextViews then use LinearLayout with orientation "vertical" in activity_home_page instead of RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
And add mainLinear.addView(tableLayout); at the end of your loop
LinearLayout mainLinear = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 4; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
mainLinear.addView(tableLayout);
And remove setContentView(tableLayout);
Hope this works
Add this to your activity file:
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/display_user_name"
android:layout_marginTop="15dp" >
</TableLayout>
And in your class :
TableLayout tableLayout = (TableLayout)findViewById(R.id.table1);
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
Related
I have a table defined in .xml file but rows and headers have to be added dynamically. I have a problem with fill width with table row on horizontal and vertical orientation. I have tried stretchColumns parameter but it did not help. What is the best way to do it?
table = (TableLayout) findViewById(R.id.testList);
TableRow headerRow = new TableRow(this);
JSONObject obj;
obj = new JSONObject(loadJSONFromAsset());
JSONArray records = obj.getJSONArray("tests");
ArrayList<String> headers = new ArrayList<>();
headers.add("Header 1");
headers.add("Header 2");
headers.add("Header 3");
headers.add("Header 4");
headers.add("Header 5");
for(int i = 0; i < headers.size(); i++) {
TextView header = new TextView(this);
header.setText(headers.get(i));
header.setTextColor(Color.WHITE);
header.setBackgroundColor(Color.rgb(24, 116, 205));
header.setPadding(15, 15, 15, 15);
header.setTextSize(20);
header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
header.setMaxLines(1);
headerRow.addView(header);
}
table.addView(headerRow);
and xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/testList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</GridLayout>
Try this way. The following will make sure TableRow is aligned to full available width.
After base components, put your TableLayout in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Now in your logic, you can add according to the number of items from response. This should be in a for loop.
for(int i = 0; i < leftComponent.size(); i++)
{
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.fenceTableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText(leftComponent[i]);
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.CENTER);
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText(rightComponent[i]);
detailHead.setGravity(Gravity.CENTER);
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
}
You may need to customize according to your requirement.
I have created a table like this:
TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);
tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
The result of this code is:
And I want to achieve this:
How is this possible?
With this
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
<TableRow
android:layout_weight="1"
android:gravity="center">
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_1"
android:background="#null" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_2"
android:background="#null"/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_3"
android:background="#null" />
</TableRow>
</TableLayout>
The three button appears aligned. I think you need gravity="center"and/or android:stretchColumns="*" in your code.
UPDATE
Try with this:
TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);
table.setStretchAllColumns(true);
tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
UPDATE
I did a mock of your problem and use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout table = new TableLayout(getApplicationContext());
table.setStretchAllColumns(true);
for (int i = 0; i<20; i++) {
TableRow[] tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);
TextView pos = new TextView(getApplicationContext());
pos.setGravity(Gravity.LEFT);
pos.setText(String.valueOf(i) + ". " + getName(i));
TextView a = new TextView(getApplicationContext());
a.setGravity(Gravity.LEFT);
a.setText("2/9");
TextView points = new TextView(getApplicationContext());
points.setGravity(Gravity.LEFT);
points.setText("2/9");
tableRow[i].addView(pos);
tableRow[i].addView(a);
tableRow[i].addView(points);
table.addView(tableRow[i]);
}
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.addView(table);
}
private String getName(int i) {
if (i == 2) {
return "Recooooooooooooooord";
} else if (i == 3) {
return "Recooooooord";
}
return "Fran";
}
The result is like the one you want, you can see the result here
I am using a table layout to show information based on a user specifications. The user enters data into a database, then this data is taken and displayed on a table were each row has 3 textView entries. The problem is that if the data entered by the user is to long, it goes off screen and is not visible. Any ideas? The code I used to do this is below:
xml
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FF0000"/>
....
</TableRow>
</TableLayout>
Java
for (final String time : timesArray)
{
i++;
TableRow row1= new TableRow(this);
event = db.getEvent(i, gameId);
player = db.getPlayer(i, gameId);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
timeRow.setText(time);
timeRow.setTextColor(Color.WHITE);
playerRow.setText(player);
playerRow.setTextColor(Color.WHITE);
eventRow.setText(event);
eventRow.setTextColor(Color.WHITE);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1);
}
And it looks like this if the data is too long
SOLUTION!
Setting layout_width to 0 and then assigning layout_weight to a float based on the amount of the screen you want a column to take up (For example, 0.5 would let the column take up half the screen) allows all the information to be shown on the screen. The code to do this is below.
XML
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="0px"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="Time"/>
<TextView
android:id="#+id/playerCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Player"/>
<TextView
android:id="#+id/eventCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Event"/>
</TableRow>
java
TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableRow row1= new TableRow(this);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
TableRow.LayoutParams text1Params = new TableRow.LayoutParams();
text1Params.width = 0;
text1Params.weight = (float) 0.2;
TableRow.LayoutParams text2Params = new TableRow.LayoutParams();
text2Params.weight = (float) 0.4;
text2Params.width = 0;
TableRow.LayoutParams text3Params = new TableRow.LayoutParams();
text3Params.weight = (float) 0.4;
text3Params.width = 0;
timeRow.setLayoutParams(text1Params);
playerRow.setLayoutParams(text2Params);
eventRow.setLayoutParams(text3Params);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1, params);
I'm a newbie.
I want to add 3 EditText's when a user click on a button.
I try to realize it, but it isn't work.
Widgets do not appear.
P.S. Widgets must be in others layout's.
Thank you for help.
Sorry for my bad English.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_fill"
android:id="#+id/simple_main_layout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/for_main_text">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Buy List"
android:id="#+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/do_new_list"
android:layout_marginLeft="153dp"
android:layout_marginRight="2dp"
android:background="#drawable/btn_yes" android:textColor="#804f29" android:textStyle="bold"/>/>
</LinearLayout>
<EditText
android:layout_width="305dp"
android:layout_height="wrap_content"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="91dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code:
private Button do_new_field;
private EditText[] text, many, cost;
private final int fields = 50;
private int last_field = 0;
private LinearLayout layoutik;
LinearLayout.LayoutParams layoutParams;
LinearLayout.LayoutParams layoutParams2;
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_buy);
do_new_field = (Button) findViewById(R.id.do_new_list);
layoutik = (LinearLayout) findViewById(R.id.for_main_text);
do_new_field.setOnClickListener(this);
text = new EditText[fields];
many = new EditText[fields];
cost = new EditText[fields];
layoutParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,0f);
layoutParams2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,0f);
}
#Override
public void onClick(View view) {
if(view == do_new_field)
{
if(last_field <= fields)
{
text[last_field] = new EditText(this);
many[last_field] = new EditText(this);
cost[last_field] = new EditText(this);
text[last_field].setWidth(305);
text[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
text[last_field].setMaxLines(3);
many[last_field].setWidth(91);
many[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
many[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
many[last_field].setMaxLines(1);
cost[last_field].setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
cost[last_field].setMaxLines(1);
text[last_field].setId(100 + last_field);
many[last_field].setId(200 + last_field);
cost[last_field].setId(300 + last_field);
text[last_field].setLayoutParams(layoutParams);
many[last_field].setLayoutParams(layoutParams);
cost[last_field].setLayoutParams(layoutParams);
layoutik.addView(text[last_field]);
layoutik.addView(many[last_field]);
layoutik.addView(cost[last_field]);
last_field++;
}
}
}
}
You might have to add "setId" to the editText and check.
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0f);
editText.setId(EDT_ID + sub_count + i);
editText.setTag(insertAdAl.get(i).getmApiAppend());
editText.setLayoutParams(params);
editText.setTextAppearance(NewAdverts.this,
android.R.style.TextAppearance_Small);
editText.setGravity(Gravity.LEFT | Gravity.CENTER);
editText.setPadding(4, 4, 4, 4);
editText.setTextAppearance(NewAdverts.this,
android.R.attr.textAppearanceSmall);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(R.drawable.notification_field);
What i'm trying to do is add create an application that locates some local business. So the activity I'm doing displays the business information. Some business have severals stores located on the city, and some others only have one. So here is my problem: In the View I have Scroll View with all the elements, and a linear layout inside the scrollview. If the business has more then one store, i will add each store information in a new text view and add the text view to the layout (this is done all by code). But at the moment to display the layout, it only displays me one store, instead of showing me 3 or 4. What I'm I doing wrong? Here is the method setupViews(), which is the one in chanrge of the displaying:
private void setupViews() throws SQLException {
ImageView iv = (ImageView) findViewById(R.id.negocio_logo);
try {
iv.setImageBitmap(BitmapFactory.decodeByteArray(toShow.getImgSrc(),
0, toShow.getImgSrc().length));
} catch (NullPointerException npe) {
iv.setBackgroundColor(Color.WHITE);
}
TextView nombreEmpresa = (TextView) findViewById(R.id.nombre_empresa);
nombreEmpresa.setText(toShow.getNombre());
TextView descripcionEmpresa = (TextView) findViewById(R.id.descripcion_empresa);
descripcionEmpresa.setText(toShow.getDescripcion());
TextView direccionEmpresa = (TextView) findViewById(R.id.direccion_empresa);
direccionEmpresa.setText(toShow.getDireccion());
LinearLayout rl = (LinearLayout) findViewById(R.id.linear_layout_si);
TextView suc = (TextView) findViewById(R.id.sucursales_empresa);
sucursalDAO sDAO = new sucursalDAO();
boolean tieneSucursales = sDAO.hasSucursales(toShow.getId());
if (tieneSucursales == false) {
suc.setVisibility(View.GONE);
// sucs.setVisibility(View.GONE);
} else {
suc.setVisibility(View.VISIBLE);
ArrayList<String> sucursales = sDAO.getStringSucursales(toShow
.getId());
ArrayList<TextView> tvs = new ArrayList<TextView>();
for (int i = 0; i < sucursales.size(); i++) {
TextView tv = new TextView(this);
tv.setText(sucursales.get(i));
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tvs.add(tv);
}
for (int i = 0; i < tvs.size(); i++) {
rl.addView(tvs.get(i), i);
}
}
}
And here is the XML of my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widgetscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:orientation="vertical" >
<ImageView
android:id="#+id/negocio_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="#string/logo_negocio" />
<TextView
android:id="#+id/datos_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/negocio_logo"
android:background="#drawable/barra"
android:text="#string/datos_empresa"
android:textColor="#color/White" />
<TextView
android:id="#+id/nombre_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/datos_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/descripcion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nombre_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/direccion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/descripcion_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<LinearLayout
android:id="#+id/linear_layout_si"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa" >
</LinearLayout>
<TextView
android:id="#+id/contacto_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linear_layout_si"
android:text="#string/testing" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I think you forgot to set orientation on LinearLayout and it's showing horitzontal
I bet you forgot to set the orientation of the LinearLayout.