I'm sorry if my english isn't perfect.
I am learning Java and Android Studio since 2 weeks, i'm trying to add sliding tabs, and inside a tab, a RecyclerView with CardViews as a row item having a name and a picture.
My Sliding tabs works, and I can put a cardview in tabs, with a picture and a name. But when i use the RecyclerView, it doesn't work and i have an error. I can't resolve this problem since yesterday.
Here my program.
My Class Person
public class Person {
String name;
int photoId;
public Person(String name, int photoId) {
this.name = name;
this.photoId = photoId;
}
private List<Person> persons;
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Benjamin", R.drawable.team));
persons.add(new Person("Thomas", R.drawable.haltere));
}
//GETTER
public String getName() {
return this.name;
}
public int getPhoto() {
return this.photoId;
}
My Class Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardView);
personName = (TextView)itemView.findViewById(R.id.cardTitle);
personPhoto = (ImageView)itemView.findViewById(R.id.cardPhoto);
}
}
List<Person> persons;
public RVAdapter(List<Person> persons){
this.persons = persons;
}
#Override
public int getItemCount() {
return persons.size();
}
#Override
public PersonViewHolder onCreateViewHolder (ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
return new PersonViewHolder(v);
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
My Activity
public class ActivityHome extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"1","2","3","4"};
int Numboftabs =4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
ArrayList<Person> persons = new ArrayList<>();
RVAdapter rAdapter = new RVAdapter(persons);
rv.setAdapter(rAdapter);
rv.setLayoutManager(layoutManager);
}
(There is a few code after, but it only uses for the Sliding Tabs, and it works.)
My recycler_view.xml
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
My activity_home.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
And my cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#ffffff"
app:cardCornerRadius="5dp"
app:cardElevation="7dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityHome"
android:background="#ffffff">
<TextView
android:id="#+id/cardTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Card Title"
android:textColor="#000000"
android:textSize="17sp" />
<ImageView
android:id="#+id/cardPhoto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="50dp"
android:src="#drawable/haltere"
android:scaleType="center"
/>
</RelativeLayout>
My error :
Process: com.android.frutii.fitness5, PID:640 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.frutii.fitness5/com.android.frutii.fitness5.ActivityHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.android.frutii.fitness5.ActivityHome.onCreate(ActivityHome.java:45)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
I have read a lot of answers on internet but cannot get rid of this error.
Activity.findViewById() method is searching for my_recycler_view inside the layout which was set with setContentView(). So you should move RecyclerView to R.layout.activity_home layout.
Your RecyclerView with Id R.id.my_recycler_view does not exist in the activity_home layout. Use the R.layout.recycler as layout the contentView in setContentView();
I move my RecyclerView.xml, i put the code in activity_home.xml like this :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
</android.support.v4.view.ViewPager>
And now i'ts work partially, I have my 4 tabs, and I have a cardview too, but only one :( Why my second is not here ? Maybe this is not the good thing and I don't have to go in this direction.. ?
Related
I am using card view within recycler view and I want to change the color of each single card view means Every card view has different color like in the picture I'm using retrofit technique for fetching data. please tell me where to put what code as I'm new in java android TIA
This is my adapter class
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
public DataAdapter(Context context, List<Corona> dataList) {
this.context = context;
this.dataList = dataList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.data,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTv.setText(dataList.get(position).getName());
holder.cityTv.setText(dataList.get(position).getCity());
}
#Override
public int getItemCount() {
return dataList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView nameTv, cityTv;
public MyViewHolder(View itemView) {
super(itemView);
nameTv = (TextView) itemView.findViewById(R.id.tvName);
cityTv = (TextView) itemView.findViewById(R.id.tvCity);
}
}
}
here is XML of Card View
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
here is my main activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DataAdapter dataAdapter ;
private List<Corona> dataArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.recview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
getCoronaData();
}
private void getCoronaData() {
ApiInterface apiInterface;
apiInterface = ApiClient.getClient().create(ApiInterface.class);
dataArrayList = new ArrayList<>();
Call<List<Corona>> call = apiInterface.getCoronaDataForParsing();
call.enqueue(new Callback<List<Corona>>() {
#Override
public void onResponse(Call<List<Corona>> call, Response<List<Corona>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}
#Override
public void onFailure(Call<List<Corona>> call, Throwable t) {
}
});
}
}
first create integer.xml file under /res/values like below:
#Add as many color items as your want.
integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="color_array">
<item>#color/color1</item>
<item>#color/color2</item>
<item>#color/color3</item>
<item>#color/color4</item>
<item>#color/color5</item>
</integer-array>
Then, Inside RecyclerViewAdapter pass it through the constructor like below:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
private int[] colorList;
public DataAdapter(Context context, List<Corona> dataList){
this.context = context;
this.dataList = dataList;
colorList = context.getResources().getIntArray(R.array.color_array);
}
........
#Override
public void onBindViewHolder(.....){
......
//for setting the different colors to each cardview.
GradientDrawable drawable = (GradientDrawable) holder.cardLayout.getBackground();
drawable.setColor(colorList[position % colorList.length]);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
CardView cardLayout;
.........
public MyViewHolder(View itemView){
super(itemView)
.........
cardLayout = itemView.findViewById(R.id.cardLayout);
}
}
}
And, finally Inside XML of CardView provide id to Cardview as follow:
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
First of all set an ID to cardView in your XML file, then initialize cardView in MyViewHolder :
CardView cv = (CardView) itemView.findViewById(R.id.YOUR_CARDVIEW_ID);
for adding random colors, generate some colors and add them to a List and pass this list to the adapter.
now go to onBindViewHolder and add this code :
holder.cv.setCardBackgroundColor(YourList.get(i));
public class HomeFragment extends Fragment implements HomeView{
private HomeViewModel homeViewModel;
public static final String EXTRA_CATEGORY = "category";
public static final String EXTRA_POSITION = "position";
public static final String EXTRA_DETAIL = "detail";
#BindView(R.id.viewPagerHeader)
ViewPager viewPagerMeal;
#BindView(R.id.recyclerCategory)
RecyclerView recyclerViewCategory;
HomePresenter presenter;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
super.onCreate(savedInstanceState);
ButterKnife.bind(getActivity());
presenter = new HomePresenter(this);
presenter.getMeals();
presenter.getCategories();
return root;
}
#Override
public void showLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.VISIBLE);
}
#Override
public void hideLoading() {
getView().findViewById(R.id.shimmerMeal).setVisibility(View.GONE);
getView().findViewById(R.id.shimmerCategory).setVisibility(View.GONE);
}
#Override
public void setMeal(List<Meals.Meal> meal) {
ViewPagerHeaderAdapter headerAdapter = new ViewPagerHeaderAdapter(meal, getActivity());
viewPagerMeal.setAdapter(headerAdapter);
viewPagerMeal.setPadding(20, 0, 150, 0);
headerAdapter.notifyDataSetChanged();
headerAdapter.setOnItemClickListener((view, position) -> {
TextView mealName = view.findViewById(R.id.mealName);
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra(EXTRA_DETAIL,mealName.getText().toString());
startActivity(intent);
});
}
#Override
public void setCategory(List<Categories.Category> category) {
RecyclerViewHomeAdapter homeAdapter = new RecyclerViewHomeAdapter(category, getActivity());
recyclerViewCategory.setAdapter(homeAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3,
GridLayoutManager.VERTICAL, false);
recyclerViewCategory.setLayoutManager(layoutManager);
recyclerViewCategory.setNestedScrollingEnabled(true);
homeAdapter.notifyDataSetChanged();
homeAdapter.setOnItemClickListener((view, position) -> {
Intent intent = new Intent(getActivity(), CategoryActivity.class);
intent.putExtra(EXTRA_CATEGORY, (Serializable) category);
intent.putExtra(EXTRA_POSITION, position);
startActivity(intent);
});
}
#Override
public void onErrorLoading(String message) {
Utils.showDialogMessage(getActivity(), "Title", message);
}
}
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
}
}
fragment_home.xml
<android.support.design.widget.CoordinatorLayout 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"
tools:context=".ui.home.HomeFragment"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground">
<RelativeLayout
android:id="#+id/headerView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp">
<View
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#color/colorPrimary" />
<TextView
android:id="#+id/title"
android:textColor="#color/colorWhite"
android:text="#string/app_name"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
android:id="#+id/cardSearch"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="15dp"
app:cardBackgroundColor="#color/colorPrimaryDark"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:text="#string/search_your_recipes"
android:textColor="#color/colorPrimaryLight"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_search_primary_light"
android:drawableStart="#drawable/ic_search_primary_light"
android:drawablePadding="10dp"/>
</android.support.v7.widget.CardView>
<android.support.v4.view.ViewPager
android:id="#+id/viewPagerHeader"
android:foregroundGravity="center"
android:overScrollMode="never"
android:clipToPadding="false"
android:layout_below="#id/cardSearch"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="150dp" />
<include android:id="#+id/shimmerMeal"
layout="#layout/item_view_pager_header_shimmer" />
<TextView
android:id="#+id/titleCategory"
android:text="#string/meal_categories"
android:textSize="19sp"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/viewPagerHeader"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerCategory"
android:scrollbars="none"
android:layout_marginTop="10dp"
android:clipToPadding="false"
android:layout_below="#id/titleCategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="50dp" />
<include android:id="#+id/shimmerCategory"
layout="#layout/item_recycler_category_shimmer" />
<TextView
android:padding="10dp"
android:gravity="center"
android:textColor="#color/colorWhite"
android:text="#string/app_name"
android:background="#color/colorPrimaryLight"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Here is Logcat file:
2019-12-20 18:04:26.459 4910-4910/com.vikaskonaparthi.origin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vikaskonaparthi.origin, PID: 4910
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.vikaskonaparthi.origin.ui.home.HomeFragment.showLoading(HomeFragment.java:63)
at com.vikaskonaparthi.origin.ui.home.HomePresenter.getMeals(HomePresenter.java:29)
at com.vikaskonaparthi.origin.ui.home.HomeFragment.onCreateView(HomeFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:874)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6729)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Getting error with the shimmer in HomeFragmentActivity how to resolve this issue?
Should showloading() be kept in FragmentViewModel?
Since I had converted Activity to the fragment here.
The issue is not with the shimmer.
The issue is your getView() in getView().findViewById(R.id.shimmerMeal).setVisibility(View.VISIBLE);
is null.
If you are having your shimmer views in fragment then you need to call showLoading() after your fragments onViewCreated() method is called.
I think you must put everything that connects with layout, like constraints and others widget when onViewCreated() is called.
Call it below your onCreateView(). This error happens because of calling widget but the widget itself not yet exist or created by fragment. Widgets will exist in onViewCreated(). Tell me if it's work
I have recyclerview in activity_main.xml and I have there the image which shows to user - empty or not empty recyclerview. activity_main.xml:
<RelativeLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:id="#+id/main_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<LinearLayout
android:visibility="visible"
android:id="#+id/block_no_alarms"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/ic_nothing"
android:layout_width="match_parent"
android:layout_height="70dp" />
<TextView
android:text="#string/no_alarms"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
And I have recyclerview one item xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="7dp"
app:cardBackgroundColor="#color/colorDark"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_time_alarm_one_item"
android:textColor="#color/colorWhite"
android:layout_margin="15dp"
android:textSize="25sp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_description_alarm_one_item"
android:textColor="#color/colorWhite"
android:layout_toRightOf="#id/tv_time_alarm_one_item"
android:textSize="14sp"
android:maxLines="2"
android:layout_marginRight="60dp"
android:ellipsize="end"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/tv_time_alarm_one_item"
android:layout_marginEnd="60dp" />
<Switch
android:id="#+id/s_switch_alarm_one_item"
android:layout_alignParentRight="true"
android:checked="true"
android:layout_centerVertical="true"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
And I wanna during long press click to this cardview to delete one item from the recyclerview. Adapter:
public class AlarmsAdapterMain extends RecyclerView.Adapter<AlarmsAdapterMain.ViewHolder> {
private List<String> listTimes = new ArrayList<>();
private List<String> listDescriptions = new ArrayList<>();
private List<Boolean> listStarted = new ArrayList<>(); // checked a switch
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.alarm_item, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.time.setText(listTimes.get(i));
viewHolder.description.setText(listDescriptions.get(i));
viewHolder.aSwitch.setChecked(listStarted.get(i));
}
#Override
public int getItemCount() {
return listTimes.size();
}
public void deleteAll() {
listDescriptions.clear();
listTimes.clear();
listStarted.clear();
}
public void add(String time, String description) {
listTimes.add(time);
listDescriptions.add(description);
listStarted.add(true); // by default
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView time, description;
private Switch aSwitch;
ViewHolder(#NonNull final View itemView) {
super(itemView);
time = itemView.findViewById(R.id.tv_time_alarm_one_item);
description = itemView.findViewById(R.id.tv_description_alarm_one_item);
aSwitch = itemView.findViewById(R.id.s_switch_alarm_one_item);
// I try to set long listener here
}
}
}
I've done it, but I need to show or hide image or recyclerview if it need. For example, I have one alarm, user deletes it alarm and he sees empty layout but he must see image(LinearLayout) and hide recyclerview. So, how can I do it inside my adapter?
control your arrayLists from outside of your adapter, for example in your activity you should have a list of your data and pass the arrayList to your adapter's constructor, and manipulate your adapter data from activity and just notifyDataChange after any changes on your arrayList. so when you delete data or add some data, you can check your arrayList size in last line of your ad or delete operations (in your activity), and if arrayList size equals to 0 you should do changes on your UI.
also there is another way, you can have an interface that implements in your activity and it should have two methods for zeroMode or nonZeroMode and you can pass this interface to your adapter and invoke these methods when your arrayList size become zero or non-zero in your adapter.
i have builded an app that works perfectly on android with api higer than 21. The problem is that this instruction of my code:
mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
Works in different way on API less then 21.
On Api less then 21 the new fragment hide the previus activity, so that i can't click on my Floating Action Button.
Here are two images that explain in abetter way my problem.
API HIGER THAN 21
API LESS THAN 21
So my question is: How can i have the same result in API less then 21 that i have on API Higer then 21?
Here is the affected part of the Main Activity Code:
public class MainActivity extends AppCompatActivity {
public static AppDataBase appDataBase;
public static UserDataBase userDataBase;
static FragmentManager mFragmentManager;
static FragmentTransaction mFragmentTransaction;
private DrawerLayout myDrawerLayout;
final String TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE = "Add a console!";
TextView currentConsole;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appDataBase = new AppDataBase(this);
userDataBase = new UserDataBase(this);
myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
currentConsole = (TextView) findViewById(R.id.txt_Mainactvt_currentConsole);
currentConsole.setText(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE);
tabLayoutManagement();
floatingActionButtonManagement();
leftDrawerMenuManagement();
rigthDrawerMenuManagement();
populateMyConsole();
}
void tabLayoutManagement() {
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
}
// Floating Action Button
private void floatingActionButtonManagement() {
FloatingActionButton fab_addGame = (FloatingActionButton)findViewById(R.id.fab_AddGame);
fab_addGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*
The currentConsole TextView is used to show at the user wich console is selected.
We use it to have a strng that conteins the selected console.
Call the method that manage the click event of the FloatingActionButton. We pass the console name.
*/
String currentConsoleName = currentConsole.getText().toString();
floatingActionButtonClickEvent(currentConsoleName);
}
});
}
private void floatingActionButtonClickEvent(String currentConsoleName) {
/*
Check if user have added a console. If he did start a menu for adding games, else start an
error message
*/
if (!currentConsoleName.equals(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE)) {
popUpViewAddGameBuild(currentConsoleName);
}
else
mySimpleAlertDialogMethod("Attention!", "Before you enter game, you must enter a console.", true, true);
}
private void popUpViewAddGameBuild(String currentConsoleName) {
/*
Build the view that show the menu for adding games.
*/
LayoutInflater inflater = this.getLayoutInflater();
View popupView = inflater.inflate(R.layout.popupview_addgame, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popupView, 0, 0, 0);
}
Here is the TabLayout Class:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
public class MyAdapter extends FragmentPagerAdapter {
public int position;
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0 :
return new DesireFragment();
case 1 :
return new BuyedFragment();
case 2 :
return new StartedFragment();
case 3 :
return new FinishedFragment();
case 4 :
return new AllTrophiesFragment();
}
return null;
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Desire";
case 1 :
return "Buyed";
case 2 :
return "Started";
case 3 :
return "Finished";
case 4 :
return "AllTrophies";
}
return null;
}
}
}
There is the layout of the MainActivity:
<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:fitsSystemWindows="true"
android:layout_gravity="end"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/ic_menu_white_24dp"
app:title="MyGames">
<Button
android:id="#+id/btnOpenRigthDrawer"
android:background="#drawable/ic_filter_list_white_24dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:layout_gravity="right" />
<Button
android:id="#+id/btnOpenOptions"
android:background="#drawable/ic_settings_white_24dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="17dp"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollIndicators="bottom">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txt_Mainactvt_currentConsole"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="50dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_AddGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:layout_marginRight="17dp"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="bottom|right"
android:background="#color/colorPrimary" />
</FrameLayout>
<include
layout="#layout/drawer_left"
android:id="#+id/layLeft"
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
<include
layout="#layout/drawer_rigth"
android:id="#+id/layRigth"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:scrollbars="vertical"
/>
</android.support.v4.widget.DrawerLayout>
And here is the layout code of the TabLayout:
<?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:orientation="vertical"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
android:background="#color/colorPrimary"
app:tabIndicatorColor="#android:color/holo_orange_dark"
app:tabSelectedTextColor="#android:color/holo_orange_dark"
app:tabTextColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
You replace the content of your FrameLayout with an Fragment. this leads to your strange result.
Add a Layout to your Framelayout instead and use it as your FragmentContainer:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txt_Mainactvt_currentConsole"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="50dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_AddGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:layout_marginRight="17dp"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="bottom|right"
android:background="#color/colorPrimary" />
</FrameLayout>
Try to place your FrameLayout with this button to the end of layout
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