I have made a sliding menu like that of facebook by following the tutorial which is available in the following site:
http://www.devexchanges.info/2016/05/creating-your-own-sliding-menu-like.html?m=1
Now in the main layout I have added a long paragraph of text so in order to view everything I used ScrollView. But when I use ScrollView I cannot pull the menu out, I can only open the drawer by clicking on the button. Please help me on how to scroll through the para and also be able to pull the menu drawer.
The MainActivity XML file:
<info.devexchanges.slidingmenu.SlidingLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This holds our menu -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarmenu"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#102961"
android:orientation="horizontal"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<ListView
android:id="#+id/activity_main_menu_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbarmenu"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<!-- This holds our content-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/toolbar"
android:orientation="horizontal"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:id="#+id/menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:onClick="toggleMenu"
android:src="#drawable/menu" />
<TextView
android:id="#+id/title"
style="#style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:gravity="center"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
<!-- Fragments container layout -->
<FrameLayout
android:id="#+id/activity_main_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
the_fiesta Fragment (the home Fragment) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/white">
<ScrollView
android:layout_width="800dp"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:src="#drawable/fiesta_title"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_alignParentTop="true"
android:id="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/the_fiesta"
android:id="#+id/textView"
android:textSize="17sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="#+id/imageView"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>
Java file of MainActivity:
package info.devexchanges.slidingmenu;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// The SlidingLayout which will hold both the sliding menu and our main content
// Main content will holds our Fragment respectively
SlidingLayout slidingLayout;
// ListView menu
private ListView listMenu;
private String[] listMenuItems;
private Toolbar toolbar;
private TextView title; //page title
private ImageView btMenu; // Menu button
private Fragment currentFragment;
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the mainLayout
setContentView(R.layout.activity_main);
slidingLayout = (SlidingLayout) findViewById(R.id.sliding_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
title = (TextView) findViewById(R.id.title);
setSupportActionBar(toolbar);
// Init menu
listMenuItems = getResources().getStringArray(R.array.menu_items);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listMenu = (ListView) findViewById(R.id.activity_main_menu_listview);
listMenu.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listMenuItems));
listMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
onMenuItemClick(parent, view, position, id);
}
});
// handling menu button event
btMenu = (ImageView) findViewById(R.id.menu_icon);
btMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Show/hide the menu
toggleMenu(v);
}
});
// Replace fragment main when activity start
FragmentManager fm = MainActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
TheFiesta fragment = new TheFiesta();
ft.add(R.id.activity_main_content_fragment, fragment);
ft.commit();
currentFragment = fragment;
title.setText("The Fiesta");
}
public void toggleMenu(View v) {
slidingLayout.toggleMenu();
}
// Perform action when a menu item is clicked
private void onMenuItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment;
if (position == 0) {
fragment = new TheFiesta();
title.setText("The Fiesta");
} else if (position == 1) {
fragment = new ListViewFragment();
title.setText("Events");
} else if (position == 2) {
fragment = new TextViewFragment();
Bundle args = new Bundle();
args.putString("KEY_STRING", "This is a TextView in the Fragment");
fragment.setArguments(args);
title.setText("Schedule");
} else if (position == 3) {
fragment = new ListViewFragment();
title.setText("Schools");
} else if (position == 4) {
fragment = new ListViewFragment();
title.setText("Updates");
} else if (position == 5) {
fragment = new ListViewFragment();
title.setText("Our Team");
} /*else if (position == 6) {
fragment = new ListViewFragment();
title.setText("About Us");
}*/ else {
fragment = new DummyFragment();
title.setText("About Us");
}
if(!fragment.getClass().equals(currentFragment.getClass())) {
// Replace current fragment by this new one
ft.replace(R.id.activity_main_content_fragment, fragment);
ft.commit();
currentFragment = fragment;
}
// Hide menu anyway
slidingLayout.toggleMenu();
}
#Override
public void onBackPressed() {
if (slidingLayout.isMenuShown()) {
slidingLayout.toggleMenu();
} else {
super.onBackPressed();
}
}
#Override
protected void onStart() {
super.onStart();
getSupportActionBar().setTitle("");
}
}
try this instead of Fragments container. add this code below
</android.support.v7.widget.Toolbar> this may work.
<LinearLayout
android:id="#+id/activity_main_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Related
In one of my fragment class, it has two buttons (A and B) on top, used to switch to another fragment when clicked using viewPager.
When buttonA is clicked, it should switch to page A and so on.
When I click button A, how can I make the text in A show below the two buttons?
fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="ButtonA"
android:textSize="12sp" />
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_toRightOf="#+id/buttonB"
android:backgroundTint="#color/materialGrey600"
android:padding="15dp"
android:text="ButtonB"
android:textSize="12sp" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MenuFragment
public class MenuFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_menu, container, false);
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getActivity().getFragmentManager()));
Button btnA = (Button) v.findViewById(R.id.buttonA);
Button btnB = (Button) v.findViewById(R.id.buttonB);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(0, true);
}
});
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(1, true);
}
});
return v;
}
}
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new A()
case 1:
return new B()
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
Output
try with below code.hope it is helpful for you ;)
P.S. You just need to set button style as you want :)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_above="#id/llButton"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/llButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="ButtonA"
android:textSize="12sp" />
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:backgroundTint="#color/colorAccent"
android:padding="15dp"
android:text="ButtonB"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
Since you are using Relative Layout you can add layout_below
<androidx.viewpager.widget.ViewPager
android:layout_below="#id/buttonB"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
you are doing it wrong, You cannot align a button to itself. That's why your buttons are not working properly. In your button B do this:
android:layout_toRightOf="#+id/buttonA"
instead of this:
android:layout_toRightOf="#+id/buttonB"
And to your viewPager add "android:layout_below:"
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/buttonA" />
You have to set your layout like this....
[I think you want something like this 2 button fisrt and then viewpager for change the content ][1]
[1]: https://i.stack.imgur.com/dluXA.png
**Code for main Page **
<?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"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/frame_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/btn_donner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#drawable/rect_outline"
android:text="Donor's"
android:textAlignment="center"
android:textColor="#color/white" />
<Button
android:id="#+id/btn_blood_bank"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blood Bank"
android:background="#drawable/rect_outline"
android:layout_weight="2"
android:textAlignment="gravity"
android:textColor="#color/white" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
Code For Java File of two Buttons And ViewPager
//Click Event for donor's and blood bank .....
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment = null;
if (view == view.findViewById(R.id.btn_donner))
{
fragment = new TabDonnerFragment();
}
else
{
fragment = new TabBloodBankFragment();
}
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame_content, fragment);
transaction.commit();
}
};
btn_donner.setOnClickListener(listener);
btn_blood_bank.setOnClickListener(listener);
return root;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment fragment = null;
fragment = new TabDonnerFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame_content, fragment);
transaction.commit();
}
}
I am trying to achieve similar effect to the one found in Todoits application where you can click on FAB and it shows a floating popup for adding new task.
This is how it looks:
The closest to it I managed to achieve is this:
It is nearly there, but I want the popup to be about 10dp above the keyboard and scroll the recycle viewer to the last item but I can't get it there :/ Currently, the popup is centred.
I have tried before adding the popup to the bottom of the screen and setting android:windowSoftInputMode="adjustResize" but this was moving my bottom navigation above the keyboard too. Setting windowsSoftInputMode to panAdjust was cutting away the toolbar and few items from the list.
Have you got any suggestions about how to achieve similar effect to the one in Todoist? Maybe with a DialogFragment?
Below is my current code (navigation bar implementation is still not completed):
MainActivity:
public class MainActivity extends AppCompatActivity {
private WorkoutsListFragment workoutsListFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar actionBar = getSupportActionBar();
this.workoutsListFragment = new WorkoutsListFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_layout, workoutsListFragment)
.commit();
}
}
WorkoutListFragment:
public class WorkoutsListFragment extends Fragment
implements View.OnClickListener, WorkoutListViewHolderInterface {
private RecyclerView recyclerView;
private WorkoutListRecycleViewAdapter recycleViewAdapter;
private AddBoxView addBoxView;
private FloatingActionButton actionButton;
private InputMethodManager imm;
private WorkoutViewModel workoutViewModel;
private boolean addBoxStatus;
public WorkoutsListFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addBoxStatus = false;
//Irrelevant code removed (Dagger 2 DI)
this.workoutViewModel =
ViewModelProviders.of(this, workoutViewModelFactory).get(WorkoutViewModel.class);
workoutViewModel.init(7);
workoutViewModel.getWorkout().observe(this, w -> this.updateWorkoutsList(w, false));
this.imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
}
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_workouts_list, container, false);
this.recyclerView = view.findViewById(R.id.workoutsList);
// this.recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(getContext());
this.recyclerView.setLayoutManager(recyclerViewLayoutManager);
this.recycleViewAdapter = new WorkoutListRecycleViewAdapter(this);
this.recyclerView.setAdapter(recycleViewAdapter);
this.actionButton = view.findViewById(R.id.floating_action_add_workout);
this.actionButton.setOnClickListener(this);
this.addBoxView = new AddBoxView(Objects.requireNonNull(getContext()));
FrameLayout.LayoutParams addBoxLayoutParams =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
this.addBoxView.setLayoutParams(addBoxLayoutParams);
this.addBoxView.getButton().setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.floating_action_add_workout:
this.addBoxView((FrameLayout) v.getParent(), v);
break;
case R.id.overlay_layout:
removeAddBoxView((FrameLayout) v);
break;
case R.id.button:
this.addNewWorkout(this.addBoxView.getInputText());
break;
}
}
//Irrelevant code removed....
private void addBoxView(FrameLayout viewGroup, View v) {
this.addBoxStatus = true;
viewGroup.setClickable(true);
viewGroup.setOnClickListener(this);
viewGroup.setBackgroundColor(
getResources().getColor(R.color.cardview_shadow_start_color, null));
viewGroup.removeView(v);
viewGroup.addView(this.addBoxView);
this.addBoxView.requestInputFocus();
this.imm.showSoftInput(this.addBoxView.getInput(), InputMethodManager.SHOW_IMPLICIT);
}
private void removeAddBoxView(FrameLayout viewGroup) {
viewGroup.setClickable(false);
this.imm.hideSoftInputFromWindow(this.addBoxView.getInput().getWindowToken(), 0);
viewGroup.removeView(this.addBoxView);
viewGroup.setBackgroundColor(getResources().getColor(R.color.cardview_shadow_end_color, null));
viewGroup.addView(this.actionButton);
this.addBoxStatus = false;
}
}
Fragment_workouts_list layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#e1e1e1"
android:padding="0dp"
tools:context=".Activities.MainActivity.Fragments.Workouts.WorkoutsListFragment"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/workoutsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="#+id/add_box_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
<FrameLayout
android:id="#+id/overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:outlineProvider="bounds">
<android.support.design.widget.FloatingActionButton
android:id="#+id/floating_action_add_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="40dp"
android:layout_marginEnd="40dp"
android:clickable="true"
android:src="#android:color/holo_blue_dark" />
</FrameLayout>
</FrameLayout>
MainActivity Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/root_view"
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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/AppTheme.AppBar" />
<FrameLayout
android:id="#+id/main_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/my_toolbar">
</FrameLayout>
<android.support.design.widget.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimary"
android:foregroundGravity="bottom"
android:visibility="visible"
app:itemTextColor="#color/textColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/my_navigation_items" />
</android.support.constraint.ConstraintLayout>
I was following this tutorial https://www.youtube.com/watch?v=zcnT-3F-9JA I've used his code from github, but output is wrong. I've put 3 tabs on the top, and when I press on tab, activity supposed to change, but in reality, nothing happens, I still have only my main_activity on the screen. Hope someone will help. Here is my codes
PS - Yes I have also 3 xml files for every fragment (even I have also main_activity, but I have to find out what's wrong, and then will asign 1tab with main activity). I have also 3 java files for this 3 tabs.
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<TextView
android:id="#+id/lvltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/expa"
android:layout_centerInParent="true"
android:fontFamily="#font/futuracondensed"
android:text="#string/leveltext"
android:textColor="#color/black"
android:textSize="30sp" />
<TextView
android:id="#+id/lvlnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvltext"
android:layout_centerInParent="true"
android:fontFamily="#font/futuracondensed"
android:text="#string/levelnum"
android:textColor="#color/black"
android:textSize="30sp" />
<ImageView
android:id="#+id/girl"
android:layout_width="wrap_content"
android:layout_height="262dp"
android:layout_below="#id/lvlnum"
android:src="#drawable/girl" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/girl"
android:fontFamily="#font/futuracondensed"
android:text="#string/button"
android:textColor="#color/black" />
<ImageView
android:id="#+id/girl2"
android:layout_width="match_parent"
android:layout_height="258dp"
android:layout_below="#id/button"
android:src="#drawable/fitnessmodel" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/girl2"
android:fontFamily="#font/futuracondensed"
android:text="#string/button2"
android:textColor="#color/black" />
<LinearLayout
android:layout_below="#id/tabs"
android:id="#+id/expa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="0"
android:textColor="#color/black"
android:textSize="60sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="100"
android:textColor="#color/black"
android:textSize="60sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Java
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "TAB1");
adapter.addFragment(new Tab2Fragment(), "TAB2");
adapter.addFragment(new Tab3Fragment(), "TAB3");
viewPager.setAdapter(adapter);
}
#Override
public void onResume() {
super.onResume();
}
}
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
use this XML as given in the library u have followed then whatever design u want to add them to the fragment layouts u have taken.things will work fine
<?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"
android:orientation="vertical"
tools:context="com.example.chirag.slidingtabsusingviewpager.MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SlidingTabsUsingViewPager"
android:textSize="20dp"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tablayout"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/pager">
</android.support.v4.view.ViewPager>
</LinearLayout>
On your getItem() method. Use Switch and return fragments their instead of adding all the fragments in addFragment method.
public Fragment getItem(int position){
switch(position){
case 0 : return new Tab1Fragment();
case 1 : return new Tab2Fragment();
case 2 : return new Tab3Fragment();
}
}
try this code:
OneFragment.java
OneFragment.java
package info.androidhive.materialtabs.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import info.androidhive.materialtabs.R;
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
fragment_one.xml
fragment_one.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"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one"
android:textSize="40dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
activity_main.xml
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package info.androidhive.materialtabs.activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.fragments.OneFragment;
import info.androidhive.materialtabs.fragments.ThreeFragment;
import info.androidhive.materialtabs.fragments.TwoFragment;
[![enter image description here][1]][1]public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new
ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
and add the main activity calling 3 tabs
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return SimpleFragment.newInstance("FirstFragment, Instance 1");
case 1: return PieView.newInstance("SecondFragment, Instance 1");
case 2: return PieView1.newInstance("ThirdFragment, Instance 1");
case 3: return DataSaveDetails.newInstance("ThirdFragment, Instance 1");
default:
}
return null;
}
it works please try this
I started to develop an app for my final year project. I created all the navigation drawers with no issue and they worked fine when I had run the program. When I added a search-page that would link in with the mapsAcivity I added. No errors are appearing however the app will run fine but when I click on the navigation drawer to access the search page and mapsActivity page the app crashes. Any ideas what the issue is?Screengrab of the app failing
04-26 23:48:25.311: E/AndroidRuntime(13703): Caused by: android.view.InflateException: Binary XML file line #285: ScrollView can host only one direct child
04-26 23:48:25.311: E/AndroidRuntime(13703): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
Below is the code for my search screen
package com.example.mcdai.derrytourism;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SearchScreen extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Spinner restaurantSpinner, accommidationSpinner;
ArrayAdapter<CharSequence> adapter;
Button btnSearch;
TextView buttonLogout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_screen);
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);
final EditText freeTextEntry = (EditText)findViewById(R.id.editFreeTextSearch);
//final EditText editMinDestination = (EditText)findViewById(R.id.editMinDestination);
final EditText editMaxDestination = (EditText)findViewById(R.id.editMaxDestination);
final EditText editMinPrice = (EditText)findViewById(R.id.editMinPrice);
final EditText editMaxPrice = (EditText)findViewById(R.id.editMaxPrice);
final Spinner spinnerRestautantType = (Spinner)findViewById(R.id.spinnerRestaurantType);
final Spinner spinnerAccommidationType = (Spinner)findViewById(R.id. spinnerAccommidationType);
buttonLogout = (TextView)findViewById(R.id.buttonLogout);
editMinPrice.setEnabled(false);
editMaxPrice.setEnabled(false);
spinnerRestautantType.setEnabled(false);
spinnerAccommidationType.setEnabled(false);
//restaurant drop down menu
restaurantSpinner = (Spinner)findViewById(R.id.spinnerRestaurantType);
adapter = ArrayAdapter.createFromResource(this,R.array.restaurant_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
restaurantSpinner.setAdapter(adapter);
restaurantSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
TextView defaultPleaseSelect = (TextView) view;
if(position == 0)
{
// Set hint size and colour
defaultPleaseSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
defaultPleaseSelect.setTextColor(Color.GRAY);
}
else
{
// Set dropdown selection and display an onscreen message
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//Accommidation drop down menu
accommidationSpinner = (Spinner)findViewById(R.id.spinnerAccommidationType );
adapter = ArrayAdapter.createFromResource(this,R.array.accommidation, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
accommidationSpinner.setAdapter(adapter);
accommidationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
TextView defaultPleaseSelect = (TextView) view;
if(position == 0)
{
// Set hint size and colour
defaultPleaseSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
defaultPleaseSelect.setTextColor(Color.GRAY);
}
else
{
// Set dropdown selection and display an onscreen message
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intentMapsActivity = new Intent(SearchScreen.this, MapsActivity.class);
//code to get min desitance for destination
//String minDestination = editMinDestination.getText().toString().trim();
//code to get max distance for destination
if(editMaxDestination.getText().toString().equals(""))
{
Toast.makeText(SearchScreen.this, "Please Populate Distance Field", Toast.LENGTH_LONG).show();
}
else
{
Double maxDestination = Double.parseDouble(editMaxDestination.getText().toString());
//code to get from editTextFreeSearch
String freeTextSearchParam = freeTextEntry.getText().toString();
//code to get restaurant type spinner
String restaurantTypeSpinner = spinnerRestautantType.getSelectedItem().toString();
//code to get accommidation type spinner
String accommidationSpinner = spinnerAccommidationType .getSelectedItem().toString();
if(freeTextSearchParam.equals("") && restaurantTypeSpinner.equals("Select") && accommidationSpinner.equals("Select"))
{
Toast.makeText(SearchScreen.this, "Please Populate Free Text, Price or Restaurant Type", Toast.LENGTH_LONG).show();
}
else
{
freeTextSearchParam = freeTextSearchParam.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("freeTextSearch", freeTextSearchParam);
intentMapsActivity.putExtra("maxDestination", maxDestination);
restaurantTypeSpinner = restaurantTypeSpinner.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("restaurantType", restaurantTypeSpinner);
accommidationSpinner = accommidationSpinner.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("accommidation", accommidationSpinner);
startActivity(intentMapsActivity);
}
}
}
});
buttonLogout = (TextView)findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), SearchScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent( event );
}
public void onRadioButtonClicked(View view)
{
//EditText editMinDestination = (EditText)findViewById(R.id.editMinDestination);
EditText editMaxDestination = (EditText)findViewById(R.id.editMaxDestination);
EditText editMinPrice = (EditText)findViewById(R.id.editMinPrice);
EditText editMaxPrice = (EditText)findViewById(R.id.editMaxPrice);
Spinner spinnerRestautantType = (Spinner)findViewById(R.id.spinnerRestaurantType);
Spinner spinnerAccommidationType = (Spinner)findViewById(R.id.spinnerAccommidationType );
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
boolean unchecked = !((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radioPrice:
if (checked)
{
editMinPrice.setEnabled(true);
editMaxPrice.setEnabled(true);
}break;
case R.id.radioRestuarantType:
if (checked)
{
spinnerRestautantType.setEnabled(true);
} break;
case R.id.radioAccommidationType:
if (checked)
{
spinnerAccommidationType.setEnabled(true);
}break;
}
}
#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.search_screen, 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;
}
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_MainActivity) {
Intent intentMainActivity = new Intent(SearchScreen.this, MainActivity.class);
startActivity(intentMainActivity);
finish();
} else if (id == R.id.nav_SearchScreen) {
Intent intentSearchScreen = new Intent(SearchScreen.this,SearchScreen.class);
startActivity(intentSearchScreen);
finish();
} else if (id == R.id.nav_Attractions) {
Intent intentAttractions = new Intent(SearchScreen.this, Attractions.class);
startActivity(intentAttractions);
finish();
} else if (id == R.id.nav_Food) {
Intent intentFood = new Intent(SearchScreen.this, Food.class);
startActivity(intentFood);
finish();
} else if (id == R.id.nav_Accommidation) {
Intent intentAccommidation = new Intent(SearchScreen.this,Accommidation.class);
startActivity(intentAccommidation);
finish();
} else if (id == R.id.nav_Entertainment) {
Intent intentEntertainment = new Intent(SearchScreen.this,Entertainment.class);
startActivity(intentEntertainment);
finish();
} else if (id == R.id.nav_MapsActivity) {
Intent intentMapsActivity = new Intent(SearchScreen.this,MapsActivity.class);
startActivity(intentMapsActivity);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Code below was taken from activity_search_screen.
<?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="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mcdai.derrytourism.SearchScreen">
<TextView
android:id="#+id/txtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:paddingBottom="50sp"
android:textAlignment="center"
android:textSize="20sp"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/txtFreeTextSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Free-Text Search"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp"
android:paddingBottom="20sp"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_below="#+id/txtSearch"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignTop="#+id/txtAccommidationType"
android:layout_toRightOf="#+id/txtDestination"
android:layout_centerHorizontal="true"
android:layout_marginTop="50sp" />
<TextView
android:id="#+id/txtAccommidationType"
android:text="AccommidationType"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_marginLeft="40dp"
android:layout_alignBottom="#+id/radioGroup"
android:layout_toRightOf="#+id/radioGroup"
android:paddingBottom="10sp" />
<EditText
android:id="#+id/editFreeTextSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:ems="10"
android:inputType="text"
android:layout_below="#+id/txtFreeTextSearch"
android:layout_centerHorizontal="true" />
<Spinner
android:id="#+id/spinnerAccommidationType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txtAccommidationType"
android:layout_alignParentRight="true" />
<Spinner
android:id="#+id/spinnerRestaurantType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtRestuarantType"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/spinnerAccommidationType" />
<TextView
android:id = "#+id/buttonLogout"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:text = "Logout"
android:textSize="15sp"
android:textAlignment="center"
android:clickable="false"
android:textColor="#android:color/holo_blue_dark"
android:onClick="onClick"
android:paddingBottom="50sp"
android:layout_alignBaseline="#+id/txtSearch"
android:layout_alignBottom="#+id/txtSearch"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/txtMaxPrice"
android:text="Max(£)"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textAlignment="center"
android:layout_above="#+id/editMinPrice"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/editMaxPrice"
android:layout_marginBottom="-4sp" />
<EditText
android:id="#+id/editMaxPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="10"
android:textSize="15sp"
android:textAlignment="center"
android:background="#android:color/transparent"
android:inputType="numberDecimal"
android:layout_marginBottom="21dp"
android:layout_above="#+id/spinnerRestaurantType"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/txtMinPrice"
android:text="Min(£)"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textAlignment="center"
android:layout_above="#+id/editMinPrice"
android:layout_alignLeft="#+id/editMinPrice"
android:layout_toLeftOf="#+id/txtMaxPrice"
android:layout_marginBottom="-4sp"
android:paddingRight="55sp" />
<EditText
android:id="#+id/editMaxDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="10"
android:textSize="15sp"
android:background="#android:color/transparent"
android:textAlignment="center"
android:inputType="number"
android:layout_below="#+id/txtDestination"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/txtCriteriaSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Criteria Search"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp"
android:paddingTop="20sp"
android:textAppearance="#style/TextAppearance.AppCompat"
android:paddingBottom="10sp"
android:layout_marginTop="41dp"
android:layout_below="#+id/editFreeTextSearch"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/txtDestination"
android:text="Max Distance (km)"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_below="#+id/editFreeTextSearch"
android:layout_centerHorizontal="true"
android:paddingTop="10sp" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="14sp"
android:paddingBottom="6sp"
android:layout_below="#+id/txtCriteriaSearch"
android:layout_alignParentLeft="true">
<RadioButton
android:id="#+id/radioPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="10sp"
android:layout_marginBottom="3sp" />
<RadioButton
android:id="#+id/radioRestuarantType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="10sp"/>
<RadioButton
android:id="#+id/radioAccommidationType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioRestuarantType"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="15sp" />
</RadioGroup>
<EditText
android:id="#+id/editMinPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="0"
android:textSize="15sp"
android:textAlignment="center"
android:background="#android:color/transparent"
android:inputType="numberDecimal"
android:layout_alignBaseline="#+id/editMaxPrice"
android:layout_alignBottom="#+id/editMaxPrice"
android:layout_toLeftOf="#+id/buttonLogout"
android:paddingRight="55sp" />
<TextView
android:id="#+id/txtPrice"
android:text="Price"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:paddingTop="7sp"
android:layout_alignTop="#+id/txtMaxPrice"
android:layout_alignLeft="#+id/txtRestuarantType" />
<TextView
android:id="#+id/txtRestuarantType"
android:text="Restuarant Type"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_marginBottom="26dp"
android:paddingBottom="2sp"
android:layout_above="#+id/txtAccommidationType"
android:layout_alignLeft="#+id/txtAccommidationType"
android:layout_alignStart="#+id/txtAccommidationType" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_search_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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:headerLayout="#layout/nav_header_search_screen"
app:menu="#menu/activity_search_screen_drawer" />
</android.support.v4.widget.DrawerLayout>
</ScrollView>
The reason you're crashing is your ScrollView has more than one child element in it. You'll need to either wrap them both in a parent view sitting inside the ScrollView or move your DrawerLayout outside of the ScrollView.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mcdai.derrytourism.SearchScreen">
<!-- RelativeLayout Children go here -->
</RelativeLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_search_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<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:headerLayout="#layout/nav_header_search_screen"
app:menu="#menu/activity_search_screen_drawer"/>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
</ScrollView>
I'm not sure exactly what you want your layout to look like but if you follow the above you shouldn't be crashing. Make sure you are using Code > Reformat Code to keep your indentations consistent. Good luck!
i have builded an app that works perfectly on android with api higer than 21. The problem is that this instruction of my code:
mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
Works in different way on API less then 21.
On Api less then 21 the new fragment hide the previus activity, so that i can't click on my Floating Action Button.
Here are two images that explain in abetter way my problem.
API HIGER THAN 21
API LESS THAN 21
So my question is: How can i have the same result in API less then 21 that i have on API Higer then 21?
Here is the affected part of the Main Activity Code:
public class MainActivity extends AppCompatActivity {
public static AppDataBase appDataBase;
public static UserDataBase userDataBase;
static FragmentManager mFragmentManager;
static FragmentTransaction mFragmentTransaction;
private DrawerLayout myDrawerLayout;
final String TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE = "Add a console!";
TextView currentConsole;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appDataBase = new AppDataBase(this);
userDataBase = new UserDataBase(this);
myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
currentConsole = (TextView) findViewById(R.id.txt_Mainactvt_currentConsole);
currentConsole.setText(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE);
tabLayoutManagement();
floatingActionButtonManagement();
leftDrawerMenuManagement();
rigthDrawerMenuManagement();
populateMyConsole();
}
void tabLayoutManagement() {
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
}
// Floating Action Button
private void floatingActionButtonManagement() {
FloatingActionButton fab_addGame = (FloatingActionButton)findViewById(R.id.fab_AddGame);
fab_addGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*
The currentConsole TextView is used to show at the user wich console is selected.
We use it to have a strng that conteins the selected console.
Call the method that manage the click event of the FloatingActionButton. We pass the console name.
*/
String currentConsoleName = currentConsole.getText().toString();
floatingActionButtonClickEvent(currentConsoleName);
}
});
}
private void floatingActionButtonClickEvent(String currentConsoleName) {
/*
Check if user have added a console. If he did start a menu for adding games, else start an
error message
*/
if (!currentConsoleName.equals(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE)) {
popUpViewAddGameBuild(currentConsoleName);
}
else
mySimpleAlertDialogMethod("Attention!", "Before you enter game, you must enter a console.", true, true);
}
private void popUpViewAddGameBuild(String currentConsoleName) {
/*
Build the view that show the menu for adding games.
*/
LayoutInflater inflater = this.getLayoutInflater();
View popupView = inflater.inflate(R.layout.popupview_addgame, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popupView, 0, 0, 0);
}
Here is the TabLayout Class:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
public class MyAdapter extends FragmentPagerAdapter {
public int position;
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0 :
return new DesireFragment();
case 1 :
return new BuyedFragment();
case 2 :
return new StartedFragment();
case 3 :
return new FinishedFragment();
case 4 :
return new AllTrophiesFragment();
}
return null;
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Desire";
case 1 :
return "Buyed";
case 2 :
return "Started";
case 3 :
return "Finished";
case 4 :
return "AllTrophies";
}
return null;
}
}
}
There is the layout of the MainActivity:
<LinearLayout
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:fitsSystemWindows="true"
android:layout_gravity="end"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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/colorPrimary"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/ic_menu_white_24dp"
app:title="MyGames">
<Button
android:id="#+id/btnOpenRigthDrawer"
android:background="#drawable/ic_filter_list_white_24dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:layout_gravity="right" />
<Button
android:id="#+id/btnOpenOptions"
android:background="#drawable/ic_settings_white_24dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="17dp"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollIndicators="bottom">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txt_Mainactvt_currentConsole"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="50dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_AddGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:layout_marginRight="17dp"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="bottom|right"
android:background="#color/colorPrimary" />
</FrameLayout>
<include
layout="#layout/drawer_left"
android:id="#+id/layLeft"
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
<include
layout="#layout/drawer_rigth"
android:id="#+id/layRigth"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:scrollbars="vertical"
/>
</android.support.v4.widget.DrawerLayout>
And here is the layout code of the TabLayout:
<?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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
android:background="#color/colorPrimary"
app:tabIndicatorColor="#android:color/holo_orange_dark"
app:tabSelectedTextColor="#android:color/holo_orange_dark"
app:tabTextColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
You replace the content of your FrameLayout with an Fragment. this leads to your strange result.
Add a Layout to your Framelayout instead and use it as your FragmentContainer:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txt_Mainactvt_currentConsole"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="50dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_AddGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:layout_marginRight="17dp"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="bottom|right"
android:background="#color/colorPrimary" />
</FrameLayout>
Try to place your FrameLayout with this button to the end of layout