I'm currently having trouble with adding a second+ items to a RecyclerView after inputting data in an alertdialog box.
I can enter 1 set of data but when I try to add more, it doesn't do anything.
This is my Java file for the fragment i'm working with:
public class tab1Expenses extends Fragment {
List<ExRow> expenseList = new ArrayList();
RecyclerView recyclerView;
ExpensesAdapter mAdapter;
Button btnEx;
EditText txtExName;
EditText txtExAmount;
public void expenseData() {
String Na = txtExName.getText().toString();
String Am = txtExAmount.getText().toString();
ExRow exs = new ExRow(Na, Am);
expenseList.add(exs);
mAdapter.notifyDataSetChanged();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.tab1expense, container, false);
btnEx = (Button) rootView.findViewById(R.id.btnEx);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mAdapter = new ExpensesAdapter(expenseList);
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
btnEx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = LayoutInflater.from(tab1Expenses.this.getActivity())
.inflate(R.layout.add_ex, null);
txtExName = (EditText) view.findViewById(R.id.exName);
txtExAmount = (EditText) view.findViewById(R.id.exAmount);
AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity());
add.setCancelable(true)
.setTitle("Enter Expense:")
.setView(view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
expenseData();
}
});
Dialog dialog = add.create();
dialog.show();
}
});
return rootView;
}
}
And the Java file for the adapter:
public class ExpensesAdapter extends RecyclerView.Adapter<ExpensesAdapter.MyViewHolder> {
private List<ExRow> expenseList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, amount;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.name);
amount = (TextView) view.findViewById(R.id.amount);
}
}
public ExpensesAdapter(List<ExRow> expenseList) {
this.expenseList = expenseList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.expense_list, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ExRow expense = expenseList.get(position);
holder.title.setText(expense.getTitle());
holder.amount.setText(expense.getAmount());
}
#Override
public int getItemCount() {
return expenseList.size();
}
}
The XML to format the recyclerview list items:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/name"
android:textColor="#color/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/amount"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
And this is the XML for the fragment:
<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.example.ojemz.expensetracker.tab1Expenses"
android:background="#android:color/darker_gray">
<Button
android:text="Add Expense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnEx"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:width="800dp"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.Switch"
android:background="#android:color/black"
android:textColor="#android:color/holo_green_light"
android:textColorLink="#android:color/holo_green_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="17dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="450dp"
android:scrollbars="vertical"
android:keepScreenOn="true"
android:isScrollContainer="true" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
This is what I currently see AFTER adding a 2nd input
You can't do so:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="17dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</ScrollView>
Your RecyclerView has 0 height now. You need to add a some fixed height to it and set a scroll behaviour. Or you can use NestedScrollView or write LayoutManager with the full height expansion.
See this thread for details.
ADDED
Use this XML instead of your but I wrote it without IDE so it can contain some errors.
<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: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.example.ojemz.expensetracker.tab1Expenses"
android:background="#android:color/darker_gray">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:keepScreenOn="true" />
<Button
android:text="Add Expense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnEx"
android:layout_gravity="bottom|center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.Switch"
android:background="#android:color/black"
android:textColor="#android:color/holo_green_light"
android:textColorLink="#android:color/holo_green_light" />
</FrameLayout>
I solved my problem. It was to do with the height of the relative layout for the recycler items. It was on match parent, i switched to wrap content and it now displays the inputs on the screen
Related
I am populating Recycler View with a List of 7 string items but Recycler view only loads two of them
by the way my data is long text and it loads all items when text is short
this is my ContentAdapter.java
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private LayoutInflater mInflater;
private List<String> mContent;
ContentAdapter(Context context, List<String> Content) {
this.mInflater = LayoutInflater.from(context);
mContent = Content;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.rvcontent_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ContentAdapter.ViewHolder holder, int position) {
//loads only two times !?
holder.txtContentPage.setText(mContent.get(position));
}
#Override
public int getItemCount() {
return mContent.size(); // size is 7
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txtContent;
ViewHolder(View itemView) {
super(itemView);
txtContent = itemView.findViewById(R.id.txtContent);
}
}
}
rvcontent_item.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
and Activity
rvContents.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
rvContents.setLayoutManager(layoutManager);
ContentAdapter contentAdapter = new ContentAdapter(this, Data); // Data has 7 items
rvContents.setAdapter(contentAdapter);
and this is my layout activity xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Try with
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:nestedScrollingEnabled="false"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
The issue is
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Here android:layout_height="match_parent" is a bug, since you are inside a scroll view your recyclerview should have height wrap_content. So that the parent will allow it to scroll.
Try changing ConstraintLayout height match_parent instead of wrap_content.
and if persists, Remove scrollView from root because RecyclerView itself makes layout scrollable.
I've been struggling to figure out this weird bug for a day and a half. Whenever I click on an EditText that is a child of an ExpandableListView, the screen gets obscured such that the user is unable to see what is being typed, as shown in the gif below: \n
https://giant.gfycat.com/GenuineUnluckyHyrax.mp4
I started digging into the Hierarchy View to see what could be doing this, here is a screenshot of it:
https://i.imgur.com/B4kkHv7.png
To me it looks like the Toolbar up top is extending downwards to cover the screen, which is odd because it doesnt do that on other activities that contain EditText widgets. I've checked that parent views are using "wrap_content" for height instead of "match_parent" as well, except for my DrawerLayout which must match_parent. Below is the layout code for the activity:
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rlcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<!--SideBar views -->
<RelativeLayout
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#color/sidebarColor">
<ExpandableListView
android:id="#+id/qwedit_sidebar_elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
<!--MainView views -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView
android:id="#+id/qwedit_main_elv"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp">
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:layout_marginTop="25dp"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
Here is the EVL's group layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/qwedit_sidebar_elv_parent_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:padding="3dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:textSize="20sp"
android:text="Category"/>
<ImageView
android:id="#+id/qwedit_elv_parent_status"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="35dp"
android:src="#drawable/ic_warning"/>
<TextView
android:id="#+id/qwedit_elv_parent_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_gravity="left"
android:gravity="right"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:text="Score"/>
</LinearLayout>
and the item layout
<?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">
<LinearLayout
android:id="#+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/badScore">
<TextView
android:id="#+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/mediocreScore">
<TextView
android:id="#+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/goodScore">
<TextView
android:id="#+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/worldclassScore">
<TextView
android:id="#+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="#+id/qwedit_elv_item_score_container"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Comments"/>
</RelativeLayout>
I'm assuming that this is all due to the layouts, but here is the CustomExpandableListAdapter that I wrote for it just in case the answer lies in how the views are being inflated:
/**
* Created by Tadhg on 9/8/2017.
*/
public class QwEditMainCustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
//Values inits
private int categoryScore;
private int subCategoryScore;
private boolean completedCategory;
private boolean completedSubCategory;
// ChildView views
private View badScoreView;
private View mediocreScoreView;
private View goodScoreView;
private View worldclassScoreView;
TextView badScoreTextView;
TextView mediocreScoreTextView;
TextView goodScoreTextView;
TextView worldClassScoreTextView;
EditText questionCommentEditText;
public QwEditMainCustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail
.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_item, null);
}
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
}
});
/**
* Use variables categoryScore and subCategoryScore to update scores
*/
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.qwedit_main_elv_parent_title);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() { return false; }
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) { return true; }
}
Any points or tips would be great. I think that this may be due to the number of views in my hierarchy (or at least a subissue of it), but I'm not sure and am relatively new to Android Development.
Thanks!
I solved the issue, simply adding android:windowSoftInputMode="adjustNothing|stateHidden" caused the ActionBar to not expand to 996px when the EditText gained focus
I'm developing a material design app. I want to display a list using RecyclerView in HelpActivity.
The problem is that I'm getting the following errors: java.lang.RuntimeException: java.lang.IllegalStateException: RecyclerView has no LayoutManager on line (HelpActivity.java:28).
Here's HelpActivity.java file's code:
public class HelpActivity extends AppCompatActivity {
public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.Adapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
mRecyclerView = (RecyclerView) findViewById(R.id.helpActivityContent);
// use a linear layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
mAdapter = new HelpContentAdapter(helpContents);
mRecyclerView.setAdapter(mAdapter);
initializeData();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#2196F3"));
setSupportActionBar(toolbar);
/*final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(upArrow);*/
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
class HelpActivityContent {
String option;
String subOption;
int icon;
HelpActivityContent(String option, String subOption, int icon) {
this.option = option;
this.subOption = subOption;
this.icon = icon;
}
}
public List<HelpActivityContent> helpContents;
// This method creates an ArrayList that has three Person objects
// Checkout the project associated with this tutorial on Github if
// you want to use the same images.
private void initializeData(){
helpContents = new ArrayList<>();
helpContents.add(new HelpActivityContent("123", "", R.drawable.ic_action_a));
helpContents.add(new HelpActivityContent("123", "", R.drawable.ic_action_b));
helpContents.add(new HelpActivityContent("321", "111", R.drawable.ic_action_c));
helpContents.add(new HelpActivityContent("1", "2", R.drawable.ic_action_d));
}
public class HelpContentAdapter extends RecyclerView.Adapter<HelpContentAdapter.HelpContentViewHolder>{
public class HelpContentViewHolder extends RecyclerView.ViewHolder {
TextView option;
TextView subOption;
ImageView icon;
HelpContentViewHolder(View itemView) {
super(itemView);
option = (TextView)itemView.findViewById(R.id.option);
subOption = (TextView)itemView.findViewById(R.id.subOption);
icon = (ImageView)itemView.findViewById(R.id.optionIcon);
}
}
List<HelpActivityContent> helpContents;
HelpContentAdapter(List<HelpActivityContent> helpContents){
this.helpContents = helpContents;
}
#Override
public int getItemCount() {
return helpContents.size();
}
#Override
public HelpContentViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_help, viewGroup, false);
HelpContentViewHolder pvh = new HelpContentViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(HelpContentViewHolder helpContentViewHolder, int i) {
helpContentViewHolder.option.setText(helpContents.get(i).option);
helpContentViewHolder.subOption.setText(helpContents.get(i).subOption);
helpContentViewHolder.icon.setImageResource(helpContents.get(i).icon);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
supportFinishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here's content_help.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/helpActivityContent"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_help"
tools:context="com.abc.xyz.HelpActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/optionIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option"
android:layout_toRightOf="#+id/optionIcon"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/subOption"
android:layout_toRightOf="#+id/optionIcon"
android:layout_below="#+id/option"
/>
</RelativeLayout>
</android.support.v7.widget.RecyclerView>
I'm unable to figure out what the problem is.
Please let me know.
Thanks in advance.
You cannot put your child elements directly inside the recycler view.
You need to create separate row layout like the one you create in listview.
Just remove the xml code written inside recycler view and insert it inside row_layout xml file and then inflate that row layout file inside your adapter instead.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_help"
tools:context="com.abc.xyz.HelpActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/optionIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option"
android:layout_toRightOf="#+id/optionIcon"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/subOption"
android:layout_toRightOf="#+id/optionIcon"
android:layout_below="#+id/option"
/>
</RelativeLayout>
Just create separate file for row_layout containing above content and inflate it like below inside your oncreateviewholder:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.**<new_layout_you_will_create>**, viewGroup, false);
I just made a small change in your content_help.xml and it works fine for me..
content_help.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_help"
tools:context="com.abc.xyz.HelpActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/optionIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option"
android:layout_toRightOf="#+id/optionIcon"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/subOption"
android:layout_toRightOf="#+id/optionIcon"
android:layout_below="#+id/option"
/>
If you are using the recyclerview in activity_help.xml, no need to use the same for your row-content.
Hope this will solve your problem
I've been spending a few hours on a problem and I still can't figure it out. The setOnClickListener in HotelOverviewFragment is not firing when I click an item in my ListView. However, the setOnClickListener does work from my custom adapter (NowArrayAdapter).
My question is: why the setOnClickListener not working in HotelOverviewFragment (Class where the ListView is shown)?
Here's a list of what I've tried:
Setting android:focusable="false", android:focusableInTouchMode="false", android:descendantFocusability="blocksDescendants" in the hotel_row_layout.xml and fragment_hotel_overview.xml.
Setting android:clickable on true and false. Both didn't work.
Changing from BaseAdapter implementation to arrayAdapter.
I tried different listeners in HotelOverviewFragment: setOnClickListener, setOnItemClickListener, setOnItemSelectedListener and setOnTouchListener. Unfortunately, none of those worked for me.
Here's my code:
Custom adapter
public class NowArrayAdapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
private TextView item;
public NowArrayAdapter(Context context, ArrayList<String> commandsList) {
super(context, R.layout.hotel_row_layout, commandsList);
this.context = context;
values = new ArrayList<String>();
values.addAll(commandsList);
typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
inflater = LayoutInflater.from(this.context);
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public View getView(int position, View convertView, ViewGroup parent) {
String myText = getItem(position);
if(convertView == null) {
convertView = inflater.inflate(R.layout.hotel_row_layout, parent, false);
item = (TextView) convertView.findViewById(R.id.maps_button);
item.setTypeface(typeface);
convertView.setTag(item);
} else {
item = (TextView) convertView.getTag();
}
item.setText(myText);
//myListItem.descText.setTextSize(14);
convertView.setOnClickListener(new View.OnClickListener() {
// WORKS!
#Override
public void onClick(View view) {
Log.d("click", "Don't look at me!");
}
});
return convertView;
}
}
Fragment
public class HotelOverviewFragment extends Fragment {
private static Hashtable fontCache = new Hashtable();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hotel_overview, container, false);
ListView list = (ListView) v.findViewById(android.R.id.list);
// Set up listview and buttons
setUp(v, list);
// Inflate the layout for this fragment
return v;
}
static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}
public void setUp(View v, ListView l){
TextView address = (TextView) v.findViewById(R.id.content);
TextView header = (TextView) v.findViewById(R.id.header);
TextView hotelName = (TextView) v.findViewById(R.id.hotelName);
Typeface typeface = getTypeface(getActivity(), "fonts/Roboto-Light.ttf");
address.setText("some street \nZipCode, City \nCountry \nEmail \nphoneNumber");
address.setTypeface(typeface);
header.setText("Hotel info");
header.setTypeface(typeface);
header.setTextSize(20);
hotelName.setText("Hotel name");
hotelName.setTypeface(typeface);
// Set up button
ArrayList<String> n = new ArrayList<String>();
n.add(0, "More info");
// Show button
NowArrayAdapter adapter = new NowArrayAdapter(getActivity(), n);
l.setAdapter(adapter);
// THIS IS THE STUFF I'VE BEEN TRYING
try {
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("click", "Success");
}
});
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
});
l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
l.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("click", "Success");
return false;
}
});
}catch (Exception e){
Log.d("click", e + "");
}
}
}
Layout xml of HotelOverviewFragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example"
android:background="#ffebebeb">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:src="#drawable/banner"
android:id="#+id/hotelBanner"
android:layout_gravity="top"
android:adjustViewBounds="false"
android:scaleType="fitXY" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hotelname"
android:id="#+id/hotelName"
android:gravity="center"
android:textSize="40dp"
android:layout_alignBottom="#+id/hotelBanner"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:textColor="#ffc4c4c4"
android:layout_marginBottom="5dp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:background="#drawable/header_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Header"
android:layout_toRightOf="#+id/headerImage"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/headerImage"
android:src="#drawable/ic_action_live_help"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="#drawable/content_card">
<TextView
android:id="#+id/content"
android:layout_gravity="left|center_vertical"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
The custom xml for a listview item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="#drawable/content_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/maps_button"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<ImageView
android:src="#drawable/ic_action_arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="false"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout>
Thanks in advance.
Thanks to Daniel Nugent's suggestion I got it working. Removing the convertView.setOnClickListener() is part of the answer. I think it was blocking the other listeners in the HotelOverviewFragment.
My next mistake was that I used setOnClickListener on a ListView for testing.
setOnClickListener should be used for buttons not ListViews.
So after removing setOnClickListener all the other listeners started working.
Thanks for your time.
i am a beginer android-developer and i need some help:
There is a Fragment in my Project - TasksFragment, there is a ListView element on it. On ListView, using ArrayAdapter Items are formed by a custom layout for Item and then added to ListView. The problem is, that ListView don't react on click processing events.
Here are the code fragments:
TaskAdapter class...
public class TaskAdapter extends ArrayAdapter<Task> {
public TaskAdapter(ArrayList<Task> tasks) {
super(getActivity(), 0, tasks);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_task, null);
}
Task task = getItem(position);
TextView titleTextView = (TextView) convertView.findViewById(R.id.task_list_item_titleTextView);
titleTextView.setText(task.getTitle());
CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.task_list_item_solvedCheckBox);
solvedCheckBox.setChecked(task.isSolved());
return convertView;
}
}
ItemClick fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tasks_fragment, null);
mTasks = TaskLab.get(getActivity()).getTasks();
final ListView lvMain = (ListView) view.findViewById(R.id.listViewTasks);
lvMain.setClickable(true);
TaskAdapter adapter = new TaskAdapter(mTasks);
setHasOptionsMenu(true);
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Task task = (Task)lvMain.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationContext(), task.getTitle() + " was clicked", Toast.LENGTH_LONG).show();
Intent i = new Intent(getActivity(), AddTaskActivity.class);
startActivity(i);
}
});
return view;
}
XML Layout for List_Item_Task
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentRight="true"
android:enabled="false"
android:padding="4dp"/>
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="22dp"/>
</RelativeLayout>
Xml layout for tasks_fragment.xml
<?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"
android:background="#3f638b">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTasksFragmentHeader"
android:text="Задачи"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewTasks" />
</LinearLayout>
Yes your listview is not clickable.
Just add this in your checkbox layout
android:focusable="false"
Your items xml should look like this
<?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="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/task_list_item_solvedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" //Add this
android:layout_alignParentRight="true"
android:gravity="center"
android:padding="4dp" />
<TextView
android:id="#+id/task_list_item_titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/task_list_item_solvedCheckBox"
android:paddingBottom="5dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="5dp"
android:textSize="22dp"
android:textStyle="bold" />
Now it should work. I have tested it :)