How to replace a fragment from the main activity - android-studio

I have a two fragments, mainactivity initially have fragment called "Dashboard fragment" Upon clicking this a button I have to replace this fragment by new fragment called "Dashboard1 fragment" but problem is Dashboard fragment is not hide/remove and now it showing both
My code
public void onClick(View view) {
Fragment fragment = null;
fragment = new Dash_Section();
replaceFragment(fragment);
}
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
How can I remove or replace the Dashboard fragment?
Any help will be appreciated.

First of all, you have to declare a FragmentContainer in you Layout file of the MainActivity. This is the space where both fragments will be shown. Then you can instantiate both Fragments and change between them by adding:
Dashboard1Fragment dFragment = new Dashboard1Fragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, dFragment);
fragmentTransaction.commit();
This will replace the content of your FragmentContainer and instantiate a new Fragment, without overlapping both Fragments.

Related

No view found for id "bla bla bla" for grammatically created framlayout

I am creating a framelayout pro-grammatically. Then I create fragment pro-grammatically..Then I want to add the fragment to the framelayout.. this process is happens every time when I click the button. For the framelayout id and fragment tag I created a integer increment variable("id" is initiated as to "0" when beginning)..
When very time I click the button new framelayout with new id create, create fragment with new tag, fragement is added to that framelayout...And contentious overlapping views..
Here below my code,
public void add_tab(View view)
{
id++;
String b = String.valueOf(id);
tab_frame = new FrameLayout(this);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
tab_frame.setLayoutParams(layoutparams);
tab_frame.setId (id);
FragmentManager fragment_manager1 = getSupportFragmentManager (); // fragment manager for dealing with fragments
frag_one frag_one_obj = new frag_one(); // creating a onj of fragment "frag_one"
FragmentTransaction fr_transaction = fragment_manager1.beginTransaction ();
fr_transaction.add (tab_frame.getId (),frag_one_obj,b);
fr_transaction.addToBackStack ("Fragment Add");
fr_transaction.commit ();
frag_one_obj.setArguments (bundle);
tab_frame.setVisibility (View.VISIBLE);
}
When I debug I am getting this error in logcat,
No view found for id 0x1 (unknown) for fragment frag_one{42bb0cc8 (d45032ae-5aa4-485f-87a6-da20f3d5ce1f) id=0x1 1}
Idon't know why that happens..I am already creating framelayout and assign a id for that..But this error tells there is no view assgned to this perticular id, so Fragment can't be added..
Pls help me..
Thanks in advance..
You need to add the created FrameLayout as child of your hosting view. Otherwise it is attached no where. That's why the view is not being find with it's id.

(Animation) Hide Toolbar in Fragment

Hey i want to hide my toolbar when i click inside an editText Therefore i added these lines
mORMetWeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!enterWeightClicked) {
enterWeightClicked = true;
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mToolbar.setVisibility(View.GONE);
mContainer.removeView(cv1);
mContainer.removeView(cv2);
mContainer.getChildAt(0);
header.getLayoutParams().height = (int) getResources().getDimension(R.dimen.smallEnterWeightHeight);
In my onCreateView method i added the transition effect but the animation now of the Toolbar looks horrible..
LayoutTransition transition = mContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
I simply like to let the Toolbar scroll out and the view to the top? Is this possible with the "simple" animation style?

how to show same Dialog in Activity and Fragment in Different situation?

I am doing a sample Project where I am using Navigation Drawer with Fragments.When I click in row item1 it opens Dialog Fragment1 .In Dialog Fragment1 I have a button .
My requirement is I want to open same Dialog which is triggred from navigation row item1 when button in Fragment is clicked ...
I am using Following code to make Dialog in Activity
public void showRegisterDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_register);
dialog.show();
}
and following code to open Dialog in Fragment
private void LoadFragmentView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new Fragment2();
showRegisterDialog();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
default:
break;
}
I need some guidelines,thank you..
I did some research and got the solution.
You need to use callback method in fragment.
For this purpose,you need to use the following codes:
your_button_on_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((YourActivityName)getActivity()).showRegisterDialog();
}
});

can i put a dialog in a spinner item?

I'm new in the android world but I got experience in java.
Im trying to show a dialog when I select an especific item in my spinner but, when I do it, my application has stopped.
I have 1 fragment and 1 class for the listener, instantiate an object from the fragment in the listener and try to call the dialog.
The code is somthing like this:
//Instantiate of class Guardar extends SherlockFragment.
Guardar controlador = new Guardar();
public void onItemSelected(final AdapterView parent, View view, int pos,long id) {
String addSM = parent.getItemAtPosition(pos).toString();
if (addSM == “Anyadir”){
// custom dialog
final Dialog dialog = new Dialog(controlador.context);
dialog.setContentView(R.layout.dialog_afegirsuper);
dialog.setTitle(“Title…”);
// set the custom dialog components – text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(“Android custom dialog example!”);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Is this possible to implement? another similar idea?
thanks a lot for any solution

ListView clipping

i working little bit with the ListView from JavaFx2. I´m running into one issue.
Is it possible to turn off the clipping of the ListCell/ListView?
I add an ImageView that has to be wider than the ListView and JavaFx2 shows automatically a scrollbar.
This my code snipped how i add the ImageView to my List:
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> blub = new ListCell<String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
StackPane p = new StackPane();
Label label = new Label(item);
p.getChildren().addAll(img, label);
setGraphic(p);
p.setAlignment(Pos.CENTER_LEFT);
}
}
};
blub.setStyle("-fx-background-color:transparent");
return blub;
}
});
Big thanks!
I don't think it's possible.
Maybe try to play with the Skin of the ListView. It seems that the scroll bar are managed in this class. It do not use a scroll pane.
Another solution could be replacing the ListView by a VBox in a ScrollPane.
Finally, you could try to modify img (by the way, where it come from, and what Class is it ?) to only show what you need.
Anyway, I'm interested by the solution you will use.

Resources