Android - ListView's height just fits 1 ListView item - java

I am working with Android Studio and designing a layout as below. However, my ListView lv_comics only displays enough space for 1 item. I have tried to set layout_height to fill_parent, but it still does not work. However, when I set layout_height to a specific size, it is OK, but this is not good when I switch to another screen size. Anyone can help me?
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/layout_swiperefresh"
android:background="#color/grey">
<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">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Latest Update"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<Gallery
android:id="#+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Most Popular"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<Gallery
android:id="#+id/gallery2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="New Manga"
android:textColor="#color/white"
android:background="#color/indigo_main"
android:textSize="10dp"/>
<ListView
android:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/lv_comics" />
</LinearLayout>
</ScrollView>
Here is my layout show.
http://i.stack.imgur.com/KFIJy.jpg

try to this to calculate height dynamically
public static void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, MeasureSpec.AT_MOST);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

Related

Tablerow and TableLayout not aligning straight

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);
}
}

TextView not appearing in embedded LinearLayout

I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.

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.

Button Component Cuts off my custom View

My button component takes space of my custom view class,if i put more it takes up more space,any reason why is that
My oncreate method :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
MyCustomPanel view = new MyCustomPanel(this);
DisplayMetrics metrics;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = getDPI(500, metrics);
int width = getDPI(800,metrics);
layout = (LinearLayout) findViewById(R.id.r2) ;
layout1 = (LinearLayout) findViewById(R.id.l2);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,
height);
layout.addView(view,params);
}
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2"
android:gravity="bottom|start"
android:layout_gravity="bottom|start"
tools:context="company.ciso.com.wheelchair.Control">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="top|end"
android:layout_gravity="top|end"
android:id="#+id/l2"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1"
android:text="S1" />
</LinearLayout>
</LinearLayout>
these are the screen shots
Additionally is there anyway to set gravity of my custom view.??
Update your layout with this code.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|end"
android:gravity="top|end"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S1" />
</LinearLayout>
<LinearLayout
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

Categories