Show/Hide BottomNavigationView for specific fragment - java

In my android application, I want the bottom menu bar to disappear when the user focuses the SearchView (this also pops the soft keyboard up). When the SearchView loses focus, I want to show the bottom navigation bar again.
I have tried using setVisibility() and the view does hide or show, but it always retains its height for some reason.Below is the code for my BottomNavigationView:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="#menu/bottom_navigation_menu"
app:elevation="80dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#color/bottom_nav_color"
app:itemIconTint="#color/bottom_nav_color"
android:background="?attr/backgroundColor">
Code that handles hiding/showing of the navigation bar:
// Needed to close the SearchView when pressing back (instead of just losing focus)
mSearchView.setOnQueryTextFocusChangeListener(
(v, hasFocus) -> {
if (!hasFocus) {
adapter.isSearchMode = false;
bottomNavigationView.setVisibility(View.VISIBLE);
searchMenuItem.collapseActionView();
adapter.notifyDataSetChanged();
} else {
adapter.isSearchMode = true;
bottomNavigationView.setVisibility(View.GONE);
searchMenuItem.collapseActionView();
adapter.notifyDataSetChanged();
}
});
The BottomNavigationView is held by a LinearLayout like so:
<LinearLayout
android:id="#+id/footer"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/bottom_bar_top_shadow"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="#menu/bottom_navigation_menu"
app:elevation="80dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#color/bottom_nav_color"
app:itemIconTint="#color/bottom_nav_color"
android:background="?attr/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="6dp"
android:weightSum="5"
android:elevation="16dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|top">
<TextView
android:id="#+id/missed_calls"
style="#style/unread_count_font"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/unread_message_count_bg"
android:layout_marginStart="20dp"
android:gravity="center"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|top">
<TextView
android:id="#+id/missed_chats"
style="#style/unread_count_font"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/unread_message_count_bg"
android:layout_marginStart="20dp"
android:gravity="center"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
</com.google.android.material.bottomnavigation.BottomNavigationView>
</LinearLayout>

On your BottomNavigation use this code snippet:
setVisibility(View.GONE)
View.Gone = No Space lefts for view.

Related

Can hide ProgressBar but cannot show it

I'm using the default ProgressBar widget, for indeterminate progress, in a Linear Layout with Style: #android:style/Widget.DeviceDefault.Light.ProgressBar.Small
If I start the activity with the ProgressBar VISIBLE and never make it INVISIBLE or GONE it shows ok. But if I put progressBar.setVisibility(progressBar.GONE); or progressBar.setVisibility(progressBar.INVISIBLE); anywhere in my code, the space for the ProgressBar is on the UI, but I cant see it.
I thought it might be a leaky parallel thread and removed all multi-threading from my app, but it still won't appear if INVISIBLE or GONE is anywhere in my code.
It works fine on other Activities.
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="260dp">
<ScrollView
android:id="#+id/myTeamsScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<LinearLayout
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:paddingTop="32dp">
<ImageView
android:id="#+id/menu1ImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:contentDescription="#string/userNavigationMenuButtonContent"
android:onClick="openDrawer"
app:srcCompat="#drawable/menu_icon" />
</LinearLayout>
</LinearLayout>
<EditText
android:id="#+id/enterNewTeamEditText"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/enterTeamNameEditTextcontent"
android:ems="10"
android:hint="#string/teamNameHint"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary" />
<ProgressBar
android:id="#+id/myTeamsProgressBar"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:alpha="1"
android:foregroundTint="#FFFFCC"
android:progressTint="#FFFFCC"
android:visibility="visible" />
<TextView
android:id="#+id/deckTypeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:contentDescription="#string/remotePokerlabelcontent"
android:enabled="false"
android:text="#string/newTeamDeckType"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/remotePokerTextView" />
<Spinner
android:id="#+id/newTeamDeckTypeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="#drawable/spinner"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNewTeamEditText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/createTeamButton"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="createTeam"
android:text="Create Team"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/receivedJoinRequestsButton"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="showJoinRequests"
android:text="#string/myTeamsJoinRequestsButton"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:layout_weight="1" />
<Switch
android:id="#+id/teamTypesSwitch"
android:layout_width="143dp"
android:layout_height="wrap_content"
android:checked="false"
android:fontFamily="#string/fontfamily"
android:text="#string/switchMemberTeams"
android:textColor="#color/colorPrimary"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:orientation="vertical">
<ListView
android:id="#+id/myTeamsListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="32dp"
android:layout_weight="1"
android:alwaysDrawnWithCache="true"
android:background="#drawable/spinner"
android:contentDescription="#string/myTeamsListViewcontent"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:onClick="onSwitchChange"
app:headerLayout= "#layout/nav_header_main"
app:menu="#xml/drawer_view" />
</androidx.drawerlayout.widget.DrawerLayout>
In the Activity onCreate I call populateMemberTeams(); I added a CountDownTimer to check if the progressBar is on the screen and it is...Before the 2 seconds, there is a empty space, but I can't see the ProgressBar
When the timer expires the UI adjusts when the ProgressBar changes to GONE.
private void populateMemberTeams() {
ListView myTeamsListView = findViewById(R.id.myTeamsListView);
ProgressBar progressBar = findViewById(R.id.myTeamsProgressBar);
progressBar.setVisibility(progressBar.VISIBLE);
mAuth = FirebaseAuth.getInstance();
String userId = mAuth.getCurrentUser().getUid();
callTeamManagement.populateMemberTeams(TAG, passedActivity, context, userId);
new CountDownTimer(2000, 1000) {
public void onFinish() {
progressBar.setVisibility(progressBar.GONE);
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
I have tried:
Changing the ProgressBar style in case it was a colour issue;
INVISIBLE instead of GONE;
Putting the ProgressBar inside its own LinearLayout
progressBar.setVisibility(progressBar.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(ProgressBar.VISIBLE);
You should use runOnUiThread.
Helper method for running part of a method on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
});

Custom Dialog is not opening on click in fragment?

I build my own custom Dialogbut its not opening when i am clicking on ImageView. I also checked by putting break point Dialog is coming null how to rectify it. I want to shift TextView of id unread_count to the right of parent . How can I do this ?
If I set android:layout_alignParentRight="true" then I have the following picture :
<?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"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:minHeight="120dp"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:tint="#android:color/white"
app:srcCompat="#drawable/info" />
<TextView
android:id="#+id/Dialogtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Latest # LootBox"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/Dialogcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="All These Deals, Offers etc Are For Limited Period of Time And Can be Over at Any Time Without Any Prior Notice."
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#666666" />
</LinearLayout>
<LinearLayout
android:id="#+id/lyt_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded_green"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Get Started"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
Java code
private ImageView dialogBox;
Dialog customDialog;
customDialog = new Dialog(getActivity());
dialogBox = (ImageView) view.findViewById(R.id.dialogBox);
dialogBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog();
}
});
private void showDialog() {
customDialog.setContentView(R.layout.dialog_info);
}
You can try this way, you have to find dialog views using dialog.findViewById
Dialog dialog = new Dialog(mActivity);
dialog.setContentView(R.layout.dialog_info);
//dialog.setCancelable(false); //set Cancelable
ImageView dialogBox = (ImageView) dialog.findViewById(R.id.imgViewID);
dialogBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
//for big size dialog
/* Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);*/
I check you dialog_info.xml code
You need to improve your design code, you don't need to take static height and weight
I update your layout code here
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:tint="#android:color/white"
app:srcCompat="#drawable/ic_close" />
<TextView
android:id="#+id/Dialogtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Latest # LootBox"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/Dialogcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="All These Deals, Offers etc Are For Limited Period of Time And Can be Over at Any Time Without Any Prior Notice."
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#666666" />
</LinearLayout>
<LinearLayout
android:id="#+id/lyt_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Get Started"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
You forgot to call show method on the custom dialog. Change your code to
private void showDialog() {
customDialog.setContentView(R.layout.dialog_info);
// Add this line
customDialog.show();
}

Check recyclerview scroll state inside nestedscrollview

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

Listview not scrolling untill the last Item

Hello there I am have having problem scrolling untill the last item scrolling stops in the middle, here is my layout code.
<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="fragment.Equivalent">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:focusable="false"/>
<TextView
android:id="#+id/txt404"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Message that will be shown if the listview is empty"
android:textSize="21dp"
android:textStyle="bold"
android:visibility="invisible" />
</RelativeLayout>
Try with a scroll view:
<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="fragment.Equivalent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:scrollbarStyle="insideOverlay"
style="#android:style/Widget.Holo.ScrollView">
<TableLayout
android:id="#+id/table_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
</RelativeLayout>
in java file, do this:
TableLayout ll = (TableLayout) findViewById(R.id.table_layout_id);
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);

 
 butt = new Button(this);
 //You can add button too, FYI
butt.setText("TEST");
 
 
TextView tv = (TextView) findViewById(R.id.txt404);

tv.setText("Text view");
tv2 = new TextView(this);
 //your job: add list view here
tv2.setText("Text View 2");
 
 
row.addView(butt);

 row.addView(tv1);
row.addView(tv2);
 
ll.addView(row, i); // where i is the row number, increment on addition
Solution was changing the layout to a constraint layout and changing the size of the listview
<android.support.constraint.ConstraintLayout
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"
tools:context="fragment">
<ListView
android:id="#+id/listView"
android:layout_width="399dp"
android:layout_height="485dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="0dp"
android:focusable="false"
android:visibility="visible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/txt404"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="21dp"
android:textStyle="bold"
android:visibility="invisible"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintVertical_bias="0.391" />
</android.support.constraint.ConstraintLayout>

How to find the ID of a dynamic UI element in android

I can make the dynamic UI without problems, but I don't know how to access the view with findviewbyid (R.id._____) I don't know what to put in the blank.
.java code
public class newList extends AppCompatActivity {
Button btnAddItem;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list);
// allows the creation of dynamic rows
btnAddItem = (Button) findViewById(R.id.addItem);
btnAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = (ViewGroup) findViewById(R.id.linearVert);
inflater.inflate(R.layout.rows, parent);
}
});
}
}
xml that is associated with the java file
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.shoppingbuddy.shoppingbuddy.newList">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearVert">
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Subtotal"
android:id="#+id/subtotal"
android:textColor="#ffc800"
android:layout_above="#+id/tax"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tax"
android:id="#+id/tax"
android:textColor="#ee00ff"
android:layout_above="#+id/total"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total"
android:id="#+id/total"
android:textColor="#ff0000"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/subtotalAmount"
android:layout_alignBottom="#+id/subtotal"
android:layout_alignEnd="#+id/scrollView"
android:textColor="#ffc800" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/taxAmount"
android:layout_above="#+id/total"
android:layout_alignParentEnd="true"
android:textColor="#ee00ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/totalAmount"
android:layout_alignBottom="#+id/total"
android:layout_alignEnd="#+id/taxAmount"
android:textColor="#ff0000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/addItem"
android:layout_above="#+id/subtotal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>
xml used to create rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:id="#+id/linearHor">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/itemImage"
android:src="#drawable/box"
android:padding="5dp" />
<EditText
android:layout_width="109dp"
android:layout_height="wrap_content"
android:id="#+id/itemName"
android:hint="Name"
android:layout_weight="0.78" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/itemPrice"
android:hint="$ Price" />
</LinearLayout>
</LinearLayout>
There will be no unique R.id value at the activity level. For example, suppose the user taps the button 10 times, so you inflate the R.layout.rows layout 10 times. As a result, there will be 10 R.id.linearHor widgets, 10 R.id.itemPrice widgets, and so on.
Always call findViewById() on something that is guaranteed to give you a unique result. In this case — as with ListView, GridView, RecyclerView, etc. — you need to use other approaches to find the correct row View, then call findViewById() on that View to get at its child widgets.

Categories