How to change font size in TabLayout? - java

In my application I want to use TabLayout and I use this library:
https://github.com/LiushuiXiaoxia/TabLayoutPlus
However, the text size is big and I want to change it. For this I set custom style in style.xml, but it does not change the text size.
XML Code:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="#+id/fullSearch_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fullSearch_toolbar"
android:background="#color/colorPrimary"
android:paddingBottom="#dimen/size2"
android:paddingLeft="#dimen/size2"
android:paddingRight="#dimen/size2"
app:tabIndicatorColor="#color/whiteMe"
app:tabSelectedTextColor="#color/whiteMe"
app:tabTextAppearance="#style/allCapsTabLayout_search"
app:tabTextColor="#color/unSelectTab" />
<style name="allCapsTabLayout_search" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">#dimen/font5</item>
</style>
How can I change text size?

Go to
1> Your Library Module
2> open a file "tablayoutplus_custom_view.xml"
3> set android:textSize="20sp" into "android:id="#+id/tvTabText""
OR
Set Programatically in Library module in "BadgedTabCustomView.java":
tvTabText.setTextSize(20);
Hope this helps you now.

Use this: use Parent style as parent="Widget.Design.TabLayout"
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<!--<item name="tabMaxWidth">#dimen/tab_max_width</item>-->
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">10dp</item>
<item name="tabPaddingEnd">10dp</item>
<item name="tabBackground">#color/grey_200</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">#color/appred</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#color/grey_800</item>
<item name="textAllCaps">true</item>
</style>
Then apply your style in Tablayout:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
//
style="#style/MyCustomTabLayout"
// />
(or)
Use this change progrmmatically
for (int i = 0; i < tabLayout.getTabCount(); i++) {
BadgedTabCustomView customView = tabLayout.getTabCustomViewAt(i);
if (customView != null) {
customView.setTabText("Tab" + (i + 1));
customView.setTabCount(i);
customView.setTextSize(20);
}
}

#RedBounce I hope below method work for you. style attribute may be not work in your TabLayout because you have used custome tabview.
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
open TabLayoutPlus.java file and find the below method inside this class
private BadgedTabCustomView initTab(TabLayout.Tab tab) {
BadgedTabCustomView customView = new BadgedTabCustomView(getContext());
customView.tvTabText.setTextColor(getTabTextColors());
//add your tab font size here as you want
customView.tvTabText.setTextSize(20);
customView.tvTabSubText.setTextColor(getTabTextColors());
if (subTextSize > 0) {
customView.tvTabSubText.setTextSize(TypedValue.COMPLEX_UNIT_PX, subTextSize);
}
customView.tvTabCount.setTextColor(countTextColor);
if (countTextSize > 0) {
customView.tvTabCount.setTextSize(TypedValue.COMPLEX_UNIT_PX, countTextSize);
}
customView.tvTabCount.setBackgroundDrawable(countTextBackground);
customView.setTabText(tab.getText());
tab.setCustomView(customView);
return customView;
}
just put below line in this method
customView.tvTabText.setTextSize(20); //20 is font size

Related

PreferenceScreen change colors

I have an issue styling the (PreferenceScreen) in my project.
How can I change the colors of of (Title) & (Summary) texts.
No luck after trying for hours with different methods, I must be doing something wrong
SettingsFragment.Java
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.flContent, new SettingsPreference())
.commit();
}
public static class SettingsPreference extends PreferenceFragment {
public SettingsPreference() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
fragment_setings.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/app_background"
android:clickable="true"
android:focusable="true"
tools:context="com.companyv.app.fragments.SettingsFragment">
</FrameLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="pref_key_storage_settings"
android:title="First">
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_auto_delete"
android:summary="Sub 1 summary"
android:title="Sub 1" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_sms_delete_limit"
android:summary="Sub 2 summary"
android:title="Sub 2" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_mms_delete_limit"
android:summary="Sub 3 summary"
android:title="Sub 3" />
</PreferenceCategory>
</PreferenceScreen>
I have done this before by using a custom preference theme and setting the
android:textColor(title color) and android:textColorSecondary(summary color) in the custom theme. For example, black title and white summary:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/AppPreferenceTheme</item>
</style>
<!-- Custom Preference Theme -->
<style name="AppPreferenceTheme"
parent="#style/PreferenceThemeOverlay.v14.Material">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
You can rename "AppPreferenceTheme" to whatever name you like.

Text Color Not Changing in AlertDialogue

I am trying to apply custom color in AlertDialogue. Its changing text color as well background color in it but not changing SingleChoiceItems text color. You can see it black color in it with page number like below image.
I am using style like below code
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:textColor">#color/TextColorLite</item>
<item name="android:background">#color/colorPrimaryDarkLite</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">?attr/MainAccentColor</item>
<item name="android:buttonBarButtonStyle">#style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">?attr/TextColor</item>
<item name="android:textStyle">bold</item>
</style>
Java Code For Dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.AlertDialogTheme);
builder.setTitle("Go to Page:");
builder.setSingleChoiceItems(mPageOptions, mPageIndx - 1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
mOptionDiag.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
mOptionDiag = builder.create();
mOptionDiag.show();
What I should do for change this black text color ?
Thanks
I have got solved adding this items in style.
<!--For List Text-->
<item name="textColorAlertDialogListItem">#color/TextColorLite</item>
<!--For Radio-->
<item name="android:textColorSecondary">#color/TextColorLite</item>
Thanks
In your resources folder open styles.xml and add a custom theme like for example this:
<style name="radiobuttonstyle" parent="Material.Drawable.RadioButton">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/md_green_900</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/md_green_400</item>
<item name="colorControlNormal">#color/md_green_900</item>
<item name="colorControlActivated">#color/md_green_400</item>
</style>
and in your RadioButton in your layout add it as the theme of your RadioButton like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioyes"
android:theme="#style/radiobuttonstyle"
android:text="Yes"
android:textColorLink="#color/md_light_green_900"
android:textColorHighlight="#color/md_green_900"
android:textColor="#color/md_light_green_900" />
PS: that is my own custom radio button so it should work.

Can't change AlertDialog items colors

I created multiple alert dialogs in different activities. Now all of sudden the AlertDialog items appear in red !
Here is my code
AlertDialog.Builder builder = new AlertDialog.Builder(this, MyDialogStyle);
builder.setTitle("Select Method :");
final String[] choices = new String[]{"Weight Based", "Body Surface Area"};
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on choice[which]
switch (choices[which]) {
case "Weight": {
create_item(false);
break;
}
case "Body Surface Area": {
create_item(true);
break;
}
}
}
});
builder.show();
styles.xml (v21)
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:colorPrimary">#000000</item>
<item name="android:colorAccent">#000000</item>
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearanceListItemSecondary">#000000</item>
<item name="android:textAppearance">#000000</item>
<item name="android:color">#000000</item>
styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>\
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearance">#000000</item>
</style>
I tried changing the parent style or deleting it, but both give the same bright red result ! any ideas ?
try use this style ,,, hope it'll work for you
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>

This Activity already has an action bar supplied by the window decor?

I am making an Android app using Toolbar with Custom style but when i'm running the app, i'm getting the following error:
Caused by: java.lang.IllegalStateException: This Activity already has
an action bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in
your theme to use a Toolbar instead.
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:textColor">#color/textColorPrimary</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:textColorSecondary">#color/textColorPrimary</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
</style>
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Used for the bottom line when not selected / focused -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:colorBackground">#color/colorBackground</item>
<item name="android:textStyle">bold</item>
<item name="android:endColor">#color/colorPrimary</item>
<!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>
<style name="TabTextAppearance" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:colorBackground">#color/colorBackground</item>
<item name="android:textAllCaps">false</item>
<item name="android:textStyle">bold</item>
</style>
MainActivity:
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_information_category1);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar_information);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new TodayInformationFragment(), "Today");
adapter.addFragment(new ThisWeekInformationFragment(), "This Week");
adapter.addFragment(new ThisMonthInformationFragment(), "This Month");
adapter.addFragment(new AllyearInformationFragment(), "All");
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Clearly error says:
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead
Use this in your Styles:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Updated:
Android studio's default example is like this, take a look at the AppTheme.NoActionBar:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
And in your Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And make sure your Toolbar implemented correctly.
For example, with AppbarLayout:
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarmain"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ColorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Just go to your Manifest file and in the specific activity add
android:theme="#style/AppTheme.NoActionBar"/>
in my case that happened because the phone was in dark mode and I didn't provide a dark mode for my app maybe. it works fine after disabling the dark mode.
in my case i was testing on mobile which was in dark theme i got this and the following code done work for me
<style name="Theme.QrCodeScanner"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
set this in both themes.xm and themes.xml(night)
or if you want it for a specific activty then do the following in your manifest in specific activity
android:theme="#style/AppTheme.NoActionBar"/>

How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme?

I have black background with white letters.
Any ideas?
Thanks.
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:textColor">#android:color/white</item>
</style>
Normal:
Click in menu button, and show :
Modify the style like this:
<style name="PacerTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#android:color/white</item>
</style>
For "myCustomMenuTextApearance" style use for parent
#android:style/TextAppearance.Widget.IconMenu.Item
instead of
#style/Widget.AppCompat.Light.ActionBar.Solid
I found the solution, program change text color, as follows:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
for(int i=0; i<menu.size(); i++)
{
MenuItem mi = menu.getItem(i);
String title = mi.getTitle().toString();
Spannable newTitle = new SpannableString(title);
newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(newTitle);
}
return true;
}
The XML is as follows:
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
</style>

Categories

Resources