Tablerow and TableLayout not aligning straight - java

data_shifted_down
Im creating an android project(java) that pulls data from server and places that data into a table so I have to create a table programmatically. I followed a few examples here but for every column in row its being shifted down...
I attached a pic.
activity_main.xml:
<?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"
>
<TableLayout
android:id="#+id/details_table"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<TableRow>
<TextView
android:text="User"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textSize="50px"
android:textColor="#color/black" />
<TextView
android:text="Store"
android:layout_width="match_parent"
android:layout_column="1"
android:layout_weight="1"
android:textSize="50px"
android:textColor="#color/black" />
<TextView
android:text="Item"
android:layout_width="match_parent"
android:layout_column="2"
android:layout_weight="1"
android:textSize="50px"
android:textColor="#color/black" />
<TextView
android:text="QTY"
android:layout_width="match_parent"
android:layout_column="3"
android:layout_weight="1"
android:textSize="50px"
android:textColor="#color/black"
/>
</TableRow>
</TableLayout >
</LinearLayout>
table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow
android:id="#+id/tr"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="#+id/tableCell1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="20dp" />
<TextView
android:id="#+id/tableCell2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_toRightOf="#+id/tableCell1"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="20dp" />
<TextView
android:id="#+id/tableCell3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_toRightOf="#+id/tableCell2"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="20dp" />
<TextView
android:id="#+id/tableCell4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="3"
android:layout_toRightOf="#+id/tableCell3"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="20dp" />
</TableRow>
</RelativeLayout>
MainActivity.java where it iterates through the data to add to TableRow then add that TableRow into TableLayout:
for (int c = 0; c < a.length; c++) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
View v=getLayoutInflater().inflate(R.layout.tablerow, null);
TableRow tableRow = (TableRow) v.findViewById(R.id.tr);
detailsTable.setStretchAllColumns(true);
TextView tv = null;
switch (c) {
case 0:
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
break;
case 1:
tv = (TextView) tableRow.findViewById(R.id.tableCell2);
break;
case 2:
tv = (TextView) tableRow.findViewById(R.id.tableCell3);
break;
case 3:
tv = (TextView) tableRow.findViewById(R.id.tableCell4);
break;
}
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText(a[c]);
tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT));
//new Log("here: ", Integer.toString(tv.getId()));
//Add row to the table
if(tableRow.getParent() !=null){
((ViewGroup)tableRow.getParent()).removeView(tableRow);
}
detailsTable.addView(tableRow);
Tried every solution on the site. Changed to RelativeLayout no luck. Tried gravity and every other attribe...

Looks a horrible way to try and do it but your problem is you are inflating a new tablerow for each item of data with:-
View v=getLayoutInflater().inflate(R.layout.tablerow, null);
who's parent is null
therefore:-
if(tableRow.getParent() !=null){ ((ViewGroup)tableRow.getParent()).removeView(tableRow); }
won't remove a view (not that you should be removing a view)
not tested but something like this might work but for only where a has 4 data items (because you switch statement won't work for anything longer - could also use modulus calculation for that)
View v;
for (int c = 0; c < a.length; c++) {
// Every 4th data item (remainder is zero) e.g. 0,4,8,etc
if ((c%4) == 0) {
// inflate the view
v=getLayoutInflater().inflate(R.layout.tablerow, null);
}
// Rest of your code
....
//Add row to the table
if ((c%4) == 0) {
detailsTable.addView(tableRow);
}
}

Related

How to give equal space programmatically(Java) in the following code?

I'm just an beginner.
I'm unable to provide equal space between Item Name and quantity. I want Item Name to occupy space available.
I had tired using setGravity and setColumnStretchable but I'm unable to provide equal space.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/Table_Text_View"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:id="#+id/Item_Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Item Name" />
<TextView
android:id="#+id/quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Quantity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Purchase" />
</TableRow>
</TableLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/Edit_Text_View1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name of Item"
android:imeOptions="actionNext"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/Edit_Text_View2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Edit_Text_View1"
android:hint="Quantity"
android:imeOptions="actionDone"
android:inputType="number" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_toRightOf="#id/Edit_Text_View2"
android:onClick="doneClick"
android:text="Done" />
</RelativeLayout>
public void doneClick(View view) {
EditText editTextView1 = (EditText) findViewById(R.id.Edit_Text_View1);
String itemTextView1 = editTextView1.getText().toString();
EditText editTextView2 = (EditText) findViewById(R.id.Edit_Text_View2);
String itemTextView2 = editTextView2.getText().toString();
TableLayout table = (TableLayout) findViewById(R.id.Table_Text_View);
TableRow row = new TableRow(this);
TableRow.LayoutParams lay = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lay);
TextView itemName = new TextView(this);
TextView quantity = new TextView(this);
itemName.setText(itemTextView1);
quantity.setText(itemTextView2);
row.addView(itemName);
row.addView(quantity);
table.addView(row);
}
I expect Item Name to occupy available space, show that it matches with upper row and row that is being created.

to display two listViews in the same Android Studio page

in fact I have the following problem:
I have two ListView loaded automatically with two adapters, the problem is in the XML, both list only displays the first element.
here is my xml code:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hrizi.onescore.home_links.searshscore">
<include
android:id="#+id/toolbar"
layout="#layout/apptoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:fillViewport="true">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/sv_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/edittextbackground"
android:hint="Search ..."
android:inputType="text"
android:textAlignment="center"></EditText>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="#color/coloronscore"
android:onClick="btnsearchOnClick"
android:text="Search"
android:textAllCaps="false"
android:textColor="#color/colorwhite" />
</LinearLayout>
<!--Result of research-->
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingBottom="5dp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingBottom="5dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
the problem I think in the ScrollView or something else that flushes to that.
thank you in advance .
You can use this function to display all the items in the listview.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Example:
setListViewHeightBasedOnChildren(yourlistview);

linear layout java and xml

I'm just trying to add spinners dynamicallyand I found my way here Add Spinner dynamically in a loop
that works good but my issue is the view takes place instead of my original layout (a header, a footer and an image view).
I would like to place it in a specific layout in the xml file, between the header and the footer for example.
Is there a way to do this?
my code
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
conteneur = "container No : " + i.getStringExtra("container");
conteneur_s = i.getStringExtra("container");
fichano = i.getStringExtra("fichano");
pos = i.getIntExtra("pos", 0);
latitude = i.getDoubleExtra("geolat", 0);
longitude = i.getDoubleExtra("geolon", 0);
volume = i.getStringExtra("volume");
saver = i.getStringExtra("saver");
poids = i.getStringExtra("poids");
tel = i.getStringExtra("telpass");
driver = i.getStringExtra("driverpass");
tour = i.getStringExtra("tour");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parc);
container = (TextView) findViewById(R.id.nom);
container.setText(conteneur);
RelativeLayout lr = (RelativeLayout) findViewById(R.id.full_layout);
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFFFFFFFF,0xFFB0B0B0});
gd.setCornerRadius(0f);
lr.setBackground(gd);
showImg = (ImageView) findViewById(R.id.imageView1);
String pathparc = Environment.getExternalStorageDirectory() + "/SmartCollecte/PARC/PARAM";
File f = new File(pathparc);
final File file[] = f.listFiles();
LinearLayout layout = new LinearLayout(MAIN_parcActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
layoutParams.setMargins(24, 50, 24, 0);
layout.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < file.length; j++) {
String[] namewext;
namewext = FilenameUtils.removeExtension(file[j].getName()).split("_");
Toast.makeText(getApplicationContext(),namewext[1],Toast.LENGTH_SHORT).show();
Spinner spinner = new Spinner(getApplicationContext());
String[]choix = new String[]{"choisissez"+j, "Propreté", "Détérioration", "Accès", "Maintenance", "autre"};
List<String> str = new ArrayList<>();
str.add(namewext[1]);
Collections.addAll(str,choix);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MAIN_parcActivity.this,
android.R.layout.simple_spinner_item, str);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
layout.addView(spinner);
setContentView(layout,layoutParams);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/full_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#909090"
>
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:background="#E25D63"
android:layout_alignParentTop="true"
android:weightSum="3">
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:gravity="left"
android:src="#drawable/backinblack"
android:background="#CFCFCF"
android:layout_weight="0.20"
android:onClick="fermer"
/>
<TextView
android:id="#+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#220c0c"
android:text="Tournée"
android:textStyle="bold"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_weight="0.80"
android:background="#E25D63"
android:layout_gravity="center_vertical"
>
</TextView>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/anom_ctn"
android:layout_weight="0.20"
android:background="#CFCFCF"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:fillViewport="true">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/spin"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:text="Etat de parc"
android:textSize="25sp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/spin2"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spin">
<Spinner
android:id="#+id/gametime"
android:layout_marginTop="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/anomalie"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/boutons"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spin2">
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_below="#id/boutons"
android:layout_margin="40sp"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_height="fill_parent"
android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:background="#cfcfcf"
android:weightSum="3"
android:layout_alignParentBottom="true"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/photo"
android:layout_weight="0.80"
android:paddingRight="30sp"
android:onClick="photo"
android:clickable="true"
/>
<View android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1.40"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/validation_test"
android:layout_weight="0.80"
android:paddingTop="10sp"
android:paddingLeft="25sp"
android:onClick="enregistrer"
android:clickable="true"
/>
</LinearLayout>
I'm not terribly sure here, I wonder if it is replacing the view because you aren't setting a unique ID for each view? That or failing to call layout.invalidate() after finishing a run through the loop.

Increasing scrollview by adding buttons in a relativelayout (or linearlayout ) from bottom to top

Could really use some help with this one.
I'm trying to start with a single button at the bottom of my screen. When my counter++ button is clicked, a second button will be dynamically created above the button at the bottom of the screen. I was able to get this far, the problem is that the height of my scrollview doesn't increase when my buttons reach the top of the screen. Please see my example below.
Example
I'm assuming the scrollview only increases when items are added below the current view although if that's the case, is there no way I can create several buttons starting at the bottom going upwards?
Here my code.
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.bbetzner.ttt.Map">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Java:
public void addfloor(){
Button myButton = new Button(this);
myButton.setText(""+floor);
margincount += 100;
RelativeLayout ll = findViewById(R.id.layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
}
I've also tried doing this in a linear layout to see if the scrollview would increase but I couldn't figure out how to stack buttons from bottom to top in a linear layout. :S
Thanks in advance, yall are awesome!
#Texas use below code using that you are achieving what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:id="#+id/lladdViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0" />
</LinearLayout>
</ScrollView>
</LinearLayout>
and here is Java File:
public class SampleActivity extends Activity {
LinearLayout lladdViews;
Button count;
int floor = 0;
private int margincount = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
lladdViews = (LinearLayout) findViewById(R.id.lladdViews);
count = (Button) findViewById(R.id.count);
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addfloor();
}
});
}
public void addfloor() {
Button myButton = new Button(this);
myButton.setText("" + floor);
margincount += 10;
RelativeLayout ll = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
lladdViews.addView(ll);
}
}
Here is your designing part happy coding.....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
<Button
android:id="#+id/floor1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="7"
/>
<Button
android:id="#+id/floor2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="6"
/>
<Button
android:id="#+id/floor3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="5"
/>
<Button
android:id="#+id/floor4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="4"
/>
<Button
android:id="#+id/floor5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/><Button
android:id="#+id/floor6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="3"
/><Button
android:id="#+id/floor7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="2"
/>
<Button
android:id="#+id/floor8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="1"
/>
<Button
android:id="#+id/floor9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Android: Layout short within a ScrollView

I recently just overhauled my application to use ListView instead of using the classic TextView.setText and looping the inflation of a layout. I was able to acheive this very well but now I'm having a problem having it fill the screen. Heres the section of my code that does the work.
//Place JSON data into array one item at a time
JSONArray jArray = new JSONArray(result);
//Loop through each record in the database
//Get ListView
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"lblQuote", "lblBuzzed", "lblShared", "lblSaid", "lblLikes", "lblHates", "lblLocation", "lblDate"};
int[] to = new int[] { R.id.lblQuote, R.id.lblBuzzed, R.id.lblShared, R.id.lblSaid, R.id.lblLikes, R.id.lblHates, R.id.lblLocation, R.id.lblDate };
for(int i=0;i<jArray.length();i++){
Log.i("uDrew Debug", "Made it into JSONArray Loop");
//Get this record
JSONObject json_data = jArray.getJSONObject(i);
//Put each result into variables for later handling
strFName = json_data.getString("FName");
strLInitial = json_data.getString("LInitial");
strCity = json_data.getString("City");
strState = json_data.getString("State");
strDate = json_data.getString("Date");
strQuote = json_data.getString("Quote");
intLikes = Integer.parseInt(json_data.getString("Likes"));
intHates = Integer.parseInt(json_data.getString("Hates"));
strFNameSaid = json_data.getString("FNameSaid");
strLInitialSaid = json_data.getString("LInitialSaid");
intBuzz = Integer.parseInt(json_data.getString("Buzz"));
Log.i("uDrew Debug", "Made it past JSON Parsing");
switch(intBuzz){
case 1:
strBuzzed = ("One Beer\nSyndrome");
break;
case 2:
strBuzzed = ("Buzzed");
break;
case 3:
strBuzzed = ("Drunk");
break;
case 4:
strBuzzed = ("Trashed");
break;
case 5:
strBuzzed = "Retarded";
break;
}
HashMap<String, String> map = new HashMap<String, String>();
map.put("lblQuote", strQuote);
map.put("lblBuzzed", strBuzzed);
map.put("lblShared", strFName + " " + strLInitial);
map.put("lblSaid",strFNameSaid + " " + strLInitialSaid);
map.put("lblDate", strDate);
map.put("lblLocation", strCity + ", " + strState);
map.put("lblLikes", intLikes.toString());
map.put("lblHates", intHates.toString());
fillMaps.add(map);
}//End For loop
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.myviews, from, to);
lv.setAdapter(adapter);
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:id="#+id/myMainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.ads.AdView android:id="#+id/adViewer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="a14de539f600385"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="RandomDrunkQuotes.com"
android:id="#+id/lblTitle"
android:textSize="16px"
android:padding="5px"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<!-- List Divider -->
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<!-- ListView (grid_items) -->
<LinearLayout android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ListView android:id="#+id/listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
myviews.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Filler Text"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<ImageView
android:id="#+id/imgUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:src="#drawable/thumbsup"
android:layout_gravity="left"/>
<TextView
android:id="#+id/lblLikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:textSize="4pt"
android:text="10"
android:layout_gravity="left"/>
<TextView
android:id="#+id/lblBuzzedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="Buzz Level:"/>
<TextView
android:id="#+id/lblShared"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Shared By: Filler Name"
android:textSize="4pt"/>
<TextView
android:id="#+id/lblSaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Said By: Filler Name"
android:textSize="4pt"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<ImageView
android:id="#+id/imgDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:src="#drawable/thumbsdn"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblHates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:textSize="4pt"
android:text="2"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblBuzzed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="High"
android:textSize="4pt"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="04/04/1987 4:32 PM"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="Broomfield, CO"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text=" "
android:textSize="1pt"
android:background="#6F7285"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Heres what the App looks like:
There are also no scrollbars and if I change the main.xml ScrolView layout_height="fill_parent" it extends to the end of the screen but the scrollable option isn't there.
Use android:layout_height="fill_parent" in your ScrollView and in the first LinearLayout.
Don't wrap the ListView inside a LinearLayout (the one below the divider View). There's absolutely no need for that.
(Optional) you might want to move the AdView outside the ScollView so it is always visible and not scrolled away
The following code fixed my problem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:id="#+id/BigLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:id="#+id/myMainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.ads.AdView android:id="#+id/adViewer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="a14de539f600385"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="RandomDrunkQuotes.com"
android:id="#+id/lblTitle"
android:textSize="16px"
android:padding="5px"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<!-- List Divider -->
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
</LinearLayout>
<!-- ListView (grid_items) -->
<ListView
android:id="#+id/listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>

Categories