showing textview on viewpager with delay of time - android

I am trying to display text view with 20 seconds delay in a each view page. The view page also display 1 minute delay.means,in each view page i want show three text views, on next page it will show another three text.
please help me out.
private int[] ImageIds={R.drawable.image_asana,R.drawable.image_a,R.drawable.image_c,R.drawable.image_d,
R.drawable.image_e,R.drawable.end_image};
public static ScreenSlideFragment create(int PageNumber){
ScreenSlideFragment fragment=new ScreenSlideFragment();
Bundle args=new Bundle();
args.putInt(ARG_PAGE, PageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlideFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mPageNumber=getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView=(ViewGroup)inflater.inflate(R.layout.fragment_screenslide_page, container, false);
((ImageView)rootView.findViewById(R.id.image3)).setImageResource(ImageIds[mPageNumber]);
if (mPageNumber==ImageIds.length) {
handler.removeCallbacks(null);
}
final Runnable textUpdtae = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
((TextView) rootView.findViewById(R.id.imagetextview)).setText(ImageTextIds[count++]);
}
};
newTimer = new Timer();
newTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
handler.post(textUpdtae);
}
}, 0, 5000);
return rootView;
}

You can try setting layoutanimation to viewpager object. This animation will be applied to every view that view pager contains.
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 50.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(1000);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
<ViewPager>.setLayoutAnimation(controller);

Related

Show animations on every viewpager change

I want to show animations on every content of layout inflater. Below is the code that I am using but the problem is animations shows only on first page of view pager.
What I have understood so far is that animations on all pages content gets loaded on position 0 but I may be wrong.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = null;
if (position == 0) {
Home.rel_footer.setVisibility(View.VISIBLE);
v = inflater.inflate(R.layout.reward_points_circles, container,
false);
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.circle_1));
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.circle_2));
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.circle_3));
} else if (position == 1) {
Home.rel_footer.setVisibility(View.GONE);
v = inflater.inflate(R.layout.qrcode_scanner_promotion, container,
false);
YoYo.with(Techniques.Landing).duration(5000)
.playOn(v.findViewById(R.id.imgView_iphone));
YoYo.with(Techniques.Landing).duration(5000)
.playOn(v.findViewById(R.id.imgView_ipad));
} else if (position == 2) {
Home.rel_footer.setVisibility(View.GONE);
v = inflater.inflate(R.layout.deals_promotion, container, false);
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.imgView_iphone_left));
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.imageView_iphone_front));
YoYo.with(Techniques.ZoomIn).duration(5000)
.playOn(v.findViewById(R.id.imgView_iphone_right));
}
return v;
}
In my opinion you should not apply animations in onCreateView if you want to see your animation on each page change. You should instead use OnPageChangeListener interface and apply your animation there once user has changed a pager (switching to another page).
A simple code snippet to get you started.
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageSelected (int position){
// Apply animations here w.r.t the position
}
... Other overridden methods ...
});
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 1:
//load ur animations here
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});

Cannot resolve method 'findViewById(int)' in Fragment

I am trying to implement a button into a fragment in order to use the soundPool in order to play a sound with a button. At the moment the playSound1 is coming up as never used and I have tried to implement the onClick Method but it is now coming up saying it cannot resolve the method. How do I link the soundPool to a button in a fragment? this is the .java file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
Change your method to :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
}
});
return veiw;
}
You did a lot of mistakes here. So im not sure that my approach help you enough ) Write your program result into comment and we`ll try more.
There are multiple mistakes in your code.
You return a value way to early, the code beyond your return statement isn't executed
You're inflating the View, but all your inner elements eg your button is in that view so you have to find that view by id in that view.
I corrected it:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one_layout, container, false);
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
Button buttonA = (Button) root.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
return root;
}

Fragments and Thread in Android

I have a fragment[MainMenu] which has just menus and background image.I want to load the MainMenu fragment image and after some time i want to load the menus one by one after certain interval. And I tired to do this:
public class MainMenu extends Fragment{
public MainMenu(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
return rootView;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
ShowMenu(view);
}
private static void ShowMenu(final View container){
String[] menu = container.getResources().getStringArray(R.array.array_menu_string);
final LinearLayout llMenu = (LinearLayout)container.findViewById(R.id.llButtons);
for(int i = 0; i<menu.length; i++){
final String menuName = menu[i];
try {
Thread.sleep(2000);
TextView tvMenu = new TextView(container.getContext());
tvMenu.setText(menuName);
tvMenu.setPadding(5, 5, 5, 5);
llMenu.addView(tvMenu);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
System.out.println(menuName);
}
}
}
}
But the problem is the menus are already loaded then only the fragment is displayed? Please help to understand me why is this happening... and what should be done?

animate listView inside fragment

I have an app that shows a listView inside a fragment ok
now I need to animate the list view out
but the animation
animate()
is not working
here the code,
public class PhoneMainView extends SherlockFragment{
ListView listView ;
TranslateAnimation mAnimation;
//testeo
private Button btnNewEmpresa;
RelativeLayout lLayoutFrgValidate;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.d("mensa", "ONCREATE");
lLayoutFrgValidate=(RelativeLayout) inflater.inflate(R.layout.activity_main, container, false);
btnNewEmpresa=(Button) lLayoutFrgValidate.findViewById(R.id.button_anima);
btnNewEmpresa.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("mensa", "sikas");
animate();
}
});
return lLayoutFrgValidate;
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d("mensa", "onActivityCreated");
listView = (ListView) getView().findViewById(R.id.listViewCats);
String[] values = new String[] { "C0","C1","C2","C3", "C4", "C5", "C6","C7"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
public void animate() {
mAnimation = new TranslateAnimation(0, 0, 0, 599);
mAnimation.setDuration(10000);
mAnimation.setFillAfter(true);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
listView.setAnimation(mAnimation);
}
}
You need to start the animation.
listView.startAnimation(mAnimation); // Use this

How can I embed a loading progress indicator in a ListActivity and Fragment

Is there a way I can embed a loading progress indicator in a ListActivity or Fragment the same way that I can call setListShown() in a ListFragment? I can use a loading dialog but I would prefer to keep things consistent as I am already using setListShown() in other ListFragments.
Thanks.
I accomplished this by writing a wrapper for the fragment class similar to ListFragment.
public class AsyncFragment extends Fragment {
static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0001;
static final int INTERNAL_CONTENT_CONTAINER_ID = 0x00ff0002;
View mProgressContainer;
View mContentContainer;
boolean mContentShown = true;
public AsyncFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
FrameLayout root = new FrameLayout(context);
// ------------------------------------------------------------------
LinearLayout pframe = new LinearLayout(context);
pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
pframe.setOrientation(LinearLayout.VERTICAL);
pframe.setVisibility(View.GONE);
pframe.setGravity(Gravity.CENTER);
ProgressBar progress = new ProgressBar(context, null,
android.R.attr.progressBarStyleLarge);
pframe.addView(progress, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
root.addView(pframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
FrameLayout lframe = new FrameLayout(context);
lframe.setId(INTERNAL_CONTENT_CONTAINER_ID);
root.addView(lframe, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// ------------------------------------------------------------------
root.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return root;
}
public void setView(View v) {
FrameLayout lframe = (FrameLayout) getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);
lframe.addView(v, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mProgressContainer = getView().findViewById(INTERNAL_PROGRESS_CONTAINER_ID);
mContentContainer = getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);
setContentShown(false, false);
}
#Override
public void onDestroyView() {
mContentShown = false;
mProgressContainer = mContentContainer = null;
super.onDestroyView();
}
public void setContentShown(boolean shown) {
setContentShown(shown, true);
}
public void setContentShownNoAnimation(boolean shown) {
setContentShown(shown, false);
}
private void setContentShown(boolean shown, boolean animate) {
mProgressContainer = getView().findViewById(INTERNAL_PROGRESS_CONTAINER_ID);
mContentContainer = getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);
if (mContentShown == shown) {
return;
}
mContentShown = shown;
if (shown) {
if (animate) {
mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
getActivity(), android.R.anim.fade_out));
mContentContainer.startAnimation(AnimationUtils.loadAnimation(
getActivity(), android.R.anim.fade_in));
} else {
mProgressContainer.clearAnimation();
mContentContainer.clearAnimation();
}
mProgressContainer.setVisibility(View.GONE);
mContentContainer.setVisibility(View.VISIBLE);
} else {
if (animate) {
mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
getActivity(), android.R.anim.fade_in));
mContentContainer.startAnimation(AnimationUtils.loadAnimation(
getActivity(), android.R.anim.fade_out));
} else {
mProgressContainer.clearAnimation();
mContentContainer.clearAnimation();
}
mProgressContainer.setVisibility(View.VISIBLE);
mContentContainer.setVisibility(View.GONE);
}
}
I use this for fragments that I'm populating asynchronously. To use it, do not override onCreateView(). Instead, inflate your view in onStart() and call setView() with it. The fragment will start with a loading spinner. Call setContentShow(true) after you are done populating your view to remove the loading animation and show your content.

Categories

Resources