SwipeToRefreshview swipe error - java

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.

Related

How to Refresh Fragments with swipe to refresh widget?

I wanted to refresh my fragment with SwipeToRefresh widget.
This is my XML Code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
and this is my JAVA code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1, container, false);
newconfig= getResources().getConfiguration();
db=new DBHandler(getActivity());
swipeRefreshLayout=view.findViewById(R.id.swipe);
recyclerView=view.findViewById(R.id.listview);
progressBar=view.findViewById(R.id.progress);
if(book.size()==0) {
book = db.readCourses();
}
progressBar.setVisibility(View.GONE);
BookFragAdapter adapter = new BookFragAdapter(Fragment_land1.this,book);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false));
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
this.onRefresh();
getFragmentManager().beginTransaction().detach(Fragment_land1.this).attach(Fragment_land1.this).commit();
swipeRefreshLayout.setRefreshing(false);
}
});
return view;
}
When I try to refresh it doesn't refresh and the widget doesn't stops after 250ms. I assume that the code inside onRefreshListener is not actually executing therefore the next line is ignored. I don't know how change it with. I surfed in Stackoverflow and Developer.android none of the answers solved my problem. Can anyone help me?
first check that you have added dependencies in build.gradle file
i.e implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
Secondly swipe to refresh layout tag should start from start instead of fragment
for eg :
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
third then write swipeRefreshLayout.setOnRefreshListener code in java file

When click searchbar it the value below come up

its image showing value and search bar
it is the image showing when clicking the search bar it will come up the values in list-view
I want to know why this happen and how to avoid the situation please help me thanks in advance
public class ListItem extends AppCompatActivity
{
ListView listView1;
ArrayList<String> listproduct;
ArrayAdapter<String > adapternew;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("Please Select the Item");
SearchView search=(SearchView)findViewById(R.id.Search);
ScrollView scroll=(ScrollView)findViewById(R.id.Scroll);
ListView listView1=(ListView)findViewById(R.id.Listview);
listproduct=new ArrayList<>();
listproduct.add("Stove Top Stuffing");
listproduct.add("Campell's Soup");
listproduct.add("Tide");
listproduct.add("Pampers");
listproduct.add("Pepsi Products");
listproduct.add("Tang");
listproduct.add("Top Ramen");
listproduct.add("Knorr");
listproduct.add("Palmolive");
listproduct.add("Scotch-Brite");
listproduct.add("Bounty Paper Towls");
listproduct.add("Oreo Cookies");
listproduct.add("Quaker Oats");
listproduct.add("Lays Potato Chips");
adapternew = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listproduct);
listView1.setAdapter(adapternew);
SearchManager manager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
search.clearFocus();
if(listproduct.contains(query))
{
adapternew.getFilter().filter(query);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapternew.getFilter().filter(newText);
return false;
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int newposition, long id) {
Toast.makeText(ListItem.this,listproduct.get(newposition)+"",Toast.LENGTH_SHORT).show();
Intent i=new Intent(ListItem.this,AddOrder.class);
i.putExtra("position",listproduct.get(newposition));
//adapternew.notifyDataSetChanged();
startActivity(i);
}
});
}
}
here is the code that is to show the list view item and i want to know whether the value in list view come up when clicking the search bar
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.00" />
<ScrollView
android:id="#+id/Scroll"
android:layout_width="100dp"
android:layout_height="150dp"
tools:layout_editor_absoluteX="-8dp"
tools:layout_editor_absoluteY="396dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.200">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the xml file of the listview
I think this is the code you are wanting.
No need to use scrollview here because ListView itself is scrollable. LinearLayout with Vertical Orientation will solve your issue
<?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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"/>
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp">
</ListView>
</LinearLayout>
I think with ConstraintLayout , you can use app:layout_constraintTop_toBottomOf to assign it below the desired view instead of layout_below
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/search" <!-- change here -->
app:layout_constraintVertical_bias="0.200">

SwipeRefresh Layout is covering the recycleview

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.

RecyclerView is shown as empty

My recyclerView is not showing anything. I looked through the other similar questions posted on the site, but I can't find anything that seems relevant. When I open the activity, I just see the bar on top with the app name and nothing else below it.
My goal is to get a list of items, where each item has a "delete" icon that can be clicked to delete it. For this purpose, I have a custom layout for the recyclerView item.
I made a simplified version of the code to try and isolate the problem. Here it is:
Activity:
public class CategoryManagerActivity extends AppCompatActivity {
private RecyclerView categoryRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_manager);
categoryRecyclerView = findViewById(R.id.category_edit_recycler);
ArrayList<String> categoryList = new ArrayList<>();
categoryList.add("CAT1");
categoryList.add("CAT2");
categoryList.add("CAT3");
CategoryEditRecyclerViewAdapter adapter = new CategoryEditRecyclerViewAdapter(categoryList);
categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
categoryRecyclerView.setAdapter(adapter);
}
}
Adapter class:
public class CategoryEditRecyclerViewAdapter extends
RecyclerView.Adapter<CategoryEditRecyclerViewAdapter.CategoryEditViewHolder>{
private ArrayList<String> categories;
public CategoryEditRecyclerViewAdapter(ArrayList<String> items){
categories = items;
}
#NonNull
#Override
public CategoryEditViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.categories_edit_recycler_item,
parent, false);
return new CategoryEditRecyclerViewAdapter.CategoryEditViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryEditViewHolder holder, int position) {
final String category = categories.get(position);
holder.categoryName.setText(category);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
delete_category(category);
}
});
}
#Override
public int getItemCount() {
return categories.size();
}
private void delete_category(String category){
//I have code here to delete an item
}
public class CategoryEditViewHolder extends RecyclerView.ViewHolder{
TextView categoryName;
ImageView deleteButton;
public CategoryEditViewHolder(View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryEditText);
deleteButton = itemView.findViewById(R.id.deleteCategory);
}
}
}
recyclerView item layout:
(I used the #drawable/delete in other activities in the app, so I don't think it should be a problem)
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:layout_marginBottom="690dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="679dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this for item layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/deleteCategory"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this for activity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Modify your RecyclerView Item Layout s below.
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_drawable_delete" />
</RelativeLayout>
Please modify your categories_edit_recycler_item Item Layouts below:
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

how to add bottom layout in Modal BottomSheet Dialog?

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>

Categories

Resources