Im trying to go from MainFragment to AddPostFragment with an ImageButton.
How is this possible...I tried a lot of things...
I want to use a fragment to keep my navigation drawer on top instead of a new activity.
MainFragment:
public class MainFragment extends Fragment implements View.OnClickListener{
ImageButton floatButton;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
floatButton = (ImageButton)rootView.findViewById(R.id.postButtonMain);
floatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent post = new Intent(getActivity(), AddPostFragment.class);
startActivity(post);
}
});
return rootView;
}
#Override
public void onClick(View v) {
}
}
AddPostFragment:
public class AddPostFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_add_post, container, false);
return rootView;
}
}
XML MainFragment:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Home"
android:id="#+id/frag_home"
android:layout_gravity="center_horizontal|top" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/postButtonMain"
android:src="#drawable/rsz_add_car"
android:background="?android:attr/selectableItemBackground"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="right|bottom" />
XML AddPostFragment:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Add Post"
android:id="#+id/add_post"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Airport"
android:id="#+id/text_airport"
android:layout_marginTop="88dp"
android:layout_below="#+id/add_post"
android:layout_toLeftOf="#+id/add_post"
android:layout_toStartOf="#+id/add_post" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:id="#+id/text_date"
android:layout_below="#+id/text_airport"
android:layout_alignRight="#+id/text_airport"
android:layout_alignEnd="#+id/text_airport"
android:layout_marginTop="36dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Time"
android:id="#+id/text_time"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/text_date"
android:layout_alignEnd="#+id/text_date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Persons"
android:id="#+id/text_persons"
android:layout_marginTop="43dp"
android:layout_below="#+id/add_time"
android:layout_toLeftOf="#+id/add_time"
android:layout_toStartOf="#+id/add_time" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Address"
android:id="#+id/text_address"
android:layout_marginTop="48dp"
android:layout_below="#+id/text_persons"
android:layout_alignLeft="#+id/text_persons"
android:layout_alignStart="#+id/text_persons" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="#+id/add_time"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/text_time"
android:layout_alignRight="#+id/add_post"
android:layout_alignEnd="#+id/add_post" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/add_date"
android:layout_alignTop="#+id/text_date"
android:layout_toRightOf="#+id/text_date"
android:layout_alignRight="#+id/add_time"
android:layout_alignEnd="#+id/add_time" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/add_persons"
android:layout_above="#+id/text_address"
android:layout_toRightOf="#+id/text_persons"
android:layout_alignRight="#+id/add_post"
android:layout_alignEnd="#+id/add_post" />
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_search"
android:layout_above="#+id/text_date"
android:layout_toRightOf="#+id/text_airport"
android:layout_toEndOf="#+id/text_airport" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_address"
android:layout_alignBottom="#+id/text_address"
android:layout_alignRight="#+id/add_post"
android:layout_alignEnd="#+id/add_post"
android:layout_toRightOf="#+id/text_address"
android:layout_toEndOf="#+id/text_address" />
MainMenu with navigation drawer:
public class MainMenu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Fragment someFragment;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_main, new MainFragment()).commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#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
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm = getFragmentManager();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the home action
fm.beginTransaction().replace(R.id.content_main, new MainFragment()).commit();
} else if (id == R.id.nav_demand) {
fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
} else if (id == R.id.nav_posts) {
fm.beginTransaction().replace(R.id.content_main, new PostsFragment()).commit();
} else if (id == R.id.nav_messages) {
fm.beginTransaction().replace(R.id.content_main, new MessagesFragment()).commit();
} else if (id == R.id.nav_settings) {
fm.beginTransaction().replace(R.id.content_main, new SettingsFragment()).commit();
} else if (id == R.id.nav_logout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
You have to do just the same thing you're doing on the onNavigationItemSelected
floatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction().replace(R.id.content_main, new PostsFragment()).commit();
}
});
Related
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've implemented TabLayout from the support library, but it overwrites the ActionBar that i need.
This is the activity that sets it up and inflates the tabs and loads the fragment for the tab.
public class TabsActivity extends android.support.v4.app.FragmentActivity {
FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
setTitle("Fuel Logger");
ActionBar a = getActionBar();
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(new FragmentPagerAdapter(
getSupportFragmentManager(), TabsActivity.this
));
TabLayout tabLayout = (TabLayout) findViewById(R.id.fuel_sliding_tabs);
tabLayout.setupWithViewPager(vp);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fuel_menu, menu);
MenuItem refresh = menu.findItem(R.id.action_settings);
refresh.setEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
break;
case R.id.action_favorite:
return true;
default:
}
return super.onOptionsItemSelected(item);
}
}
and the xml is just the view pager and tab layout
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
style="#style/FuelTabLayout"
android:id="#+id/fuel_sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1.27"
android:background="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_below="#+id/fuel_sliding_tabs" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#android:drawable/ic_menu_set_as"
app:layout_anchorGravity="bottom|right|end"
app:backgroundTint="#color/colorAccent"
android:layout_gravity="right"
android:cropToPadding="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp" />
</RelativeLayout>
and an example of fragments code which is more or less the same for each fragments class
Public class SummaryFragment extends android.support.v4.app.Fragment {
public static final String ARG_PAGE = "SUMM_PAGE";
private int mTab;
public static SummaryFragment newInstance(int page){
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
SummaryFragment frag = new SummaryFragment();
frag.setArguments(args);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTab = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_summary, null);
return view;
}
first add ToolBar , and use LinearLayout witch have horizontal orientation
and set the in the style NoActionBar
hope its help you
When I change the layout of a fragment within an activity, the menu options in the action bar seem to stop working. The buttons (Search icon and NavDrawer icon) become unresponsive.
This is the layout file of the fragment with the working menu options:
activity_login.xml (working)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginFragment"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login."
android:id="#+id/login_title_text"
android:layout_alignParentTop="true"
android:textSize="40sp"
android:layout_marginTop="100dp"
android:fontFamily="sans-serif-light"
android:textColor="#color/colorAlt"
android:layout_centerHorizontal="true" />
<!-- E-mail section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:ems="10"
android:id="#+id/login_emailField"
android:layout_above="#+id/login_passwordField" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_email_icon"
android:layout_alignBottom="#id/login_emailField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<!-- Password section -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_password_icon"
android:layout_alignBottom="#id/login_passwordField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:hint="Password"
android:id="#+id/login_passwordField"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="25dp" />
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_login_button"
android:layout_gravity="center_vertical"
android:layout_below="#+id/login_passwordField"
android:layout_marginBottom="29dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
card_view:cardBackgroundColor="#E91E63"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<TextView
android:id="#+id/material_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:layout_gravity="center_horizontal"
android:textColor="#FFF"
android:textSize="19sp"
android:fontFamily="sans-serif-medium"
android:text="Login" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgotten your username or password?"
android:layout_below="#id/login_login_button"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/login_footer_text"
android:layout_marginTop="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tap here"
android:textColor="#000000"
android:layout_below="#id/login_footer_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/loginForgottenTapText"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>
This is the layout file that seems to break the buttons:
activity_login.xml (non-working)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginFragment"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login."
android:id="#+id/login_title_text"
android:layout_alignParentTop="true"
android:textSize="40sp"
android:layout_marginTop="100dp"
android:fontFamily="sans-serif-light"
android:textColor="#color/colorAlt"
android:layout_centerHorizontal="true" />
<!-- E-mail section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:ems="10"
android:id="#+id/login_emailField"
android:layout_below="#+id/login_title_text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_email_icon"
android:layout_alignBottom="#id/login_emailField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<!-- Password section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:hint="Password"
android:layout_below="#id/login_emailField"
android:id="#+id/login_passwordField"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="25dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_password_icon"
android:layout_alignBottom="#id/login_passwordField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_login_button"
android:layout_gravity="center_vertical"
android:layout_below="#+id/login_passwordField"
android:layout_marginBottom="29dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
card_view:cardBackgroundColor="#E91E63"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<TextView
android:id="#+id/material_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:layout_gravity="center_horizontal"
android:textColor="#FFF"
android:textSize="19sp"
android:fontFamily="sans-serif-medium"
android:text="Login" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgotten your username or password?"
android:layout_below="#id/login_login_button"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/login_footer_text"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tap here"
android:textColor="#000000"
android:layout_below="#id/login_footer_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/loginForgottenTapText"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, LoginFragment.OnLoginCallback {
MenuItem register;
MenuItem login;
MenuItem logout;
MenuItem completeListing;
MenuItem expiredListing;
MenuItem activeListing;
NavigationView navigationView;
UserCredentialHandler userStatus;
protected static final String KEY_USER_STATUS = "USER_STATUS";
protected static final String USER_PREFS = "userNamePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userStatus = new UserCredentialHandler();
final HomeFragment homeFragment = new HomeFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), CreateListingActivity.class);
startActivity(i);
}
});
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getMenuItems(navigationView);
if(userStatus.checkIfUserIsLoggedIn(getApplicationContext())){
userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
updateNavDrawer("login", register, login, logout, expiredListing, completeListing, activeListing);
} else {
userStatus.setNavHeaderOnLogout(navigationView);
updateNavDrawer("logout", register, login, logout, expiredListing, completeListing, activeListing);
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment);
fragmentTransaction.commit();
}
#Override
public void onLoginSuccess() {
HomeFragment homeFragment = new HomeFragment();
userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
updateNavDrawer("login",register,login,logout, expiredListing, completeListing, activeListing);
register.setVisible(false);
login.setVisible(false);
logout.setVisible(true);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment);
fragmentTransaction.commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
getSupportFragmentManager().popBackStack();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_main, menu);
return true;
}
#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;
} else if (id == R.id.search_mag_icon){
SearchFragment searchFragment = new SearchFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, searchFragment)
.addToBackStack(null)
.commit();
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_register) {
final RegisterFragment registerFragment = new RegisterFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, registerFragment)
.addToBackStack(null)
.commit();
} else if (id == R.id.nav_login) {
final LoginFragment loginFragment = new LoginFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, loginFragment)
.addToBackStack(null)
.commit();
} else if(id == R.id.nav_logout) {
userStatus.logoutUser(getApplicationContext());
userStatus.setNavHeaderOnLogout(navigationView);
updateNavDrawer("logout",register,login,logout, expiredListing, completeListing, activeListing);
} else if (id == R.id.nav_my_listings) {
// navigate to my listings
} else if (id == R.id.nav_how) {
// navigate to how it works page
} else if (id == R.id.nav_help) {
// navigate to help page
} else if (id == R.id.nav_contact_us) {
// navigate to contact page,
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void updateNavDrawer(String action, MenuItem register, MenuItem login, MenuItem logout, MenuItem expiredListing, MenuItem completeListing, MenuItem activeListing){
if(action=="login"){
register.setVisible(false);
login.setVisible(false);
logout.setVisible(true);
completeListing.setVisible(true);
activeListing.setVisible(true);
expiredListing.setVisible(true);
} else {
register.setVisible(true);
login.setVisible(true);
logout.setVisible(false);
completeListing.setVisible(false);
activeListing.setVisible(false);
expiredListing.setVisible(false);
}
}
private void getMenuItems(NavigationView nv){
register = nv.getMenu().getItem(0);
login = nv.getMenu().getItem(1);
logout = nv.getMenu().getItem(2);
activeListing = nv.getMenu().getItem(3);
completeListing = nv.getMenu().getItem(4);
expiredListing = nv.getMenu().getItem(5);
}
}
I cannot seem to workout why changing the layout is breaking the ActionBar menu options, any help would be appreciated.
Edit:
Changing the <ScrollView> element to <LinearLayout> seems to solve it but I need a ScrollView. The ScrollView seems to overlap the ActionBar and is on the top.
Try setting android:layout_height on the ScrollView to "match_parent" instead of "wrap_content"
i have some problem here when i want to use my own customize Toolbar instead the default system Toolbar, now i want to use that toolbar on my MainActivity.
i have make my own xml resource file for the toolbar, and i have included it on my MainActivity layout file, but when i run the app i got some error said
android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.toolbar
I don't know what happen, i have try to change the setting on my xml file but still the error appear.
Please master, help me.
Thanks Before.
NB. This my code for app_bar.xml (toolbar resource file)
<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor">
and this one for my MainActivity.xml
<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"
android:background="#fafafa"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Button
android:id="#+id/btnNewTrans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:background="#drawable/custom_button"
android:text="#string/NewTransaction" />
<Button
android:id="#+id/btnViewCashflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:background="#drawable/custom_button"
android:text="#string/ViewCashflow" />
<Button
android:id="#+id/btnAddCateg"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_marginBottom="73dp"
android:layout_toLeftOf="#+id/btnRate"
android:layout_toStartOf="#+id/btnRate"
android:background="#drawable/custom_button"
android:text="#string/AddCategory" />
<Button
android:id="#+id/btnRate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnAddCateg"
android:layout_alignLeft="#+id/spnCategSelect"
android:layout_alignStart="#+id/spnCategSelect"
android:text="#string/RateUs" />
<Button
android:id="#+id/btnSetting"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnRate"
android:layout_toEndOf="#+id/btnRate"
android:layout_toRightOf="#+id/btnRate"
android:text="#string/Setting" />
<Button
android:id="#+id/btnAbout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnSetting"
android:layout_toEndOf="#+id/btnSetting"
android:layout_toRightOf="#+id/btnSetting"
android:text="#string/About" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/Trademark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Spinner
android:id="#+id/spnCategSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddCateg"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/btnViewCashflow"
android:layout_alignRight="#+id/textView"
android:layout_alignStart="#+id/btnViewCashflow"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNewTrans"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/SelectCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
and this one for my MainActivity.java
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
public static String catSelected = null;
private Toolbar toolbar;
//ArrayAdapter<String> adapterCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
catSelected = label;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public String getCatSelected(){
return catSelected;
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
#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;
}
return super.onOptionsItemSelected(item);
}
}
Classes are case sensitive: you have android.support.v7.widget.toolbar with a lowercase 't' when it should be android.support.v7.widget.Toolbar
This is my MainActivity.java File.
I got error while MainActivity is running in app. What should i do ?
public class MainActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
private Button btnTutor;
private Button btnStudent;
private TextView repIssue;
private TextView appVersion;
/**
* 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);
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
setContentView(R.layout.activity_main);
OnClickListener socialsClickListener = new OnClickListener(){
#Override
public void onClick(View v) {
Intent viewIntent = null;
viewIntent = new Intent(getApplicationContext(), MainActivity.class);
viewIntent.putExtra("PAGE", getSocialClicked(v.getId()));
startActivity(viewIntent);
}
};
// Loading Font Face
Typeface tfTitle = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
Typeface tfButtons = Typeface.createFromAsset(getAssets(), "Eraser.ttf");
((TextView) findViewById(R.id.txt_title)).setTypeface(tfTitle);
btnTutor = (Button)findViewById(R.id.btn_tutor);
btnTutor.setOnClickListener(socialsClickListener);
btnTutor.setTypeface(tfButtons);
btnStudent = (Button)findViewById(R.id.btn_student);
btnStudent.setOnClickListener(socialsClickListener);
btnStudent.setTypeface(tfButtons);
repIssue = (TextView) findViewById(R.id.txt_report);
repIssue.setOnClickListener(socialsClickListener);
appVersion = (TextView) findViewById(R.id.txt_app_version);
appVersion.setOnClickListener(socialsClickListener);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).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 = getSupportActionBar();
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.main, 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();
if (id == R.id.action_settings) {
return true;
}
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_main, container,
false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
}
private String getSocialClicked(int id){
switch (id) {
case R.id.btn_tutor:
return "http://m.getedgeucated.com/sshs/dashboard/login.html";
case R.id.btn_student:
return "http://m.getedgeucated.com/sshs/timecheck.html";
case R.id.txt_app_version:
return "http://m.getedgeucated.com/sshs/appsupport.html";
case R.id.txt_report:
return "http://sshs.getedgeucated.com/chatsupport_redirect.html";
default:
return "";
}
}
}
`
This is my activity_main.xml.
`
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_gravity="center"
android:src="#drawable/getedgeucated_logo"
android:adjustViewBounds="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold"
android:text="#string/logo_desc"
android:textColor="#android:color/darker_gray"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp_margin"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold"
android:text="#string/subtitle"
android:textColor="#android:color/darker_gray"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<Button
android:id="#+id/btn_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/half_padding"
android:background="#drawable/btn_home"
android:gravity="center"
android:text="I am a student"
android:textColor="#ffffff"
android:textSize="#dimen/text_size_large"
android:textStyle="bold" />
<Button
android:id="#+id/btn_tutor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="I am a tutor"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="#dimen/text_size_large"
android:background="#drawable/btn_home"
android:layout_margin="#dimen/half_padding"/>
</LinearLayout>
<TextView
android:id="#+id/txt_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:text="#string/report_issue"
android:textColor="#color/background"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/txt_app_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/app_version"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/background"
android:textStyle="bold" />
`
I still get error of FATAL EXCEPTION: main Java.io.NullpointerException error in debugger.
Begin with these steps :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//The rest of your code