i'm using a TableLayout on my applycation. This is my XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none">
<include
android:id="#id/action_bar"
layout="#layout/actionbar_toolbar" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/action_bar">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</HorizontalScrollView>
And, i fill the table with Java code:
TableRow tbrow = new TableRow(this);
TextView txt_plazo = new TextView(this);
txt_plazo.setText(" Plazo ");
txt_plazo.setTextColor(Color.WHITE);
txt_plazo.setTextSize(16);
txt_plazo.setMinimumHeight(0);
txt_plazo.setBackgroundColor(Color.DKGRAY);
txt_plazo.setGravity(Gravity.CENTER);
tbrow.addView(txt_plazo);
TextView txt_saldoInicial = new TextView(this);
txt_saldoInicial.setText(" Saldo Inicial");
txt_saldoInicial.setTextColor(Color.WHITE);
txt_saldoInicial.setTextSize(16);
txt_saldoInicial.setBackgroundColor(Color.DKGRAY);
txt_saldoInicial.setGravity(Gravity.CENTER);
tbrow.addView(txt_saldoInicial);
TextView txt_parcialidades = new TextView(this);
txt_parcialidades.setText(" Parcialidades ");
txt_parcialidades.setTextColor(Color.WHITE);
txt_parcialidades.setTextSize(16);
txt_parcialidades.setBackgroundColor(Color.DKGRAY);
txt_parcialidades.setGravity(Gravity.CENTER);
tbrow.addView(txt_parcialidades);
TextView txt_interes = new TextView(this);
txt_interes.setText(" Interés ");
txt_interes.setTextSize(16);
txt_interes.setBackgroundColor(Color.DKGRAY);
txt_interes.setGravity(Gravity.CENTER);
txt_interes.setTextColor(Color.WHITE);
tbrow.addView(txt_interes);
TextView txt_total = new TextView(this);
txt_total.setText(" Abono Capital");
txt_total.setTextSize(16);
txt_total.setBackgroundColor(Color.DKGRAY);
txt_total.setGravity(Gravity.CENTER);
txt_total.setTextColor(Color.WHITE);
tbrow.addView(txt_total);
table.addView(tbrow);
fillTable();
Etc...
All works fine but i'm trying to view the table in landscape mode, the problem is that the table don't fill all screen. In portrait mode looks good.
P.D: android:shrinkColumns and android:stretchColumns don't work for me.
Use match_parent for your views.
Related
So I am trying to inflate a TableLayout with more rows in my java app.
Here is the code I am currently running:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
//The part that fails
TableLayout table = (TableLayout) findViewById(R.id.toastTable);
View row = getLayoutInflater().inflate(R.layout.toast_table_layout_row, (ViewGroup) findViewById(R.id.toast_row));
table.addView(row);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0 ,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
And here are my xml files:
toast_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#c3484848">
<TableLayout
android:id="#+id/toastTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:tag="toast_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="Kultuur"
android:textAllCaps="true"/>
<TextView
android:tag="toast_params"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="1000 tera mass g"
android:textAllCaps="true"/>
</TableRow>
</TableLayout>
</RelativeLayout>
toast_table_layout:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:tag="toast_row"
android:id="#+id/toast_row"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:tag="toast_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="Kultuur"
android:textAllCaps="true"/>
<TextView
android:tag="toast_params"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="1000 tera mass g"
android:textAllCaps="true"/>
</TableRow>
The idea is that I want to add more rows into my existing TableLayout
If I remove the code that is commented with //The part that fails then I get a toast that displays a TableLayout with a little bit of text in it.
But adding more rows fails with with a NullPointException.
I am not sure how it should be done correctly. End result should be that I add these rows in a loop and change their TextField text to something else by getting the TextField by the tag they have.
you have call findViewById() from the inflated view because your toastTable is in the toast_layout_root. just change
TableLayout table = (TableLayout) findViewById(R.id.toastTable);
to
TableLayout table = (TableLayout)layout.findViewById(R.id.toastTable);
I have a MainActivity initially empty. When I click on the "plus" button, you open a DialogFragment where there are two buttons("cancel" and "continue") and a EditText. In EditText you can write the name of the project (project 1 in this case) and by clicking "continue" you can create at run time the "Project 1", a "play" button and a TextView "hello" placed as shown in the image below.
I would like the TextView "hello" was positioned next to the "play" button on the same line.
Here is my code:
Method of the button "continue":
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
String projectName = editText.getText().toString();
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.END;
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp1);
projectButton.setOnClickListener(projectListener);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.START;
lp2.setMarginStart(10);
Button playButton = new Button(this);
playButton.setBackgroundResource(R.drawable.play_button_green);
playButton.setLayoutParams(lp2);
TextView buttonView = new TextView(this);
buttonView.setText("Hello");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
linearLayout.addView(buttonView);
}
MainActivity XML layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/button_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</LinearLayout>
</RelativeLayout>
The whole layout must be created dynamically and then using java code not XML.
Anyone have any ideas?
Put Button and TextView inside LinearLayout with orientation="horizontal"
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(playButton);
ll.addView(buttonView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(ll);
by using horizontal android:orientation
<?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="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:layout_weight="1"/>
</LinearLayout>
Rather than using Button and Text View you can use a single button also.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
Button playButton = new Button(this);
playButton.setText("Hello");
playButton.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
playButton.setLayoutParams(lp2);
now you will get a button with text and icon on its left.
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 can't seem to get these image views working. I wan't to create a few imageviews dynamically but I want them to have a template or so from xml.
My loop looks as so:
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
for (int i=0; i<10; i++){
ImageView imgView = new ImageView(this);
imgView.setAdjustViewBounds(true);
imgView.setId(R.id.coverview1);
layout.addView(imgView);
Picasso.with(context).load("http://blah.com/image.jpg").into(imgView);
}
I'm creating a few images and just placing some temporary image in them but can't seem change the image size at all.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainLinearLayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/coverview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:maxWidth="20dp"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Taking the advice of the answer below, I added what he said but now am not able to change the scale type.
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setAdjustViewBounds(true);
But the image is still the height of the container and not stretching.
You Need to add LayoutParams for your ChildView Like:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
yourCalculatedWidth, LayoutParams.FILL_PARENT);
params.gravity=Gravity.CENTER_HORIZONTAL;
imgView.setLayoutParams(params);
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.