I can't find AppCompatTextView of ViewPager2(Capture Screen) - java

problem:
Problem
if I fix:
After
I want to see AppCompatTextView with Android-Studio.
How to see the AppCompatTextView?
Nothing Error! I don't know why..
Please help this problem..
activity_searchresult.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:orientation="vertical"
android:background="#color/cfb"
tools:context=".ResultActivity">
<FrameLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="#color/cfb"
tools:context=".ResultActivity">
<TextView
android:id="#+id/resultText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/result_text1"
android:textColor="#color/black"
android:textSize="23sp"
android:layout_marginLeft="30dp"
app:layout_constraintBottom_toTopOf="#+id/resultText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/resultText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/result_text2"
android:textColor="#color/black"
android:textSize="23sp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toTopOf="#+id/resultViewPager2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="#+id/resultFilterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="28dp"
android:background="#color/cfb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/dots_indicator"
app:srcCompat="#drawable/filter" />
<TextView
android:id="#+id/filterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginRight="40dp"
android:text="#string/filter"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resultFilterButton" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/resultViewPager2"
android:layout_width="270dp"
android:layout_height="270dp"
android:layout_marginBottom="190dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.3" />
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:id="#+id/dots_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
app:dotsColor="#color/white"
app:dotsCornerRadius="8dp"
app:dotsSize="14dp"
app:dotsSpacing="4dp"
app:dotsWidthFactor="2.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resultViewPager2"
app:layout_constraintBottom_toBottomOf="parent"
app:progressMode="true"
app:selectedDotColor="#color/black" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toTopOf="#+id/resultViewPager2"
app:srcCompat="#drawable/cafebot" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/navigationView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemTextColor="#drawable/bottom_icon_color"
app:itemIconTint="#drawable/bottom_icon_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:menu="#menu/bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</LinearLayout>
result_item_viewpager.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:id="#+id/result_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/result_menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#android:color/black"
android:textSize="26sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result_sample_image" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/result_cafe_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="23sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result_menu_name" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/result_product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#android:color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result_cafe_name" />
<ImageView
android:id="#+id/result_sample_image"
android:layout_width="270dp"
android:layout_height="270dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
ResultDataPage.java:
package com.example.myapplication;
public class ResultDataPage {
int resultImage;
String resultCafeName;
String resultMenuName;
int resultPrice;
public ResultDataPage(int image, String menuName, String cafeName, int price){
this.resultImage = image;
this.resultMenuName = menuName;
this.resultCafeName = cafeName;
this.resultPrice = price;
}
public int getImage(){
return resultImage;
}
public void setImage(int image) {
this.resultImage = image;
}
public String getMenuName(){
return resultMenuName;
}
public void setMenuName(String menuName) {
this.resultMenuName = menuName;
}
public String getCafeName() {
return resultCafeName;
}
public void setCafeName(String cafeName) {
this.resultCafeName = cafeName;
}
public int getPrice(){
return resultPrice;
}
public void setPrice(int price){
this.resultPrice = price;
}
}
ResultViewPagerAdapter.java:
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ResultViewPagerAdapter extends RecyclerView.Adapter<ResultViewHolderPage> {
private ArrayList<ResultDataPage> listData;
ResultViewPagerAdapter(ArrayList<ResultDataPage> data) {
this.listData = data;
}
#Override
public ResultViewHolderPage onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.result_item_viewpager, parent, false);
return new ResultViewHolderPage(view);
}
#Override
public void onBindViewHolder(ResultViewHolderPage holder, int position) {
if(holder instanceof ResultViewHolderPage){
ResultViewHolderPage viewHolder = (ResultViewHolderPage) holder;
viewHolder.onBind(listData.get(position));
}
}
#Override
public int getItemCount() {
return listData.size();
}
}
ResultViewHolderPage.java:
package com.example.myapplication;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
public class ResultViewHolderPage extends RecyclerView.ViewHolder {
private ImageView resultImage;
private TextView resultCafeName;
private TextView resultMenuName;
private TextView resultPrice;
ResultDataPage resultData;
ResultViewHolderPage(View itemView) {
super(itemView);
resultCafeName = itemView.findViewById(R.id.result_cafe_name);
resultImage = itemView.findViewById(R.id.result_sample_image);
resultPrice = itemView.findViewById(R.id.result_product_price);
resultMenuName = itemView.findViewById(R.id.result_menu_name);
}
public void onBind(ResultDataPage resultData){
this.resultData = resultData;
resultMenuName.setText(resultData.getMenuName());
resultCafeName.setText(resultData.getCafeName());
resultImage.setImageResource(resultData.getImage());
resultPrice.setText(Integer.toString(resultData.getPrice()).concat("원"));
}
}
ResultActivity.java:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.tbuonomo.viewpagerdotsindicator.DotsIndicator;
import java.util.ArrayList;
public class ResultActivity extends AppCompatActivity {
// 가운데
private ViewPager2 result_ViewPager2;
private DotsIndicator dotsIndicator;
private ArrayList<ResultDataPage> list;
// 바텀네비게이션뷰
private BottomNavigationView bottomNav;
private FragmentManager fragmentManager = getSupportFragmentManager();
private SearchPage fragmentSearch = new SearchPage();
private HomePage fragmentHome = new HomePage();
private CafePage fragmentCafe = new CafePage();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchresult);
this.viewpage(); // 뷰페이저 (메뉴검색결과값)
// 바텀네비게이션
bottomNav = findViewById(R.id.navigationView);
bottomNav.setOnNavigationItemSelectedListener(new ResultActivity.ItemSelectedListener());
}
private void viewpage(){
list = new ArrayList<>();
list.add(new ResultDataPage(R.drawable.sample_1, "아메리카노","스타벅스", 4900));
list.add(new ResultDataPage(R.drawable.sample_2, "아메리카노","투썸플레이스", 4100));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","이디야커피", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
list.add(new ResultDataPage(R.drawable.sample_3, "아메리카노","EDIYA", 3000));
result_ViewPager2 = findViewById(R.id.resultViewPager2);
result_ViewPager2.setAdapter(new ResultViewPagerAdapter(list));
dotsIndicator = findViewById(R.id.dots_indicator);
dotsIndicator.setViewPager2(result_ViewPager2);
}
//바텀네비
class ItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (menuItem.getItemId()) {
case R.id.searchItem:
Intent intent1 = new Intent(getApplicationContext(), com.example.myapplication.SearchActivity.class);
startActivity(intent1);
//transaction.replace(R.id.frameLayout, fragmentSearch).commitAllowingStateLoss();
break;
case R.id.homeItem:
Intent intent2 = new Intent(getApplicationContext(), com.example.myapplication.MainActivity.class);
startActivity(intent2);
//transaction.replace(R.id.frameLayout, fragmentHome).commitAllowingStateLoss();
break;
case R.id.cafeItem:
Intent intent3 = new Intent(getApplicationContext(), com.example.myapplication.MainCflistActivity.class);
startActivity(intent3);
//transaction.replace(R.id.frameLayout, fragmentCafe).commitAllowingStateLoss();
break;
}
return true;
}
}
}

UPDATE: (Comments below for details)
At the end, text was hidden because the ViewPager was too small.
The AppCompatTextView is a retro-compatible text view for old Support Library support.
Which is your minimum supported SDK version?
You don't need this if you are using AndroidX.
However, it comes with this dependecy you have to put in your module build.gradle file:
implementation 'com.android.support:appcompat-v7:28.0.0'
After this, sync Gradle and you can use the "AppCompatTextView":
References:
https://developer.android.com/topic/libraries/support-library/packages#v7
https://developer.android.com/topic/libraries/support-library/setup#choosing
https://developer.android.com/reference/androidx/appcompat/widget/AppCompatTextView

Related

Why my recyclerview just don't show anything?

I'm using a recyclerview to show restaurants that are previously saved in a database, and I'm using volley to get their information, but it is not working, I suspect that is a problem with volley code, can you guys help me at this point? I will be very happy!
I've discovered that if I start another activity then I go back to home, it returns the recyclerview again, but if I change the fragment it disapears.
Here is how the app is working for now: Result
Fragment Home code:
package com.example.techtable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Home extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ArrayList<restaurantes> restaurantesArrayList;
private static final String url = "https://cybersquare.dev.br/php/obterRestaurantes.php";
TextView txterro;
private RecyclerView recyclerview;
public Home() {
// Required empty public constructor
}
public static Pedidos newInstance(String param1, String param2) {
Pedidos fragment = new Pedidos();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedIntanceState){
super.onViewCreated(view, savedIntanceState);
dataInitialize();
recyclerview = view.findViewById(R.id.recyclerView);
recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerview.setHasFixedSize(true);
adapterRestaurante myAdapter = new adapterRestaurante(getContext(),restaurantesArrayList);
recyclerview.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
private void dataInitialize() {
restaurantesArrayList = new ArrayList<>();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
response -> {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
String titulo = object.getString("nome");
String descricao = object.getString("descricao");
int avaliacao = object.getInt("avaliacao");
int valor = object.getInt("valor");
String fotoResto = object.getString("fotoRestaurante");
restaurantesArrayList.add(new restaurantes(titulo, descricao, avaliacao, valor, fotoResto));
}
}catch (Exception e){
}
}, error -> Toast.makeText(getActivity(), error.toString(),Toast.LENGTH_LONG).show());
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
}
Recyclerview adapter:
package com.example.techtable;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class adapterRestaurante extends RecyclerView.Adapter<adapterRestaurante.MyViewHolder>{
Context context;
ArrayList<restaurantes> restaurantesArrayList;
public adapterRestaurante(Context context, ArrayList<restaurantes> restaurantesArrayList) {
this.context = context;
this.restaurantesArrayList = restaurantesArrayList;
}
#NonNull
#Override
public adapterRestaurante.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.layout_linha_pesquisa, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull adapterRestaurante.MyViewHolder holder, int position) {
restaurantes restaurantes = restaurantesArrayList.get(position);
holder.titResto.setText(restaurantes.titResto);
holder.descResto.setText(restaurantes.descResto);
Picasso.get().load("fotoPerfil").into(holder.fotoResto);
}
#Override
public int getItemCount() {
return restaurantesArrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView fotoResto;
ImageView primeiraEstrela;
ImageView segundaEstrela;
ImageView terceiraEstrela;
ImageView primeiroDolar;
ImageView segundoDolar;
ImageView terceiroDolar;
TextView titResto;
TextView descResto;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
titResto = itemView.findViewById(R.id.titResto01);
descResto = itemView.findViewById(R.id.descResto01);
fotoResto = itemView.findViewById(R.id.fotoResto01);
primeiraEstrela = itemView.findViewById(R.id.estrela11);
segundaEstrela = itemView.findViewById(R.id.estrela12);
terceiraEstrela = itemView.findViewById(R.id.estrela13);
primeiroDolar = itemView.findViewById(R.id.valor11);
segundoDolar = itemView.findViewById(R.id.valor12);
terceiroDolar = itemView.findViewById(R.id.valor13);
}
}
}
Arraylist restaurantes
package com.example.techtable;
import android.net.Uri;
import android.widget.ImageView;
public class restaurantes {
String titResto;
String descResto;
int avaliacaoResto;
int valorResto;
String fotoResto;
public restaurantes(String titResto, String descResto, int avaliacaoResto, int valorResto, String fotoResto) {
this.titResto = titResto;
this.valorResto = valorResto;
this.avaliacaoResto = avaliacaoResto;
this.fotoResto = fotoResto;
this.descResto = descResto;
}
}
MyViewHolder layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/ConstainerResto01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="15dp"
android:background="#drawable/botaoresto"
android:onClick="teste"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/fotoResto01"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="20dp"
android:scaleType="centerInside"
android:src="#drawable/botaoredondo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginStart="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fotoResto01"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout7"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/estrela11"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="#drawable/ic_posticestrelacheia"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/estrela12"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="25dp"
android:src="#drawable/ic_posticestrelameia"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/estrela13"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="50dp"
android:src="#drawable/ic_posticestrelavazia"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/valor11"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="#drawable/ic_posticdolarcheio"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/valor12"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="25dp"
android:src="#drawable/ic_posticdolarcheio"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/valor13"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="50dp"
android:src="#drawable/ic_posticdolarmeia"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginEnd="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/constraintLayout7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/descResto01"
android:layout_width="118dp"
android:layout_height="49dp"
android:fontFamily="#font/roboto_thin"
android:text="Lorem Ipsum Dolor Sit Amet Lorem Ipsum Dolor Sit Amet"
android:textColor="#color/cinza_medio"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/titResto01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_black"
android:text="Restaurante 01"
android:textColor="#color/laranja"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Home layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
tools:context=".Home">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="#+id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/txtUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:fontFamily="#font/roboto_medium"
android:text="Olá usuário!"
android:textColor="#color/black"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="#+id/textView29"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView20" />
<ImageView
android:id="#+id/imageView20"
android:layout_width="200dp"
android:layout_height="80dp"
android:src="#drawable/logotechtablecolorida"
app:layout_constraintBottom_toTopOf="#+id/txtUsuario"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:fontFamily="#font/roboto_light"
android:text="Escolha seu restaurante e faça seu pedido"
android:textAlignment="viewStart"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtUsuario" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout8" />
</androidx.constraintlayout.widget.ConstraintLayout>
JSON file:
<?php
header('content-type: application/json');
$con = mysqli_connect("localhost","cyber536_admin","mJEn#*a]0Jc*","cyber536_techtable");
mysqli_set_charset($con,'utf-8');
$query = "select nome, avaliacao, descricao, fotoRestaurante, valor from restaurantes;";
$res = mysqli_query($con,$query);
$json_data = array();
while($row = mysqli_fetch_assoc($res)){
$json_data[] = $row;
}
echo json_encode($json_data);
?>

Recycler view in Horizontal Scroll bar shows only two items

I am building a social media app, and I wanted to add story scroll view. The problem I am facing is my recycler view is showing only two items however I gave it 4 or more.
I am using horizontal scroll view because I have a first item that says "click to add story" which is different than those items in recycler view. I am new in android building so any help would be appreciated, and I checked different solutions but none were helpful. The only success I got was to increase layout-width of recycler view to 1000dp that helps in scrolling but that's not efficient. My home fragment code, where problem is occurred:
<HorizontalScrollView
android:id="#+id/horizontal_story_scroll_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:isScrollContainer="true"
android:paddingLeft="5dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbars="none"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:paddingRight="5dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/click_add_story_img_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="#drawable/rain_drops_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="20dip"
app:riv_mutate_background="true"
app:riv_tile_mode="repeat" />
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/user_story_img_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black_shade"
app:background="#color/light_red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/click_add_story_img_id"
app:layout_constraintStart_toStartOf="#+id/click_add_story_img_id"
app:layout_constraintTop_toTopOf="parent"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="20dip"
app:riv_mutate_background="true"
app:riv_tile_mode="repeat" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/add_story"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:padding="5dp"
android:src="#drawable/add_3_icon"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/click_add_story_img_id" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:fontFamily="#font/baloo_bhai"
android:text="Click to add Story"
android:textColor="#color/white"
app:layout_constraintBottom_toTopOf="#+id/click_add_story_img_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_story" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/story_rv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
My Adapter:
package com.assadcoorp.socialstar.Package;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.assadcoorp.socialstar.DataTypes.StoryDataType;
import com.assadcoorp.socialstar.R;
import java.util.ArrayList;
public class StoryAdapter extends RecyclerView.Adapter<StoryAdapter.viewHolder>{
ArrayList<StoryDataType> list;
Context context;
public StoryAdapter(ArrayList<StoryDataType> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.sample_story,parent,false);
return new viewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
StoryDataType model=list.get(position);
holder.story_image.setImageResource(model.getStory());
holder.profile_pic.setImageResource(model.getProfile());
holder.name.setText(model.getName());
holder.live_status.setImageResource(model.getStatus());
}
#Override
public int getItemCount() {
return list.size();
}
public class viewHolder extends RecyclerView.ViewHolder{
ImageView story_image,profile_pic,live_status;
TextView name;
public viewHolder(#NonNull View itemView) {
super(itemView);
story_image=itemView.findViewById(R.id.user_story_img_id);
profile_pic=itemView.findViewById(R.id.user_profile_pic_img_id);
live_status=itemView.findViewById(R.id.live_status_id);
name=itemView.findViewById(R.id.user_name_txt_id);
}
}
}
My database:
package com.assadcoorp.socialstar.DataTypes;
public class StoryDataType {
int status;
String name;
int story,profile;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStory() {
return story;
}
public void setStory(int story) {
this.story = story;
}
public int getProfile() {
return profile;
}
public void setProfile(int profile) {
this.profile = profile;
}
public StoryDataType(int status, String name, int story, int profile) {
this.status = status;
this.name = name;
this.story = story;
this.profile = profile;
}
}
and home fragment:
package com.assadcoorp.socialstar;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.Toast;
import com.assadcoorp.socialstar.DataTypes.StoryDataType;
import com.assadcoorp.socialstar.Package.StoryAdapter;
import java.util.ArrayList;
public class HomeFragment extends Fragment {
RecyclerView story_rv;
ArrayList<StoryDataType> story_list;
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_home, container, false);
story_rv=view.findViewById(R.id.story_rv_id);
story_list = new ArrayList<>();
story_list.add(new StoryDataType(R.drawable.live,"Dragon",R.drawable.rain_drops_bg,R.drawable.profile_icon));
story_list.add(new StoryDataType(R.drawable.live,"Dragon",R.drawable.rain_drops_bg,R.drawable.profile_icon));
story_list.add(new StoryDataType(R.drawable.live,"Dragon",R.drawable.rain_drops_bg,R.drawable.profile_icon));
story_list.add(new StoryDataType(R.drawable.live,"Dragon",R.drawable.rain_drops_bg,R.drawable.profile_icon));
StoryAdapter adapter=new StoryAdapter(story_list,getContext());
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
story_rv.setLayoutManager(linearLayoutManager);
//story_rv.setNestedScrollingEnabled(true);
story_rv.setAdapter(adapter);
return view;
}
}
and sample recycler view:
<?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="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/user_story_img_id"
android:layout_width="160dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:src="#drawable/rain_drops_bg"
app:background="#color/light_red"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_border_color="#333333"
app:riv_corner_radius="20dip"
app:riv_mutate_background="true"
app:riv_tile_mode="repeat" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_profile_pic_img_id"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="70dp"
android:padding="5dp"
android:src="#drawable/profile_icon"
app:layout_constraintBottom_toBottomOf="#+id/user_story_img_id"
app:layout_constraintEnd_toEndOf="#+id/user_story_img_id"
app:layout_constraintTop_toTopOf="#+id/user_story_img_id" />
<View
android:id="#+id/user_profile_pic_ring_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/circle_4"
app:layout_constraintBottom_toBottomOf="#+id/user_profile_pic_img_id"
app:layout_constraintEnd_toEndOf="#+id/user_profile_pic_img_id"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/user_profile_pic_img_id"
app:layout_constraintTop_toTopOf="#+id/user_profile_pic_img_id"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/live_status_id"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="#+id/user_story_img_id"
app:layout_constraintTop_toTopOf="#+id/user_story_img_id"
app:srcCompat="#drawable/live" />
<TextView
android:id="#+id/user_name_txt_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginBottom="8dp"
android:background="#color/light_black_shade"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="#+id/user_story_img_id"
app:layout_constraintStart_toStartOf="#+id/user_story_img_id" />
</androidx.constraintlayout.widget.ConstraintLayout>
try to initialize your Arraylist in the HomeFragment class like this ArrayList story_list=new ArrayList();
and if you are using firebase to get the data create a empty constructor in the StoryDataType class;

How do I create a fragment template(class with variables and methods, layout) for multiple fragments with the same content?

On the main screen I have 7 buttons (days) and a fragment.
For every day of the week I wanted to generate a fragment with some EditTexts and a ListView, but creating 7 fragments with the same layout and almost the same class content seems too repetitive.
I only did it for Monday and Tuesday.
The only difference between MondayFragment and TuesdayFragment is that in TuesdayFragment I renamed the variable mondayTV to tuesdayTV and in layout I changed the ids of the submit button and the ListView. Also the key for Bundle is different. I don't think it's worth posting it since it's so similar to MondayFragment.
I want to know if it's possible to create a fragment template and use it to generate a fragment for every button of the activity given this code I wrote. There is still a lot to work on when it comes to functionality but I can't get this idea out of my head.
MainActivity.java
package com.example.dietmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button monday, tuesday, wednesday, thursday, friday, saturday, sunday;
final MondayFragment mondayFragment = new MondayFragment();
final TuesdayFragment tuesdayFragment = new TuesdayFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Intent i = getIntent();
String current_user = i.getStringExtra("current_user");
TextView current_user_txtview = findViewById(R.id.current_user);
current_user_txtview.setText("Welcome, " + current_user);*/
monday = (Button)findViewById(R.id.monday_btn);
tuesday = (Button)findViewById(R.id.tuesday_btn);
wednesday = (Button)findViewById(R.id.wednesday_btn);
thursday = (Button)findViewById(R.id.thursday_btn);
friday = (Button)findViewById(R.id.friday_btn);
saturday = (Button)findViewById(R.id.saturday_btn);
sunday = (Button)findViewById(R.id.sunday_btn);
monday.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFragment(mondayFragment);
getDay(mondayFragment, "monday", (String) monday.getContentDescription());
}
});
tuesday.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFragment(tuesdayFragment);
getDay(tuesdayFragment, "tuesday", (String) tuesday.getContentDescription());
}
});
}
private void openFragment(final Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.daysfragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void getDay(final Fragment fragment, String key, String value)
{
Bundle bnd = new Bundle();
bnd.putString(key, value);
fragment.setArguments(bnd);
}
}
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"
android:background="#color/background_green"
tools:context=".MainActivity">
<Button
android:id="#+id/tuesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:contentDescription="#string/tuesday_context"
android:text="#string/tuesday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.194"
app:layout_constraintStart_toEndOf="#+id/monday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="#+id/main_title"
android:layout_width="147dp"
android:layout_height="93dp"
android:fontFamily="sans-serif-medium"
android:text="#string/welcome_label"
android:textColor="#FFFFFF"
android:textSize="70sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.414"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<Button
android:id="#+id/monday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:contentDescription="#string/monday_context"
android:text="#string/monday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="#+id/thursday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/thursday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.018"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/sunday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/sunday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.786"
app:layout_constraintStart_toEndOf="#+id/saturday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/saturday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/saturday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.225"
app:layout_constraintStart_toEndOf="#+id/friday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/friday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/friday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toEndOf="#+id/thursday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/wednesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/wednesday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toEndOf="#+id/tuesday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<ImageView
android:id="#+id/logo"
android:layout_width="52dp"
android:layout_height="58dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:contentDescription="#string/app_name"
app:layout_constraintStart_toEndOf="#+id/main_title"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_logo" />
<TextView
android:id="#+id/current_user"
android:layout_width="362dp"
android:layout_height="27dp"
android:text="#string/current_user"
android:textAlignment="viewEnd"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/main_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.938"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.318" />
<FrameLayout
android:id="#+id/daysfragment"
android:layout_width="match_parent"
android:layout_height="460dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MondayFragment.java
package com.example.dietmanagement;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MondayFragment extends Fragment {
public TextView mondayTV;
public ArrayList<String> hour_food;
public ArrayAdapter<String> listViewAdapter;
public ListView listView;
public EditText input_meal;
public EditText input_time;
public Button submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_monday, container, false);
mondayTV = (TextView) v.findViewById(R.id.day);
Bundle bndMon = getArguments();
String day = bndMon.getString("monday");
mondayTV.setText(day);
hour_food = new ArrayList<String>();
listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = (ListView)v.findViewById(R.id.monday_list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
}
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = (Button) v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(input_meal.getText())){
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
}
else
{
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
}
});
return v;
}
}
fragment_monday.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_green"
tools:context=".MondayFragment">
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="#string/time"
android:hint="#string/time"
android:inputType="time" />
<EditText
android:id="#+id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="#string/meal"
android:hint="#string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="#+id/submit_food_btn_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"
android:background="#color/white"
android:textColor="#color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="#+id/monday_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
Create DayFragment and layout only for one day. Pass extra information for Fragment and handle situations for different days with that extra information (in your case it looks like only String from bundle is different).
fragment_day.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_green">
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="#string/time"
android:hint="#string/time"
android:inputType="time" />
<EditText
android:id="#+id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="#string/meal"
android:hint="#string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="#+id/submit_food_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"
android:background="#color/white"
android:textColor="#color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="#+id/list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
DayFragment.java
public class DayFragment extends Fragment {
private TextView dayTV;
private ArrayList<String> hour_food;
private ArrayAdapter<String> listViewAdapter;
private ListView listView;
private EditText input_meal;
private EditText input_time;
private Button submit;
private String text;
public DayFragment(String text) {
this.text = text;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_day, container, false);
dayTV = v.findViewById(R.id.day);
dayTV.setText(text);
hour_food = new ArrayList<>();
listViewAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = v.findViewById(R.id.list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener((parent, view, position, id) -> {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(v1 -> {
if (TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(input_meal.getText())) {
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
} else {
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
});
return v;
}
}
Use constructor for passing information.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button monday = findViewById(R.id.monday_btn),
tuesday = findViewById(R.id.tuesday_btn),
wednesday = findViewById(R.id.wednesday_btn),
thursday = findViewById(R.id.thursday_btn),
friday = findViewById(R.id.friday_btn),
saturday = findViewById(R.id.saturday_btn),
sunday = findViewById(R.id.sunday_btn);
openFragment(monday);
openFragment(tuesday);
openFragment(wednesday);
openFragment(thursday);
openFragment(friday);
openFragment(saturday);
openFragment(sunday);
}
private void openFragment(Button btn) {
btn.setOnClickListener(v -> {
String contentDescription = btn.getContentDescription().toString();
getSupportFragmentManager().beginTransaction()
.replace(R.id.daysfragment, new DayFragment(contentDescription))
.addToBackStack(null)
.commit();
});
}
}
In MainActivity openFragment() method takes a Button parameter and with that Button and sets onClickListener to that Button. When you click any Button it gets content description from that Button and passing it to Fragment and opens that Fragment with that content description.

Display buttons dynamically in recyclerview android studio

I am working on a student tracker app and I am trying to display dynamic buttons that get created under each of my students. I will paste an image below of what I hope for it to look like.
At the moment, I am hardcoding the buttons for the first image. I am getting the picture of the student and their name from my firbase db. Now my questions is how can I generate the checkBox for each of the student's and make sure it's directly under their picture and name like above.
In my mind, I am thinking the algorithm would be something like
Loop through my Students ArrayList
Generate the checkBox for each student
Place them onto my recyclerView.
I will paste relevant code down below.
daily_grading.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=".DailyGrading">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/recyclerView"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.261"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
student_item.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="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
DailyGrading.class
public class DailyGrading extends AppCompatActivity{
RecyclerView recyclerView;
Button addStudent;
private DatabaseReference myRef;
public ArrayList<Students> students;
private RecyclerAdapter recyclerAdapter;
private Button orderStudents;
private EditText mEditTextAge;
private EditText mEditTextAssignment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_grading);
recyclerView = findViewById(R.id.recyclerView);
addStudent = findViewById(R.id.addStudentButton);
mEditTextAge = findViewById(R.id.EditTextAge);
mEditTextAssignment = findViewById(R.id.EditTextAssignment);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setHasFixedSize(true);
myRef = FirebaseDatabase.getInstance().getReference();
students = new ArrayList<>();
ClearAll();
GetDataFromFirebase();
}
// fetches images and name from firebase
private void GetDataFromFirebase() {
Query query = myRef.child("student");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ClearAll();
for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
Students student = new Students();
if (snapshot.child("url").getValue() == null) {
student.setImageUrl(snapshot.child("imageUrl").getValue().toString());
}
else {
student.setImageUrl(snapshot.child("url").getValue().toString());
}
student.setName(snapshot.child("name").getValue().toString());
students.add(student);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
// will clear recyclerAdapter
private void ClearAll() {
if (students != null) {
students.clear();
if(recyclerAdapter != null) {
recyclerAdapter.notifyDataSetChanged();
}
}
students = new ArrayList<>();
}
// method to generate checkboxes dynamically
public void generateButtonsDynamically() {
for(int i = 0; i < students.size(); i++){
}
}
RecyclerAdapter.class
package com.example.studenttracker;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
private static final String Tag = "RecyclerView";
private Context mContext;
private ArrayList<Students> studentsArrayList;
public RecyclerAdapter(Context mContext, ArrayList<Students> studentsArrayList) {
this.mContext = mContext;
this.studentsArrayList = studentsArrayList;
}
#NonNull
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.student_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//TextView
holder.textView.setText(studentsArrayList.get(position).getName());
Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
}
#Override
public int getItemCount() {
return studentsArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
}
}
});
}
}
}
Change the code of your student_item.xml to this :
<?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="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
<CheckBox
android:id="#+id/passc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:text="pass" />
<CheckBox
android:id="#+id/failc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:layout_toRightOf="#+id/passc"
android:text="fail" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and control the behaviour of the checkboxes from RecyclerAdapter.class
Try this:
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/recyclerView"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.261"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
and
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//TextView
holder.textView.setText(studentsArrayList.get(position).getName());
Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox2.setVisibility(View.VISIBLE);
} else {
holder.checkBox.setVisibility(View.GONE);
holder.checkBox2.setVisibility(View.GONE);
}
}
Step 1: Add the recyclerview in one xml file.
Step 2: The elements that must be inside a reecyclerview in another xml file (Here add a button that you need).
Step 3: Link these two xml files.
Thus after each insertion dynamically a button will be added.
Hope you will find the solution.
Add Check Boxes in student_item.xml instead daily_grading.xml

onclick listener not working in surfaceview of swipe layout

I am trying to call constructor on onClick listener of getSurfaceView for swipe layout but it is not happening.
RideAdapter.java
#Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == RIDE_ITEM_TYPE) {
view = inflater.inflate(R.layout.item_upcoming_share_ride, parent, false);
swipeLayout = (SwipeLayout) view.findViewById(R.id.sample2);
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
swipeLayout.addDrag(SwipeLayout.DragEdge.Right, swipeLayout.findViewWithTag("Bottom2"));
// sample2.setShowMode(SwipeLayout.ShowMode.PullOut);
swipeLayout.findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Star", Toast.LENGTH_SHORT).show();
}
});`
swipeLayout.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Magnifier", Toast.LENGTH_SHORT).show();
}
});
swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v = inflater.inflate(R.layout.item_upcoming_share_ride, parent, false);
new OfferedRideCardViewHolder(v);
}
});
return new OfferedRideCardViewHolder(view);
and OfferedRideCardViewHolder class:
class OfferedRideCardViewHolder extends BaseRideViewHolder {
#BindView(R.id.imgVechicleImage) ImageView imgVechicle;
#BindView(R.id.imgRideIcon) ImageView imgRideIcon;
#BindView(R.id.fromTextView) TextView txtFromLocation;
#BindView(R.id.toTextView) TextView txtToLocation;
#BindView(R.id.txtVichecleModel) TextView txtVichecleModel;
#BindView(R.id.txtRegisterationNo) TextView txtRegisterationNo;
#BindView(R.id.txtDateTime) TextView txtDateTime;
#BindView(R.id.buttonStartEnd) Button startEndRideButton;
#BindView(R.id.matchCountTextView) TextView matchCountTextView;
#BindView(R.id.matchesTextView) TextView matchesTextView;
#BindView(R.id.vehicleContainer) LinearLayout vehicleContainer;
OfferedRideCardViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
item_upcoming_share_ride.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
app:cardCornerRadius="4dp">
<com.daimajia.swipe.SwipeLayout
android:id="#+id/sample2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:tag="Bottom2"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/edit"
android:src="#drawable/edit"
android:layout_width="70dp"
android:background="#00FF00"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/delete"
android:src="#drawable/delete_grey"
android:layout_width="70dp"
android:background="#FF0000"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/llMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
>
<View
android:id="#+id/view"
android:layout_width="16dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:background="#drawable/vetical_rounded_corner_orange_background"
app:layout_constraintBottom_toBottomOf="#+id/toTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/fromTextView"
/>
<TextView
android:id="#+id/fromTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="12dp"
android:layout_marginTop="16dp"
android:ellipsize="end"
android:fontFamily="#font/lato_regular"
android:maxLines="1"
android:text="Sector 39A, Sector 32, Sector 39, Noida, Uttar Pradesh 201303"
android:textColor="#color/secondary_text"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/view"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/toTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ellipsize="end"
android:fontFamily="#font/lato_regular"
android:maxLines="1"
android:text="Sector 39A, Sector 32, Sector 39, Noida, Uttar Pradesh 201303"
android:textColor="#color/secondary_text"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="#+id/fromTextView"
app:layout_constraintStart_toStartOf="#+id/fromTextView"
app:layout_constraintTop_toBottomOf="#+id/fromTextView"
/>
<ImageView
android:id="#+id/from_dot_view"
android:layout_width="8dp"
android:layout_height="8dp"
android:src="#drawable/current_location"
android:tint="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/fromTextView"
app:layout_constraintEnd_toEndOf="#+id/view"
app:layout_constraintStart_toStartOf="#+id/view"
app:layout_constraintTop_toTopOf="#+id/fromTextView"
/>
<ImageView
android:id="#+id/to_dot_view"
android:layout_width="8dp"
android:layout_height="wrap_content"
android:src="#drawable/pin_red"
android:tint="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/toTextView"
app:layout_constraintEnd_toEndOf="#+id/view"
app:layout_constraintStart_toStartOf="#+id/view"
app:layout_constraintTop_toTopOf="#+id/toTextView"
/>
<TextView
android:id="#+id/txtDateTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/montserrat_regular"
android:gravity="center_vertical"
android:text="23rd jan,2016 2.30 pm"
android:textColor="#color/secondary_text"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/toTextView"
app:layout_constraintStart_toStartOf="#+id/toTextView"
app:layout_constraintTop_toBottomOf="#+id/view"
/>
<ImageView
android:id="#+id/imageView11"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:tint="#color/colorPrimaryLight"
app:layout_constraintBottom_toBottomOf="#+id/txtDateTime"
app:layout_constraintEnd_toEndOf="#+id/view"
app:layout_constraintStart_toStartOf="#+id/view"
app:layout_constraintTop_toBottomOf="#+id/view"
app:srcCompat="#drawable/ic_clock_white_24dp"
/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:id="#+id/vehicleContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp_10"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="#dimen/dp_5"
android:paddingLeft="#dimen/dp_10"
android:paddingRight="#dimen/sp_10"
android:paddingTop="#dimen/dp_5"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgVechicleImage"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/imgcar_fullsize"
tools:layout_editor_absoluteX="18dp"
tools:layout_editor_absoluteY="38dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
>
<TextView
android:id="#+id/txtVichecleModel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/montserrat_regular"
android:text="Camaro 45"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
<TextView
android:id="#+id/txtRegisterationNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:fontFamily="#font/lato_regular"
android:text="Reg no. 1234"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="#dimen/dp_5"
>
<ImageView
android:id="#+id/imgRideIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="0dp"
android:padding="#dimen/dp_5"
android:src="#drawable/driver"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/matchCountTextView"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="8dp"
android:background="#drawable/fill_circle_red"
android:fontFamily="#font/lato_regular"
android:gravity="center"
android:text="5"
android:textColor="#color/white"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintRight_toRightOf="#+id/imgRideIcon"
app:layout_constraintTop_toTopOf="parent"
/>
<android.support.constraint.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
<TextView
android:id="#+id/matchesTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/montserrat_regular"
android:padding="4dp"
android:text="You have 12 matches available"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:visibility="visible"
/>
<!--<Button
android:id="#+id/button_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Invite"
app:font="MontserratRegular"
android:textColor="#color/white"
android:background="#color/colorAccent"
android:layout_marginTop="8dp"
/>-->
<android.support.v7.widget.AppCompatButton
android:id="#+id/buttonStartEnd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:background="#FF8A65"
android:fontFamily="#font/montserrat_regular"
android:text="Start Ride"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16sp"
android:visibility="visible"
/>
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
</android.support.v7.widget.CardView>
DisplayMyRideFragmentViewHolder
package com.techugo.buno.fragments;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.daimajia.swipe.SwipeLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.techugo.buno.ActivityNavigator;
import com.techugo.buno.R;
import com.techugo.buno.activities.NewRideActivity;
import com.techugo.buno.activities.RatePassengersActivity;
import com.techugo.buno.app.MyApplication;
import com.techugo.buno.callbacks.RetrofitCallback;
import com.techugo.buno.common.BaseViewHolder;
import com.techugo.buno.common.events.HideLoadingEvent;
import com.techugo.buno.common.events.RefreshDependenciesEvent;
import com.techugo.buno.common.events.ShowLoadingEvent;
import com.techugo.buno.constant.AppConstant;
import com.techugo.buno.constant.ServerConstants;
import com.techugo.buno.dialog.RateDriverDialog;
import com.techugo.buno.helpers.AppUtilis;
import com.techugo.buno.home.events.ShowAddNameDialogEvent;
import com.techugo.buno.home.searchrides.SearchRideFragmentViewHolder;
import com.techugo.buno.home.sharerides.RidesAdapter;
import com.techugo.buno.loginsignup.models.User;
import com.techugo.buno.modals.NotificationResponseModel;
import com.techugo.buno.modals.UpcomingPromoModel;
import com.techugo.buno.modals.UpcomingResultModel;
import com.techugo.buno.networkInterFace.RetrofitApiService;
import com.techugo.buno.offers.OffersActivity;
import com.techugo.buno.ridedetail.Ride;
import com.techugo.buno.ridedetail.events.RideDetailEvents;
import com.techugo.framework.helper.TUGPrefs;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.OnClick;
public class DisplayMyRideFragmentViewHolder extends BaseViewHolder<List<Ride>> {
#Inject
RetrofitApiService retrofitApiService;
#Inject #Named("currentLoggedInUser")
User user;
#Inject
ActivityNavigator activityNavigator;
#BindView(R.id.upcomingRidesRecyclerView)
RecyclerView recyclerView;
#BindView(R.id.emptyLayout)
LinearLayout emptyLayout;
#BindView(R.id.text_no_internet_connection)
TextView noInternetTextView;
#BindView(R.id.loading_layout) LinearLayout progressBar;
#BindView(R.id.promo_card_view)
CardView promoCard;
#BindView(R.id.promo_title) TextView promoTitle;
private RidesAdapter ridesAdapter;
private List<Ride> myRidesModels;
private UpcomingPromoModel myPromoModel;
public DisplayMyRideFragmentViewHolder(View view, List<Ride> data, Context context) {
super(view,data,context);
this.myRidesModels = data;
this.context = context;
setUpRecyclerView();
setData(myRidesModels);
}
private void getUpComingRides() {
HashMap<String, Object> params = new HashMap<>();
params.put("method", ServerConstants.ALL_UPCOMING_RIDES);
params.put("user_id", user.getUser_id());
params.put("offset", "0");
params.put("limit", "20");
emptyLayout.setVisibility(View.GONE);
//progressBar.setVisibility(View.VISIBLE);
EventBus.getDefault().post(new ShowLoadingEvent(DisplayMyRideFragmentViewHolder.this.getClass().getSimpleName()));
//DisplayUtils.disableUserInteraction((Activity)context);
retrofitApiService.getUpComingRides(params)
.enqueue(new RetrofitCallback<UpcomingResultModel>() {
#Override protected void onSuccess(UpcomingResultModel upcomingResultModel) {
//progressBar.setVisibility(View.GONE);
EventBus.getDefault()
.post(new HideLoadingEvent(
DisplayMyRideFragmentViewHolder.this.getClass().getSimpleName()));
//DisplayUtils.enableUserInteraction((Activity)context);
myRidesModels.clear();
myRidesModels.addAll(upcomingResultModel.getData());
if(upcomingResultModel.getOffers() != null && upcomingResultModel.getOffers().size() > 0) {
myPromoModel = upcomingResultModel.getOffers().get(0);
promoCard.setVisibility(View.VISIBLE);
promoTitle.setText(myPromoModel.getPromo_title());
} else {
promoCard.setVisibility(View.GONE);
}
TUGPrefs.putString(AppConstant.IS_ANY_ACTIVE,
upcomingResultModel.getActive_ride_status());
initializeView(myRidesModels);
}
#Override protected void onError(String errorMessage) {
//progressBar.setVisibility(View.GONE);
//DisplayUtils.enableUserInteraction((Activity)context);
EventBus.getDefault()
.post(new HideLoadingEvent(
DisplayMyRideFragmentViewHolder.this.getClass().getSimpleName()));
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
if (!AppUtilis.isNetworkAvailable(context)) {
emptyLayout.setVisibility(View.GONE);
noInternetTextView.setVisibility(View.VISIBLE);
}
}
});
}
private void setUpRecyclerView() {
ridesAdapter = new RidesAdapter(myRidesModels, context);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
// recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(ridesAdapter);
// ridesAdapter.notifyDataSetChanged();
}
#Override
protected void attachListeners(List<Ride> data) {
}
#Override
protected void initializeView(#NonNull List<Ride> data) {
if (ridesAdapter != null && data.size() != 0) {
emptyLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
ridesAdapter.setData(data);
} else {
recyclerView.setVisibility(View.GONE);
emptyLayout.setVisibility(View.VISIBLE);
}
noInternetTextView.setVisibility(View.GONE);
}
public void refreshView() {
MyApplication.getInstance().getNetComponent().inject(this);
getUpComingRides();
}
#OnClick(R.id.fab_search_ride) void onShareRideFabClick() {
//Intent intent = new Intent(context, FindRideActivity.class);
//context.startActivity(intent);
//if (user.getSignin_type().equals("1") && user.getReg_process().equals("0")) {
// AppUtilis.goToActivity(context, PhoneVerifyActivity.class);
// TUGPrefs.putString("ISFINDRIDE", "yes");
//} else {
//
//}
if (user.getFull_name() == null || user.getFull_name().isEmpty()) {
EventBus.getDefault().post(new ShowAddNameDialogEvent());
return;
}
activityNavigator.startActivity(context, NewRideActivity.class);
}
#OnClick(R.id.promo_card_view) void onPromoCardClick() {
activityNavigator.startActivity(context, OffersActivity.class);
}
#Override public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
refreshView();
}
#Override public void onResume() {
super.onResume();
}
#Override public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onReloadComponents(RefreshDependenciesEvent event) {
MyApplication.getInstance().getNetComponent().inject(this);
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onRideRefresh(RideDetailEvents.RefreshRideDetailEvent event) {
refreshView();
String noty_retrieve = TUGPrefs.getString(AppConstant.IS_RATE_HAS_VALUE, "");
if (noty_retrieve.length() > 0) {
Gson gson = new Gson();
Type type = new TypeToken<NotificationResponseModel>() {
}.getType();
NotificationResponseModel notificationResponseModel = gson.fromJson(noty_retrieve, type);
if (notificationResponseModel != null) {
if (notificationResponseModel.getUser_type() != null
&& notificationResponseModel.getUser_type().equalsIgnoreCase("Driver")) {
Intent intent =
activityNavigator.getIntent(context, RatePassengersActivity.class);
intent.putExtra(AppConstant.NOTY_PASSANGER_DATA, noty_retrieve);
context.startActivity(intent);
} else {
new RateDriverDialog(context, notificationResponseModel).show();
}
}
}
}
}
Toast is working but constructor is not doing anything

Categories