I have a problem with implementing a refresh layout. It is covering everything.
I don't know what is the problem, but I tried changing from fragment just to activity, didn't help
here is my code:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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=".MainActivity"
android:background="#color/background">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/topheadelines"
android:textColor="#color/colorTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Top Headlines"
android:fontFamily="sans-serif-light"
android:textSize="17sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:visibility="invisible"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Also, I did think that there was a problem with a toolbar and put an actionbar instead( didn't help)
UPDATE:
It is my function for the swiperefresh layout, just in case:
#Override
public void onRefresh() {
LoadJson("");
}
private void OnLoadingSwipeRefresh(final String keyword){
swipeRefreshLayout.post(
new Runnable() {
#Override
public void run() {
LoadJson(keyword);
}
}
);
}
I found my error. It wasn't in my layout. It was in the code of the adapter.
#Override
public int getItemCount() {
return articles.size();
}
return 0; is set by default and it was my error.
I know it is stupid but it is just in case if someone like me skips that error and not going to know why.
Related
Hello good morning in my view i cant handled the setOnItemClickListener
I am trying to create an application which dynamically lists installed applications in a gridview.I am able to display the applications in a gridview form but not able to make these items clickable. Nothing happens when i click these items. The code inside the setOnItemClickListener does not get triggered on click event.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Tab2profile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
//my scrollview
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/MainRLyout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/girislayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/usernameTv"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:textSize="#dimen/text_size_Orta"
android:textColor="#color/cardview_dark_background"
android:layout_height="wrap_content"
tools:ignore="PrivateResource" />
<Button
android:id="#+id/Login_LogOut_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/logout_rounded"
android:drawableRight="#drawable/logout_icon"
android:padding="0dp"
android:text="#string/Logout_Text_str"
android:textColor="#android:color/white"
android:textSize="12sp"
tools:ignore="RelativeOverlap,RtlHardcoded" />
</RelativeLayout>
//in here i cant handled the item click funktşon
<syr13.comapp4.Custum_Adapters.ExpandableHeightGridView
android:id="#+id/Your_Apps_Li"
android:numColumns="3"
android:layout_below="#id/Your_Apps_Tv"
android:horizontalSpacing="6dp"
android:layout_margin="5dp"
android:verticalSpacing="6dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
</syr13.comapp4.Custum_Adapters.ExpandableHeightGridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</FrameLayout>
I think you can Change your Code
from
button.setOnItemClickListener(this);
To
button.setOnClickListener(this);
for example :-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
//Do Something
break;
}
}
}
I am having an issue with nested scroll view when reach bottom.
Inside nestedScrollView I have a button, in bottom of page I have a input field to enter phone number. When press button, if phone number filed not filled, I want to scroll to Bottom of Activity to highlight input field.
But in my activity it scrolling only half, not scrolling till end.
I tried all beloved java codes to fix this issue, but all works same (Not reaching bottom, stops on middle).
Java Codes
CoordinatorLayout mainCoordinate = findViewById(R.id.mainCoordinate);
NestedScrollView nestedScrollView = findViewById(R.id.nestedScroll);
1.
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
#Override
public void run() {
nestedScrollView.smoothScrollTo(0, mainCoordinate.getBottom());
}
});
2.
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
#Override
public void run() {
nestedScrollView.fullScroll(View.FOCUS_DOWN);
}
});
3.
nestedScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
final int scrollViewHeight = nestedScrollView.getHeight();
if (scrollViewHeight > 0) {
nestedScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
final View lastView = nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1);
final int lastViewBottom = lastView.getBottom() + nestedScrollView.getPaddingBottom();
final int deltaScrollY = lastViewBottom - scrollViewHeight - nestedScrollView.getScrollY();
/* If you want to see the scroll animation, call this. */
nestedScrollView.smoothScrollBy(0, deltaScrollY);
/* If you don't want, call this. */
nestedScrollView.scrollBy(0, deltaScrollY);
}
}
});
XML Codes
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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/mainCoordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:ignore="RtlHardcoded">
<android.support.design.widget.AppBarLayout
android:id="#+id/flexibleAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="invisible">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/flexible.example.collapsing"
android:layout_width="match_parent"
android:layout_height="258dp"
app:contentScrim="?colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/imageMain"
android:layout_width="match_parent"
android:layout_height="258dp"
android:foreground="#drawable/ripple_effect_circle_card"
android:src="#drawable/back_button_white" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:behavior_overlapTop="48dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/price.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="#color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
.........................
.........................
.........................
//My Other All Views
.........................
.........................
.........................
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="#color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="5dp"
android:text="Phone No." />
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="end"
android:hint="Enter your phone"
android:inputType="phone"
android:text=""/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="#+id/btnBookNow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</android.support.design.widget.CoordinatorLayout>
I attached a video that explains my exact problem link
Please, Does anyone having a better way or other suggestion to fix this issue?
Thanks in Advance
Pls try Android layout inspector to check height of your NestedScroll.
Try to add
android:layout_gravity="bottom|center" to Button.
In NestedScrollView set layout heigh to android:layout_height="wrap_content"
I have a swipe to refresh view and a listview within in the swipe to refresh view
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
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/refreshView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data available"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:textSize="20sp"
android:visibility="gone"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
<ListView
android:id="#+id/ArrayList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent">
</ListView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.SwipeRefreshLayout>
I can scroll down the list but when i want to go back up, the swipe to refresh kicks in and I got to be able to view the entire list, any recommendations ??
and this is the implementation for the listener
srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView);
srl.setOnRefreshListener(srfListener());
function for listener
protected SwipeRefreshLayout.OnRefreshListener srfListener() {
return new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
update();
}
};
}
and update function
#Override
protected void update() {
HomeActivity.getInstance().onResume();
srl.setRefreshing(false);
}
Use like this
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ArrayList"
android:scrollbars="vertical"
android:paddingBottom="85dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v4.widget.SwipeRefreshLayout
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/refreshView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/ArrayList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Swipe refresh view should only wrap around the listview/recyclerViews .....not around the parent view
Try this:
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
#Override
public void onRefresh()
{
update();
}
});
instead of this:
protected SwipeRefreshLayout.OnRefreshListener srfListener() {
return new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
update();
}
};
}
Xml:
Take Linear layout instead of ConstraintLayout.
I want to achieve the design like this using BottomSheetDialogFragment. But the problem is the bottom layout get scrolled when i drag. I want the bottom layout always in bottom until the BottomSheetDialogFragment gets dismissed.
Please find the screenshot
Here is my code
bottomsheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--The main content goes over here-->
</LinearLayout>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</FrameLayout>
ModalBottomSheetFragment.java
public class ModalBottomSheetFragment extends BottomSheetDialogFragment {
public ModalBottomSheetFragment() {}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {}
};
#TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
dialog.setContentView(mContentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = getScreenHeight(getActivity());
final double desiredHeight = 0.85 * height;
mContentView.getLayoutParams().height = height;
((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);
}
}
public static int getScreenHeight(Context ctx) {
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
return metrics.heightPixels;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button mShowBottomSheetDialog;
private BottomSheetDialogFragment bottomSheetDialogFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialogFragment = new ModalBottomSheetFragment();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
});
}
}
use this layout instead of your layout ....
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical">
<!--The main content goes over here-->
</LinearLayout>
</ScrollView>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Note:- Weight divide your Layout into different View which is constant in your layout ......
use this layout:
<?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:background="#android:color/white"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="18sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your content here-->
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="#+id/bottomView"
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Footer"
android:textSize="18sp"/>
</FrameLayout>
</LinearLayout>
Why is there a Scroll Bar in List View even when the data in displayed in it doesn't require scrolling?
here is my xml file
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
Comparing getLastVisiblePosition() with getCount() in a Runnable, you will get to know whether your list view will fit into the screen or not. Also check if the last visible row fits entirely on the screen or not.
ListView listView;
Runnable fitsOnScreen = new Runnable() {
#Override
public void run() {
int last = listView.getLastVisiblePosition();
if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) {
listView.setScrollContainer(false);
}
else {
listView.setScrollContainer(true);
}
}
};
Finally in ListView's Handler: onCreate()
listView.post(fitsOnScreen);
You can do this by
listView.setScrollContainer(false);
for more please check
How to get a non scrollable ListView?
If you are not want to hide the scrollbar for ever. Then it will help you.
Due to android:layout_height="match_parent" on listView and TextView scrollbar appears.
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" // change 1
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" //change 2
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
android:scrollbars="none"
Try putting the above line in your listview in xml.