How to set FragmentTabHost tab text color - android

How to set FragmentTabHost tab text color. I tried the following code but, it didn't work.
((TextView) mTabHost.getCurrentTabView()
.findViewById(android.R.id.title)).setTextColor(0xFFFFFFFF);
It gives NPE saying it couldn't find the TextView.

It was a bit tricky. I used the following code and it worked for me.
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
final TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
// Look for the title view to ensure this is an indicator and not a divider.(I didn't know, it would return divider too, so I was getting an NPE)
if (tv == null)
continue;
else
tv.setTextColor(0xFFFFFFFF);
}

let's try this :
for example when you add your tab make your Indicator :
TextView view = ....
vew.setTextColor(...)
then setIndicator with your custom view :
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),
FragmentStackSupport.CountingFragment.class, null);

Related

how to get the id of the title of the scrollview to rename(title in the app bar)

i want to get the id of the scrollview title so that I can rename it with user input. is there anything I can place in the xml or java to give it some kinda of id that I can reference
Maybe this will be useful, if you have a toolbar, try this:
TextView toolbarTextView;
for (int i = 0; i < yourToolbar.getChildCount(); ++i) {
final View toolbarChild = yourToolbar.getChildAt(i);
if (toolbarChild instanceof TextView) {
toolbarTextView = (TextView) toolbarChild;
}
}
Or if you have a supportActionBar, try this:
final ActionBar myActionBar = getSupportActionBar();
if (myActionBar != null) {
myActionBar.setTitle("This a new text");
}

How to apply font to whole application?

Everyone
I want to apply my custom font to whole application not just a textview or not for an activity. if it is possible make just change in manifest file so it is easy for me.
so help me out.
You can create custom textview class and use that class whenever you need to use custom fonts.
Check this link: How to make a custom TextView?
There's also a library that does this. Very simple to use and set up.
https://github.com/chrisjenx/Calligraphy
Try this method for all activity. Or you can make this method in you utility class and use in you activity class see bellow.
Method in your utility.java file.
public static void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText
|| v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
And then in your Activity.java file.
ll_Homescreen = (LinearLayout) findViewById(R.id.ll_Homescreen);
FontStyle = Typeface.createFromAsset(getAssets(), path of you font);
utility.setFont(ll_Homescreen, FontStyle);
I hope this will help you Happy Coding.

How to change the selected tab color in swipe tabs?

Currently the selected tab in my app is highlighted in blue, like shown below.
How can I give it a different color?
You can do something like this :
TabHost host = (TabHost)view.findViewById(R.id.tab_host);
TabWidget widget = host.getTabWidget();
for(int i = 0; i < widget.getChildCount(); i++) {
View v = widget.getChildAt(i);
// Look for the title view to ensure this is an indicator and not a divider.
TextView tv = (TextView)v.findViewById(android.R.id.title);
if(tv == null) {
continue;
}
v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
}
Check this answer https://stackoverflow.com/a/15750561/3651574.
It will solve your problem. Cheers!!

FragmentTabHost return null when I get the tag of the tab

Here is how I create the tabhost, what I would like to do is get the tab when the user click on the tab. The problem is , it return null instead of the tag value.
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.meun_home)),
HomeFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("form").setIndicator("",
getResources().getDrawable(R.drawable.meun_register)),
FormFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("calculator").setIndicator("",
getResources().getDrawable(R.drawable.meun_calculator)),
CalculatorFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("news").setIndicator("",
getResources().getDrawable(R.drawable.meun_call)),
HomeFragment.class, null);
tabHost.getTabWidget().setDividerDrawable(
getResources().getDrawable(R.drawable.border));
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.transparent));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT, 1f);
tabHost.getTabWidget().getChildAt(i).setLayoutParams(params);
tabHost.getTabWidget().getChildAt(i).setPadding(0, 0, 0, 0);
tabHost.getTabWidget().getChildAt(i).setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_UP) {
Log.d("test1",""+v.getTag());
return true; // doesnt allow tab change
}
return false;
}
});
}
You can find the line Log.d("test1",""+v.getTag()); which return null. Which expected is return some string like "home" , "form", etc..... Thanks a lot
because you do not set any tag to your View of your newTabSpec. you set drawable icon but you do not set any Tag. So you can do it in two ways:
1) first create an imageView from your drawable icon:
imageView.setImageBitmap(bitmap);
imageView.setImageResource(R.drawable.my_image);
imageView.setImageDrawable(drawable);
and then set Tag to your imageView and use setIndicator(View view) to add it to your newTabSpec.
2) or after you add your drawable you can use getChildTabViewAt() to get the View and then set the Tag for it.
in summary you have not set any tag so it is obviously null.
the doc says:
TabHost.TabSpec:
A tab has a tab indicator, content, and a tag that is used to keep
track of it. This builder helps choose among these options. For the
tab indicator, your choices are: 1) set a label 2) set a label and an
icon For the tab content,
so that string is just a label, the tag and the label is two different things, tag is memory of a view that you can store any object in to it that object can be a string or anything else like ViewHolder design pattern that used in creating listview.
Here is solution 2) as recommended by the last answered author:
View vv = tabWidget.getChildAt(i);
vv.setTag("mytag_"+i);

ActionBarSherlock (ABS): how to customize the text of action mode close item?

I'm using ABS vers. 4 and I need to simply change the default "Done" text that is displayed besides the action mode close icon, but I really can't figure out how to do it.
I think that text needs to be customizable for at least two good reasons:
"Done" is not appropriate for all contexts (e.g. "Cancel" could be more appropriate, and I've seen some apps, such as the "My Files" app on Galaxy Tab, use it)
"Done" needs to be localized according to the user's language
Is it possible to do customize that text? If so can anyone tell me how to do it?
Thanks in advance.
EDIT
I've found a temporary workaround, that I post in the following:
private TextView getActionModeCloseTextView() {
// ABS 4.0 defines action mode close button text only for "large" layouts
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE)
{
// retrieves the LinearLayout containing the action mode close button text
LinearLayout action_mode_close_button =
(LinearLayout) getActivity().findViewById(R.id.abs__action_mode_close_button);
// if found, returns its last child
// (in ABS 4.0 there is no other way to refer to it,
// since it doesn't have an id nor a tag)
if (action_mode_close_button != null) return (TextView)
action_mode_close_button.getChildAt(action_mode_close_button.getChildCount() - 1);
}
return null;
}
That's the method I came up with. Please NOTE that it does heavily rely upon the structure of the abs__action_mode_close_item.xml of ABS 4.0.
This works for my scenario, but, as you can see, it cannot be considered sufficiently satisfying to promote it to a real "answer", that's why I only edited my previous post.
Hope that helps someone else, but I also hope that someone could share a better and cleaner solution.
You can use a theme to override the default icon:
<item name="actionModeCloseDrawable">#drawable/navigation_back</item>
<item name="android:actionModeCloseDrawable">#drawable/navigation_back</item>
I edited the code from PacificSky to be able to customize the color and font size of the close button, both in pre ICS and >ICS.
I created a method named customizeActionModeCloseButton
private void customizeActionModeCloseButton() {
int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
View v = getGSActivity().findViewById(buttonId);
if (v == null) {
buttonId = R.id.abs__action_mode_close_button;
v = getGSActivity().findViewById(buttonId);
}
if (v == null)
return;
LinearLayout ll = (LinearLayout) v;
if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
TextView tv = (TextView) ll.getChildAt(1);
tv.setText(R.string.close_action_mode);
tv.setTextColor(getResources().getColor(R.color.white));
tv.setTextSize(18);
}
}
and I call it just after calling startActionMode()
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
actionMode = getActivity().startActionMode(this);
customizeActionModeCloseButton();
return true;
}
It's been a while, but here's a slightly less hacky solution - putting it out there for posterity.
For Android versions < ICS
Put the following line in your application's strings.xml:
<string name="abs__action_mode_done">Cancel</string>
This overrides the TextView's (defined in ActionBarSherlock/res/layout-large/abs__action_mode_close_item.xml) android:text attribute.
For Android versions ICS and above
The native ActionBar functionality is used on ICS and up. You need to find and override the string associated with the done button, using the following code:
int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
if (buttonId != 0)
{
View v = findViewById(buttonId);
if (v != null)
{
LinearLayout ll = (LinearLayout)v;
View child = ll.getChildAt(1);
if (child != null)
{
TextView tv = (TextView)child;
tv.setText(R.string.cancel);
}
}
}
Thanks for PacificSky's answer. It's useful for my case.
Something needs to be explained here is that findViewById(buttonId) might return null in some cases such as called in onCreateActionMode() function, because the LinearLayout for ActionMode close button not yet initialized at that time I guess.
I want to hide the action mode close button, so i just sendEmptyMessageDelayed in onCreateActionMode() and call PacificSky's 200ms later. It works for me.
Here is my approach with Java code:
private void customizeActionModeCloseButton(String title, int iconID) {
int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
View v = findViewById(buttonId);
if (v == null) {
buttonId = R.id.abs__action_mode_close_button;
v = findViewById(buttonId);
}
if (v == null)
return;
LinearLayout ll = (LinearLayout) v;
if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
//custom icon
ImageView img = (ImageView) ll.getChildAt(0);
img.setImageResource(iconID);
//custom text
TextView tv = (TextView) ll.getChildAt(1);
tv.setText(title);
tv.setTextColor(Color.WHITE);
}
}
com.actionbarsherlock.view.ActionMode contains method:
setTitle
It is used to change text near Close Icon in the ActionBar.
ActionMode is available in your com.actionbarsherlock.view.ActionMode.Callback interface implementation methods, like onCreateActionMode.
What you can do - is save incoming ActionMode reference and use it later to change title as your like. Or, if it is not dynamic - you can setup at with your constant in onCreateActionMode.

Categories

Resources