I want to animated text to fade in and out when changing I am extending TextSwitcher in order to define this once since I have many TextSwitcher objects.
However I keep on getting a Null pointer "java.lang.NullPointerException
android.widget.TextSwitcher.setText(TextSwitcher.java:80) when doing this :
homeTeamTextView.setText("Text");
I decided to extend the TextSwitcher class like this:
public class MyTextSwitcher extends TextSwitcher{
public MyTextSwitcher(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
setInAnimation(context,R.anim.fade_in);
setOutAnimation(context,R.anim.fade_out);
}}
Activity
private MyTextSwitcher homeTeamTextView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeTeamTextView = (MyTextSwitcher) findViewById(R.id.hometeam);
awayScoreTextView = (TextView) findViewById(R.id.awayteamscore);
homeScoreTextView = (TextView) findViewById(R.id.hometeamscore);
gameStatusTextView = (TextView) findViewById(R.id.gamestatus);
gameClockTextView = (TextView) findViewById(R.id.gameclock);
gameStartTextView = (TextView) findViewById(R.id.gamestart);
gameClockSectionTextView = (TextView) findViewById(R.id.gameclocksection);
LoadDataAsyncTask loadDataTask = new LoadDataAsyncTask();
loadDataTask.execute();
}
Layout:
<com.MyTextSwitcher
android:id="#+id/hometeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
It looks that TextSwitcher is common ViewSwitcher which can contain only TextViews. When you call setText() it try to get next view. You can find it in source: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/TextSwitcher.java, but it's null. I think you must add textView inside your TextSwitcher (or even two textView to properly working animation).
Related
So, I am trying to get reference to a xml layout from the class of a simple widget I made.
So ,my widget contains an ImageView and two TextViews.I will add the code for this widget, just so no one gets confused.
public class Item extends LinearLayout{
TextView tv1,tv2;
ImageView img;
public Item(Context context,int resid, String t1, String t2) {
super(context);
setOrientation(HORIZONTAL);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
img = new ImageView(context);
tv1 = new TextView(context);
tv2 = new TextView(context);
img.setBackgroundResource(resid);
img.setVisibility(View.VISIBLE);
img.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
tv1.setText(t1);
tv1.setTextSize(15);
tv1.setGravity(Gravity.CENTER);
tv1.setLayoutParams(new ViewGroup.LayoutParams(250, 100));
tv2.setText(t2+"lei");
tv2.setTextSize(15);
tv2.setGravity(Gravity.CENTER);
tv2.setLayoutParams(new ViewGroup.LayoutParams(250,100));
tv1.setBackgroundColor(Color.GREEN);
tv2.setBackgroundColor(Color.BLUE);
addView(img);
addView(tv1);
addView(tv2);
}
So, as you can see, there's a clickListener added for each "Item".What I want to do ,is to be able to refer to a xml layout that is a second activity ,so that I can manipulate what is in this layout from within this widget.
The second activity's class:
public class Final extends Activity {
LinearLayout fl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_layout);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
fl = (LinearLayout) rootView.findViewById(R.id.fl);
}
}
The XML file for this second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
So, once again, to sum up, I want to be able to add stuff to this second activity ,from the class Item ,but I am not able to take reference to the XML layout coresponding to the second activity.
So,practically ,the only way to do this, is to pass variables through the onclick method.
What I am doing right now is this: I get two String variables and one Int.Strings being used for the text wrote in textViews and int for the resource ID for the imageView's backgroundResource, pass them to the second activity and use them there to re-create the Item.
If anyone needs more details on this, leave a comment here and I'll do my best to help.
I'm developing a View that shows a stack of messages making fadeIn,fadeOut and wait time between them. It has a stack of messages and when I need to display some of them I added to it. This view hasn't a specific design so the user can customize like he wants.
I have written well the 3 super constructors.
The XML layout is the next:
[... Others views ...]
<com.example.lectorrss.Views.StatusMessage
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/statusMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#6894ed">
<ProgressBar
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/message"
android:layout_toRightOf="#id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Hola"
android:textColor="#fff"></TextView>
</RelativeLayout>
</com.example.lectorrss.Views.StatusMessage>
[... Other views ...]
In this case, I want my custom view shows the views that are inside (progress bar + textview) and then apply the behaviour I want.
The code of my CustomView is:
public class StatusMessage extends ViewGroup{
private List<String> messagesStack;
private Worker worker;
public StatusMessage(Context context) {
super(context);
}
public StatusMessage(Context context, AttributeSet attrs){
super(context, attrs);
}
public StatusMessage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
TextView txtView = (TextView) this.findViewById(R.id.message);
if(txtView == null){
Log.d("BrowsePortal", "TextView null");
}else{
// HERE, THE CODE CAN FIND OK THE VIEW I WANT
Log.d("BrowsePortal", "TextView not null");
}
worker = new Worker(this);
worker.execute();
}
[Other logic code about view]
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
But as my view needs time of wait between messages, and uses animation, I need to create an AsyncTask:
public class Worker extends AsyncTask<Void,String,Void>{
private StatusMessage view;
public Worker(StatusMessage view) {
this.view = view;
}
#Override
protected Void doInBackground(Void... arg0) {
[I supressed all logic code here]
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
TextView txtView = (TextView) view.findViewById(R.id.message);
if(txtView == null){
// But here the code return null
Log.d("BrowsePortal", "TextView null");
}else{
Log.d("BrowsePortal", "TextView not null");
}
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
[I supressed all logic code here]
}
}
My problem is that from asynctask, on the progressUpdate, when I need to access to the textview, findviewbyid returns null. But trying to access the same view from custom view class instead asynctask, it works. Why from view class I can access to textview but from asynctask not?
Also, in post execute, the same code returns me null.
My asynctask receives well in her constructor the view so from here would work well as from customview.
The issue was with this line:
import android.R which I change to import [app_package].R;.
Then, when I put this line:
TextView txtView = (TextView) view.findViewById(R.id.message);
... the method could not find the textview because was using as ID, a resource from android.R instead of package.R.
The most strange is that along activities I made this time ago, sometimes I did the same mistake, but I could fix quickly because Eclipse said me that the resource specified wasnt found underlining the resource line, but for some reason, in this case didn't notify me. Maybe R.id.message is reserved for android.R.
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
This should be very easy to do, but somehow after some 15 minutes searching I still can't get the answer:
I want to make a custom Android View combining a TextView and a Button, plus some customized behaviors/methods, let's say when I click the button it should change the TextView to "Hello, world!".
I know that I'll have to extends the View class, and design the layout in a XML, then do some magic to link the two. Could you tell me what the magic is? I know how to do this in an Activity, but not in a custom View.
EDITED
Ok, so I found out I need to use a Inflater to inflate my class with the child views defined in a layout. Here's what I got:
public class MyView extends View {
private TextView text;
private Button button;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.myview, null);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
text = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
}
}
However, the text and button child views were null. Any idea? (The XML is very simple without any fancy edit, i just grabbed a TextView and a Button from the eclipse toolbar and throw in.)
Ok, so the answer to my own question is: (i) go get dinner, (ii) extends LinearLayout instead of View, this makes it a ViewGroup and thus can be passed into the inflate(...) method but don't have to override the onLayout(...) method. The updated code would be:
public class MyView extends LinearLayout {
private TextView text;
private Button button;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.myview, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
text = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
}
}
I have a TextView .I am using it like a link by using
t2.setMovementMethod(LinkMovementMethod.getInstance())
for this textview in .java files so that it blinks when I clicks, but I want the color of textview to be changed when clicked. I used
t2.setLinkTextColor(0xff0000)
but does not work. my code is as follows:
public class TextHyperLink extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
TextView t2;
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t2 = (TextView) findViewById(R.id.text2); t2.setMovementMethod(LinkMovementMethod.getInstance());
t2.setLinkTextColor(0xff0000);
t2.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==t2)
{
// t2.setColor()
// System.out.println("Link TextViewwwwww");
}
}
}
my xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/link_text_manual"
android:textColorLink="#FFFF00"
/>
Can any one help me in solving this issue.?
The obvious answer is that you aren't calling setLinkTextColor() in the onClick method and when you add it if you want the color to change it has to be a different color than 0xff000.