I have a activity with only 2 buttons and a FrameLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<Button
android:id="#+id/btPrincipal1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ver Fragment 1"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
/>
<Button
android:id="#+id/btPrincipal2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ver Fragment 2"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<FrameLayout
android:layout_width="250dp"
android:layout_height="match_parent"
android:id="#+id/contenedor"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/linearLayout">
</FrameLayout>
When I click button 1, I call the fragment and, when I click button 2, I want to call the same fragment but with different properties. For example:
I click button 1 , call fragment A with background color green.
I click button 2 , call fragment A with background color red.
It's possible? and how can I do this? Thanks.
In your MainActivity, do:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.btPrincipal1);
Button button2 = (Button) findViewById(R.id.btPrincipal2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString(COLOR_PARAM, "green");
supportFragmentManager.beginTransaction()
.replace(R.id.contenedor, YourFragment.newInstance("Button1"))
.commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString(COLOR_PARAM, "red");
supportFragmentManager.beginTransaction()
.replace(R.id.contenedor, YourFragment.newInstance("Button2"))
.commit();
}
});
}
Then, in YourFragment.java, do:
public class YourFragment extends Fragment {
public static final String COLOR_PARAM = "param1";
private String currentColor;
public YourFragment() {}
public static Fragment newInstance(String param) {
Fragment fragment = new Fragment();
Bundle args = new Bundle();
args.putString(COLOR_PARAM, param);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
currentColor = getArguments().getString(COLOR_PARAM);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_layout);
if (currentColor.equals("green")) {
frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_green_dark));
}
else if (currentColor.equals("red")) {
frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_red_dark));
}
return view;
}
}
If you want to change the background just create and object of your fragment, and after clicking on buttons call a method to change the fragment background. but if you wanna to change fragments layout or other things create a new fragment cause of code readability.
try this:
public class FragmentA extends Fragment {
public static FragmentA newInstance(int colorResId) {
FragmentA fragment = new FragmentA();
Bundle bundle = new Bundle();
bundle.putInt("colorId", colorResId);
fragment.setArguments(bundle);
return new FragmentA();
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
int resId = getArguments().getInt("colorId", 0);
if (resId != 0) {
//set background
}
}
public void changeBackground(int colorId){
//set background
}
}
Related
I have BottomSheetFragment with recyclerview and fab button. I am having issue in showing up Floating action button in BottomSheetBehavior.STATE_COLLAPSED. Fab button come up as soon as i expand bottom sheet to fullscreen i tried several method but none work.
I tried different fab option in layout to make it visible but no luck till now.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/topBarBottomSheet"
android:clipToPadding="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/topBarBottomSheet">
<include layout="#layout/progressbar_framelayout" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleviewGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:listitem="#layout/recycler_view_item_3"
tools:spanCount="3"
tools:layoutManager="GridLayoutManager" />
</FrameLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabBottomSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:backgroundTint="#color/greenElaxer"
app:fabSize="normal"
app:srcCompat="#drawable/ic_check"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:overScrollMode="always"
>
</com.google.android.material.floatingactionbutton.FloatingActionButton>
</RelativeLayout>
BottomSheetFragment
public class BottomSheet extends BottomSheetDialogFragment {
FloatingActionButton fab;
RecyclerView recyclerView;
// TODO: Customize parameters
public static BottomSheet newInstance() {
/*final Bundle args = new Bundle();
args.putInt(ARG_ITEM_COUNT, itemCount);
fragment.setArguments(args);*/
return new BottomSheet();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycleviewGallery);
fab = view.findViewById(R.id.fabBottomSheet);
BottomSheetAdapter bottomSheetAdapter = new BottomSheetAdapter();
GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(bottomSheetAdapter);
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
//Get the BottomSheetBehavior
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
if (bottomSheet != null) {
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheet.setMinimumHeight(350);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View view, int i) {
switch (i){
case BottomSheetBehavior.STATE_COLLAPSED:
Log.d(TAG,"Collapsed");
break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.d(TAG,"Dragging");
break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.d(TAG,"Expanded");
break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
Log.d(TAG,"Half Expanded");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.d(TAG,"Hidden");
dismiss();
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.d(TAG,"Settling");
break;
}
}
#Override
public void onSlide(#NonNull View view, float v) {
}
});
}
}
});
return dialog;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
final Fragment parent = getParentFragment();
Log.d(TAG,"Parent = "+parent+" Context "+context);
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context;
}
}
#Override
public void onDetach() {
mListener = null;
super.onDetach();
}
}
Whenever i collapsed bottom sheet the fab button also make itself down with bottom sheet.i need to make my fab button stick to same place whether i expand or collapsed bottom sheet. Thanks in advance.
I need to stick to same layout(Relative layout)
You can't make an UI element from a certain fragment stick around when you're exiting that fragment. If a bottomsheet is collapsed, all its UI elements are collapsed. You can't make on stick around.
You could write a duplicate fab that shows after you closed the bottomsheet in the underlying fragment.
For example, I have this code in my bottomsheet to exit the bottomsheet dialog.
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetDialog.dismiss();
mTimePickerDialogController.show(0, 0, makeTimePickerDialogTag());
}});
When you dismiss the bottomsheet, you could use the following code to make a fab in the underlying fragment appear or disappear.
mFloatingActionButton.hide();
mFloatingActionButton.show();
Edit: A better solution for handling the second fab.
BottomSheetBehavior sheetBehavior;
sheetBehavior = BottomSheetBehavior.from(yourBottomSheet);
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
mFloatingActionButton.show();
case BottomSheetBehavior.STATE_EXPANDED: {
mFloatingActionButton.hide();
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
mFloatingActionButton.show();
}
}
}
I want to open another fragment using a button from another fragment.
This is the fragment with the button
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_myday, container, false);
Button buttonNext = (Button)view.findViewById(R.id.next);
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment someFragment = new TomorrowFragment();
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, someFragment);
fr.commit();
}
});
return view;
Fragment xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next to 2"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:id="#+id/next"/>
after I press the button nothing happens. I have followed code from other posts but nothing worked..
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment someFragment = new TomorrowFragment();
FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, someFragment);
fr.commitAllowingStateLoss();
}
});
I want to open a fragment from my activity, but when I do both the Activity layout and fragment layout is inflated, I only want the fragment layout to display. Why is this?
Activity Code:
public class Login extends Activity {
public static String res="";
private EditText login_username,login_pass;
private TextView test_login;
private SharedPreferences sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btn_login = (Button) findViewById(R.id.btnLogin);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login(login_username.getText().toString(),login_pass.getText().toString());
}
});
private void login(final String user,String pass){
new Login_Server("http://192.168.1.100/test/index.php/client_users/Login",user,pass).execute();
final ProgressDialog pd=new ProgressDialog(Login.this);
pd.setMessage("Please Wait...");
pd.show();
final Timer tm=new Timer();
tm.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (res.equals("login")) {
Index index = new Index();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, index); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
tm.cancel();
res = "";
}
}
});
}
}, 1, 1500);
}
Fragment code:
public class Index extends Fragment implements View.OnClickListener {
private Button Index,Share;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.index, container, false);
Index=(Button)rootview.findViewById(R.id.index_page);
Share=(Button)rootview.findViewById(R.id.share_page);
btn_click();
return rootview;
}
public void btn_click(){
Index.setOnClickListener(this);
Share.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share_page:
Share share_page = new Share();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, share_page); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
break;
}
}
}
xml layout fragment:
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:weightSum="100">
<include layout="#layout/main_index"></include>
<include layout="#layout/bottom_menu"></include>
</LinearLayout>
On your login activity
setContentView(R.layout.login);
inflates the activity layout
and on your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.index, container, false);
Index=(Button)rootview.findViewById(R.id.index_page);
Share=(Button)rootview.findViewById(R.id.share_page);
btn_click();
return rootview;
}
inflates the fragment's layout
therefor you inflate 2 layouts
anyway if u wish to use the fragment layout allover the activity screen
u should change the activity layout to host a container with an ID
like in your code on the activity u have this line:
transaction.replace(android.R.id.content, index);
you should just have a view on the activity layout with the id=content and make it match_parent on height and width
this one should be the parent of all the other childs on the layout
that way the fragment will cover all of the activity layout when u replace the container
gl..
I would like to put a fragment inside a custom dialog in activity.
I receive this error:
java.lang.IllegalArgumentException: No view found for id 0x7f0c007e (copyworld.rebootcw:id/container_schedule1) for fragment FragmentGiorno{418215c0 #0 id=0x7f0c007e}
This is my code:
layout.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/spinner_dialog_schedulazione"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="#+id/container_schedule1">
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Popup_schedule();
}
//******POPUP PER LA SCHEDULAZIONE******************
public void Popup_schedule() {
dialog_schedule=new Dialog(MainActivity.this);
dialog_schedule.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog_schedule.setContentView(R.layout.dialog_schedule);
dialog_schedule.setCancelable(false);
lp = new WindowManager.LayoutParams();
window = dialog_schedule.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog_schedule.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window = dialog_schedule.getWindow();
window.setAttributes(lp);
FragmentGiorno fragment = new FragmentGiorno();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_schedule1, fragment);
fragmentTransaction.commit();
dialog_schedule.show();
}
}
How I add a fragment inside a custom dialog in MainActivity.java that extend Activity?
Try this
dialog_schedule.findViewById(R.id.container_schedule1)
Instead Of
R.id.container_schedule1
In your Code
If you want to load fragment inside dialog then you can use DialogFragment instead.
Below is the working example of using DialogFragment.
public class Demo extends DialogFragment{
View mainView;
LinearLayout layoutArrow;
Bundle bundle;
ArrayList<BookingDetail> bookingDetails;
View.OnClickListener onClickListener;
public static Demo newInstance(#NonNull ArrayList<BookingDetail> bookingDetails) {
Demo alertPopUpMapReservedBedInfo = new Demo();
Bundle args = new Bundle();
args.putSerializable("bookingDetails", bookingDetails);
alertPopUpMapReservedBedInfo.setArguments(args);
return alertPopUpMapReservedBedInfo;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.InvitationDialogWithSimpleAnim);
bundle = getArguments();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.alert_info, container, false);
getDialog().setCanceledOnTouchOutside(true);
return mainView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
layoutArrow = (LinearLayout) mainView.findViewById(R.id.layoutArrow);
FragmentGiorno fragment = new FragmentGiorno();
android.support.v4.app.FragmentTransaction fragmentTransaction =
this.getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_schedule1, fragment);
fragmentTransaction.commit();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if(onClickListener!=null){
onClickListener.onClick(null);
}
}
}
And call it by,
Demo newFragment = Demo.newInstance(bookingDetails);
newFragment.show(ft,"dialog");
I have an OnClickListener in a fragment activity as shown here:
public class Menu1_Fragment extends Fragment {
Button CreateGroupButton;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
CreateGroupButton = (Button) rootview.findViewById(R.id.create_study_group);
CreateGroupButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Menu1_Fragment.this.getActivity(), CreateGroup.class);
startActivity(intent);
}
});
return rootview;
}
}
Here is my menu1_layout.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:id="#+id/create_study_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/CreateGroupBtn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is the page where you can create and view existing groups"
android:id="#+id/textView"
android:layout_below="#+id/create_study_group"/>
</RelativeLayout>
When the Button is clicked, the "Test" message is displayed but the app closes right after.
Here is the CreateGroup java class:
public class CreateGroup extends Fragment{
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.create_group, container, false);
return rootview;
}
}
and the create_group.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is the page where the form is displayed"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
NavigationActivity:
public class NavigationActivity extends FragmentActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
FragmentManager fragmentManager = getSupportFragmentManager();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager =getSupportFragmentManager();
Fragment fragment= new Fragment();
switch (position){
case 0:
mTitle = getString(R.string.title_section1);
fragment = new Menu1_Fragment();
break;
case 1:
mTitle = getString(R.string.title_section2);
fragment = new Menu2_Fragment();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.navigation, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//if the log out button is selected, log out of Parse and go back to log in page
if (id == R.id.action_logout) {
ParseUser.logOut();
Intent intent = new Intent(NavigationActivity.this,LoginSignupActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Successfully Logged out", Toast.LENGTH_LONG).show();
finish();
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((NavigationActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
I'm having trouble figuring out why the CreateGroup class/layout file aren't being displayed as a new page.
The CreateGroup class is a fragment , the intent should be using an activity if startActivity is being used
CreateGroup is a fragment, so you should change fragments and not start an activity:
CreateGroupButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
// Create new fragment and transaction
Fragment createGroupFragment = new CreateGroup();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, createGroupFragment)
.commit();
}
});