It has been awhile so I am struggling with fragments a bit. I am working on demos right now to get familiar again. What I am doing is in the main activity, I have a button to click which opens a fragment to enter information like name, address, etc. For some reason, the main activity doesn't go away and shows over the fragment. What am I doing wrong???
Note: I have not wired the cancel button yet to go back to Main Activity without doing anything.
Main Activity:
public class MainActivity extends AppCompatActivity {
public static User mUser = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button requestServiceButton = findViewById(R.id.servicesButton);
requestServiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
NewUser fragment = new NewUser();
transaction.replace(R.id.fragmentHolder, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
NewUser Class:
public class NewUser extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.dialog_new_user, container, false);
final EditText fName = v.findViewById(R.id.firstNameEditText);
final EditText lName = v.findViewById(R.id.lastNameEditText);
final EditText address = v.findViewById(R.id.editTextAddress);
final EditText city = v.findViewById(R.id.cityEditText);
final EditText email = v.findViewById(R.id.editTextEmailAddress);
final EditText password = v.findViewById(R.id.editTextPassword);
final EditText phoneNumber = v.findViewById(R.id.editTextPhone);
final EditText username = v.findViewById(R.id.usernameEditText);
final EditText zipCode = v.findViewById(R.id.zipEditText);
Button okButton = v.findViewById(R.id.btnOK);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.mUser.setFirstName(fName.getText().toString());
MainActivity.mUser.setLastName(lName.getText().toString());
MainActivity.mUser.setAddress(address.getText().toString());
MainActivity.mUser.setCity(city.getText().toString());
MainActivity.mUser.setEmailAddress(email.getText().toString());
MainActivity.mUser.setPassword(password.getText().toString());
MainActivity.mUser.setPhoneNumber(phoneNumber.getText().toString());
MainActivity.mUser.setUsername(username.getText().toString());
MainActivity.mUser.setZipCode(zipCode.getText().toString());
}
});
Button cancelButton = v.findViewById(R.id.btnCancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return v;
}
Related
I wanted to make a Comment & Reply function for social media app
something like instagram
enter image description here
is there a way to have a nested recyclerview by using FirestoreRecyclerAdapter?
here is my plan
enter image description here
and here is the structure of my FirestoreRecyclerAdapter
homeRecycler = findViewById(R.id.homeRecycler);
linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
homeRecycler.setLayoutManager(linearLayoutManager);
homeRecycler.setItemAnimator(null);
StorageReference profileRef = storageReference.child("ProfileImage/"+fAuth.getCurrentUser().getUid()+"/profile.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(prof_img);
}
});
Query query = fStore.collection("Post")
.orderBy("homePostDate", Query.Direction.DESCENDING)
.limit(50);
FirestoreRecyclerOptions<HomeModel> options = new FirestoreRecyclerOptions.Builder<HomeModel>()
.setQuery(query, HomeModel.class)
.build();
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onRefresh() {
adapter.notifyDataSetChanged();
swipeRefresh.setRefreshing(false);
}
});
adapter = new FirestoreRecyclerAdapter<HomeModel, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull HomeModel model) {
holder.bind(model);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.home_each, group,false);
return new ViewHolder(view);
}
};
homeRecycler.setAdapter(adapter);
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView author, title, body, authorUid, docId, timestamp, upvotecount, downvotecount;
Button addcoment;
ToggleButton upvote, downvote;
ImageView prof_img;
HomeModel model;
RecyclerView replyRecycler;
public ViewHolder(#NonNull View itemView) {...}
public void bind(HomeModel homeModel) {...}
edit:
I tried just duplicating everything within the firestorerecycleradapter viewholder but couldnt make it work and dont even know if this is the proper way..
commentRecycler = findViewById(R.id.commentRecycler);
linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
commentRecycler.setLayoutManager(linearLayoutManager);
commentRecycler.setItemAnimator(null);
Query query = fStore.collection("Post")
.document(pdocId.getText().toString()).collection("comment")
.orderBy("commentPostDate", Query.Direction.DESCENDING)
.limit(50);
FirestoreRecyclerOptions<CommentModel> options = new FirestoreRecyclerOptions.Builder<CommentModel>()
.setQuery(query, CommentModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<CommentModel, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull CommentModel model) {
holder.bind(model);
linearLayoutManager2 = new LinearLayoutManager(HomeView.this, RecyclerView.VERTICAL, false);
holder.replyRecycler.setLayoutManager(linearLayoutManager2);
holder.replyRecycler.setItemAnimator(null);
Query query2 = fStore.collection("Post")
.document(pdocId.getText().toString()).collection("comment")
.document(model.docId).collection("reply")
.orderBy("commentPostDate", Query.Direction.DESCENDING)
.limit(50);
FirestoreRecyclerOptions<ReplyModel> options2 = new FirestoreRecyclerOptions.Builder<ReplyModel>()
.setQuery(query2, ReplyModel.class)
.build();
adapter2 = new FirestoreRecyclerAdapter<ReplyModel, ViewHolder2>(options2) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder2 holder, int position, #NonNull ReplyModel model) {
holder.bind(model);
Toast.makeText(HomeView.this, "this is test", Toast.LENGTH_SHORT).show();
}
#NonNull
#Override
public ViewHolder2 onCreateViewHolder(#NonNull ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.comment_each, group,false);
return new ViewHolder2(view);
}
};
holder.replyRecycler.setAdapter(adapter2);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.comment_each, group,false);
return new ViewHolder(view);
}
};
commentRecycler.setAdapter(adapter);
and then added another viewholder class
class ViewHolder extends RecyclerView.ViewHolder{
TextView author, body, authorUid, docId, timestamp, pReply, upvotecount, downvotecount;
TextInputLayout tilpReply;
ImageView prof_img;
Chip replyChip;
ToggleButton upvote, downvote;
ToggleButton replypop;
Button replyBtn;
CommentModel model;
RecyclerView replyRecycler;
public ViewHolder(#NonNull View itemView){...}
public void bind(CommentModel commentModel){...}
}
class ViewHolder2 extends RecyclerView.ViewHolder{
TextView author, body, authorUid, docId, timestamp, pReply, upvotecount, downvotecount;
TextInputLayout tilpReply;
ImageView prof_img;
Chip replyChip;
ToggleButton upvote, downvote;
ToggleButton replypop;
Button replyBtn;
CommentModel model;
RecyclerView replyRecycler;
public ViewHolder2(#NonNull View itemView){...}
public void bind(CommentModel commentModel){...}
Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out
I just got started with Firestore. So I tried fitting my data directly into a recyclerview, here's the code I used for reference.
Here's my UserRecyclerView Fragment :
public class UserRecyclerView extends Fragment{
private ArrayList<User> user;
RecyclerView mRecyclerView;
public UserRecyclerView(){}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view,container,false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter();
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
#NonNull
private FirestoreRecyclerAdapter getFirestoreRecyclerAdapter() {
Query query = FirebaseFirestore.getInstance()
.collection("users");
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class).build();
return new FirestoreRecyclerAdapter<User, UserViewHolder>(options) {
#Override
protected void onBindViewHolder(UserViewHolder holder, int position, User model) {
Log.d("USER", "DATA: "+model.getName());
holder.BindView(position, model);
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_list, parent, false);
return new UserViewHolder(view);
}
};
}
private class UserViewHolder extends RecyclerView.ViewHolder{
TextView info;
UserViewHolder(View itemView) {
super(itemView);
info = (TextView) itemView.findViewById(R.id.info_text);
}
void BindView(int position, User model){
info.setText(model.getName()+" , "+model.getId());
}
}
There's nothing in my MainActivity except for starting the above fragment. I don't know what's wrong with my code. It shows no errors, when I run the app it doesn't crash. It just enters into the recyclerview layout, I checked it by placing a textview field for being sure(now removed). I want you to help me out with the code. It's probably because of the adapter? Help me figure out. I have also uploaded a snippet of my database.
Thanks in advance!
Make sure you call startListening on the adapter to initiate the connection to Cloud Firestore, otherwise no data will be loaded.
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
See:
https://github.com/firebase/FirebaseUI-Android/blob/master/firestore/README.md#firestorerecycleradapter-lifecycle
Create recyclerview like below:
FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter();
//add below line
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
the code works fine at the first time but when i rotate the screen to landscape it is disabling my fragment button even it is no longer usable after i change the orientation back to normal(portrait). why this is happening??
Main Activity
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
TextView textView;
fragment_one fragment_one;
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container,fragment_one).commit();
button = (Button) findViewById(R.id.mybutton_main);
textView = (TextView) findViewById(R.id.mytextView_main);
editText = (EditText) findViewById(R.id.myedittext_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("button Clicked");
editText.setText("button Clicked");
}
});
}
}
Fragment
public class fragment_one extends Fragment implements View.OnClickListener {
Button button;
TextView textView;
public fragment_one() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.mybutton_frag);
textView = (TextView) getActivity().findViewById(R.id.mytextView_frag);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
textView.setText("clicked");
}
}
Try this:
if (savedInstanceState == null) {
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container,fragment_one)
.commit();
}
I have a button which adds a textview to my fragment. When I close the activity the textview disappears. The only thing I like to know is how to save and retrieve the created textview.
public class trainingsplan extends Fragment {
ImageButton Btn;
LinearLayout Layout;
private ArrayList<View> mViews= new ArrayList<View>();
int i;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.trainingsplan, container, false);
setHasOptionsMenu(true);
Btn = (ImageButton) rootView.findViewById(R.id.imageButton3);
Layout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView Test = new TextView(getActivity());
Test.setLayoutParams(lparams);
Test.setText("Hallo wie gehts dir ?");
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Layout.addView(Test);
mViews.add(Test);
}
});
return rootView;
}