Stop animation on button click - java

I am setting a animation on the imageview
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
i=(ImageView)findViewById(R.id.imageView1);
d=(Button)findViewById(R.id.button1);
a=new AnimationSet(false);
ScaleAnimation s=new ScaleAnimation(0, 2, 0, 2);
TranslateAnimation t=new TranslateAnimation(0, 100, 0, 0);
a.addAnimation(s);
a.addAnimation(t);
a.setRepeatCount(0);
a.setDuration(500);
a.setFillAfter(false);
a.setInterpolator(new AccelerateDecelerateInterpolator()); i.clearAnimation();
d.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i.clearAnimation();
}
});
i.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
i.startAnimation(a);
System.out.println("OnTouch called>>>>>>>>>>>");
return false;
}
});
}
}
now the problem is to stop the animation and restore the previous state on the button click till that time the image should stay as it is set to be in animation.
I

Hi try adding this Code
public void onClick(View v) {
// TODO Auto-generated method stub
i.clearAnimation();
a.cancel();
a.reset();
}

Related

Changing ImageViews with For Loop in AnimationListener - Android Studio

I want to make an animation where a set of ImageViews fade in/out one after another after the user presses a button. I made an array of the images (r1, r2, and r3) called list but it says the index "i" has to be declared final. Is there a way to traverse the array so each ImageView is animated?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton) findViewById(R.id.button);
r1 = (ImageView) findViewById(R.id.r1);
r2 = (ImageView) findViewById(R.id.r2);
r3 = (ImageView) findViewById(R.id.r3);
final ImageView[] list = {r1, r2, r3};
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.INVISIBLE);
animation1 = new AlphaAnimation(0.0f, 1.0f);
animation1.setDuration(1000);
animation1.setStartOffset(2000);
for (int i = 0; i < list.length; i++) {
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
list[i].startAnimation(animation2);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
animation2 = new AlphaAnimation(1.0f, 0.0f);
animation2.setDuration(1000);
animation2.setStartOffset(2000);
animation2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
list[i].startAnimation(animation1);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
list[i].startAnimation(animation1);
}
});
}}
}

how to dynamically change textview location?

I have a textview that shows the value of a seeker bar. I want to display that textview on top of the scrubber/slider as it is pulled left and right by the user.
What code do I need to do this?
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
final TextView seekBarValue = (TextView)findViewById(R.id.seekervalue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
Just use TextView.setX(float X) (and if you need vertical movement) TextView.setY(float Y) inside your onProgressChanged method.
Just adjust the values depending on what you want.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.textView);
SeekBar sb=(SeekBar)findViewById(R.id.seekBar);
final float startpos=tv.getX();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv.setX(startpos+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

Profile implementation in Eclipse

This is my Profile.java file in which i want to add the code for "item 1" that when it was clicked it must associate with my custom.java file.
package com.haider.first;
public class Profile
extends ListActivity
implements OnItemLongClickListener {
Activity activity;
//on create method
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
//Registering list for context menu
activity = this;
ListView lv = (ListView) findViewById(android.R.id.list);
registerForContextMenu(lv);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.profile)));
}
//this is code for create context menu
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
// this is code for selecting context item to perform desired actions
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()) {
case R.id.item1:
// Here i need code from which after click custom.java will be selected or choosen
case R.id.item2:
startActivity(new Intent(Profile.this, Custom.class));
return true;
default:
return super.onContextItemSelected(item);
}
}
//This is code for Enable Single CLick while opening Context Menu
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
activity.openContextMenu(l);
super.onListItemClick(l, v, position, id);
}
//This code disbale long click
#Override
public boolean onItemLongClick(AdapterView<?> enter code here parent, View view, int position, long id) {
// TODO Auto-generated method stub
activity.closeContextMenu();
return false;
}
}
code for custom.java file in which all settings are implemented
public class Custom
extends Activity {
private SeekBar mediaVlmSeekBar = null;
private AudioManager audioManager = null;
//Oncreate Method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.custom_sett);
initControls();
// Code for vibration but my app vibrates after short time intervals even it is closed
ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//this method is for changing status of mutiple options.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) {
Intent vibintent = new Intent(getApplicationContext(), Vibration.class);
startService(vibintent);
} else {
}
}
});
//Code for Controlling Volume, here i am facing issue that i cannot change volume of my app with device buttons
/Here is my code for Volume
private void initControls () {
// TODO Auto-generated method stub
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
mediaVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//Set the progress with current Media Volume
mediaVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
try {
mediaVlmSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
..........
Now my problem is when user will select item 1 named "General" it would select custom.java file without opening. In other words , when user select General the whole settings for volume, vibration, ringtone etc will be automatically selected.

How to Use button with Page indicator activity in android

I use the one Fragment and use three images for page slider. When i use this code(Button visible on 3rd screen) then the indicator not shifted or run only images are sliced. So, how i use the button and indicator together.
public class MainActivity extends FragmentActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
Button button = (Button) findViewById(R.id.nextbutton1);
CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator = indicator;
indicator.setViewPager(mPager);
float density = getResources().getDisplayMetrics().density;
indicator.setBackgroundColor(0xFFFFFFFF);
indicator.setRadius(7 * density);
indicator.setPageColor(0xFFFFFFFF);
indicator.setFillColor(0xFFE63F36);
indicator.setStrokeColor(0xFF000000);
indicator.setStrokeWidth((float) (0.5* density));
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Button button = (Button) findViewById(R.id.nextbutton1);
if(position==2) {
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
Intent i=new Intent(MainActivity.this,Login.class);
startActivity(i);
}
});
} else {
button.setVisibility(View.GONE);
}
/* FragmentChangeListener listener = (FragmentChangeListener) //cast fragment at this position to FragmentChangeListener
listener.onCentered();*/
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}

How to Keep Highlighted ListView Item and Its Status in the Contextual Action Bar during Device Orientation Change

I have a listview implemented with its choice mode set to CHOICE_MODE_MULTIPLE_MODAL. Here is some of the code:
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int check = 0;
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
adapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
check = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, arg1);
return true;
}
#Override
public boolean onActionItemClicked(final ActionMode arg0,
MenuItem arg1) {
// TODO Auto-generated method stub
if ((PinPad.mMediaPlayer != null)
&& (!PinPad.mMediaPlayer.isPlaying())) {
switch (arg1.getItemId()) {
case R.id.item_delete:
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(
R.layout.delete_confirmation, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainListActivity.this);
alertDialogBuilder.setView(dialogLayout);
final AlertDialog confirmationDialog = alertDialogBuilder
.create();
confirmationDialog.show();
Button buttonNo = (Button) dialogLayout
.findViewById(R.id.buttonNo);
Button buttonYes = (Button) dialogLayout
.findViewById(R.id.buttonYes);
buttonNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
weGotTheTime.clear();
weGotTheType.clear();
weGotTheDays.clear();
weGotTheRings.clear();
weGotTheIDs.clear();
confirmationDialog.dismiss();
adapter.clearSelection();
adapter.notifyDataSetChanged();
refreshListView();
check = 0;
arg0.finish();
}
});
buttonYes
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
databaseData.open();
databaseData.deleteEntries(
weGotTheTime, weGotTheType,
weGotTheDays);
databaseData.close();
new AlarmTask(MainListActivity.this,
weGotTheIDs, weGotTheDays,
weGotTheRings, weGotTheType,
weGotTheTime).cancelAl();
weGotTheTime.clear();
weGotTheType.clear();
weGotTheDays.clear();
weGotTheRings.clear();
weGotTheIDs.clear();
confirmationDialog.dismiss();
adapter.clearSelection();
adapter.notifyDataSetChanged();
refreshListView();
check = 0;
arg0.finish();
}
});
break;
}
return true;
} else if (PinPad.mMediaPlayer == null) {
switch (arg1.getItemId()) {
case R.id.item_delete:
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(
R.layout.delete_confirmation, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainListActivity.this);
alertDialogBuilder.setView(dialogLayout);
final AlertDialog confirmationDialog = alertDialogBuilder
.create();
confirmationDialog.show();
Button buttonNo = (Button) dialogLayout
.findViewById(R.id.buttonNo);
Button buttonYes = (Button) dialogLayout
.findViewById(R.id.buttonYes);
buttonNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
weGotTheTime.clear();
weGotTheType.clear();
weGotTheDays.clear();
weGotTheRings.clear();
weGotTheIDs.clear();
confirmationDialog.dismiss();
adapter.clearSelection();
adapter.notifyDataSetChanged();
refreshListView();
check = 0;
arg0.finish();
}
});
buttonYes
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
databaseData.open();
databaseData.deleteEntries(
weGotTheTime, weGotTheType,
weGotTheDays);
databaseData.close();
new AlarmTask(MainListActivity.this,
weGotTheIDs, weGotTheDays,
weGotTheRings, weGotTheType,
weGotTheTime).cancelAl();
weGotTheTime.clear();
weGotTheType.clear();
weGotTheDays.clear();
weGotTheRings.clear();
weGotTheIDs.clear();
confirmationDialog.dismiss();
adapter.clearSelection();
adapter.notifyDataSetChanged();
refreshListView();
check = 0;
arg0.finish();
}
});
break;
}
return true;
} else {
Toast.makeText(
getApplicationContext(),
"Cannot delete alarm(s). Make sure no alarm is currently ringing.",
Toast.LENGTH_SHORT).show();
adapter.clearSelection();
check = 0;
arg0.finish();
return true;
}
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// TODO Auto-generated method stub
td = results.get(position).getDays();
if (checked) {
check++;
adapter.setNewSelection(position, checked);
weGotTheTime.add(results.get(position).getTime());
weGotTheType.add(results.get(position).getType());
weGotTheDays.add(results.get(position).getDays());
weGotTheIDs.add(results.get(position).getID());
weGotTheRings.add(results.get(position).getTitle());
} else {
check--;
adapter.removeSelection(position);
weGotTheTime.remove(results.get(position).getTime());
weGotTheType.remove(results.get(position).getType());
weGotTheDays.remove(results.get(position).getDays());
weGotTheIDs.remove(results.get(position).getID());
weGotTheRings.remove(results.get(position).getTitle());
}
mode.setTitle(check + " selected");
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
listView.setItemChecked(arg2, !adapter.isPositionChecked(arg2));
vibrator.vibrate(50);
return false;
}
});
This works great, long clicking a listview item highlights it and it works fine. However, once I change the device orientation from portrait to landscape and vice versa, the highlighting is gone and the Contextual Action Bar is still showing however it doesn't show anything is selected. Any help? I've tried making selectors and drawables and manually doing it that way and still no luck. I've spent weeks on this one issue and I'm wondering if somebody out there knows how to solve it.
Thanks.
See if you can using this helps:
android:configChanges="orientation|keyboardHidden"
For more info on complete implementation refer this:
http://developer.android.com/guide/topics/resources/runtime-changes.html

Categories

Resources