Android goto another fragment from dialog fragment - android

I had created DialogFragment in my application. Here is the code:
public static class SkipDialogFragment extends DialogFragment implements
OnClickListener {
private Button btnYes;
private Button btnCancel;
//private PerformActionListener mListener;
private AlertDialog ad;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//mListener = (PerformActionListener) getActivity();
LayoutInflater li = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customDialogView = li.inflate(R.layout.dialog_alert_yes_no,
null);
TextView txtMessage = (TextView) customDialogView
.findViewById(R.id.txtMessage);
txtMessage.setText(getResources().getString(
R.string.disagreement_stmt));
btnYes = (Button) customDialogView.findViewById(R.id.btnOk);
btnYes.setOnClickListener(this);
btnCancel = (Button) customDialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(this);
ad = new AlertDialog.Builder(getActivity()).setView(
customDialogView).create();
return ad;
}
#Override
public void onClick(View v) {
if (v.equals(btnYes)) {
new CreateProfileFragment().gotoHome();
//mListener.onActionPerformed();
ad.dismiss();
}
if (v.equals(btnCancel)) {
ad.dismiss();
}
}
}
When the user clicks on Positive button in the dialog, it should navigate to another fragment. Here is the code for fragment to be displayed:
private void gotoHome() {
HomeFragment homeFragment = (HomeFragment) getFragmentManager()
.findFragmentById(R.id.home_fragment);
if (homeFragment != null) {
homeFragment.initializeView();
} else {
homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.replace(R.id.linFragmentContainer, homeFragment,
AppUtils.HOME_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
}
But, whenever I click on Yes button in the dialog, getFragmentManager() in the gotoHome() returns null. I am not getting why is this happening.

Related

Visibility in an OnClickListener

I have these TextViews on ImageViews. When you click an ImageView, the text on that particular ImageView should appear and the rest of the TextViews on the other ImageViews should disappear. How can I do this?
I know it's to do with if/else statements, but cannot seem to figure out what exactly I should use with these statements.
public class MainActivity extends AppCompatActivity {
//Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
private FirebaseAuth mAuth;
//Declaring the Toolbar instance variable.
private Toolbar mMainAppBar;
//ViewPager on the activity_main.xml file
private ViewPager mMainActivityViewPager;
private TextView mShopTextView;
private TextView mSocialTextView;
private TextView mChatTextView;
private TextView mInventoryTextView;
private TextView mSettingsTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
mAuth = FirebaseAuth.getInstance();
//Setting the App Bar on the MainActivity page.
mMainAppBar = findViewById(R.id.mainToolBar);
//setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
//Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
setSupportActionBar(mMainAppBar);
mShopTextView = findViewById(R.id.shopTextView);
mSocialTextView = findViewById(R.id.socialTextView);
mChatTextView = findViewById(R.id.chatTextView);
mInventoryTextView = findViewById(R.id.inventoryTextView);
mSettingsTextView = findViewById(R.id.settingsTextView);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
//Setting up the clicking of ImageViews on the Bottom Navigation bar.
//How I did it: https://teamtreehouse.com/community/starting-a-fragment-from-an-activity
ImageView shopImageView = (ImageView) findViewById(R.id.shopImageView);
shopImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
mShopTextView.setVisibility(View.VISIBLE);
}
});
ImageView socialImageView = (ImageView) findViewById(R.id.socialImageView);
socialImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SocialFragment socialFragment = new SocialFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
transaction.commit();
TextView socialTextView = (TextView) findViewById(R.id.socialTextView);
socialTextView.setVisibility(View.VISIBLE);
}
});
ImageView chatImageView = (ImageView) findViewById(R.id.chatImageView);
chatImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChatFragment chatFragment = new ChatFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
transaction.commit();
TextView chatTextView = (TextView) findViewById(R.id.chatTextView);
chatTextView.setVisibility(View.VISIBLE);
}
});
ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
inventoryImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InventoryFragment inventoryFragment = new InventoryFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
transaction.commit();
TextView inventoryTextView = (TextView) findViewById(R.id.inventoryTextView);
inventoryTextView.setVisibility(View.VISIBLE);
}
});
ImageView settingsImageView = (ImageView) findViewById(R.id.settingsImageView);
settingsImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SettingsFragment settingsFragment = new SettingsFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
transaction.commit();
TextView settingsTextView = (TextView) findViewById(R.id.inventoryTextView);
settingsTextView.setVisibility(View.VISIBLE);
}
});
}
EDIT:
Here is what the bar looks like: Screenshot.
all those icons are ImageViews and the "hello" are TextViews. When I click one ImageView, I want that particular TextView to appear and all the other TextViews to disappear.
The answer by Momen Zaqout is correct
I'll just rewrite the code in a more optimized way
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
private FirebaseAuth mAuth;
//Declaring the Toolbar instance variable.
private Toolbar mMainAppBar;
//ViewPager on the activity_main.xml file
private ViewPager mMainActivityViewPager;
private TextView mShopTextView,mSocialTextView,mChatTextView,mInventoryTextView,mSettingsTextView;
private ImageView shopImageView,socialImageView,chatImageView,inventoryImageView,settingsImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
mAuth = FirebaseAuth.getInstance();
//Setting the App Bar on the MainActivity page.
mMainAppBar = findViewById(R.id.mainToolBar);
//setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
//Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
setSupportActionBar(mMainAppBar);
mShopTextView = findViewById(R.id.shopTextView);
mSocialTextView = findViewById(R.id.socialTextView);
mChatTextView = findViewById(R.id.chatTextView);
mInventoryTextView = findViewById(R.id.inventoryTextView);
mSettingsTextView = findViewById(R.id.settingsTextView);
shopImageView = (ImageView) findViewById(R.id.shopImageView);
socialImageView = (ImageView) findViewById(R.id.socialImageView);
chatImageView = (ImageView) findViewById(R.id.chatImageView);
inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
settingsImageView = (ImageView) findViewById(R.id.settingsImageView);
shopImageView.setOnClickListener(this);
socialImageView.setOnClickListener(this);
chatImageView.setOnClickListener(this);
inventoryImageView.setOnClickListener(this);
settingsImageView.setOnClickListener(this);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shopImageView:
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
mShopTextView.setVisibility(View.VISIBLE);
break();
case R.id.socialImageView:
SocialFragment socialFragment = new SocialFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
transaction.commit();
mSocialTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.chatImageView:
ChatFragment chatFragment = new ChatFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
transaction.commit();
mChatTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.inventoryImageView:
InventoryFragment inventoryFragment = new InventoryFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
transaction.commit();
mInventoryTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
break();
case R.id.settingsImageView:
SettingsFragment settingsFragment = new SettingsFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
transaction.commit();
mSettingsTextView.setVisibility(View.VISIBLE);
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
break();
}
}
you can use this method to disappear all textviews and then appear the desired textview.
void changeTexViews(View desired ){
mShopTextView.setVisibility(View.GONE);
mSocialTextView.setVisibility(View.GONE);
mChatTextView.setVisibility(View.GONE);
mInventoryTextView.setVisibility(View.GONE);
mSettingsTextView.setVisibility(View.GONE);
desired .setVisibility(View.VISIBLE);
}
and in each ImageView.setOnClickListener use this method,
for example
shopImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShopFragment shopFragment = new ShopFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
transaction.commit();
changeTexViews(mShopTextView);
}
});
**Here I want to provide some help tips to **
if you want hide or show any element first thing is if you want show all element visible at first time you can set by default visibility to View.VISIBLE else you can use View.Gone.
and after that in which textview or image view click you want show or hide any element you can perform inside the onClickListener() like below example.
ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
inventoryImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
demotext.setVisibility(View.VISIBLE);
demoImage.setVisibility(View.Gone)
}
});

AlertDialog not working in a Fragment

Hello I'm doing an app in which a button is pressed and an AlertDialog window pops up with a TimePicker. However it is in a Fragment so AppCompatActivity will not do.
Fragment itself:
public class time_change extends Fragment implements TimePickerFragment.TimeDialogListener {
private static final String DIALOG_TIME = "MainActivity.TimeDialog";
private View v;
private Button timePickerAlertDialog;
private ImageButton back;
#Override
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.pamokos, container, false);
timePickerAlertDialog = (Button) v.findViewById(R.id.alert_dialog_time_picker);
timePickerAlertDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimePickerFragment dialog = TimePickerFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");
}
});
back = (ImageButton) v.findViewById(R.id.back_btn1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Pamokos fr = new Pamokos();
time_change fr2 = new time_change();
android.app.FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.activity_frag, fr);
fragmentTransaction.remove(fr2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return v;
}
#Override
public void onFinishDialog(String time) {
Toast.makeText(getActivity(), "Selected Time : " + time, Toast.LENGTH_SHORT).show();
}
}
But It shows an error at
dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");
Saying "Cannot resolve method 'show(android.app.FragmentManager, java.lang.String)'"
Full code is from this tutorial https://www.androidtutorialpoint.com/basics/android-alert-dialog-tutorial-working-time-picker-date-picker-list-dialogs/
I'm quite lost.
Replace
TimePickerFragment dialog = TimePickerFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");
with this
FragmentManager fm = getActivity().getSupportFragmentManager();
TimePickerFragment dialog = new TimePickerFragment ();
dialog.show(fm, "TimePickerFragment");
add
below
import inside your DialogFragment class
import android.support.v4.app.DialogFragment;
Replace
dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");
with
dialog.show(getSupportFragmentManager(), "TimePickerFragment");

Remove All Fragment in AppCompatActivity when clicking the Checkbox?

I have issue in fragment, i have to remove all added fragment when clicking the check box, but now fragments are not removed, please guide me to resolve this issue, here by i have added my code below,
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtVw_showfirstfragment,txtVw_showsecondfragment;
FragmentTransaction ft;
CheckBox checkBox;
fragment1 frag1 = new fragment1();
fragment2 frag2 = new fragment2();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtVw_showfirstfragment = (TextView) findViewById(R.id.txtVw_showfirstfragment);
txtVw_showsecondfragment = (TextView) findViewById(R.id.txtVw_showsecondfragment);
checkBox = (CheckBox)findViewById(R.id.checkBox);
txtVw_showfirstfragment.setOnClickListener(this);
txtVw_showsecondfragment.setOnClickListener(this);
checkBox.setText("I Agree"); // Prompting "Hide Password"
// Begin the transaction
checkBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
if(frag1!=null) ft.remove(frag1);
if(frag2!=null) ft.remove(frag2);
}
}
} );
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtVw_showfirstfragment:
String backFragment1 = getApplicationContext().getClass().getName();
ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder,frag1);
ft.addToBackStack(backFragment1);
// Complete the changes added above
ft.commit();
break;
case R.id.txtVw_showsecondfragment:
String backFragment2 = getApplicationContext().getClass().getName();
ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder,frag2);
ft.addToBackStack(backFragment2);
// Complete the changes added above
ft.commit();
break;
}
}
}
Use getSupportFragmentManager().beginTransaction().remove(frag1).commit(); instead of ft.remove(frag1);

How to setOnclick Listener to button in fragment

I have button in fragment1. I want to click this buttton and replace fragment1 by fragment2 but my app still close when I try to run it
This is my code in fragment1
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_menu, container, false);
// gallery = (Gallery) rootView.findViewById(android.R.id.list);
//gallery.setAdapter(new ItemAdapter(generateData(), getActivity()));
Button button = (Button) rootView.findViewById(R.id.imageButtonAll);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment);
transaction.commit();
}
});
return rootView;
}
}
You cant open a fragment from inside another fragment.
So you have to move your code from the onClick to your activity and run it there.
In your Activity (lets assume its MainActivity):
public void openMyFragment(){
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment);
transaction.commit();
}
And then, add this in your fragments onClick:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) getActivity()).openMyFragement();
}
});
The problem is that you are trying to replace a view that does not exist within the view that you are inflating. You have to switch these fragments from your FragmentActivity, which has the contentview containing the layout you are trying to replace.
MainActivity:
class MainActivity extends FragmentActivity {
public Fragment frag1;
public Fragment frag2;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
frag1 = new Frag1();
frag2 = new Frag2();
//assumption to switch to frag 1 immediately
switchToFragment(R.id.frame_container, homeFragment);
...
}
/** Switch UI to the given fragment (example) */
void switchToFragment(Fragment newFrag) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newFrag)
.commitAllowingStateLoss();
currentFragment = newFrag;
}
}
Fragment:
....
final Activity activity = getActivity();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
about.setTextColor(Color.WHITE);
if(activity instanceof MainActivity) {
((MainActivity) activity).switchToFragment(((MainActivity) activity).frag1));
}
}
});
...

Android: activity does not work after backing from a fragment

i have an activity like WelcomeActivity and a fragment like GuideFragment.
i have a button in WelcomeActivity which cause a navigation to GuideFragment. Also i have a button in GuideFragment which navigates back to the WelcomeActivity.
For first time it goes to GuideFragment by clicking on the button and cames back to WelcomeActivity by pressing btn_back. Hwever after coming back to activity, the button does not work any more.
whats wrong with my code?
activity clas:
public class WelcomeActivity extends FragmentActivity
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GuideFragment guideFragment = new GuideFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
guideFragment = new GuideFragment();
}
public void onGuideClick(View view)
{
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentManager.popBackStack();
fragmentTransaction.commit();
}
}
and fragment class:
public class GuideFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.guideFrag);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
View view = inflater.inflate(R.layout.fragment_guide, container, false);
Button button = (Button) view.findViewById(R.id.btn_back);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
getActivity().getFragmentManager ().popBackStack();
}
});
return view;
}
}
Thanks
Remove fragmentManager.popBackStack(); from onGuideClick(View view). Also the transaction should not be a member field.
public void onGuideClick(View view) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
You also initiate the GuideFragment twice, once is enough.
I changed WelcomeActivity like this:
public class WelcomeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
public void onGuideClick(View view)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GuideFragment guideFragment = new GuideFragment();
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}

Categories

Resources