Animation for changing Action bar title for each fragment swipe on android -


i have set custom action bar swipe (viewpager) tabs using toolbar widget , fragments.

i want have different action bar "title" each tab. great if set animation while swiping.

i appreciate suggestions , links tutorials! in advance:)

mainactivity.java

public class mainactivity extends fragmentactivity implements json.listener {   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      toolbar toptoolbar = (toolbar) findviewbyid(r.id.toolbar);     toptoolbar.settitle("weather");     toptoolbar.inflatemenu(r.menu.menu_home);     toptoolbar.setonmenuitemclicklistener(new toolbar.onmenuitemclicklistener() {         @override         public boolean onmenuitemclick(menuitem item) {             int id = item.getitemid();              //noinspection simplifiableifstatement             if (id == r.id.add_city) {                 try {                     autocompletefilter typefilter = new autocompletefilter.builder()                             .settypefilter(autocompletefilter.type_filter_cities)                             .build();                     intent intent =                             new placeautocomplete.intentbuilder(placeautocomplete.mode_fullscreen)                                     .setfilter(typefilter)                                     .build(mainactivity.this);                     startactivityforresult(intent, 1);                 } catch (googleplayservicesrepairableexception e) {                     // todo: handle error.                 } catch (googleplayservicesnotavailableexception e) {                     // todo: handle error.                 }                 return true;             }              return false;         }     });       viewpager pager = (viewpager) findviewbyid(r.id.viewpager);     pager.setadapter(new mypageradapter(getsupportfragmentmanager()));      string city_name = (preferencemanager.getdefaultsharedpreferences(this).getstring("name", ""));;     callapi(city_name);  }  @override public void onloaded(response androidlist) {     new dbhandler(this).insertdetailsdb(androidlist); }  @override public void onerror() {     toast.maketext(this, "error !", toast.length_short).show(); }  private class mypageradapter extends fragmentpageradapter {      public mypageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int pos) {         switch (pos) {              case 0:                 return new citylistfragment();             case 1:                 return new weatherdeatailsfragment();             default:                 return new weatherdeatailsfragment();         }     }      @override     public int getcount() {         return 2;     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/back" tools:context="in.edu.apoorv.weather.mainactivity">  <android.support.v7.widget.toolbar     android:id="@+id/toolbar"     android:minheight="?attr/actionbarsize"     android:background="@android:color/transparent"     android:layout_width="match_parent"     android:titletextcolor="#fff"     android:layout_height="60dp"> </android.support.v7.widget.toolbar>   <android.support.v4.view.viewpager     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/viewpager"     android:layout_margintop="60dp"     android:layout_width="match_parent"     android:layout_height="match_parent"     /> 

you can below

1.enclose textview inside toolbarlike this

<android.support.v7.widget.toolbar android:id="@+id/toolbar" android:minheight="?attr/actionbarsize" android:background="@android:color/transparent" android:layout_width="match_parent" android:titletextcolor="#fff" android:layout_height="60dp">      <textview     style="@style/textappearance.appcompat.widget.actionbar.title"     android:id="@+id/toolbar_title"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     />        </android.support.v7.widget.toolbar> 

2.get view in activity

textview titletv = (textview) findviewbyid(r.id.toolbar_title); 

3.create animation xml file in res/anim folder.

slide.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillafter="true" >  <scale   android:duration="500"   android:fromxscale="1.0"   android:fromyscale="1.0"   android:interpolator="@android:anim/linear_interpolator"   android:toxscale="1.0"   android:toyscale="0.0" /> </set> 

4.initialize animation textview.

animation animation1 = animationutils.loadanimation(getapplicationcontext(), r.anim.slide); 

5.set onpagechangelistener viewpager. call animation in onpageselected() method of listener.

viewpager.setonpagechangelistener(new onpagechangelistener() {     public void onpagescrollstatechanged(int state) {}     public void onpagescrolled(int position, float positionoffset, int  positionoffsetpixels) {}      public void onpageselected(int position) {           titletv.startanimation(animation1);           switch(position){          case 0:             titletv.settext("title1");//set title here             break;           case 1:             titletv.settext("title2");//set title here             break;         }     } }); 

i hope works. :)


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -