I'm trying to add a text switcher dynamically but I get the error:
The method setFactory(ViewSwitcher.ViewFactory) in the type ViewSwitcher is not applicable for the arguments...
Here's my code:
TextSwitcher ts = (TextSwitcher) new TextSwitcher(this);
ts.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
ts.setInAnimation(in);
ts.setOutAnimation(out);
The setFactory method expects a ViewSwitcher.ViewFactory type argument and not Activity (which I presume is your this.)
You have 3 options:
you can implement your own ViewSwitcher.ViewFactory. you can see an example here.
you can use ViewSwitcher.addView to dynamically add your child views
you can define the child views in your layout xml
Either one should work OK for you.
Your activity class needs to implement android.widget.ViewSwitcher.ViewFactory interface.
Related
I'm using a particular TextView: TextView txtStatusView = (TextView)findViewById(R.id.txtStatus); in several methods in my view's .java file. In each method I'm using the above line of code to find and initialize the same txtStatus view. Should I instead be putting this once at the top of the class (my thought: to prevent wasting time or power finding the view every time), or is it better that I have this line for each method that uses the view?
Declare the variable globally and define it inside your onCreate()
Declare like:
TextView txtStatusView;
Inside onCreate():
txtStatusView = (TextView)findViewById(R.id.txtStatus);
I have an android app that I have decided to rewrite, one of the reasons for the rewrite is because I could have 10+ TextViews with text set based on a variable in a class e.g.:
MyClass myClass = new MyClass();
myClass.myNumber = 5; // inside MyClass - public int myNumber;
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.mainLayout);
TextView myTextView = new TextView(getActivity()); //In a fragment
myTextView.setText(String.format("myNumber currently has a value of %d", myClass.myNumber));
mainLayout.addView(myTextView);
return view;
Up until now I have been using .setOnClickListener on the buttons/views that change myNumber, to set the text of the view again when the value of myNumber changes, which then calls .invalidate() to redraw the TextView, this has worked fine, but I use this method very heavily and code is getting quite repetitive and changing one integer can affect quite a lot of views (all of which use it differently - such as different wording, or a calculation (e.g. myNumber * 2)). I guess it's because it's made an immutable string in the TextView.
I have tried to create a custom TextView that implements Observer (making MyClass extend Observable) and in the update method I can get it to invalidate itself for the refresh, but it still has the same text. I have also tried creating single element arrays, in an attempt to pass the reference not the value in the hope that when it is changed and then the view is invalidated it will pick up the new value but the text still ends up remaining the same.
How can I get a TextView that will auto update when the value of myNumber has changed? Some sort of binding?
Bindroid works perfectly for this, just a note for users, using fragments the sample application is using this from an Activity so the bind method using Activity is called, so in the fragment I was using getActivity() which caused it to not work properly, digging around in the library I found a bind method that takes a View and passed in my view which gets inflated in the fragment and it works great!!! This is super easy to integrate btw it was just me not getting it!
FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
This gives me an error
error: cannot find symbol
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
^
symbol: method findViewById(int)
I have already imported the required R package
It seems like you are trying to access the layout of your current Activity from a different class. Instead of trying to find your FrameLayout in the different class, save the reference to the FrameLayout inside of your Activity, and pass the FrameLayout to your seperate class (the class where you are currently seeing this issue).
E.g.
Activity Class:
...
OtherObject myOtherObject = new OtherObject();
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.my_frame_layout);
myOtherObject.doStuffWithFrameLayout(frameLayout);
...
OtherObject Class:
...
public void doStuffWithFrameLayout(FrameLayout frameLayout) {
//You can use the FrameLayout here and do stuff with it.
//You will likely also want to pass in a Context object if you want to
//create a LayoutInflater or do other Context-dependent stuff
}
...
Try: FrameLayout content = (FrameLayout) findViewById(R.id.content);
In case this does not work remove the import of yourPackage.R and hit the button 'fix imports' not sure the import you did is correct. I always get 2 different options.
You can just call setContentView() again, but keep in mind that this will invalidate all of your View references, so make sure that you initialize them again.
There's almost never a reason to do this, so I would suggest you look into using Fragments, and just swap out the Fragments instead.
To use android.R.id.content, you must import android.R, not yourAppPackage.R .
And to use multiple layouts within one activity, you have to use ViewFlipper or ViewAnimator (or call setContentView multiple times, but it's resource expensive if you have huge layouts).
Hi I have an activity which holds a layout, the layout is divided into two linear layouts. The first layout has 4 buttons. The second one has fragments. Basically with each button press, a new fragment is displayed. All the fragments hold a layout . This layout has an edit text field, a button and a list view. Now everything is working fine, no errors till now. But the problem I am having is , when I try to create a database object and pass the context of the fragment class as parameter in the contructor, it simply shows an error. Here's the code...Please have a look and guide me how can I solve the problem.
String text = null;
EditText enter_task;
// enter_task would be provided with its id, not a problem,
text = enter_task.getText().toString();
try{
// this is where te problem is
// normally I could pass the context of the activity within the constructr of database class as parameter. But since this class is a fragment, I am simply not able to do so.
myDatabase_today = new Database(MyFragment_today.this);
}
The solution which eclispe provides to the problem are:
1-> Change contructor Database(Context) to Database(MyFragment_today).
// here MyFragment_today is the fragment class's name
2-> Create contructor Database(MyFragment_today).
Could anyone please solve this problem. I mean we can pass the activity's context , but not the fragment class's context, then how to proceede further.
Use getActivity() instead of the Fragment class name
myDatabase_today = new Database(getActivity());
I'm having issues with understanding how I should organize my user interface in Android. My original plan was to create TextViews and ListViews programatically and change them when buttons are clicked, etc.
Here's my first simple attempt. viewFriends is a method within my Activity class. It's called when a menu button is pressed.
private void viewFriends()
{
mText = new TextView(this);
mText.setText("Gathering information...");
setContentView(mText);
...irrelevant code follows
Why doesn't this seemingly simple example work? How should I logically organize and manage my user interface objects (TextViews, ListViews, Buttons, etc).
Thanks.
The best work would be having those listviews and textviews in your XML files and give them a suitable ID like following:
<ListView
android:id="#+id/myList"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
Just like above have your text view too in XML file an add the android:id attribute.
Once you define this way in your java file have references to them:
ListView myListObj = (ListView)findViewById(R.id.myList);
Now you have an object called myListObj in your java file and now you can do whatever you want to do with it.
:)
Let me if you find any issue in this so that I can update the answer to meet your specific need.
Don`t use setContentView in your method. Usually it should only be called once in the onCreate method of your activity.
Best predefine your bottons/TextViews in xml, get a handle for them (findViewbyId...)
and modify them that way.
If you create them programaticly, just add them to a view containter from your xml.
Like :
setContentView(R.layout.main);
Lets say in main.xml there is a LinearLayout with the id: root.
// get accces to that layout:
LinearLayout rootLayout = (LinearLayout) findViewById (R.id.root);
// create a new TextView
TextView tv1 = new TextView (this);
tv.setText("Hello!");
// add it to your base layout
rootLayout.addView(tv1);
// done! :)
Make a double check on what you are getting in "this".
change it to your java file name.this
You have to reload/refresh your activity once you change it.
Try this
#Override
protected void onResume() {
if(param.equalsIgnoreCase("gr"))
{
finish();
Intent myIntent = new Intent(yourActivity.this, yourActivity.class);
startActivity(myIntent);
}