Android app android studio Tab Layout using ViewPager and Fragment in APP
HELLO GUYS THIS ARTICLE YOU LEARN CRATE TAB LAYOUT USING VIEWPAGER AND FRAGMENT IN ANDROIOD STUDIO
Gues create your first Project in Android studio. Now your Project is Ready MainActivaty.xml and MainActivaty.java just like Image
Then the next layout folder right Mouse click and open Layout Resource file you name any want. ichose tab_bar. after that constanat layout change Linerlayout <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/textView" android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:fontFamily="sans-serif-light"
android:text="php"
android:textAlignment="center"
android:textColor="#2822CA"
android:textSize="25dp"
android:textStyle="bold|italic" />
<TextView android:id="@+id/textView1" android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:fontFamily="sans-serif-light"
android:text="html"
android:textAlignment="center"
android:textColor="#2822CA"
android:textSize="25dp"
android:textStyle="bold|italic" /> </LinearLayout>
Now go to MainActivaty.xml Layout change Relativelayout just type this
<?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"tools:context=".MainActivity"> <include layout="@layout/tab_bar"></include><android.support.v4.view.ViewPagerandroid:layout_width="match_parent"android:layout_marginTop="55dp"android:id="@+id/viewpager"android:layout_height="match_parent"></android.support.v4.view.ViewPager> </RelativeLayout>Now create 4 Resource file In layout Floder you name any want chose that.now create javaclasse just like Imageabove fist dicklar you textview and viewPager and pagerviewAdaptor but fist pagerviewAdaptoryou create Ovride meathod OncreateView . you don't how to create Override Mathode I willtech you keyboard press ctrl + Insert create Overide mathed genarate. return Ininflter.inflate(R.Layout.youresorce file name,null); this type ok about Image. now copyyour java classe and past java floder. just change your java clase name and resorce filename change.now go to MainActivaty.java Below i coding you copy it amd past you MainActivaty.java.public class MainActivity extends AppCompatActivity {private TextView php,html; ViewPager viewPager; PagerViewadatar pagerViewadatar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); php=(TextView)findViewById(R.id.textView); html=(TextView)findViewById(R.id.textView1); viewPager=(ViewPager)findViewById(R.id.viewpager); pagerViewadatar = new PagerViewadatar(getSupportFragmentManager()); viewPager.setAdapter(pagerViewadatar); php.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewPager.setCurrentItem(0); } }); html.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewPager.setCurrentItem(1); } }); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) { } @Override public void onPageSelected(int position) { changeTab(position); } @Override public void onPageScrollStateChanged(int state) { } }); } private void changeTab(int position) { if (position==0){ php.setTextColor(getColor(R.color.colorAccent)); php.setTextSize(25); html.setTextSize(getColor(R.color.colorPrimary)); html.setTextSize(20); } if (position==1){ php.setTextColor(getColor(R.color.colorPrimary)); php.setTextSize(20); html.setTextSize(getColor(R.color.colorAccent)); html.setTextSize(25); } }}
clase create. after that extend fragamentpagerAdaptor and Implemet three time. below i coad
past you look this.
class PagerViewadatar extends FragmentPagerAdapter { public PagerViewadatar(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment = null; switch (position){ case 0: fragment= new fragament_php(); break; case 1: fragment=new fragament_html(); break; } return fragment; } @Override public int getCount() { return 2; } }
there are fragament get item (int position) in here switch do that. and return value fragament
return 2; taking here but you you do you layout Resource file do you cout.then go to
MainActivaty.java pagerviewAdaptor red alart android studio.now create you tab bar layout
create. thank for your reading.
No comments