C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

Android Tabbed Activity - 1

In this article, we are going to learn how to make use of tabbed activity in Android?
Submitted by Manu Jemini, on January 11, 2018

In the android studio, we have been given a variety of different activities to make use of any one of them is Tabbed activity. This activity can easily be included in your android studio project. To add the activity Open your android studio. Now when your project is opened that will look similar to the below picture.

Android Activity 0

Your android project have a java and resource directory, java directory contains all of your JAVA files and rec dicrectory conatins all of your resources and layout files, which anyway like a resource.

To make a new activity, right click on Java folder and choose new → activity → Tabbed Activity. This is been illustrated below. After choosing the mentioned activity, Android Studio will ask to you about the name and other confriguations.

Android Activity 1

The Next step is to give information about the activity. First you should give the name of your activity, then layout name followed by fragment layout name, Title name.

The Important option is the check-box of launcher activity. If you want to make this the activity which should come at the beginning of the app, you have to check that check-box, else the studio will leave it as a separate activity.

You can also mention the parent of this activity if it has any. It comes down to the Navigation style. You should choose the style which you prefer. The Studio will take care of the package name.

That’s all, press FINISH.

Android Activity 2

You will have three new files, one java and two resource file. All three are set for the work to do. The Java activity will have the business code and fragment file will have the menu and the last layout file will have Viewpagers to set different fragment when user swipes or uses the menu.

Android Activity 3

You can change the Number of tabs in the java file and can also give each one a title.

@Override
public int getCount() {
    // Show 3 total pages.
    return 3;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "SECTION 1";
        case 1:
            return "SECTION 2";
        case 2:
            return "SECTION 3";
    }
    return null;
}

You will find this block of code in your JAVA file’s last section. Change the return statement of getCount() function and modify the switch() in the getPageTitle() function.

For example like this,

@Override
public int getCount() {
    // Show 5 total pages.
    return 3;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "SECTION 1";
        case 1:
            return "SECTION 2";
        case 2:
            return "SECTION 3";
	case 3:
            return "SECTION 4";
        case 4:
            return "SECTION 5";
    }
    return null;
}

Output

Tabbed activity in Android


Advertisements




Liked this article? Do share with your friends :)


COMMENTS