How to show Relative Layout if it's not empty - android

I have a Relative Layout and the visisbility is set to gone if the Recycler View is empty. My problem is that it doesn't return when I add things into the recycler view
This is my Class
#Override
public View inOnCreateView(View root, #Nullable ViewGroup container, Bundle savedInstancesState) {
View view = mRoot;
addButton = view.findViewById(R.id.addItemsButton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent addButton = new Intent(getActivity(), AddItemsActivity1.class);
startActivity(addButton);
}
});
mModelList = new ArrayList<>();
mRecyclerView = view.findViewById(R.id.recycler_view1);
mAdapter1 = new RecyclerViewAdapter(mModelList);
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(manager);
editText = view.findViewById(R.id.editTxt);
btn = view.findViewById(R.id.btn1234);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mModelList.add(new Model(editText.getText().toString()));
mAdapter1.notifyDataSetChanged();
}
});
mRecyclerView.setAdapter(mAdapter1);
relativeLayout = view.findViewById(R.id.Dairy_RelativeLayout);
if(mAdapter1.getItemCount() == 0){
relativeLayout.setVisibility(View.GONE);
}
return root;
}
I also know it's not my adapters fault because when I set it to View.Invisible I can see that the view gets bigger when items are in it, since its set to wrap content.

Related

Update or switch BottomSheetDialogFragment inflated view when recyclerView dataset is changed

I have a BottomSheetDialogFragment and inside onCreateView i am inflating 2 different views based on a condition. Now my 1st view contains a recyclerView and the condition on which i want to switch my views is that if the list is empty then inflate view 1 else inflate view 2. Now my problem is when my list data count becomes 0 i want to call the onCreateView again so that based on the condition my 2nd view would be inflated. But i don't know how am i supposed to do it or rather is this the right way to do it.
My Code -
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (this.documentCount > 0) {
View v = inflater.inflate(R.layout.bottom_sheet_layout_document_attachments_added, container, false);
documentArrayList = dataToRender.get(fieldPosition).getAttachments();
Button attachMore = (Button) v.findViewById(R.id.btn_attachMore);
if(documentCount == 5){
attachMore.getBackground().setAlpha(128);
}else{
attachMore.getBackground().setAlpha(255);
}
attachMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
if(documentCount == 5){
PageLoader.showToast(getResources().getString(R.string.document_limit_message));
}else{
pickDocuments();
}
}
catch (Exception e){
Sentry.captureException(e);
}
}
});
Button btnCloseDrawer = (Button) v.findViewById(R.id.btn_closeDrawer);
btnCloseDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
recyclerview_documents = v.findViewById(R.id.recyclerview_documents);
adapter_documents = new DocumentAdapter(this, documentArrayList, fieldPosition);
recyclerview_documents.setAdapter(adapter_documents);
recyclerview_documents.setLayoutManager(new LinearLayoutManager(mContext));
return v;
} else {
View v = inflater.inflate(R.layout.bottom_sheet_layout_document, container, false);
TextView txtview_attach = (TextView) v.findViewById(R.id.txtview_attach);
txtview_attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
permissionManager.checkPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE, new PermissionManager.PermissionAskListener() {
#Override
public void onNeedPermission() {
ActivityCompat.requestPermissions(getActivity(), new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
}, REQUEST_EXTERNAL_STORAGE);
}
#Override
public void onPermissionPreviouslyDenied() {
showDocumentRational();
}
#Override
public void onPermissionPreviouslyDeniedWithNeverAskAgain() {
dialogForSettings(getResources().getString(R.string.permission_denied), getResources().getString(R.string.permission_denied_document_access_message));
}
#Override
public void onPermissionGranted() {
pickDocuments();
}
});
}
catch (Exception e){
Sentry.captureException(e);
}
}
});
return v;
}
}```
Can't use more then one views for one fragment, but you can create a fragment wrapper that depends on the conditions, will show or create the fragment you need with the view you need.
for example WrapperFragment checking list size and creating Fragment1, when list size changed Wrapper fragment removung from fragment manager Fragment1 and add Fragment2

How to Refresh the fragment, when Internet Connection is Resume

I had a fragment, in which first I check for availability for internet connection. If its available its fine, otherwise, I want to refresh the page by clicking on retry button. The problem which comes is, I am not able to refresh the fragment
This is my fragment code.
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.homework, container, false);
listView = rootView.findViewById(R.id.home_list);
adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
listView.setAdapter(adapter);
cal = rootView.findViewById(R.id.date_select);
boolean x = handleNetworkConnection();
if (x == true) {
methodListener();
SharedPreferences preferences = getActivity().getSharedPreferences("user_login", Context.MODE_PRIVATE);
HashMap<String, String> map = new HashMap<>();
map.put("school_id", preferences.getString("school_id", ""));
map.put("class", preferences.getString("classes", ""));
map.put("sec", "homeera");
defaultfetch(map);
}
else
{
final LinearLayout ll = new LinearLayout(getActivity());
TextView textView = new TextView(getActivity());
Button button = new Button(getActivity());
ImageView image = new ImageView(getActivity());
ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
ll.setBackgroundColor(Color.parseColor("#ecf0f1"));
image.setImageResource(R.drawable.errorsq);
button.setBackgroundColor(Color.parseColor("#02b48a"));
button.setText("Try Again");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(getActivity().).attach(this).commit();
*/
}
});
ll.addView(textView);
ll.addView(image);
ll.addView(button);
ll.setOrientation(LinearLayout.VERTICAL);
View rootview = ll;
return rootview;
}
return rootView;
}
In this code, I had applied a way to refresh the fragment, but the problem is that after refreshing UI comes blank. Nothing is there on the screen.
You should not refresh the fragment but refresh the list when you get an internet connection.
Since your method handleNetworkConnection() returns synchronously, you could organize the code as follows:
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.homework, container, false);
listView = rootView.findViewById(R.id.home_list);
adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
listView.setAdapter(adapter);
cal = rootView.findViewById(R.id.date_select);
prepareList();
}
void prepareList() {
boolean x = handleNetworkConnection();
if (x == true) {
// same code as now, load the list content and display it
} else {
// same code as now except the click listener:
public void onClick(View view) {
prepareList();
}
//
}
}

TextView SetTextSize not working inside fragment

I have a Activity with TabLayout and ViewPager. I have two fixed buttons floating on top of the activity at bottom right corner. These two buttons are Plus and Minus buttons for increasing/decreasing the font sizes of the text inside the fragment. I created an Interface inside Activity and implemented it inside the fragment where I am changing the textsize of TextView.
Problem is that it does not work at the time when I click the button. When I swipe the fragment then it shows with updated textsize. Am I missing something? Is this the correct way?
Inside Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_single);
//....
onCoachMark();
//....
}
public void onCoachMark() {
final FrameLayout overlayFramelayout = new FrameLayout(getApplicationContext());
setContentView(overlayFramelayout);
View view = getLayoutInflater().inflate(R.layout.post_single, overlayFramelayout, false);
final View overlay_view = getLayoutInflater().inflate(R.layout.zoom_overlay, overlayFramelayout, false);
final ImageView ivPlus = (ImageView) overlay_view.findViewById(R.id.ivPlus);
final ImageView ivMinus = (ImageView) overlay_view.findViewById(R.id.ivMinus);
overlayFramelayout.addView(view);
overlayFramelayout.addView(overlay_view);
ivPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE += 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
ivMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE -= 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
}
public interface ICoachMark {
void onTextSizeChange();
}
public void setOnCoachMark(ICoachMark iCoachMark) {
this.iCoachMark = iCoachMark;
}
Inside Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.post_content, container, false);
tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
tvDescription = (TextView) rootView.findViewById(R.id.tvDescription);
svPost = (NestedScrollView) rootView.findViewById(R.id.svPost);
String fs = CommonFunctions.getMySharedPreferences(getActivity().getApplicationContext(), "fontSize");
if (fs.matches("")) {
CommonFunctions.putMySharedPreferences(getActivity().getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
} else {
CommonFunctions.FONT_SIZE = Integer.valueOf(fs);
tvTitle.setTextSize(CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(CommonFunctions.FONT_SIZE);
}
((PostActivity) getActivity()).onTextSizeChange(new PostActivity.ICoachMark() {
#Override
public void onTextSizeChange() {
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
}
});
}

OnClickListener on a null object reference

Hello can anybody help me to handle this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at de.kwietzorek.fulcrumwebview.MainActivity$AddServerFragment.onCreateView(MainActivity.java:141)
141 is at btn_back.setOnClickListener(new View.OnClickListener() {
Fragment: AddServerFragement
public class AddServerFragment extends Fragment
implements View.OnClickListener {
#Override
public void onClick(View view) {
}
public AddServerFragment(){
}
Button btn_back, btn_add;
EditText server_ip, server_name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_ip, container, false);
server_ip = (EditText) findViewById(R.id.edit_server_address);
server_name = (EditText) findViewById(R.id.edit_server_name);
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String new_server_ip = null, new_server_name = null;
ArrayList<String> server_name_list = new ArrayList<String>();
ArrayList<String> server_ip_list = new ArrayList<String>();
new_server_ip = server_ip.getText().toString();
server_ip_list.add(new_server_ip);
new_server_name = server_name.getText().toString();
server_name_list.add(new_server_name);
}
});
return view;
}
}
Replace
btn_back= (Button) findViewById(R.id.btn_back);
with
btn_back = (Button) view.findViewById(R.id.btn_back);
Do the same for all the other layout elements you wanna initiate.
The reason is: since you inflating a layout file in your Fragment, you have to refer all UI elements to THAT file, that you called "view" here:
View view = inflater.inflate(R.layout.add_ip, container, false);
So the istruction "findViewById" has to be refered to that View view.
You need to find the view inside the view you inflated:
btn_back = (Button) view.findViewById(R.id.btn_back);

Adding a button listener in a fragment

I am trying to add a button listener to a fragment view in Android. When I tap the "Like" or "Dislike" buttons, nothing appears to happen. I don't get the "dislikeClicked" or "likeClicked" in my Logcat view.
Here is my code:
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
queryForData();
View rootView = inflater.inflate(R.layout.fragment_view1, container, false );
likeButton = (Button) rootView.findViewById(R.id.view1_likeButton);
likeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("likeClicked", "");
like();
}
});
dislikeButton = (Button) rootView.findViewById(R.id.view1_dislikeButton);
dislikeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("dislikeClicked", "");
dislike();
}
});
return rootView;
}
Any help is appreciated.
Thanks!

Categories

Resources