I'm pretty new to coding so I might not express myself correctly, please bear with me.
I have a fragment inflated inside a layout that includes a toolbar. When I open the fragment, I want to add a button (not a menu, just a button to navigate to another fragment).
My issue is I don't know how to instantiate the button from my Fragment class to add an icon and an onClickListener.
Here's my xml files:
The layout that contains my fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/blue"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<Button
android:id="#+id/toolbar_button"
android:layout_width="#dimen/margin"
android:layout_height="#dimen/margin"
android:layout_gravity="right"
android:layout_marginRight="#dimen/innerLargeMargin"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</LinearLayout>
The layout in which the fragment is inflated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
tools:showIn="#layout/app_bar_main"
android:background="#color/orange">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f1f1f1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="#menu/bottom_nav_items"
android:background="#color/orange"
android:backgroundTint="#color/white"
design:itemIconTint="#color/white"
design:itemTextColor="#color/white"
design:itemBackground="#drawable/bottom_bar_tint_selector"
/>
</LinearLayout>
So what I want to do is something like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (???).findViewById(R.id.toolbar_button)
}
activity_main.xml to add Button inside android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFA000">
<Button
android:id="#+id/toolbarbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button toolBarBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Toolbar with button example");
toolbar.setSubtitle("Toolbar subtitle");
toolbar.setLogo(android.R.drawable.ic_menu_info_details);
toolBarBtn = (Button)findViewById(R.id.toolbarbtn);
toolBarBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
"Button in ToolBar clicked",
Toast.LENGTH_LONG).show();
}
});
}
}
Set This onClick Listener in your parent activity of Fragment.
try this to access your button of toolbar from activity
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) toolbar.findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);
toolbar_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ChatActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
you have to write like below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (Button) view.findViewById(R.id.toolbar_button);
where view is your view which you bind at your onCreateView in your Fragment.
What I do is to bind the View inside the activity and then create an interface who works with that View.
public class Activity implements ButtonListener{
//findViewbyId or Butterknife
void doSomething(){
toolBarButton.something()
}
Interface
}
public interface ButtonListener{
void doSomething();
}
Fragment
public class Fragment extends Fragment{
public ButtonListener mButton;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
mButton = (ButtonListener)getActivity();
super.onCreate(savedInstanceState);
}
}
You can access button directly without toolbar using (Button) findViewById(R.id.toolbar_button).
Toolbar toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);
Related
I have made an bottom app bar app with fab and bottom sheet and I have implemented the state expanded and state hidden from navigation on item selected listener.The question is how to make bottom sheet overlay on bottom app bar the following result I have got is.
This is main activity xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_CoordinatorLayout"
android:layout_width="match_parent"
android:background="#FFB7B7B7"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
android:elevation="26dp"
app:navigationIcon="#drawable/ic_menu_black" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:elevation="26dp"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_add"
app:layout_anchor="#id/bottom_app_bar"
app:menu="#menu/menu_demo"
/>
<FrameLayout
android:id="#+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
android:visibility="visible"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/menu_demo" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Main activity class
package com.bab.BottomAppBar;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import android.view.View;
import com.google.android.material.navigation.NavigationView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
BottomAppBar bab;
BottomSheetBehavior<View> bsb;
CoordinatorLayout cl;
NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nv=findViewById(R.id.navigation_view);
cl=findViewById(R.id.activity_CoordinatorLayout);
View bottomDrawer=cl.findViewById(R.id.bottom_drawer);
bab=findViewById(R.id.bottom_app_bar);
bsb=BottomSheetBehavior.from(bottomDrawer);
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
bab.setNavigationIcon(R.drawable.ic_menu_black);
bab.replaceMenu(R.menu.menu_demo);
bab.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1) {
bsb.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
});
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(MenuItem p1) {
return false;
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
Result I got:
This is the result
Result I wanted:
See in this the frame layout as bottom sheet is overlaying on bottom app bar
Someone please help me
You need to set BottomSheet's elevation more than BottomAppBar's.
Elevation means how your views lay above each other
I am trying to make an app with Bottom Sheet. But when I run the app, the Bottom Sheet shows in main screen. It means, the Bottom Sheet appears in the main screen without clicking on the Button. An image has been attested below for clear understanding. sample image
This is the MainActivity.java class:
package com.example.qurdadzze.b;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
NestedScrollView bottomsheet;
BottomSheetBehavior behavior;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
bottomsheet = (NestedScrollView) findViewById(R.id.bottomsheet);
behavior = BottomSheetBehavior.from(bottomsheet);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(behavior.getState()==BottomSheetBehavior.STATE_COLLAPSED)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
else
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
behavior.setPeekHeight(0);
}
});
}
And this is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.qurdadzze.b.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="show bottom sheet"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#99cc99"
android:elevation="100dp"
android:id="#+id/bottomsheet"
app:layout_behavior="#string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center|top"
android:gravity="center"
android:text="it is me Bottom Sheet"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
You will have to set peekheight of Bottomsheet.
android.support.design:behavior_peekHeight The height of the bottom sheet when it is collapsed.
Try using below code in your NestedScrollView:
app:behavior_peekHeight="50dp"
For more details check below link:
http://www.truiton.com/2016/07/android-bottom-sheet-example/
In Android Studio 2.1.2, I try Basic Activity with a Fragment. Whether I see any writings, I cannot follow it. When I create a new project, it includes MainActivity.java, MainActivityFragment.java, activity_main.xml, content_main.xml and fragment_main.xml. But all writings do NOT include content_main.xml.
I try continueously and success in showing other fragment. But layout thraw. First Fragment(fragment_main.xml) dose NOT disappear. Second Fragment(fragment_sub.xml) appears in the back of first fragment.
How can I fix it? Thank you for your concerning in advance.
These are my java sources.
package com.example.john.ho05fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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;
}
#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);
}
public void clickButtonNext(View view) {
Fragment fragment = new SubActivityFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace( R.id.fragment, fragment );
fragmentTransaction.commit();
}
public void clickButtonPrevious(View view) {
Fragment fragment = new MainActivityFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace( R.id.fragment, fragment );
fragmentTransaction.commit();
}
}
package com.example.john.ho05fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
package com.example.john.ho05fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class SubActivityFragment extends Fragment {
public SubActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sub, container, false);
}
}
These are my xml sources.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.john.ho05fragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<fragment 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/fragment"
android:name="com.example.john.ho05fragment.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_main" />
fragment_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#FF0000"
tools:context="com.example.john.ho05fragment.MainActivityFragment"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! Main!!!"
android:onClick="clickButtonNext"/>
</RelativeLayout>
fragment_sub.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:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="#0000FF"
tools:context="com.example.john.ho05fragment.SubActivityFragment"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/buttonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! Sub!!!!"
android:onClick="clickButtonPrevious"/>
</RelativeLayout>
Its because your fragment layouts are having margins from screen while your fragment of content_main is not having margins, so remove padding from both fragment layouts as below:
fragment_main.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="#FF0000"
tools:context="com.example.john.ho05fragment.MainActivityFragment"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! Main!!!"
android:onClick="clickButtonNext"/>
</RelativeLayout>
fragment_sub.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="#0000FF"
tools:context="com.example.john.ho05fragment.SubActivityFragment"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/buttonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! Sub!!!!"
android:onClick="clickButtonPrevious"/>
</RelativeLayout>
in my main activity i have a toolbar. also i have a layout with a Custom list view. i want to show that toolbar here too. but whatever i do i cant. what is the problem? i want the same toolbar from my main activity to be shown here too.
About.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class About extends AppCompatActivity{
ListView list;
String[] text = {
"Version"
} ;
Integer[] imageId = {
R.drawable.ic_info_black_24dp
};
private Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Set a Toolbar to replace the ActionBar. (doesn't work)
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomList adapter = new
CustomList(About.this, text, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(About.this, "You Clicked at " + text[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
about.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">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:background="#drawable/about_background">
</ListView>
</RelativeLayout>
I think your ListView and Toolbar layout overlapping each other, that's why you are not able to see that. If you want to show ListView below the Toolbar then you have to set android:layout_below="#+id/toolbar" property in ListView like that
<?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">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_below="#+id/toolbar
android:background="#drawable/about_background">
</ListView>
</RelativeLayout>
I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?xml version="1.0" encoding="utf-8"?>
<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.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" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">