I created a fragment with some buttons and I'm trying to call other fragment by clicking on the button but nothing happens, no errors, no actions.
This is my implementation:
main_activit.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/grey"
tools:context=".MainActivity"
android:weightSum="1"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/mainToobar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="#string/mainToobarTitle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/homebuttons_fragment_container"
android:layout_width="match_parent"
android:layout_height="200dp" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
HomeButtonsFragment.java
public class HomeButtonsFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
View view = inflater.inflate(R.layout.fragment_home_buttons, container, false);
Button button = (Button) view.findViewById(R.id.schedule_home_button);
button.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment);
}
}
fragment_home_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/schedule_home_button"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:text="#string/label.home.button.schedule"
android:tag="home_button"
android:stateListAnimator="#null"
style="#style/ScheduleButton"
/>
</RelativeLayout>
You miss .commit() in your fragmentTransaction statement.
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
u missed commit();,i am sure small things will be missed sometimes,just add commit(); , it will work fine
use this
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
or
fragmentTransaction.commit();
}
Your onClick method should be,
#Override
public void onClick(View v) {
if(v.getId()==R.id.schedule_home_button){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
}
}
Related
So my goal is to swap from my CalculatorFragment to my CalculatorResultsFragment using a parent FrameLayout through a a button. I'm getting the error:
java.lang.IllegalArgumentException: No view found for id 0x7f08020f (com.example.flexplan:id/parent_fragment) for fragment CalculatorResultsFragment{1046933} (71b49e01-2328-4454-870b-5acb04ccc3d0 id=0x7f08020f)
fragment_calculator_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Calculator.CalculatorParentFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent_fragment"/>
</LinearLayout>
CalculatorParentFragment.java
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.parent_fragment, new CalculatorFragment());
transaction.commit();
CalculatorFragment.java
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.parent_fragment, new CalculatorResultsFragment());
transaction.commit();
}
});
Anyone know how I can fix this?
Error says you do not have view for your fragment, check your layout files, for each fragment you need to have one layout, if not transaction can not send you there. After all layouts in place, follow the docs link like so:
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setReorderingAllowed(true);
transaction.replace(R.id.parent_fragment, CalculatorResultsFragment.class, null);
transaction.commit();
how to open and close fragment while clicking on same button . Just like amazon filter button.
when we click first time fragment should apper and then we click on that button again fragment should disapper.
business activity java file
public class business extends AppCompatActivity {
TextView t1, t2;
Button hello;
boolean flag = false;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
hello = findViewById(R.id.hello);
t1=findViewById(R.id.text1);
t2=findViewById(R.id.textt2);
}
#Override
protected void onStart() {
super.onStart();
hello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
BusinessSort businessSort = new BusinessSort();
fragmentTransaction = manager.beginTransaction();
if (flag) {
fragmentTransaction.remove(businessSort);
flag=false;
} else {
fragmentTransaction.add(R.id.frgmCont, businessSort);
flag=true;
}
fragmentTransaction.commit();
}
});
}
public void f1(String s1, String s2) {
t1.setText(s1);
t2.setText(s2);
}
}
activity_business.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".business"
android:orientation="vertical"
android:id="#+id/businessLinearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:id="#+id/hello"
/>
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit2"
android:id="#+id/textt2"/>
<FrameLayout
android:id="#+id/frgmCont"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Fagment file BusinessSort.java
public class BusinessSort extends Fragment {
EditText editText1,editText2;
Button submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_business_sort, container, false);
editText1=view.findViewById(R.id.edit1);
editText2=view.findViewById(R.id.edit2);
submit=view.findViewById(R.id.businessfragment_submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = editText1.getText().toString();
String s2 = editText2.getText().toString();
business business = (com.example.project.business) getActivity();
business.f1(s1,s2);
}
});
return view;
}
fragment_business_sort.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BusinessSort"
android:orientation="vertical">
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<Button
android:id="#+id/businessfragment_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
</LinearLayout>
Try replacing the fragment.
fragmentTransaction.replace(R.id.frgmCont, businessSort);
flag = true;
I hope the code is mostly similar to my approach so let me know if the problem still persists so that I will look into it deeper.
public static boolean fragmentState = false;
onClick(){
if(fragmentState){
fragmentState = false;
getActivity().getFragmentManager().beginTransaction().
remove(SomeFragment.this).commit();
}
else{
fragmentState = true;
Fragment someFragment = new SomeFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment );
transaction.addToBackStack(null);
transaction.commit();
}
}
Please make sure that your button is not a part of that fragment.
You need to save the state of the fragment.
I've been looking for the cause of this exception in answers for similar issues, and couldn't find what's wrong with my code. I changed the method that instantiates a fragment a few times and got the same exception. Maybe it has something to do with the splash screen. Couldn't find information about that.
Here's the beginning of the MainActivity in which the method for instantiating a fragment is called:
public class MainActivity extends AppCompatActivity {
Context context;
FragmentTransaction ft;
Fragment mlf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
File file = new File(path);
if (file.exists()) {
Log.d(TAG, path + " exists");
new DBConnection(MainActivity.this);
if(savedInstanceState == null) {
toMovieListFragment();
}
//...
//...
Here's the method:
private void toMovieListFragment() {
mlf = new MovieListFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_ontainer, mlf);
ft.addToBackStack(null); // add to back stack
ft.commit();
}
}
The MainActivity's xml:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragments_container">
</LinearLayout>
The fragment class:
public class MovieListFragment extends Fragment {
Context context;
TextView listTtl;
RecyclerView rvMovies;
Adapter moviesAdapter;
List<Movie> moviesList;
Fragment mdf;
FragmentTransaction ft;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_list_movies, container, false);
context = getActivity();
listTtl = rootView.findViewById(R.id.moviesListTtlId);
rvMovies = rootView.findViewById(R.id.moviesRVId);
moviesList = new ArrayList<>();
moviesAdapter = new Adapter(context, moviesList);
rvMovies.setAdapter(moviesAdapter);
// setting a layout manager
rvMovies.setLayoutManager(new LinearLayoutManager(context)); // a regular one
Log.i(TAG,"In movieListFragment");
return rootView;
}
}
The fragment class's xml file:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/moviesListTtlId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dot_height"
android:text="#string/moviesListTitle"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/ttlSize" />
<android.support.v7.widget.RecyclerView
android:id="#+id/moviesRVId"
android:layout_margin="#dimen/dimen_10"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<include layout="#layout/change_name" />
Thanks.
For some reason your main activity layout is called R.layout.activity_splash I believe your intention was for it to be whatever main activity layout is called (the one that has R.id.fragments_container inside).
I have a sign up fragment whose root layout is ScrollView and the child is LinearLayout. I have edit texts inside that LinearLayout. But when I click on an EditText and try to scroll the view, it doesn't work.
To open this fragment I have to click on signup view at login fragment.
Here is my SignUpFragment.java
public class SignUpFragment extends Fragment {
private EditText etFirstName, etLastName, etEmail, etPassword, etcity, etMobile;
private String firstName, lastName, email, password, city, mobile;
private Button proceed;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sign_up, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews(view);
proceed.setOnClickListener(v -> {
getStrings();
if (checkFields()) {
Bundle bundle = new Bundle();
bundle.putString("firstName", firstName);
bundle.putString("lastName", lastName);
bundle.putString("email", email);
bundle.putString("password", password);
bundle.putString("mobile", mobile);
bundle.putString("city", city);
jumpToDeliverAddress(bundle);
}
});
}
private void jumpToDeliverAddress(Bundle bundle) {
Fragment fragment = new DeliveryAddressFragment();
FragmentManager fragmentManager = getFragmentManager();
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private boolean checkFields() {
if (firstName.trim().isEmpty()) {
etFirstName.setError("Please enter a valid first name");
return false;
}
if (lastName.trim().isEmpty()) {
etLastName.setError("Please enter a valid last name");
return false;
}
if (email.trim().isEmpty() || (!isEmailValid())) {
etEmail.setError("Please enter a valid email");
return false;
}
if (password.trim().length() < 6) {
etPassword.setError("Password must contain 6 digits");
return false;
}
if (mobile.trim().length() != 10) {
etMobile.setError("Please enter a valid mobile number");
return false;
}
if (city.trim().isEmpty()) {
etcity.setError("Please enter a valid city");
return false;
}
return true;
}
private boolean isEmailValid() {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private void getStrings() {
firstName = Constants.getString(etFirstName);
lastName = Constants.getString(etLastName);
email = Constants.getString(etEmail);
password = Constants.getString(etPassword);
city = Constants.getString(etcity);
mobile = Constants.getString(etMobile);
}
private void findViews(View view) {
etFirstName = view.findViewById(R.id.firstName);
etLastName = view.findViewById(R.id.lastName);
etEmail = view.findViewById(R.id.email);
etPassword = view.findViewById(R.id.password_edittext);
etcity = view.findViewById(R.id.city);
etMobile = view.findViewById(R.id.mobile_number_edittext);
proceed = view.findViewById(R.id.proceed);
}
}
And the fragment_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".fragments.SignUpFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/firstName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="First name"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/lastName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Last name"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/mobile_number_edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Mobile number"
android:inputType="number"
android:maxLength="10" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/city" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/proceed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Proceed to add delivery address" />
</LinearLayout>
</ScrollView>
Please note that, currently, my flow is this:-
Mainactivity loads LoginFragment -> click on signup -> signUpfragment opens.
If I directly open signup fragment from MainActivity, scroll works fine.
MainActivity.java
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener, DrawerLocker {
public static SharedPreferences sharedPreferences;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
private Toolbar toolbar;
private VolleyCallback volleyCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
initDrawer();
if (checkToken())
jumpToHome();
else
jumpToLogin();
}
private void jumpToLogin() {
Fragment loginFragment = new LoginFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, loginFragment);
fragmentTransaction.commit();
}
private void jumpToHome() {
Fragment homeFragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, homeFragment);
fragmentTransaction.commit();
}
private boolean checkToken() {
APICall apiCall = new APICall(volleyCallback, this);
String token = apiCall.getTokenFromLocal(this);
return !token.isEmpty();
}
private void initDrawer() {
toggle = new ActionBarDrawerToggle(this,
drawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
drawerLayout = findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_manage_address)
jumpToManageAddress();
else if (id == R.id.nav_book_order)
jumpToBookOrder();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void jumpToBookOrder() {
Fragment bookOrderFragment = new BookOrderFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, bookOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void jumpToManageAddress() {
Fragment manageAddressFragment = new NewAddressFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, manageAddressFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
#Override
public void setDrawerLocked(boolean enabled) {
// if (enabled) {
// drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//// WindowManager.LayoutParams.FLAG_FULLSCREEN);
// toggle.setDrawerIndicatorEnabled(false);
// } else {
// drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
//// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// toggle.setDrawerIndicatorEnabled(true);
// }
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:elevation="5dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/MyToolbarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/nav_items">
<RelativeLayout
android:id="#+id/expandable_Frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
LoginFragment.java
public class LoginFragment extends Fragment {
private Button loginButton;
private VolleyCallback volleyCallback;
private EditText mobileNumber, password;
private TextView signup;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// ((MainActivity) getActivity()).setDrawerLocked(true);
return inflater.inflate(R.layout.fragment_login, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mobileNumber = view.findViewById(R.id.mobile_number_edittext);
loginButton = view.findViewById(R.id.login);
password = view.findViewById(R.id.password_edittext);
signup = view.findViewById(R.id.signup);
// ((MainActivity) getActivity()).getSupportActionBar().setTitle("Login");
signup.setOnClickListener(v -> jumpToSignup());
loginButton.setOnClickListener(v -> {
if (checkLength(mobileNumber, 10) && checkLength(password, 6)) {
apiCallForLogin(Constants.getString(mobileNumber), Constants.getString(password));
} else {
if (!checkLength(mobileNumber, 10))
mobileNumber.setError("Enter a valid mobile number");
else
password.setError("Password should contain atleast 6 digits");
}
});
}
private void jumpToSignup() {
Fragment fragment = new SignUpFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void apiCallForLogin(String mobileNumber, String password) {
APICall apiCall = new APICall(volleyCallback, getContext());
apiCall.addParams("mobile", mobileNumber);
apiCall.addParams("password", password);
}
}
fragment_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.LoginFragment">
<android.support.design.widget.TextInputLayout
android:id="#+id/mobile_number_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/password_layout"
android:layout_centerHorizontal="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/mobile_number_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/enter_your_mobile_number"
android:inputType="number"
android:maxLength="10"
android:minEms="10" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/password_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/password_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:maxLength="10"
android:minEms="10" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/password_layout"
android:layout_centerHorizontal="true"
android:text="Login" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:text="#string/new_user_signup_here"
android:textColor="#color/black"
android:textSize="15sp"
android:id="#+id/signup"
/>
</RelativeLayout>
Its hard to understand the exact problem that you are having without seeing your MainActivity code. However, based on some common problems, I would suggest to use a NestedScrollView here in this case.
However, you might consider wrapping the ScrollView with an extra LinearLayout. So the final layout will be looking something like the following.
<LinearLayout>
<ScrollView>
<LinearLayout>
<!-- Other views -->
<LinearLayout/>
<ScrollView/>
<LinearLayout/>
I'm trying to have a fragment loaded as the default view for the activity, but I just get a blank screen and huge memory leaks.
Activity file onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getFragmentManager();
int fragmentTransaction = fragmentManager.beginTransaction()
.add(R.id.login_screen_fragment, new FragmentLogin())
.commit();
}
}
The XML for the activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_screen_fragment"
class="com.test.project.FragmentLogin"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/forgotten_password_fragment"
class="com.test.project.FragmentForgottenPassword"
/>
</FrameLayout>
And the Fragment class (relevant parts):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
final Activity activity = getActivity();
... code ...
return view;
}
I followed instructions from a tutorial someone suggested earlier from a different question, but I must be doing something horribly wrong. Any ideas?
xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/changeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
</FrameLayout>
your activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentLogin f1= new FragmentLogin();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.changeFragment, f1);
fragmentTransaction.commit();
}
}
I'm new to Android and I ran into the same problem. Here's how I fixed mine.
So in your onCreate method, use getSupportFragmentManager() from the FragmentManager class then declare FragmentTransaction separately. See below. I did this for my case and it worked.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.login_screen_fragment, new FragmentLogin());
fragmentTransaction.commit();
}
Hope this helps solve your problem as well.
Sincerely,
sylvery
Just replace the fragment element with FrameLayout, then you can see your fragment.
If you wanted to add the Fragment programatically, then remove the <Fragment> in your Framelayout and add some ID unto it.
It should look like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/login_screen_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity" />