I use ViewPager2 to create a Horizontal Scrollable layout. Now when an item of ViewPager2 is clicked a CustomTab intent is launched to load a URL, on Exiting the CustomTab Screen ViewPager2 malfunctions, the parent layout takes the height of the whole screen, and the content of ViewPager2 disappears, Also the same fragment also contains a Linear Recylerivew which works fine. I am unable to diagnose the problem.
Here is a brief view of problem
fragment_courses.xml
<FrameLayout
style="#style/parent.contentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/tickinsidecircle" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/normalPadding"
android:paddingTop="#dimen/normalPadding"
android:paddingEnd="60dp"
android:paddingBottom="#dimen/normalPadding" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/question_posts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item_question_post" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
FragmentCourses.java
package com.example.increaseyouriq.ui.fragments;
public class FragmentCourses extends Fragment implements MatchCourseClickListener, PostClickListener {
private static final String LOG_TAG = "FragmentCourses";
FragmentCoursesBinding binding;
private FirestoreScorecardViewModel scorecardViewModel;
private QuestionPost questionPost;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return binding.getRoot();
}
private void loadCourses(UserScorecard scorecard) {
CollectionReference coursesRef = FirebaseFirestore.getInstance().collection(COURSES);
FirestoreRecyclerOptions < MatchCourse > courses = new FirestoreRecyclerOptions.Builder < MatchCourse > ()
.setQuery(coursesRef, MatchCourse.class)
.setLifecycleOwner(getViewLifecycleOwner()).build();
CourseAdapter courseAdapter = new CourseAdapter(courses, getContext(), this);
binding.viewPager.setAdapter(courseAdapter);
binding.viewPager.setOffscreenPageLimit(1);
int nextItemVisiblePx = (int) getResources().getDimension(R.dimen.viewpager_next_item_visible);
int currentItemHorizontalMarginPx = (int) getResources().getDimension(R.dimen.viewpager_current_item_horizontal_margin);
binding.viewPager.setPageTransformer(new SnappyPageTransformer(nextItemVisiblePx, currentItemHorizontalMarginPx));
binding.viewPager.addItemDecoration(new HorizontalMarginItemDecoration(
requireContext(), R.dimen.viewpager_current_item_horizontal_margin_testing,
R.dimen.viewpager_next_item_visible_testing));
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
});
}
item_viewpager
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cardViewCourse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:cardCornerRadius="15dp"
app:cardPreventCornerOverlap="true"
>
<LinearLayout
android:id="#+id/shopperCardBacground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/gradient_nelson"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_cantidad_cursos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
<TextView
android:id="#+id/tv_participants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="180dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/android_developer" />
<TextView
android:id="#+id/course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="#string/fake_course_title" />
</LinearLayout>
</androidx.cardview.widget.CardView>
And the last part of Puzzle CourseAdapter.java
package com.example.increaseyouriq.ui.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.example.increaseyouriq.databinding.ItemShopCardBinding;
import com.example.increaseyouriq.ui.listeners.MatchCourseClickListener;
import com.example.increaseyouriq.ui.model.MatchCourse;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.squareup.picasso.Picasso;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class CourseAdapter extends FirestoreRecyclerAdapter < MatchCourse, CourseAdapter.ViewHolder > {
private final MatchCourseClickListener matchCourseClickListener;
public CourseAdapter(FirestoreRecyclerOptions < MatchCourse > options, Context context, MatchCourseClickListener listener) {
super(options);
this.matchCourseClickListener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ItemShopCardBinding itemShopCardBinding = ItemShopCardBinding.inflate(inflater, parent, false);
return new ViewHolder(itemShopCardBinding);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull MatchCourse model) {
holder.binding.cardViewCourse.setOnClickListener(view - > matchCourseClickListener.onScrollPagerItemClick(model));
holder.setData(model);
}
static class ViewHolder extends RecyclerView.ViewHolder {
ItemShopCardBinding binding;
ViewHolder(ItemShopCardBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void setData(MatchCourse matchCourse) {
binding.tvParticipants.setText(String.valueOf(matchCourse.getNumberOfParticipants()));
binding.courseTitle.setText(matchCourse.getName());
if (matchCourse.getImageResource().isEmpty());
else
Picasso.get().load(matchCourse.getImageResource()).into(binding.image);
}
}
}
Related
When user clicks on icon adjacent to the item in spinner, some action has to be performed. i have not added functionality for button click handler. Before that i am facing below issue. For button with icon, i have used ImageButton in Spinner.
But when item in spinner is selected, all the items are shown and list down is not closing.
I am using Android api 28 to test.
MainActivity.java
package com.example.ravispinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<FileItem> mList;
private FileAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
Spinner spinnerCountries = findViewById(R.id.spinner_countries);
mAdapter = new FileAdapter(this, mList);
spinnerCountries.setAdapter(mAdapter);
spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FileItem clickedItem = (FileItem) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getName();
Toast.makeText(MainActivity.this, clickedCountryName + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void initList() {
mList = new ArrayList<>();
mList.add(new FileItem("Edit", R.drawable.ic_action_edit));
mList.add(new FileItem("New", R.drawable.ic_action_new));
mList.add(new FileItem("Remove", R.drawable.ic_action_remove));
}
}
FileAdapter
package com.example.ravispinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
public class FileAdapter extends ArrayAdapter<FileItem> {
public FileAdapter(Context context, ArrayList<FileItem> countryList) {
super(context, 0, countryList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.file_spinner_row, parent, false
);
}
ImageButton imageViewFlag = convertView.findViewById(R.id.image_view_flag);
TextView textViewName = convertView.findViewById(R.id.text_view_name);
FileItem currentItem = getItem(position);
if (currentItem != null) {
//imageViewFlag.setImageResource(currentItem.getFlagImage());
textViewName.setText(currentItem.getName());
}
return convertView;
}
}
FileItem
package com.example.ravispinner;
public class FileItem {
private String mName;
private int mFlagImage;
public FileItem(String name, int flagImage) {
mName = name;
mFlagImage = flagImage;
}
public String getName() {
return mName;
}
public int getFlagImage() {
return mFlagImage;
}
}
file_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_view_flag"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toEndOf="#+id/image_view_flag"
android:gravity="center"
android:text="India"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="#+id/icon"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_remove"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ravispinner.MainActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow android:layout_margin="2dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="2dp"
android:padding="3dp"
android:text="Test Scenario\t: "
android:textSize="20dp" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp" />
</TableRow>
</TableLayout>
</RelativeLayout>
This is one more solution to achieve UI try once may it helps you.
change your file_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center|start"
android:text="India"
android:textColor="#android:color/black"
android:textSize="16sp" />
<ImageView
android:id="#+id/image_view_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scaleType="center"
android:src="#drawable/ic_baseline_remove_circle_24" />
</LinearLayout>
and activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight="0.5"
android:text="Test Scenario:"
android:textSize="20sp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:spinnerMode="dropdown" />
</LinearLayout>
I fetch data from database and put it to RecyclerView. My database arrives 4 items I see it with log debug then I send it to my adapter. However I see only first item on my screen.
my adapter ListArticleAdapter
package uz.sherdevs.alishernavoiy.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import uz.sherdevs.alishernavoiy.R;
import uz.sherdevs.alishernavoiy.model.Article;
public class ListArticleAdapter extends RecyclerView.Adapter<ListArticleAdapter.MyViewHolder> {
private Context context;
private List<Article> articleList;
public ListArticleAdapter(Context context, List<Article> articleList) {
this.context = context;
this.articleList = articleList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_list, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.tv_title.setText(articleList.get(position).getTitle());
}
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_title;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.article_title);
}
}
}
MainActivity
package uz.sherdevs.alishernavoiy;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import uz.sherdevs.alishernavoiy.adapter.ListArticleAdapter;
import uz.sherdevs.alishernavoiy.database.SQLiteDBHelper;
import uz.sherdevs.alishernavoiy.model.Article;
public class MainActivity extends AppCompatActivity {
private RecyclerView articleRecyclerView;
private ListArticleAdapter adapter;
private List<Article> articleList;
private SQLiteDBHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articleRecyclerView = (RecyclerView)findViewById(R.id.articles_recyclerview);
dbHelper = new SQLiteDBHelper(this);
articleList = dbHelper.getArticles();
// for (Article article : articleList) {
// Log.d("list", article.getTitle());
// }
articleRecyclerView.setHasFixedSize(true);
adapter = new ListArticleAdapter(this, articleList);
// Log.d("list", articleList.toString());
articleRecyclerView.setAdapter(adapter);
articleRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/background_icon"
>
<ImageView
android:src="#drawable/background_regtangle_green"
android:scaleType="centerCrop"
android:id="#+id/circle_background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintWidth_percent="1"
app:layout_constraintVertical_bias="0"
/>
<TextView
android:text="Dostonlar"
android:textColor="#FFDF6A"
android:textSize="60sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#id/circle_background"
app:layout_constraintLeft_toLeftOf="#id/circle_background"
app:layout_constraintRight_toRightOf="#id/circle_background"
app:layout_constraintBottom_toBottomOf="#id/circle_background"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintVertical_bias="0.3"
android:fontFamily="#font/myfont"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/articles_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="?attr/actionBarSize">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardview1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.15"
app:layout_constraintWidth_percent="0.8"
app:cardCornerRadius="20dp"
app:layout_constraintVertical_bias="0.23"
app:cardElevation="20dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/article_title"
android:textSize="45sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.6"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
I got 4 Objects from my database with command
articleList = dbHelper.getArticles();
However I see only first one on the screen.
Moreover as you see I loged in my adapter
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
That size also shows 4.
In item_list.xml, change android:layout_height="match_parent" with android:layout_height="wrap_content"
Hello I am developing an app that fetches data from the server the code is shown below
XML:-
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImageQuiz">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e2e2e2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer these questions"
android:textSize="10dp"
android:textAlignment="center"
android:padding="5dp"
android:textColor="#fff"
android:background="#05af43"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recylcerViewImage"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Submit"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textColor="#585858"
android:background="#drawable/rounded_corner"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
ImageQuizAdapter.java
package com.accolade.eventify;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import com.bumptech.glide.Glide;
import java.util.List;
public class ImageQuizAdapter extends RecyclerView.Adapter<ImageQuizAdapter.ImageQuizViewHolder> {
private Context mCtx;
private List<ImageQuizModel> quizList;
public ImageQuizAdapter (Context mCtx, List<ImageQuizModel> quizList) {
this.mCtx = mCtx;
this.quizList = quizList;
}
#Override
public ImageQuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_recycler_image_quiz, null);
return new ImageQuizViewHolder(view);
}
#Override
public void onBindViewHolder(ImageQuizViewHolder holder, int position) {
ImageQuizModel imageQuizModel=quizList.get(position);
Glide.with(mCtx)
.load(imageQuizModel.getImage())
.into(holder.imageView);
//here i used only image
}
#Override
public int getItemCount() {
return quizList.size();
}
public class ImageQuizViewHolder extends RecyclerView.ViewHolder {
RadioButton r1,r2,r3,r4;
ImageView imageView;
public ImageQuizViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView2);
r1 = itemView.findViewById(R.id.radio_button1);
r2 = itemView.findViewById(R.id.radio_button2);
r3 = itemView.findViewById(R.id.radio_button3);
r4 = itemView.findViewById(R.id.radio_button4);
}
}
}
ImagaQuiz.java
package com.accolade.eventify;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ImageQuiz extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://accoladetest.cf/MyApi/MyApiQuizPic.php";
RecyclerView recyclerView;
private Toolbar mTopToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_quiz);
mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar1);
setSupportActionBar(mTopToolbar);
recyclerView = findViewById(R.id.recylcerViewImage);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadProducts();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_favorite) {
Toast.makeText(this, "Add Feature", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadProducts() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
List<ImageQuizModel> data=new ArrayList<>();
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for(int i=0;i< array .length();i++){
JSONObject json_data = array .getJSONObject(i);
ImageQuizModel product = new ImageQuizModel();
product.id= json_data.getInt("id");
product.image= json_data.getString("image");
product.op1= json_data.getString("op1");
product.op2= json_data.getString("op2");
product.op3= json_data.getString("op3");
product.op4= json_data.getString("op4");
data.add(product);
}
//creating adapter object and setting it to recyclerview
ImageQuizAdapter adapter = new ImageQuizAdapter(ImageQuiz.this, data);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
ImageQuizModel.java
package com.accolade.eventify;
public class ImageQuizModel {
public int id;
public String image;
public String op1;
public String op2;
public String op3;
public String op4;
public ImageQuizModel(){
}
public String getImage() {
return image;
}
public int getId(){
return id;
}
public String getOp1() {
return op1;
}
public String getOp2() {
return op2;
}
public String getOp3() {
return op3;
}
public String getOp4() {
return op4;
}
}
layout_recycler_image_guiz.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:elevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button1"
android:text=" Option 1"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button2"
android:text="Option 2"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button3"
android:text="Option 3"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button4"
android:text="Option 4"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
But here the problem is recycler view is displaying in half screen as the image below, I want its width to matchparent
enter image description here
I removed ConstraintLayout and instead I used RelativeLayout as a parent and it is working, Thank you guys for responding.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
</LinearLayout>
</RelativeLayout>
Go to your imageview which you are using and make sure attributes layout height and width both are set to match_parent. Hope this will help.
You should not use match_parent for widgets within a ConstraintLayout. From the documentation:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
If you want a widget to span 100% across its parent, the way to do that in ConstraintLayout is to set the width of the widget to 0dp and specify a start and end constraint as follows for the RelativeLayout which is a child of the ConstraintLayout:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#e2e2e2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
In short, all children of ConstraintLayout should be constrained vertically and horizontally and match_parent should not be used.
I got problems on the recycler view alignment. Each time I'm creating a recycler view list, the alignment for horizontal or vertical is not really straight.
Based on the red line I've marked, showing the inconsistency of my CardView # Layout view alignment.
My expected outcome is that recycler view list is in a straight line, either vertical or horizontal.
My current display output:
My current codes for the recycler view list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_holo_light_frame"
android:padding="4dp">
<android.support.v7.widget.CardView
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="#+id/donut_progress"
custom:donut_progress="30"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="40"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/chapterNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter 1"
android:textSize="18sp" />
<TextView
android:id="#+id/chapterTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Standard Form"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
MainActivity.java
adapterListChapters = new AdapterChapterList(chaptersList);
recyclerViewChapters = findViewById(R.id.recyclerViewChapterView);
recyclerViewChapters.setHasFixedSize(true);
recyclerViewChapters.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewChapters.addItemDecoration(new SpacingItemDecoration(2, Tools.dpToPx(this, 8), true));
recyclerViewChapters.setLayoutManager(layoutManager);
recyclerViewChapters.setItemAnimator(new DefaultItemAnimator());
recyclerViewChapters.setAdapter(adapterListChapters);
adapterListChapters.setOnClickListener(new AdapterChapterList.OnClickListener() {
#Override
public void onItemClick(View view, ChapterList obj, int pos) {
final Snackbar snackbar = Snackbar.make(parent_view_chapter, "Item " + obj.chapterTitle + " clicked", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
AdapterChapterList.java
package xxx.xxxx.components.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.material.components.R;
import com.material.components.model.ChapterList;
import java.util.ArrayList;
import java.util.List;
public class AdapterChapterList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public List<ChapterList> chapterLists = new ArrayList<>();
private OnClickListener onClickListener = null;
public AdapterChapterList(List<ChapterList> chapterLists) {
this.chapterLists = chapterLists;
}
public class OriginalViewHolder extends RecyclerView.ViewHolder{
public View lyt_parent_chapter_list;
public TextView chapterNumber,chapterTitle;
public DonutProgress learningProgress;
public OriginalViewHolder(View itemView) {
super(itemView);
chapterNumber = itemView.findViewById(R.id.chapterNumber);
chapterTitle = itemView.findViewById(R.id.chapterTitle);
lyt_parent_chapter_list = itemView.findViewById(R.id.lyt_parent_chapter_list);
learningProgress = itemView.findViewById(R.id.donut_progress);
learningProgress.setTextSize(40);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chapter_list,parent,false);
vh = new OriginalViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof OriginalViewHolder)
{
OriginalViewHolder view = (OriginalViewHolder) holder;
final ChapterList c = chapterLists.get(position);
view.chapterNumber.setText(c.chapterNumber);
view.chapterTitle.setText(c.chapterTitle);
view.learningProgress.setDonut_progress(String.valueOf(c.learningProgress));
view.lyt_parent_chapter_list.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onClickListener == null) return;
onClickListener.onItemClick(v,c,position);
}
});
}
}
#Override
public int getItemCount() {
return chapterLists.size();
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public interface OnClickListener {
void onItemClick(View view, ChapterList obj, int pos);
}
}
Thanks!
In Recycler view listItem
1.Using layout_weight requires layout_width to be 0dp t function properly
Here Is Updated XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_holo_light_frame"
android:padding="4dp">
<android.support.v7.widget.CardView
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="#+id/donut_progress"
custom:donut_progress="30"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/chapterNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chapter 1"
android:textSize="18sp" />
<TextView
android:id="#+id/chapterTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Standard Form"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
In case If Above Layout Doesn't Solve Your Problem Try Removing this line of code
recyclerViewChapters.addItemDecoration(new SpacingItemDecoration(2, Tools.dpToPx(this, 8), true));
My image is not showing in the viewpager, I have the xml with viewpager at the top and recyclerview at the bottom.
I do not know if I need to do implementation with interfaces. I follow the image slider example from here at Android Studio Tutorial - 70 - Implement Swipe Views
public class Home_activity extends BaseActivity {
ViewPager viewPager;
TextView testview;
private static final String urlHome =
"http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) findViewById(R.id.viewPager);
SwipeAdapter swipeAdapter = new
SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(swipeAdapter);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
<--- PageFagment.java--->
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
public PageFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, Bundle savedInstanceState) {
TextView textView;
View view = inflater.inflate(R.layout.image_fragment_layout, container,
false);
textView = (TextView) view.findViewById(R.id.textViewFragment);
Bundle bundle = getArguments();
final String message = Integer.toString(bundle.getInt("count "));
textView.setText("This id the " + message);
return view;
}
}
<--- SwipeAdapter.java--->
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SwipeAdapter extends FragmentPagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int i) {
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("count ", i + 1);
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
this is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/topbuttonlinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/topbuttonlinearLayout"
android:layout_weight="7">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<include layout="#layout/bottom_navigation_layout" />
</FrameLayout>
</LinearLayout>