In my Android application I have a TabHost which contains multiple tabs. To create each tab, I call a function called createTabView which looks like this:
private static View createTabView(final Context context, final String text, int layoutId) {
View view = LayoutInflater.from(context).inflate(layoutId, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
if (tv != null) {
tv.setText(text.toUpperCase(Locale.getDefault()));
if(text.toLowerCase().equals("special")){
tv.setTextColor(R.color.gold);
}
}
return view;
}
The above basically adds the appropriate text to the TextView and colours the TextView to gold if the string is "special".
The problem however is that it doesn't render gold as it should. It renders a very dark blue colour.
I can confirm that R.color.gold is definitely gold. I have used it in many places. I have also tried system colours too but they do not work either. It always shows as a dark blue colour.
Any ideas?
Use-
textView.setTextColor(getResources().getColor(R.color.gold));
Use this code
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#colorcode"));
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#colorcode"));
}
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#colorcode"));
TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
tv.setTextColor(Color.parseColor("#colorcode"));
}
and this method
#Override
public void onTabChanged(String arg0) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#colorcode"));
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#colorcode"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#colorcode"));
TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
tv.setTextColor(Color.parseColor("#colorcode"));
}
Related
I'm a beginner in android developement and I want to build a small app that gives you the possibility to show the values of an Array into the TextView but I want it to show one value and then it disappears then the second value disappears and so on. This will happen after the onClick() method.
This is my Java Code :
public void GetMyIdea(View view) {
String testatrix[]={"a","s","d", "f","g"};
TextView tv = (TextView) findViewById(R.id.tv);
for(int l=0; l<1; l++){
tv.append(testatrix[l]);
}
}
String testatrix[]={"a","s","d", "f","g"};
int i=0;
TextView tv = (TextView) findViewById(R.id.tv);
public void showText(){
tv.setText(testatrix[i]);
i++;
if(i>4){
i=0;
}
}
Call showText on click
So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?
Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}
I am using Tabhost in Android app for navigation. All works well, however one of my tabs is for Messages and if the user has at least 1 message, I'd like to add a textview to the tab icon showing the number of messages the user has. So basically I have the icon and the text Messages below, and would like to have an additional textview on the top right of the tab icon showing the message count.
I've found posts related to adding text to the tab, however it merely modifies the existing tab indicator textview. Is it possible to have an additional textview on the tab that I can reference and have set to visibility gone normally, and if messages exist update the visibility to visible and show the count?
I'm guessing I'll need to create a custom XML layout for this tab and use it when calling
.setIndicator("Messages",res.getDrawable(R.drawable.tab_messages))
Any insight or examples are greatly appreciated, thank you!!
To archieve this, you would need to implement a custom TabHostthat overrides the one from the Android framework.
public class CustomTabHost extends TabHost {
public CustomTabHost(Context context) {
super(context);
}
public CustomTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Androids TabHost has an inner class called TabSpec that implements the following method:
public TabSpec setIndicator(CharSequence label, Drawable icon) {
mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
return this;
}
so in order to add another TextView to the Tab, you need to overload this method like this:
public TabSpec setIndicator(CharSequence label, Drawable icon, CharSequence text) {
mIndicatorStrategy = new LabelIconTextIndicatorStrategy(label, icon, text);
return this;
}
To make this work, you also need to implement a LabelIconTextIndicatorStrategy that works similar to the LabelAndIconIndicatorStrategy, but has a text included.
private class LabelIconTextIndicatorStrategy implements IndicatorStrategy {
private final CharSequence mLabel;
private final Drawable mIcon;
private final CharSequence mText;
private LabelIconTextIndicatorStrategy(CharSequence label, Drawable icon, CharSequence text) {
mLabel = label;
mIcon = icon;
mText = text;
}
public View createIndicatorView() {
final Context context = getContext();
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabIndicator = inflater.inflate(mTabLayoutId,
mTabWidget, // tab widget is the parent
false); // no inflate params
final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
// when icon is gone by default, we're in exclusive mode
final boolean exclusive = iconView.getVisibility() == View.GONE;
final boolean bindIcon = !exclusive || TextUtils.isEmpty(mLabel);
tv.setText(mLabel);
if (bindIcon && mIcon != null) {
iconView.setImageDrawable(mIcon);
iconView.setVisibility(VISIBLE);
}
if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT) {
// Donut apps get old color scheme
tabIndicator.setBackgroundResource(R.drawable.tab_indicator_v4);
tv.setTextColor(context.getResources().getColorStateList(R.color.tab_indicator_text_v4));
}
return tabIndicator;
}
}
Please check this example it's work. It's display badge in your tabbar
TAB_BAR SHOWING NUMBER
I am Android novice, hoping my question is clear enough. I have an image and a rating bar at a certain position in an adapter and I add them to a Fragment. I would like the brightness of the image to change as the user changes the ratings on the Ratingbar but have no clue where to begin.
This is the function where I instantiate the layout that contains them :
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.rmd1_custom_imageslider_layout, null);
ImageView image=(ImageView) layout.findViewById(R.id.myimage);
image.setImageResource(CriteriaImages.get(position));
TextView text=(TextView) layout.findViewById(R.id.myImageViewText);
text.setText("Rate my "+CriteriaNames.get(position)+" today");
RatingBar rating = (RatingBar) layout.findViewById(R.id.ratingCategory);
rating.setId(position);
Button btn=(Button) layout.findViewById(R.id.done_button);
((ViewPager) container).addView(layout);
return layout;
}
You have to set a listener, a RatingBar.OnRatingBarChangeListener.
rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
#Override
public void onRatingBarChanged( .... ) {
//DO YOUR STUFF, like setting the alpha value of your image.
}
}
I have an Activity that displays comments. The comments themselves have a layout, so I can't just use a ListView.
I'm adding the comments with a loop, and the program goes through the whole loop (checked via LogCat), but only adds the first View (comment) to the linearlayout.
My code (in reality the fillComments parameter will be something else than String[]):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_layout);
String[] comments = {"kommentaar 1", "kommentaar 2", "kommentaar 3"};
mTitle = (TextView) findViewById(R.id.comments_title);
mTextArea = (EditText) findViewById(R.id.comment_editor);
mAddButton = (Button) findViewById(R.id.add_comment);
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
mTitle.setText(getIntent().getStringExtra("name"));
fillComments(comments);
}
private void fillComments(String[] comments) {
View comment;
TextView commentator;
TextView commentDate;
TextView commentText;
LayoutInflater inflater = getLayoutInflater();
for (String s : comments) {
Log.d("Comment adder", "Adding comment " + s);
comment = inflater.inflate(R.layout.comment_row_layout, null);
commentator = (TextView) comment.findViewById(R.id.commentator);
commentDate = (TextView) comment.findViewById(R.id.comment_date);
commentText = (TextView) comment.findViewById(R.id.comment_text);
commentator.setText("Test commentator");
commentDate.setText("12-12-2012");
commentText.setText(s);
mCommentArea.addView(comment);
}
}
i think
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
this layout orientation is Horizontal so this problems occurs. please if horizontal orientations then please change it to vertical and enjoy
How have you defined the LinearLayout? It could be just a display issue.
Check the size and orientation of LinearLayout.