I am using MaterialTabHost for displaying tabs. I wanted to change the font of the MaterialTabHost when onTabSelected. I tried to get the textview of the same. but cant figured it out. I referred https://github.com/neokree/MaterialTabs.
any help would be appriciated.
Here is my XML .
<!-- for Text Tabs -->
<it.neokree.materialtabs.MaterialTabHost
android:id="#+id/materialTabHost_MainActivity"
android:layout_width="match_parent"
android:layout_height="48dp"
android:animateLayoutChanges="false"
app:accentColor="#color/white"
app:primaryColor="#color/main_blue_light"
app:textColor="#color/white"
/>
so how do we change the font ?
This is how I change font of text into tab when it is create:
Typeface fontRobotoRegular = Typeface.createFromAsset(getAssets(),"font/RobotoCondensed-Regular.ttf");
MaterialTab tab = new MaterialTab(getApplicationContext(), false);
View tabView = tab.getView();
TextView text = (TextView) tabView.findViewById(R.id.text);
tab.setText(pagerAdapter.getPageTitle(i));
text.setTypeface(fontRobotoRegular);
tab.setTabListener(this);
tabHost.addTab(tab);
So if you want to change font when the tab is selected you can do something like this on onPageSelected of your ViewPager:
public void onPageSelected(int position) {
...
Typeface fontBold = Typeface.createFromAsset(getAssets(), "font/RobotoCondensed-Bold.ttf");
TextView text = (TextView) tabs.getCurrentTab().getView().findViewById(R.id.text);
text.setTypeface(fontBold);
...
}
Related
In my app implementing tablayout with each tab having both icons and text.
When tab is selected then icon and text should be selected of same tab and
un-selected tab with different color text and icons.
below is my code to implement tabs layout but not able to change text color and icons color on tab selection.
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
tabOne.setText("Home");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_home, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
tabTwo.setText("Search");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_search, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
tabThree.setText("WishList");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_wishlist, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabFour = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
tabFour.setText("Cart");
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_cart, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
TextView tabFive = (TextView) LayoutInflater.from(mContext).inflate(R.layout.custome_tab_with_icon, null);
tabFive.setText("Account");
tabFive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.selector_accounts, 0, 0);
tabLayout.getTabAt(4).setCustomView(tabFive);
}
Please help how to change text color and icon when tab is selected.
TIA
Toggle tab text color
In your xml add the linesapp:tabTextColor and app:tabSelectedTextColor to the TabLayout .
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
app:tabTextColor="#000000"
app:tabSelectedTextColor="#FFFFFF"
android:layout_height="wrap_content"/>
Toggle tab icon In your fargment/activity add selector drawable to each tab.
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
//Set selector drawable to each tab
tabLayout.addTab(tabLayout.newTab().setText("Warm Up").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_warmup_icon,null)));
tabLayout.addTab(tabLayout.newTab().setText("Exercise").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_exercise_icon, null)));
tabLayout.addTab(tabLayout.newTab().setText("Rest").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_rest_icon, null)));
tabLayout.addTab(tabLayout.newTab().setText("Success").setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.selector_success_icon, null)));
selector_warmup_icon.xml (should be like)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_human_white_48dp" android:state_selected="true"/>
<item android:drawable="#drawable/ic_human_grey600_24dp" android:state_selected="false"/>
</selector>
You can do that by adding a TabLayout.OnTabSelectedListener and it has three methods onTabSelected(), onTabUnselected() and onTabReselected(), which you can use to change the color of both icon and text. Here's the link you can refer to it.
You can use a Color State List resource for the text color as well as for the tint of the icons. I think android:state_selected should work.
i want to get the subtitle´s textview in an android's toolbar to change it's font. Actually i'm doing it with the title, getting it on this way:
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(toolbar);
I've tried with the same code but trying to get "mSubtitleTextView" but that's not the solution.
Thanks!!
You can get the subtitle TextView like this:
View subTitleView = toolbar.getChildAt(1);
If you don't add any views to the toolbar, his default structure is:
[0] - (TextView) title
[1] - (TextView) subtitle
[2] - (ActionMenuView) menu
Hope it helps!
Definitely not the best way, but in a pinch this will work. You will have to figure out a way to implement myTypefaceSpan though; I'm using Calligraphy so it ties together decently for me.
CalligraphyTypefaceSpan myTypefaceSpan = new CalligraphyTypefaceSpan(
TypefaceUtils.load(this.getAssets(), "fonts/custom_font.ttf"));
public static void setToolbarSubtitle(String subtitle, Context context) {
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append(subtitle);
sBuilder.setSpan(MainActivity.myTypefaceSpan, 0, sBuilder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((MainActivity)context).getSupportActionBar().setSubtitle(sBuilder);
}
I am developing demo application in which I am using view pager. Now I want to know that can we change the text style of Title displayed in view pager.
Please give your suggestions on it.
Thanking you in advance.
Working demo
TextView txt = (TextView) v.findViewById(R.id.custom_font);
and then change your font
Something like this:
switch (position) {
case 0:
v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);
TextView txt = (TextView) v.findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
break;
}
This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via
mTabHost.getChildAt(index).setBackgroundColor();
but there isn't an option to change the tab's indicator. I've tried using an EditText.
private EditText tabName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
comm = new Communicator();
tabName = new EditText(this);
tabName.setText("BeginningText");
mTabHost = getTabHost();
mTabHost.addTab(mTabHost
.newTabSpec("tab_1_stat")
.setIndicator(User)
.setContent(R.id.meStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_2_stat")
.setIndicator(tabName.getText())
.setContent(R.id.themStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_3_stat")
.setIndicator("Archive")
.setContent(R.id.archiveStatsTab));
mTabHost.setOnTabChangedListener(this);
getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
mTabHost.setCurrentTab(0);
onTabChanged("tab_1_stat");
}
.....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
tabName.setText("ChangedTabText");
Bundle extras = intent.getExtras();
themStats = extras.getStringArray("themStats");
mTabHost.setCurrentTab(1);
onTabChanged("tab_2_stat");
}
That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!
try this easiest way which works for me :
tabLayout.getTabAt(index).setText("TabName");
Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.
TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");
or in one line...
((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");
where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.
Thanks again to radek-k. You definitely got me pointed in the right direction.
You can do it without knowing the magic index of the TextView by using findViewById:
TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
Here is your layout:
<com.google.android.material.tabs.TabLayout
android:id="#+id/ref_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_chpter"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_verse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.tabs.TabLayout>
Here is your code:
TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
TabWidget vTabs = .....;
// get desired tab view
View vTab = vTabs.getChildAt(i);
// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;
vText.setText(...);
You can do it like this
TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");
Where android.R.id.title is System generated, just change ChildIndex and Text according to your need
This is simple and easy. Try it. It works.
TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");
This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via
mTabHost.getChildAt(index).setBackgroundColor();
but there isn't an option to change the tab's indicator. I've tried using an EditText.
private EditText tabName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
comm = new Communicator();
tabName = new EditText(this);
tabName.setText("BeginningText");
mTabHost = getTabHost();
mTabHost.addTab(mTabHost
.newTabSpec("tab_1_stat")
.setIndicator(User)
.setContent(R.id.meStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_2_stat")
.setIndicator(tabName.getText())
.setContent(R.id.themStatsTab));
mTabHost.addTab(mTabHost
.newTabSpec("tab_3_stat")
.setIndicator("Archive")
.setContent(R.id.archiveStatsTab));
mTabHost.setOnTabChangedListener(this);
getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
mTabHost.setCurrentTab(0);
onTabChanged("tab_1_stat");
}
.....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
tabName.setText("ChangedTabText");
Bundle extras = intent.getExtras();
themStats = extras.getStringArray("themStats");
mTabHost.setCurrentTab(1);
onTabChanged("tab_2_stat");
}
That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!
try this easiest way which works for me :
tabLayout.getTabAt(index).setText("TabName");
Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.
TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");
or in one line...
((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");
where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.
Thanks again to radek-k. You definitely got me pointed in the right direction.
You can do it without knowing the magic index of the TextView by using findViewById:
TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
Here is your layout:
<com.google.android.material.tabs.TabLayout
android:id="#+id/ref_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_chpter"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:id="#+id/ref_verse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.tabs.TabLayout>
Here is your code:
TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
TabWidget vTabs = .....;
// get desired tab view
View vTab = vTabs.getChildAt(i);
// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;
vText.setText(...);
You can do it like this
TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");
Where android.R.id.title is System generated, just change ChildIndex and Text according to your need
This is simple and easy. Try it. It works.
TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");