Reputation: 60
I was trying to make and android app with two fragments in a ViewPager
together tabs, fine up to there.
The problem came when I added a FloatingActionButtom
and I wanted it to change depending on the tab in which you are in. This is what I have:
With this the icon changes when the pages are changed but I want it to do it with this animation and to do that I need to call fab.hide();
and fab.show();
but Android studio sais this methods dont exist, so what can I do?
I have all my SDKs up to date together with the IDE and have included the latest design-support library on my gradle: compile 'com.android.support:design:22.2.0'
, also Ive imported android.support.design.widget.FloatingActionButton;
into my MainActivity.
I also tried to write it on another computer (just in case) and nothing, the cannot resolve method error keeps poping up.
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
FloatingActionButton fab;
ActionBar actionBar;
ContadorFragment fragContador;
Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fabAñadir);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
getSupportActionBar().setElevation(0);
context = this;
fragContador = new ContadorFragment();
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0){ //Esta en el contado
fab.setImageResource(R.drawable.ic_save_black_24dp);
verEstadisticas = false;
} else{// esta en las estadisticas
verEstadisticas = true;
fab.setImageResource(R.drawable.ic_add_black_24dp);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(fragContador, "Contador");
adapter.addFrag(new EstadisticasFragment(), "Estadisticas");
viewPager.setAdapter(adapter);
}
And the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/tools"
app:layout_behavior="com.app.common.PatchedScrollingViewBehavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/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="exitUntilCollapsed"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#000000"
app:tabIndicatorHeight="2dp"/>
</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>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAñadir"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:layout_width="56dp"
fab:fab_type="normal"
android:layout_height="56dp"
android:clickable="true"
app:borderWidth="0dp"
android:elevation="6dp"
android:src="@drawable/ic_add_black_24dp" />
</RelativeLayout>
Upvotes: 0
Views: 596
Reputation: 7922
Check the Support Library release notes here. The show() and hide() methods for the floating action button were added in revision 22.2.1, but you're using 22.2.0. Update to a newer version, preferably the latest(currently 23.1.1) and it will work.
Upvotes: 3
Reputation: 191728
So I'm really not sure what to tell you, but I got a running example in a brand new project.
Gradle
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
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:fab="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/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="exitUntilCollapsed"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#000000"
app:tabIndicatorHeight="2dp"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle FAB"
android:id="@+id/btn_toggle_fab"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
fab:fab_type="normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/abc_ic_search_api_mtrl_alpha"/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
import android.support.design.widget.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btn_toggle_fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (fab.isShown()) {
fab.hide();
} else {
fab.show();
}
}
});
}
}
Upvotes: 0