I have a RecyclerView
that has CardView
to display items. The items contain image views and TextView
that would be populated once the user selects an item. Once they select an item I retrieve the selected
item's background and fire an intent to open a new activity, in which I create a layout dynamically and
set the selected item's background as the layout's background. This is my adapter class that will
populate my RecyclerView
where I retrieve the background of the selected item.
public class PersonsAdapter extends RecyclerView.Adapter<PersonsAdapter.MyViewHolder> {
private List<Person> mpersonsList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name .....;
View v;
Person p;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.tvName);
//and all other textviews....
v = view;
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if (view.getId() == getAdapterPosition()) {
//int itemposition = mRecyclerView.indexOfChild(v);
Intent i = new Intent(context, SelectedTemplate.class);
startActivity(i);
theBackground = view.getBackground();//retrieve the background of the selected item
//}
}
});
List<Drawable> templatesList = new ArrayList<>();
}
}
public PersonsAdapter(List<Person> personsList) {
this.mpersonsList = personsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.person, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Person person = mpersonsList.get(position);
holder.name.setText(person.getName());
//and other details of the item......
holder.p = mpersonsList.get(position);
if(position == 0){
holder.v.setBackgroundResource(R.drawable.wavy_curves_onblack);
}
//and other background images.....
}
@Override
public int getItemCount() {
return mpersonsList.size();
}
}
In the next activity I retrieve the selected item as follows.
private void getSelectedTemplate() {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.selected_template, null);
// fill in any details dynamically here
//insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.selectedtemplatelayout);
insertPoint.setBackground(TheTemplates.theBackground);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
rl = (LinearLayout) findViewById(R.id.selectedtemplatelayout);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
image.getLayoutParams().height = (int) getResources().getDimension(R.dimen.image_height);
image.getLayoutParams().width = (int) getResources().getDimension(R.dimen.image_width);
name.setText("NAME");
name.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//add all other textviews dynamically.....
rl.removeAllViews();
rl.addView(image);
rl.addView(name);
//add all remaining textviews
}
When I test it in an emulator or a device that is less than 5.5inches I see very huge background(I can scroll forever) and I can only see the two images but not the textviews. In larger devices(both real and emulator) I get the right size background. But I don't see the textviews just like the smaller devices. I see the two images though.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"><!--this two lines will hide the keyboard and make the NAME textview visible -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/selectedtemplatelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="vertical" >
//this is the layout I would like to stuff all my textviews and images and //set the background of the selected item.
</LinearLayout>
</ScrollView>
//another layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is the RecyclerView
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">-->
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/templateList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
<!--</LinearLayout>-->
I can't seem to figure out why this is happening and how I could fix it. Any help is appreciated.