I can Pause ImageView.animation() but I can't Resume - android-animation

anyone can help me on how to resume ImageView.animation()? Based on my code bellow I can Pause animation but I can Resume.
pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
pause.setVisibility(View.INVISIBLE);
pause_release.setVisibility(View.VISIBLE);
pause_release.startAnimation(in);
//ant 1
walking_1.stop();
ant_play_1.animate().setListener(null);
ant_play_1.animate().cancel();
ant_play_1.setEnabled(false);
}
});
pause_release = (Button) findViewById(R.id.pause_release);
pause_release.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
pause.setVisibility(View.VISIBLE);
pause_release.startAnimation(out);
pause_release.setVisibility(View.GONE);
//ant 1
walking_1.start();
ant_play_1.animate().start();
ant_play_1.setEnabled(true);
}
});
How to modify my code to make it can Resume onClick?

Related

highlight items in recyclerview android

I am trying to select items in recylerview. When I click an item, a checkbox appears on that item and it is highlighted. But the problem is that when I scroll, the item which is highlighted goes to its original color but the checkbox image remains as it should. Why the highlighted color is gone but the image remains after scrolling, I want the items to keep their state after scrolling.
public static List<Model> item = new ArrayList<Model>()
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int
position) {
holder.bind(item.get(position));
//
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.check);
public void bind(Model model) {
if (model.isChecked()){
imageView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(Color.GRAY);
model.setChecked(true);
} else {
imageView.setVisibility(View.GONE);
itemView.setBackgroundColor(Color.TRANSPARENT);
model.setChecked(false);
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
notifyItemChanged(getAdapterPosition());
}
});
public class Model {
private boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
Don't manually change the design in the onclick listener, just update your model (as you are already doing: model.setchecked(...) depending on state)
Then call Adapter.notifyItemChanged(getAdapterPosition())
This will force the adapter to reload the item, calling onBindViewHolder, where the design should be updated properly
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
adapter.notifyItemChanged(getAdapterPosition())
}
In the above example, replace "adapter" with a reference to your recylerview adapter

Saving button's enable disable state and getting this last state on create

I'm newbie in android and I'm trying to develop some applications. But last two weeks I've been stuck in this code. In short, I have two buttons in a layout. These buttons are Add and delete button. If I clicked the add button, add will be disabled and delete will be enabled. Or if I clicked the delete, delete will be disabled add will be enabled. I want to save state of these two buttons (which one is clicked) and get the last clicked state on create. But the problem is it's not working. When I open the app both buttons are show disable state. Or not saving either. Never working.
public class UserVideoInfoActivity2 extends AppCompatActivity {
SharedPreferences.Editor editor;
SharedPreferences preferences;
SharedPreferences.Editor editor2;
SharedPreferences preferences2;
Button buttonadd;
Button buttondelete;
private static final String ADD = "ADD_KEY";
private final static String DELETE = "DELETE_KEY";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_video_info_layout_2);
buttonadd = (Button) findViewById(R.id.buttonaddlayout);
buttondelete = (Button) findViewById(R.id.buttondeletelayout);
preferences = getSharedPreferences(ADD, Context.MODE_PRIVATE);
preferences2 = getSharedPreferences(DELETE, Context.MODE_PRIVATE);
buttonadd.setEnabled(GetState());
buttondelete.setEnabled(GetState2());
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(false);
buttondelete.setEnabled(true);
SaveState(buttonadd.isEnabled());
}
});
buttondelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(true);
buttondelete.setEnabled(false);
SaveState2(buttondelete.isEnabled());
}
});
}
private void SaveState(boolean isChecked) {
editor = preferences.edit();
editor.putBoolean(ADD, isChecked);
editor.commit();
}
private void SaveState2(boolean isChecked2) {
editor2 = preferences2.edit();
editor2.putBoolean(DELETE, isChecked2);
editor2.commit();
}
public boolean GetState() {
return preferences.getBoolean(ADD, false );
}
public boolean GetState2() {
return preferences2.getBoolean(DELETE, false);
}
}
<Button
android:id="#+id/buttondeletelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete" />

OnEditorActionListener on an EditText is not working?

I am searching a string when i click on search icon its work properly and i get a list.. but when i click on keyboard search icon API not hit its shows Failure every time because it cannot get a parameter.i searched but i cannot get solution and also i want to know that what is actionid . please suggest me. My code is here:-.
public class Medicine_search_price extends AppCompatActivity {
public static RecyclerView Recycle_medicine;
Search_medicine model;
ImageView search_medicine1, Medicine_cart, backarrow;
public static ImageView noproduct;
List<Result> mArray_patient_deatil = new ArrayList<Result>();
public static EditText edit_search_medicine;
Adapter_medicine_view medicine;
public static TextView mTitle;
String user_id, count, search_medicine;
RelativeLayout relativeLayout;
LinearLayout mainlayout;
ProgressBar progressBar;
Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medicine_search_price);
DATAGET();
FINDVIEWBYID();
CLICKLISTINER();
}
private void DATAGET() {
user_id = M.getNumber(getApplicationContext());
count = M.getStatus(getApplicationContext());
}
private void FINDVIEWBYID() {
noproduct = (ImageView) findViewById(R.id.no_product_found);
progressBar = (ProgressBar) findViewById(R.id.proress_bar);
backarrow = (ImageView) findViewById(R.id.medicine_search_price_arrow);
edit_search_medicine = (EditText) findViewById(R.id.search_medicine_txt);
search_medicine1 = (ImageView) findViewById(R.id.search_medicine_b);
Recycle_medicine = (RecyclerView) findViewById(R.id.rcy_medicine_search);
relativeLayout = (RelativeLayout) findViewById(R.id.badge_layout1);
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
mTitle = (TextView) toolbarTop.findViewById(R.id.badge_notification_1);
mainlayout = (LinearLayout) findViewById(R.id.mainLayout);
Medicine_cart = (ImageView) toolbarTop.findViewById(R.id.medicine_search_cart);
}
private void CLICKLISTINER() {
mTitle.setText(count);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Medicine_search_price.this, Preview_screen.class);
startActivity(mIntent);
}
});
search_medicine1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search_medicine = edit_search_medicine.getText().toString();
if (search_medicine.length() <= 2) {
Toast.makeText(Medicine_search_price.this, "Please Enter Atleast Three Characters", Toast.LENGTH_SHORT).show();
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainlayout.getWindowToken(), 0);
getdata(search_medicine);
}
}
});
backarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Medicine_search_price.this, MainActivity.class);
startActivity(mIntent);
}
});
edit_search_medicine.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionid, KeyEvent keyEvent) {
if (actionid== EditorInfo.IME_ACTION_SEARCH) {
getdata(search_medicine);
}
return false;
}
});
}
private void getdata(String search) {
progressBar.setVisibility(View.VISIBLE);
RestAdapter restAdapter1 = new RestAdapter.Builder().setEndpoint(AppConst.MAIN).build();
final UsersAPI searchmedicine = restAdapter1.create(UsersAPI.class);
searchmedicine.pproducts(search, new Callback<FinalSearchMedicine>() {
#Override
public void success(FinalSearchMedicine searchmedicineModel, Response response) {
Integer status = searchmedicineModel.getSuccess();
if (status == 1) {
mArray_patient_deatil = searchmedicineModel.getResult();
Recycle_medicine.setVisibility(View.VISIBLE);
medicine = new Adapter_medicine_view(Medicine_search_price.this, mArray_patient_deatil);
LinearLayoutManager layoutManager1 = new GridLayoutManager(Medicine_search_price.this, 2);
Recycle_medicine.setLayoutManager(layoutManager1);
Recycle_medicine.setAdapter(medicine);
Toast.makeText(Medicine_search_price.this, "" + mArray_patient_deatil.size() + " " + "Products", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(Medicine_search_price.this, "Failureeeee", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
i get my answer so silly mistake. search_medicine is null every time because cannot get string from edit text on action button click..
if (i == EditorInfo.IME_ACTION_SEARCH) {
search_medicne=edit_search_medicine.getText().toString();
getdata(search_medicne);
}

what is the name of following refactoring operation?

At first I have code like this
private void example(){
btnBack.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
//some code
}
});
}
and then get code like this
private void example(){
btnBack.setOnClickListener(backListener);
}
View.OnClickListener backListener = new View.OnClickListener() {
#Override public void onClick(View view) {
//some code
}
};
That is simply "Extract -> Field" (Ctrl-Alt-F in Windows/Linux keymaps)

SurfaceView Tutorial problems

I found a tutorial and it looks like this:
package com.djrobotfreak.SVTest;
public class Tutorial2D extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(_scratch, 10, 10, null);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
and it does not work, no matter what I do. I am trying to convert my code to surfaceview but I cant find any surfaceview programs that even work (besides the android-provided ones). Does anyone know what the error even is saying?
Here is my logcat info: http://shrib.com/oJB5Bxqs
If you get a ClassNotFoundException, you should check the Manifest file.
Click on the Application tab and look on the botton right side under "Attributes for".
If there is a red X mark under your Class Name, then click on the "Name" link and locate the correct class to load.