java - Unable to view tab names in tab layout with viewpager in android studio -
i have designed page consist of toolbar tablayout , viewpager. pages uses style theme.appcompat.light.noactionbar app amin theme. working fine, can view pages slide pages except unable view tab names in tablayout. please 1 me using android studio 2.2.1
xml design code of page is:
<?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=".mainactivity"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="35dp" android:layout_alignparenttop="true" android:background="?attr/colorprimary" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light"/> <android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" android:layout_gravity="end" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.tablayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar" android:background="?attr/colorprimary" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" style="@style/customtablayoutstyle"/> <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout> <relativelayout android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="#cc194740" android:orientation="vertical" > <linearlayout android:layout_width="240dp" android:layout_height="wrap_content" android:layout_marginleft="0dp" android:gravity="center_vertical" android:background="#328e80" android:orientation="horizontal" > <imageview android:id="@+id/image_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="10dp" /> <textview android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:textcolor="#fff" android:textsize="16sp" android:textstyle="bold" /> </linearlayout> <listview android:id="@+id/drawer_list" android:layout_width="240dp" android:layout_height="match_parent" android:layout_margintop="70dp" android:layout_gravity="end" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" /> </relativelayout> </android.support.v4.widget.drawerlayout> </linearlayout>
the java code of xml design is
package info.sants_thapal; import android.support.v7.widget.toolbar; import android.support.design.widget.tablayout; import android.support.v4.view.viewpager; import android.support.design.widget.tablayout.tablayoutonpagechangelistener; import android.app.alertdialog; import android.app.fragmenttransaction; import android.app.progressdialog; import android.content.contentresolver; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.sharedpreferences; import android.content.res.typedarray; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.provider.settings; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.app.fragmenttabhost; import android.support.v4.widget.drawerlayout; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.imageview; import android.widget.listview; import android.widget.popupwindow; import android.widget.relativelayout; import android.widget.simpleadapter; import android.widget.spinner; import android.widget.textview; import android.widget.toast; import com.squareup.picasso.picasso; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; import java.util.list; import info.sants_thapal.customgridviewadapter.viewholder; import static android.provider.settings.system.airplane_mode_on; public class social extends appcompatactivity implements onitemselectedlistener { private static final string tab_5_tag = null; // tab titles private string[] tabs = {"life", "friends", "groups"}; sharedpreferences spref; button login; string session_email = "", session_type = "", session_st, session_ph, session_loc; //tabhost tabhost1; arraylist<hashmap<string, object>> userslist, userslist1; int imageid; imageview imageview; textview text, tv, tv2, tv3; drawerlayout mdrawerlayout; string namess, counta; // listview represents navigation drawer listview mdrawerlist, mdrawerlist1; private relativelayout mdrawerrelativelayout; // actionbardrawertoggle indicates presence of navigation drawer in action bar actionbardrawertoggle mdrawertoggle; handler mhandler; runnable refresh; context mcontext; string o, n, afsa; int abd2; private button msubmit; relativelayout notifcount, notifcount2, notifcount3; private popupwindow pwindo; menuitem item, item2, item3; viewholder viewholder; list<hashmap<string, string>> alist; public string[] namesaram, countrycodes; private typedarray imgs; // title of action bar string mtitle = ""; static string ip = ipaddress.ip; private string pre_image2 = ip + "/images/procover/"; private static string url_all_properties = ip + "/social/pic2.php"; private static string url_all_properties3 = ip + "/social/coun.php"; private static string url_all_properties5 = ip + "/social/coun2.php"; //private static string url_all_properties6 = ip+"/social/coun6.php"; private static string url_all_properties303 = ip + "/social/logou.php"; private static string url_all_properties99 = ip + "/social/spanfspinpost.php"; private static string url_all_properties2 = ip + "/social/spanfspinpost2.php"; private static string url_all_properties999 = ip + "/social/spanfspinpost3.php"; private static string url_create_product = ip + "/social/save.php"; private static final string tag_success = "success"; //private static final string tag_success1 = "mass"; private static final string tag_user = "users"; jsonparser jparser = new jsonparser(); //private string pre_image = ip+"/upload/"; private static final string tag_pro = "propertiesgfdfgd"; private static final string tag_pro2 = "properties"; private static final string log_tag = null; protected static final int num_threads = 0; //private static final string tag_pro1 = "properties1"; // products jsonarray jsonarray users = null; //jsonarray users1 = null; jsonparser jsonparser = new jsonparser(); int abu, abd; private fragmenttabhost mtabhost; view layout; tablayout tablayout; viewpager viewpager; /** * attention: auto-generated implement app indexing api. * see https://g.co/appindexing/androidstudio more information. */ /*public class viewholder { textview tv,tv2; }*/ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.social); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); spref = this.getsharedpreferences("real", context.mode_private); // intent=getintent(); session_email = spref.getstring("session_uid", ""); session_ph = spref.getstring("session_ph", ""); session_type = spref.getstring("session_type", ""); session_st = spref.getstring("session_stat", ""); session_loc = spref.getstring("session_loc", ""); //private string[] tabs = {"life", "friends", "groups"}; tablayout = (tablayout) findviewbyid(r.id.tab_layout); tablayout.addtab(tablayout.newtab().settext("life")); tablayout.addtab(tablayout.newtab().settext("friends")); tablayout.addtab(tablayout.newtab().settext("groups")); tablayout.settabgravity(tablayout.gravity_fill); viewpager = (viewpager) findviewbyid(r.id.pager); tabspageradapter madapter = new tabspageradapter(getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.setadapter(madapter); viewpager.addonpagechangelistener(new tablayoutonpagechangelistener(tablayout)); tablayout.post(new runnable() { @override public void run() { tablayout.setupwithviewpager(viewpager); } }); //tablayout.setupwithviewpager(viewpager); tablayout.setontabselectedlistener(new tablayout.viewpagerontabselectedlistener(viewpager) { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } }
the other class file code call tab pages tabspageradapter.java
package info.sants_thapal; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; public class tabspageradapter extends fragmentstatepageradapter { int mnumoftabs; public tabspageradapter(fragmentmanager fm, int numoftabs) { super(fm); this.mnumoftabs = numoftabs; } @override public fragment getitem(int index) { switch (index) { case 0: // top rated fragment activity tabfragment1 tab1 = new tabfragment1(); return tab1; case 1: // games fragment activity tabfragment2 tab2 = new tabfragment2(); return tab2; case 2: // movies fragment activity tabfragment3 tab3 = new tabfragment3(); return tab3; default: return null; } } @override public int getcount() { // item count - equal number of tabs return mnumoftabs; } public void clear() { // todo auto-generated method stub } }
build .grade file minsdkversion 21 target sdk version 22
compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:design:24.2.1'
override method in tabspageradapter.java call tab name.
private string[] tabtitles = new string[]{"tab1", "tab2", "tab3"}; public charsequence getpagetitle(int position) { return tabtitles[position]; }
it might you.
Comments
Post a Comment