RecyclerView is shown as empty - java

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>

Related

Why ListView is always empty in Fragment Dialog

The Dialog shows properly but the listview is always empty.
Here is what I get when I launch the Activity
enter image description here
public class MainFragment extends Fragment {
String[] cities = {"Tanger", "Meknes", "Casablanca",
"Fes", "Tetouan", "Taza", "Rabat"};
Dialog dialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView currentCity = view.findViewById(R.id.city);
currentCity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner, container, false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
EditText searchTextView = spinnerView.findViewById(R.id.editText);
ListView citiesListView = spinnerView.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
}
DialogLayout:
<?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"
android:background="#drawable/alert_dialog"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
android:textColor="#color/primaryColor"
android:fontFamily="#font/alhurra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:fontFamily="#font/aj_bold"
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentLayout
<?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"
android:background="#drawable/activitybackground"
tools:context=".MainFragment">
<TextView
android:id="#+id/city"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:fontFamily="#font/alhurra"
android:gravity="center"
android:text="مكنـاس"
android:textColor="#color/primaryColor"
android:textSize="20sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is a problem with your layout. ConstraintLayout is somehow hiding the listview. You have to do some changes in your layout files. For example, this has worked for me using LinearLayout
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
//View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner,binding.getRoot());
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ListView citiesListView = dialog.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
dialog.show();
==
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_below="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_below="#id/editText"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</RelativeLayout>

OnItemClick isn't working on a ListView item

I have a custom layout for the list's cells where you have a checkbox and 3 text elements. I found on Stackoverflow that I should add android:descendantFocusability = "blocksDescendants" on my LinearLayout where the list is but it doesn't change anything.
Here is my XML for the page with the list :
<?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:id="#+id/cl_fragment_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context=".ui.home.AperoDetailFragment"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/date_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:ems="10"
android:gravity="right"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtView_shopping_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="#string/shopping_list_title" />
<Button
android:id="#+id/btn_add_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="#android:drawable/ic_menu_add"
android:text="#string/btn_add_ingredient"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:id="#+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
</LinearLayout>
then in my Java code I set the listener like this :
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
final TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.name);
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.date);
// some code that aren't useful right now
//-------------- List of ingredients for the detailed apero
final ListView list_ingredient = (ListView) root.findViewById(R.id.list_ingredient);
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
IngredientDao dbIngredient = db.getIngredientDao();
//#TODO control if we get the right ingredients
ArrayList<Ingredient> ingredient_of_this_apero = (ArrayList<Ingredient>) dbIngredient.getIngredientsFromApero(detailApero.aid);
IngredientAdapter adapter = new IngredientAdapter(root.getContext(), R.layout.ingredient_cell_layout, ingredient_of_this_apero);
list_ingredient.setAdapter(adapter);
list_ingredient.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Ingredient selected_ingredient = (Ingredient) adapter.getItemAtPosition(position);
Log.e("test","okasodkdaso");
}
});
return root;
}
I also tried using a OnItemLongClickListener but it also does nothing. How can I put a listener for the list items ?

SwipeToRefreshview swipe error

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.

adding listener to custom listview containing CheckedTextView and button

this is my row layout(rowlayout.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingRight="16dp"
android:text="CheckedTextView" />
<Button
android:id="#+id/telque"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#id/checkedTextView"
android:text="telque" />
</RelativeLayout>
and this is my main layout (activity_work_condition.xml)
<?xml version="1.0" encoding="utf-8"?>
<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="com.doctorbeingwell.doctorbeingwell.createfilepack.WorkConditions">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:onClick="passconditions"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
android:id="#+id/linearLayout">
<ListView
android:id="#+id/checkabalelist"
android:layout_width="match_parent"
android:layout_height="491dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_weight="0.2"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
and this is my class (workconditions.java):
public class WorkConditions extends Activity {
ArrayList<String> selectedItems = new ArrayList<>();
ListView checkablelist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_condition);
checkablelist = findViewById(R.id.checkabalelist);
checkablelist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] items = {"étudiant",
"chomeur",
"retraité",
"travail à domicile",
"travail au bureau"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.checkedTextView, items);
checkablelist.setAdapter(adapter);
CheckedTextView checkedTextView= findViewById(R.id.checkedTextView);
checkablelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selectedItem = ((CheckedTextView) view).getText().toString();
if (selectedItems.contains(selectedItem)) {
selectedItems.remove(selectedItem);
} else selectedItems.add(selectedItem);
}
});
and this is the result ,
but the items are not clickable and i cant retreive the data with this code
String selectedItem = ((CheckedTextView) view).getText().toString();
In rowlayout.xml, add android:descendantFocusability="blocksDescendants" to the root node(RelativeLayout in your case).
CheckedTextViewview and Button in rowlayout.xml are both focusable, which prevent ListView item from getting focus.
For more, refer android:descendantFocusability

Title in the BottomSheet

I have a BottomSheet with a RecyclerView, and I want to add a title there as in the picture below ("Add new record"), I was trying to add TextView before the RecyclerView in the bottom_sheet LinearLayout, but this text doesn't appear on the screen, also I was trying to add an item in the RecyclerView (but I have a feeling that it's not a right approach), so my question is how to add the title to the BottomSheet?
This is the method where I'm adding items to the RecyclerView, which is in the BottomSheet:
private void showBottomSheetDialog() {
if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
mBottomSheetDialog = new BottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.sheet, null);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new ItemAdapter(createItems(), new ItemAdapter.ItemListener() {
#Override
public void onItemClick(Item item) {
if (mBottomSheetDialog != null) {
mBottomSheetDialog.dismiss();
}
}
}));
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
mAdapterItem.setListener(null);
}
public List<Item> createItems() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(R.drawable.camera, "from new shoots"));
items.add(new Item(R.drawable.folder_multiple_image, "from ready images"));
return items;
}
xml of the screen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#118b0a"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/my_toolbar" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/add_white" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp">
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior">
<!-- Here I was trying to add text view-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Add new record 2"-->
<!--/>-->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="#fff" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
xml of item in the RecyclerView which is in the BottomSheet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:textColor="#787878"
android:textSize="22sp" />
</LinearLayout>

Categories

Resources