Custom footer menu appear after clicking floating button - android

How I can apply the animation of material design to display a custom footer menu after clicking on the floating button?
Components – Buttons: Floating Action Button

First create a class extends BottomSheetDialogFragment
public class FooterSheetDialogFragment extends BottomSheetDialogFragment {
public static FooterSheetDialogFragment newInstance() {
return new FooterSheetDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.footer_al_dia, container, false);
return v;
}
}
Second: `
mFloatingButton = (FloatingActionButton) view.findViewById(R.id.floatingButtonAlDia);`
mFloatingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialogFragment bsdFragment = FooterSheetDialogFragment.newInstance();
bsdFragment.show(FragmentAlDia.this.getFragmentManager(), "BSDialog");
}
}
);

Related

Making stopwatch/randomizer app but it crashes everytime I try to use one of those two

So, I wanted to make an app that has the main activity in which I can switch between two fragments containing a number randomizer and the other one a stopwatch. The stopwatch, meanwhile, must not stop if I switch to another fragment (it may only stop after I click again a start/stop button). I can switch between them normally, but when I try to generate a random number via button or I try to start the stopwatch, the app crashes. This is what I tried:
Stopwatch fragment:
public class frStopwatch extends Fragment {
View view;
EditText etTime;
Button btnStartStop;
Button btnReset;
Chronometer chronometer;
private boolean running;
private long pauseOffset;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_stopwatch, container, false);
btnStartStop = (Button) view.findViewById(R.id.btnStartStop);
btnReset = (Button) view.findViewById(R.id.btnReset);
btnStartStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.start();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
return view;
}
}
Randomizer fragment:
public class frRandomizer extends Fragment {
View view;
Button btnRandom;
EditText etShowNum;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_randomizer, container, false);
btnRandom = view.findViewById(R.id.btnRandom);
etShowNum = view.findViewById(R.id.etShowNum);
btnRandom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random r = new Random();
etShowNum.setText(r.nextInt(9));
}
});
return view;
}
}
The app crashes on both emulator and mobile phone.

How to Enable/ Disable button from another fragment in android?

i have an activity with 4 fragments from fragment number 1 I want to enable an existing button (that is disable) on fragment 3, when i click in my button in fragment1. this is my attempt:
fragment 1:
public class FragmentEvolucion extends Fragment {
//btnGuardar is in fragment1, the others are in fragment 3 and 4
Button btnGuardar, btnHabilitarMed, btnHabilitarImc;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_evolucion, container, false);
btnGuardar=(Button)rootView.findViewById(R.id.btnGuardarEvolucion);
btnHabilitarMed=(Button)rootView.findViewById(R.id.btnGuardarMedicacion);
btnHabilitarImc=(Button)rootView.findViewById(R.id.btnGuardarDiagnostico);
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnHabilitarMed.setEnabled(true);
btnHabilitarImc.setEnabled(true);
}
});
this give me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
How can i access the button and change it status enabled correctly?
First of all, create a interface.
UpdateButtonListener.java
public interface UpdateButtonListener {
void onUpdate(boolean status);
}
Now in fragment 3, implement interface to class
public class Fragment3 extends Fragment implements UpdateButtonListener{
public static UpdateButtonListener updateButton;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment3, container, false);
updateButton = this;
return view;
}
#Override
public void onUpdate(boolean status) {
// here set the functionality of button whether to disable or not .
if(status){
btnHabilitarMed.setEnabled(true);
btnHabilitarImc.setEnabled(true);
}
}
}
Now in first fragment.
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment3.updateButton.onUpdate(true);
}
Like-wise do for other's.

Add an item to ListView (using Tabs)

I'm trying to add an item to a ListView which is in one of my 3 tabs, the object will be created on another activity and after creating I want to return to the Tab and there's already shown the object in the ListView, I wanted to do FindViewById in the java class of the Tab but this doesn't work, does anyone know how i could do this?
Code:
Create Idea Activity
public class CreateIdeaActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_idea);
final EditText title = (EditText) findViewById(R.id.ideaTitle);
final EditText text = (EditText) findViewById(R.id.ideaText);
final Button create = (Button) findViewById(R.id.btn_create);
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
Idea idea = new Idea(title.getText().toString(),text.getText().toString(),"");
Manager.AddObjectToIdeaList(idea); // Manager Class: List.add(idea)
}
});
}
One of my tabs (xml contains ListView) where I want to add items from the Manager-List to the ListView (findViewById does not work):
public class ideastab_main extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ideastab_main, container, false);
return rootView;
}
}

How to set onClick listener for a Fragment?

I am working with Android Studio. I have a fragment on my Activty and I want to hide it when the user clicks on it.
For some reason I can´t just override a function for onclick like in the activties. And everytime I ask google all I can find are questions about how to
Implement onclick listeners for buttons or other elements in a fragment but that is not what I want. I want the onclick listener for the fragment itself.
Can anyone please tell me how to do that!?
You can do this by set ClickListener on the view inflating in a onCreateView of fragment like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.layout_your, container, false);
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your operation here
// this will be called whenever user click anywhere in Fragment
}
});
return v;
}
It goes like below
public class fragmentOne extends Fragment implements OnClickListener {
Button myButton;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_1, container, false);
myButton = (Button) myView.findViewById(R.id.myButton);
myButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
}

How can I close the fragment?

I am have Fragment on the Activity. Fragment has button. if i click on the button, Fragment must be close. How i am did this?
public class ItemFragment extends Fragment{
private ImageView btnApply;
private ClickButton clickButton = new ClickButton();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_info, container, false);
btnApply = (ImageView) rootView.findViewById(R.id.btnSendItem);
btnApply.setOnClickListener(clickButton);
return rootView;
}
private class ClickButton implements View.OnClickListener {
#Override
public void onClick(View v) {
if (R.id.btnSendItem == v.getId()) {
Toast.makeText(getActivity(),"CLOSE",Toast.LENGTH_LONG).show();
return;
}
}
}
}
There's no such thing like close the fragment, but you can remove the fragment from the stack. To pop the fragment use the following inside button click listener
getActivity().getFragmentManager().beginTransaction().remove(this).commit();
When this fragment is of type androidx.fragment.app.Fragment then this seems to work:
getActivity().getFragmentManager().popBackStack();
This pops the top visible fragment off the stack.

Categories

Resources