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);
Related
I am confused because in my RecyclerView, some items has their height = 0 (even when they are correctly displayed and visible) and some have their normal height.
How is that possible ?
If I monitor the height with addOnGlobalLayoutListener, 90% of item has their correct height calculated, and 10% still have 0.
Any idea of how android works for this ?
My code is in Kotlin but it doesn’t matter.
init {
var heightItemView = 0
var currentY = itemView.y
var oldY = currentY
itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (itemView.height > 0) {
heightItemView = itemView.height
itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
I also tried with measuredHeight().
My onCreateViewHolder() from the adapter:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PairMarketAdapter.ViewHolder {
val v = android.view.LayoutInflater.from(parent.context)
.inflate(R.layout.item_pair_market, parent, false)
val holder = PairMarketAdapter.ViewHolder(v)
holder.itemView.setOnClickListener {
listener.onItemAction(holder.adapterPosition)
}
return holder
}
XML of the fragment:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="#+id/lCoordinatorRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvPairMarket"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_normal"
tools:listitem="#layout/item_pair_market"
/>
</LinearLayout>
And the item XML:
<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="wrap_content"
android:background="?selectableItemBackground"
android:paddingBottom="#dimen/spacing_normal"
android:paddingTop="#dimen/spacing_small"
android:paddingEnd="#dimen/spacing_normal"
android:paddingStart="#dimen/spacing_normal"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_small"
android:layout_weight="1.5"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvSymbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="USD"
/>
<TextView
android:id="#+id/tvSymbolBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_tiny"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="BTC"
/>
<View
android:id="#+id/vIsSelected"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:layout_marginStart="#dimen/spacing_small"
android:background="#drawable/circle_green_dot"
android:visibility="gone"
/>
</LinearLayout>
<TextView
android:id="#+id/tvExchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_tiny"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_normal"
tools:text="1. Bitfinex"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
tools:text="55.5%"
/>
</LinearLayout>
</LinearLayout>
itemView.measuredHeight android will try to measure your view before display try using itemView.height inside addOnGlobalLayoutListener
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.
I'm using a recyclerview inside NestedScrollView and I want to pin my search bar to action bar after scrolling and recyclerview items must show under searchbar, I need to make lazy load for my recyclerview items (load next items from server after scrolling to end) for this I need to check recyclerview scroll change state and I can't do this perfectly when I use recyclerview inside nestedscrollview. I tried using nestedscrollview scroll state change listener and it doesn't give me what I want and doesn't work right.
Nested scroll view is not working if it's placed inside recyclerview. I have lazy load recyclerview but same layout contain other layouts like slider, menus then recyclerview. I want to scroll full layout and when recyclerview ends onscroll then onload execute to get more item from internet and load in recylerview.
Here you see my codes:
my layout xml code
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_activity_background">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimaryDark"
app:expandedTitleTextAppearance="#style/TextAppearance.AppCompat.Headline"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/bg_gradient"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large"
android:paddingTop="#dimen/spacing_mxlarge">
<LinearLayout
android:id="#+id/layout_dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#drawable/searchbox_stroke_bg"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="0.9"
android:layout_height="40dp"
android:hint="#string/search"
android:textDirection="locale"
android:textAlignment="viewStart"
android:drawablePadding="10dp"
android:layout_gravity="start"
android:paddingStart="15dp"
android:textColor="#color/searchbox_stroke"
android:maxLines="1"
android:singleLine="true"
android:maxLength="25"
android:textSize="17sp"
android:paddingEnd="5dp"
android:background="#android:color/transparent"
android:theme="#style/MainSearchEditTextTheme"
android:drawableStart="#drawable/ic_search"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="16dp"
android:src="#drawable/ic_filter"
android:layout_gravity="center_vertical" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:id="#+id/main_nested_scrollView"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true"
android:nestedScrollingEnabled="false"
android:id="#+id/main_recyclerview"
android:layout_marginBottom="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/more_items_progress"
android:visibility="gone">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progress_bar"
android:indeterminate="true"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lyt_progress"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:orientation="vertical">
<com.armanjafari.raimon.widget.ViewLoadingDotsBounce
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:background="#color/colorAccent">
</com.armanjafari.raimon.widget.ViewLoadingDotsBounce>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
My code to check scroll state and it didn't work
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
if (scrollY == ( v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight() ))
{
if (!is_loading)
{
if (current_page < all_pages)
{
//not work right scroll state
}
}
}
}
});
i got the answer, if any one have my problem see below...
for detect nestedscrollview is at end correctly use this code:
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
View view = (View) nested_content.getChildAt(nested_content.getChildCount() - 1);
int diff = (view.getBottom() - (nested_content.getHeight() + nested_content
.getScrollY()));
if (diff == 0)
{
if (merchantList.size()>0)
{
if (!is_loading)
{
if(current_page<=all_pages)
{
// nestedscrollview at end
getMerchants(current_page);
}
}
}
}
}
});
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>
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();
}