I want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}
Related
I've got a Fragment, which has a RecyclerView. This RecyclerView has a header adapter, which contains an EditText.
When the Fragment comes into view, I want the EditText to auto focus (i.e. show the keyboard automatically).
How would I do this?
The Fragment:
public class My Fragment extends Fragment {
private LinearLayoutManager layoutManager;
private FeedRecyclerView recyclerView;
private PostAdapter postAdapter;
private ConcatAdapter concatAdapter;
private HeaderAdapter headerAdapter;
private List<Post> postData = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment, parent, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler_view);
layoutManager = new FeedLinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
// This is the adapter that contains the EditText
headerAdapter = new HeaderAdapter(getContext());
postAdapter = new PostAdapter(getContext(), postData, layoutManager);
concatAdapter = new ConcatAdapter();
recyclerView.setAdapter(concatAdapter);
concatAdapter.addAdapter(searchHeaderAdapter);
}
}
The adapter class, HeaderAdapter:
public class HeaderAdapter extends RecyclerView.Adapter<HeaderAdapter.ViewHolder> {
private Context context;
public HeaderAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.my_header, parent, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//
}
#Override
public long getItemId(int position) {
return 1;
}
#Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public EditText myEditText;
public ViewHolder(View v) {
super(v);
myEditText = v.findViewById(R.id.my_edit_text);
}
}
}
As you can see, my RecyclerView's adapter, HeaderAdapter, has the EditText. When the Fragment gets shown, I want to auto focus this EditText.
How would I do this?
Thanks!
Add this line myEditText.requestFocus() into init in Header ViewHolder
init {
myEditText = v.findViewById(R.id.my_edit_text)
myEditText.requestFocus()
}
try this
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.myEditText.requestFocus();
((InputMethodManager)holder.itemView.context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(holder.myEditText, 0);
}
I am writing a app which use a recycleView in fragement. However only the textView in that fragement show,there is nothing in the recycleView ,function like onBindViewHolder() in the recycleView Adapter wasn't been called .Here is my code of recycleViewAdapter and fragement.
Adapter
import java.util.List;
public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.MyViewHolder> {
private List<Shopping> localData;
public ShoppingAdapter(List<Shopping> localData) {
this.localData = localData;
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
textView =itemView.findViewById(R.id.shopping_text);
imageView = itemView.findViewById(R.id.shopping_img);
}
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d("test", "onCreateViewHolder: ");
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_item,parent,false);
return null;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(localData.get(position).getTitle());
holder.imageView.setImageResource(localData.get(position).getImageResource());
}
#Override
public int getItemCount() {
return this.localData.size();
}
}
Fragment
public class shoppingFragment extends Fragment {
View view;
List<Shopping> shoppingList;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initShoppingList();
RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
ShoppingAdapter myAdapter=new ShoppingAdapter(shoppingList);
recyclerView.setAdapter(myAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shopping, container, false);
return view;
}
public void initShoppingList(){
shoppingList = new ArrayList<Shopping>();
shoppingList.add(new Shopping(R.drawable.data,"data"));
shoppingList.add(new Shopping(R.drawable.position,"position"));
shoppingList.add(new Shopping(R.drawable.floor,"floor"));
}
}
Looks like you are missing 2 crucial things.
onCreateViewHolder should return a view. You return null.
return new MyViewHolder(view);
You forgot to set the LayoutManager to your adapter.
recyclerView.setLayoutManager(linearLayoutManager);
Instead of returning null at onCreateViewHolder, you should return new MyViewHolder(view);
And you forgot to assign the layoutmanager you created on your fragment into your recyclerview (recyclerView.setLayoutManager(linearLayoutManager);)
Also, you don't need to cast recyclerview to recyclerview. You can remove (RecyclerView) from RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
i am trying to create a bottom sheet fragment with a recycler view. But i am getting an error in recycler view as adapter not set skipping layout
I am positive that i set the adapter for recycler view.I do not seem to understand the problem.
Bottomsheet Fragment code
public class QueueBottomSheetDialog extends BottomSheetDialogFragment {
//Views
private View fragmentView;
private RecyclerView queueRecyclerView;
private QueueRecyclerAdapter queueRecyclerAdapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.queue_bottomsheet,container,false);
Initialize();
populateSongs();
return fragmentView;
}
private void Initialize(){
queueRecyclerView = fragmentView.findViewById(R.id.queue_recyclerView);
queueRecyclerView.setHasFixedSize(true);
}
private void populateSongs(){
queueRecyclerAdapter = new QueueRecyclerAdapter(getContext(),MusicService.songsSet);
queueRecyclerView.setAdapter(queueRecyclerAdapter);
queueRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
Code for recycler adapter
public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdapter.QueueViewHolder> {
private Context context;
private SongSet songSet;
public QueueRecyclerAdapter(Context context,SongSet songSet){
this.context = context;
this.songSet = songSet;
}
#NonNull
#Override
public QueueViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.queue_cardview,parent,false);
return new QueueViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull QueueViewHolder holder, int position) {
holder.songTv.setText(songSet.get(position).getTitle());
holder.artistTv.setText(songSet.get(position).getArtist());
}
#Override
public int getItemCount() {
return songSet.size();
}
public static class QueueViewHolder extends RecyclerView.ViewHolder{
private TextView songTv;
private TextView artistTv;
public QueueViewHolder(#NonNull View itemView) {
super(itemView);
songTv = itemView.findViewById(R.id.sName);
artistTv = itemView.findViewById(R.id.aName);
}
}
}
I am calling like this in my main activity
queueBottomSheetDialog queueBottomSheetDialog = newQueueBottomSheetDialog();
queueBottomSheetDialog.show(getSupportFragmentManager(),"QUEUE BOTTOMSHEET");
i am trying to add a recyclerview on a fragment
fragments code:
public class MediaPlayerController extends Fragment{
private static final String TAG = "MediaPlayerController";
private RecyclerView recyclerViewb;
private RecycleViewAdapter myAdapter;
private ArrayList<String> myRecordings = new ArrayList<>();
//private DBHelper dbHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: started");
View v = inflater.inflate(R.layout.media_player_area, container, false);
recyclerViewb = (RecyclerView)v.findViewById(R.id.recyclerViewXml);
//dbHelper = new DBHelper(v.getContext(), null, null, 1);
myRecordings.add("hau");
myRecordings.add("hau2");
myRecordings.add("hau3");
myRecordings.add("hau4");
myRecordings.add("ha5");
myRecordings.add("hau23");
myRecordings.add("ha31u");
myRecordings.add("haudsa");
System.out.println("what does this print?"+myRecordings);
myAdapter = new RecycleViewAdapter(v.getContext(), myRecordings);
recyclerViewb.setAdapter(myAdapter);
recyclerViewb.setLayoutManager(new LinearLayoutManager(v.getContext()));
Log.d(TAG, "onCreateView: returned");
return v;
}
in the logs i see that that it doesn't even get to the RecycleViewAdapter sections of code at all the fragment is completely blank
i get not errors as well
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private static final String TAG = "RecycleViewAdapter";
private Context mContext;
private ArrayList<String> mRecName = new ArrayList<>();
public RecycleViewAdapter(Context mContext, ArrayList<String> mRecName) {
this.mContext = mContext;
this.mRecName = mRecName;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: i am here");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_recycle, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.recTxt.setText(mRecName.get(position).toString());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "sas gamaw", Toast.LENGTH_LONG);
}
});
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RelativeLayout parentLayout;
TextView recTxt;
public ViewHolder(View itemView) {
super(itemView);
recTxt = itemView.findViewById(R.id.recTextView);
parentLayout = itemView.findViewById(R.id.parentLayoutXml);
}
}
}
in the logs it only displays oncreateView started and the myRecordings arraylist
onCreateView: started
I/System.out: what does this print?[hau, hau2, hau3, hau4, ha5,
hau23, ha31u, haudsa]
MediaPlayerController: onCreateView: returned
i can't think of what is going wrong :/ please help out
i also want to add that i am fairly new to android studio thanks
your code problem it is in Recycler Adapter in getItemCount() method,
You don't return any Item , you must change it to :
#Override
public int getItemCount() {
return mRecName.size();
}
I'm not quite sure if this is possible, but I'm trying to change the content of a custom DialogFragment (read: content as in 1 imageview, and 3 textviews) inside of a Recyclerview adapter.
My current custom DialogFragment:
public class ServiceDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.service_card, container, false);
}
}
And the layout:
As you see in the image above: I have a list behind the where the DialogFragment is displayed and whenever I press one of those "buttons" that DialogFragment appears. But my question is: how do I change the content of the custom DialogFragment inside of my Recyclerview Adapter?
My adapter:
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder>{
private final String TAG = "******* Service Adapter";
private ArrayList<ServiceObject> mDataset;
private Context mContext;
private Resources mResources;
private Context location;
public ServiceAdapter(ArrayList<ServiceObject> mDataset, Context mContext, Resources mResources) {
this.mDataset = mDataset;
this.mContext = mContext;
this.mResources = mResources;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
location = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_box, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(mDataset.get(position).getTitle());
holder.icon.setImageResource(mResources.getIdentifier(Integer.toString(mDataset.get(position).getIcon()), "drawable", "net.example.adapter"));
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title, desc, price;
ImageView icon;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.service_box_title);
price = (TextView) v.findViewById(R.id.service_card_price);
icon = (ImageView) v.findViewById(R.id.service_box_icon);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
FragmentActivity activity = (FragmentActivity)(mContext);
FragmentManager fm = activity.getSupportFragmentManager();
ServiceDialogFragment alertDialog = new ServiceDialogFragment();
alertDialog.show(fm, "display_service");
}
}
}
Thanks to #raghunandan, I figured it out. Resource: http://developer.android.com/reference/android/app/DialogFragment.html