Missing function in ViewPager from Android compatibility library - android

I'm trying to use the function setOffscreenPageLimit() in ViewPager, but Eclipse says The function setOffscreenPageLimit() is undefined for the type ViewPager
However, I see it in the docs and in many examples. ViewPager
Here's my code:
import android.support.v4.view.ViewPager;
public class MyView {
private final ViewPager viewPager;
MyView (ViewPager viewPager) {
this.viewPager = viewPager;
viewPager.setOffscreenPageLimit(2); //<-- can't find it
}
}
I think it must be some strange problems with the library. My minSdk is 7 and target is 13. I've pressed "check for updates" many times and even tried manually re-importing the compatibility library but this function still doesn't show up. Any ideas? I really need this function!

I think that .setOffscreenPageLimit() was a recent addition to the android support library. You should update your support library to the newest (currently v9) using Android SDK Manager. Cheers

I was having a similar problem and the solution was to add the jar file to the libs directory AND add it to the build path in the projects properties. Do not add the file as an external jar, click "add jar".
Hope this helps.

Related

TabLayout crashing after updating support library to 23.2.0

I am using TabLayout from the design library with the ViewPager, linking them using the function setupWithViewPager. The app is crashing in scenarios where it recreates the tabs, after the tab contents have been changed. Crash trace :
java.lang.IllegalArgumentException: Tab belongs to a different TabLayout.
at android.support.design.widget.TabLayout.addTab(TabLayout.java:433)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:772)
at android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:763)
at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:715)
The crash is occuring after updating to support library 23.2.0, doesn't reproduce till v23.1.1.
Just found that this is an internal bug in Support library v23.2.0, registered at : https://code.google.com/p/android/issues/detail?id=201827
This was bug reported on google https://code.google.com/p/android/issues/detail?id=201827
But after release of Android Support Library, revision 23.2.1 (March 2016) This is fixed now.
just update Support Library to Android Support Library to 23.2.1
I have meet the same problem, and then I found the newer TabLayout use a pool to cache Tab.
in 23.1.1
public Tab newTab() {
return new Tab(this);
}
and in 23.2.0
public Tab newTab() {
Tab tab = sTabPool.acquire();
if (tab == null) {
tab = new Tab(this);
}
tab.mView = createTabView(tab);
return tab;
}
so if you use newTab() to create a Tab, and for some reason you didn't add it to the TableLayout. the next time you enter another activity with a TabLayout, this would happen.
I can still see this issue in support lib version: 25.3.1. So to avoid the crash, removedAllTabs() and created a new instance for the tab again and added to Tablayout.
gauge_tab.removeAllTabs()
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_04))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_06))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_08))

ADT blank activity created with fragment activity

I am really confused right now because whenever I create a new Android app with blank activity it always comes out with fragment_main.xml. I just wanted to create a blank activity without the fragment one.
In the first image the blank activity comes with the fragment layout:
The second image shows the created fragment_main
Now I am really confused... this only happened after updating ADT to the latest version.
I have referred to this thread: Adt doesn't create default hello world but command line does_
I just wanted to make an Android app with blank activity with no fragment view.
For those who would like instructions on how to remove Fragments from the project:
1) Copy all the contents of res/layout/fragment_main.xml. Open activity_main.xml, delete the FrameLayout, and paste in the copied contents.
2) Delete fragment_main.xml
3) In MainActivity.java, delete the whole PlaceHolderFragment class:
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,
container, false);
return rootView;
}
}
4) Delete the following lines from onCreate():
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
At this point you should be all set to run the project.
You can use the previous template by editing the template files inside the Android SDK folder. But, make sure you have a backup of the "BlankActivity" folder.
Go to /templates/tools/templates/activities/BlankActivity
Locate the template.xml file and locate the following piece of code
<parameter
id="fragmentLayoutName"
name="Fragment Layout Name"
type="string"
constraints="layout|unique|nonempty"
suggest="fragment_${classToResource(activityClass)}"
default="fragment_main"
help="The name of the layout to create for the activity's content fragment" />
and change the constraints to constraints="empty".
Locate recipie.xml.ftl and locate the following piece of code
<instantiate from="res/layout/fragment_simple.xml.ftl"
to="${escapeXmlAttribute(resOut)}/layout/${fragmentLayoutName}.xml" />
and comment the whole line.
Locate SampleActivity.java.ftl file inside root/src/app_package and delete these two lines
<#include "include_options_menu.java.ftl">
<#include "include_fragment.java.ftl">
Locate fragment_simple.xml.ftl and activity_fragment_container.xml.ftl file inside root/res/layout. Copy the contents of fragment_simple.xml.ftl file to activity_fragment_container.xml.ftl file.
Now when you try to create a new activity, you'll get this screen
You can leave the Fragment Layout Name field blank.
This works fine for me in the case of blank activity. I'm not sure if this is the right approach, with discarding fragments and all, but this works for me.
This is a new feature of ADT version 22.6.0:
http://developer.android.com/tools/sdk/eclipse-adt.html
Edit: With the latest ADT updates there is a new template called "Empty Activity" that has no fragments. it's a plain class that extends Activity (Even without the default menu).
Notice that there is also a "Blank Activity" which extends ActionBarActivity and has fragments
Thank you to the person that reported this, https://code.google.com/p/android/issues/detail?id=67513
This was my first glimpse into android-review.googlesource.com. Digging around I didn’t see a way to download the patch set that was put together and I’m unsure how to “cherry pick” tools_r22.6, If there's a thread that explains it a link would be appreciated.
Here’s how I went about it.
On your machine navigate to your SDK folder then \tools\templates\activities
Copy the BlankActivities folder and paste it into the same directory.
Rename BlankActivities_copy to EmptyActivities
Go to https://android-review.googlesource.com/#/c/88890/4
Under the Files section there is a list of links. Each link directs you to the xml that needs to be added to the new EmptyActivities folder you created.
Copy the xml from the right pane to the path\file at the top of the left pane(the link was also the path). If the file already exists delete the contents and replace. If the file doesn’t exist, create it and copy the xml into the file.
Repeat for each of the links.
Close and reopen Eclipse. If you have any of the files open in an editor Eclipse will crash.
Thank you to Josiah Gaskin at google with the unbelievable 2 day turnaround!
No training literature references the fragments in the BlankActivity. I’m sure this fix saved me countless hours trying to figure out how to navigate around fragments while trying to learn a new IDE.
Thanks again.
If you download the latest ADT 22.6.3 (with build tools 19.0.3) you will have the option to create a new Empty Activity that doesn't use fragments
I have also faced the same issue.
I just deleted the eclipse and again downloaded the ADT bundle from http://developer.android.com/sdk/index.html#download
To recover your previous projects, just change the work-space to your previous one.
This worked for me.
If you don't want the fragment part to be in your app then simply "unckeck the create activity option at project setup wizard, then
manually create the activity and layout" for your project.
A solution to this, is a copy of the older "Blank Activity" in %Wherever your ADT bundle is%\sdk\tools\templates\activities\.
If someone has an older version of it, you can rename it, place it in here %Wherever your ADT bundle is%\sdk\tools\templates\activities\ and then when starting your project, select this folder instead of "Blank Activity", so if you have a copy please share.
We found a solution to this:
When you create a project select "empty" activity. Then it create a MainActivity class that extends Activity and an activity_main.xml layout.
In other words, it give you the behavior that was there before.
This worked in ADT 22.6.3.
While creating a new Application,
just copy the Layout Name to the Fragment Layout Name (e.g. activity_main)
Voila!!!
You get an activity without the fragment part.
Remove this portion of the code from activity:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
Clean the project.
A blank activity really should be more simple than this.
It appears that a fix is currently pending:
https://code.google.com/p/android/issues/detail?id=67513&q=blank%20activity&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
You can skip the create activity option when you create your project and add in an activity manually.
If you find it really really annoying you can try rolling back ADT and the SDK:
Download the 22.3 sdk tools from: dl-ssl.google.com/android/repository/##Your platform's version of the sdk here##
For example osx is tools_r22.3-macosx.zip
Unzipping that file will give you a folder called "tools" Use this to replace the folder of the same name found in /path-to-your-android- installation/android-sdk-macosx/
Next you need to uninstall your eclipse adt plugin. Go to help -> install new software Click the link to see what's installed already Select all of the android packages and click uninstall. Restart Eclipse
Download the 22.3 ADT plugin from: http://dl.google.com/android/ADT-22.3.0.zip Unzip this file
Back in eclipse click help -> install new software Click add and select the local file you just unzipped Check what you want to install and click install Restart eclipse again
I done it by replacing the BlankActivity(22.6.2) folder to the older sdk BlankActivity(22.6.1) present in the tools/templates directory in android-sdk.
Not sure if this works completely,
Steps
1) Right click on your project goto properties->Android, remove reference library and press apply.
now you would see errors in your project go to res->values->styles, replace AppBasetheme's parent by parent="android:Theme.Light"
do this for all values folder except in values_v14 there put parent="android:Theme.Holo.Light.DarkActionBar"
2) Now goto MainActivity replace "extends ActionBarActivity" by "extends Activity". Eclipse will prompt you to import for activity do it.
now delete this part of code
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
from activity class
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Now remove all unused imports.
3) Goto res->Layouts->fragment.xml, copy code of fragment into activity_main.xml.
Delete fragment.xml.
Now goto main.xml
Replace this
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
by
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="never"/>
4) Now Click on Project and clean your project
For small projects, create a project without an activity and add/edit activities, layouts, and manifest manually.
Android heard our voice :).
Just upgrade or download ADT version to 23.0.2 or higher.
Now you can choose blank activity like this,

Android Animation on API 8

problem in this code ?
its work on android 4 successfully ! but don't work on 2.2 !
i use nineoldandroids library for android API 8
if(Build.VERSION.SDK_INT > 13) {
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
ViewPropertyAnimator localViewPropertyAnimator =
v.animate().rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
localViewPropertyAnimator.setStartDelay(0).start();
} else {
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).translationX(0.0F).translationY(height)
.rotationX(45.0F).scaleX(0.7F).scaleY(0.55F);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(0).start();
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(animDuration).start();
}
sorry for bad english !
tnx to all
-------------------------------EDIT-----------------------------
code executed right on android 2.2 but not like android 4 !
ViewPropertyAnimator is for api level >11.However; you can use nineoldandroids library project which is a proxy for this animations (and quite good).
Or you can simply use Animation class
UPDATE:
i missed the part you said u already use nineoldandroids. did you check your imports maybe you imported the native
ViewPropertyAnimation
it may cause problem too. That libray uses the native api if api level is >11 you dont need to import native one.
You need to import ViewHelper class of nineoldandroid like
import com.nineoldandroids.view.ViewHelper;
and then use following code
ViewHelper.setTranslationX( Your View, 0.0F);
ViewHelper.setTranslationY( Your View, height);
ViewHelper.setRotationX(Your View,45.0F);
ViewHelper.setScaleX(Your View,0.7F);
ViewHelper.setScaleY(Your View,0.55F);
instead of
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
Since Nineoldandroids allow api 1> use animation methods.
However, I run in API8 and Force Close is occured!
HERE is the solution, it's because of Nineoldandroids
[http://answer.techwikihow.com/962376/nineoldandroids-animation-working-api10.html][1]
Use Library of NineOldndroids Folder as Dependency instead of .jar,
Modification some code in
ObjectAnimator.Class
by following the answer in the link above!

Android Add PinIt Button using Pinterest Jar NoClassDefFoundError

I am trying to add the Pinit button to my Android App. I have downloaded the SDK and copied the pinit-sdk-1.0.Jar into my Project/libs folder. I'm even able to make the necessary PinItButton import to my code. But for whatever reason, my XML isn't finding the Button widget, and so I tried to add it just using Java with this code:
LinearLayout ll = (LinearLayout)findViewById(R.id.pinterest_layout);
PinItButton pinit = new PinItButton(this);
ll.addView(pinit);
I know this is the correct way to add something using code because I tested it by adding a generic Button first. I run my project then, and it crashes on the line where I set up the PinItButton. This is the error that my logcat spat back at me:
07-22 01:19:52.160: E/AndroidRuntime(32367): java.lang.NoClassDefFoundError: com.pinterest.pinit.PinItButton
I believe that I'm understand what it is telling me, but what else should I be attempting then to create the PinIt button? Any help would be great. Thank you!
It was #Kai who helped me figure out my problem. I needed to check my libraries. I did that by going to project --> Properties --> Java Build Path --> Order and Export tab, then checking the library and clicking OK. My problem was then solved!

SlidingMenu and ViewPager with API13 (Honeycomb)

I´m experiencing a weird problem with SlidingMenu Library and ViewPager when they are running on devices with Android 3.2 (Honeycomb).
The problem appears when we "toggle" the SlidingMenu to show the Menu that is hidden on the left of the app. When we do this, both ContentView and BehingContentView stops responding to touch events.
Thinking that this was a problem related to my application, I downloaded the last version of ABS and SlidingMenu library and configured a new project using the built-in example that comes with the SlidingMenu and, for my surprise, the same behavior occurred with the ViewPager example.
These are the steps that I did:
Configure an Emulator using API Level 13 and 7" WSVGA (Tablet);
Download ABS and SlidingMenu from GIT;
Setup a new Project, using the compatibility library android-support-v41 (Also tested with android-support-v4);
Solved the problem 'getSupportActionBar() is undefined' as described here: https://github.com/jfeinstein10/SlidingMenu/issues/145;
Run the 'Example Application' and choose 'ViewPager' example;
Swipe pages to the right and to the left, without opening the menu;
Open the menu. See that the lists don´t scroll as expected;
Close the menu. See that the viewpager doesn´t responds to touch events anymore;
Notice that this behavior was reported only on Android 3.2 devices. We have the same application running on 2.x and on 4.x devices, without this problem.
Also, noticed that the Example Application that was downloaded from Google Play doesn´t have this problem.
Does anybody have any advice? Thanks a lot!
Edit 1
Tested on a real device, and confirmed the Behavior. Does anybody have an advice?
I had the same problem and fixed it by using the following work-around.
Replace these lines in SlidingMenu.java:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 11) return;
with:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 14) return;

Categories

Resources