Shyam Zawar Shyam Zawar - 2 months ago 19
Java Question

I want to change the Title label of My Activity programatically. How to change it?

I want to change the Title of this activity programatically. The Activity.setTitle method is giving me error. Can Anyone help Please.


IconTextTabsActivity.java


package com.shyamzawar.materialtabs.activity;

public class IconTextTabsActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

private int[] tabIcons = {
R.drawable.ic_account_circle_white,
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_text_tabs);



toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}

private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[0]);
tabLayout.getTabAt(4).setIcon(tabIcons[1]);
tabLayout.getTabAt(5).setIcon(tabIcons[2]);
tabLayout.getTabAt(6).setIcon(tabIcons[0]);
tabLayout.getTabAt(7).setIcon(tabIcons[1]);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new PersonalDetailsFragment(), " PERSONAL DETAILS");
adapter.addFrag(new EducationalDetailsFragment(), "EDUCATIONAL DETAILS");
adapter.addFrag(new CareerObjectiveFragment(), "CAREER OBJECTIVE");
adapter.addFrag(new WorkExperienceFragment(), "WORK EXPERIENCE");
adapter.addFrag(new ProjectsFragment(), "PROJECTS");
adapter.addFrag(new OtherFragment(), "OTHER");
adapter.addFrag(new ReferencesFragment(), "REFERENCES");
adapter.addFrag(new DeclarationFragment(), "DECLARATION");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}



AndroidManifext.xml


<?xml version="1.0" encoding="utf-8"?>




<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyMaterialTheme" >
<activity
android:name="com.shyamzawar.materialtabs.activity.MainActivity"
android:label="@string/app_name" >
<!--<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
</activity>
<activity
android:name="com.shyamzawar.materialtabs.activity.IconTextTabsActivity"
android:label="@string/title_activity_icon_text_tabs"
android:parentActivityName="com.shyamzawar.materialtabs.activity.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>



Answer

put these line :

setTitle("Your Name");