I am new and studying online . This is my first animation study at Android. The problem is that when I choose the answer my Card View animation is suppose to show immediately. But it didn't show as soon as I click . But I found out that after I click the button, I need to press Home button and resume it from the background apps to start the animation.The animation starts only after I do this process. Toast is showing without a problem. After I choose the answer I always need to do like that to show my animation. The app is about True or false.
Also the coding is exactly the same as my online tutorial. maybe my phone problem ?? I am using Samsung S 7 edge.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView QuestionTextView,QcountTextView;
private Button Tbutton,Fbutton;
private ImageButton prev,next;
private int count = 0;
private CardView cardView;
private Animation shake;
public Questions Qarray[] = new Questions[]{
new Questions(R.string.Q1, true),
new Questions(R.string.Q2, false),
new Questions(R.string.Q3, true),
new Questions(R.string.Q4, true),
new Questions(R.string.Q5, false),
new Questions(R.string.Q6, true),
new Questions(R.string.Q7, false)
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tbutton = findViewById(R.id.tb);
Fbutton = findViewById(R.id.fb);
QuestionTextView = findViewById(R.id.questionshow);
QcountTextView = findViewById(R.id.Qcount);
next = findViewById(R.id.nb);
prev = findViewById(R.id.pb);
cardView = findViewById(R.id.cardView);
shake = AnimationUtils.loadAnimation(MainActivity.this,R.anim.shakeanimation);
Tbutton.setOnClickListener(this);
Fbutton.setOnClickListener(this);
next.setOnClickListener(this);
prev.setOnClickListener(this);
QuestionTextView.setText(Qarray[count].question);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tb:
giveanswer(true);
pagechange();
break;
case R.id.fb:
giveanswer(false);
pagechange();
break;
case R.id.pb:
if(count != 0) {
count = (count - 1);
pagechange();
}
break;
case R.id.nb:
count = (count + 1) % Qarray.length;
pagechange();
break;
}
}
private void pagechange() {
QuestionTextView.setText(Qarray[count].getQuestion());
QcountTextView.setText((count+1) + " out of " + Qarray.length);
}
private void giveanswer(boolean b) {
boolean correctanswer = Qarray[count].ans;
if(b == correctanswer){
fadeView();
Toast.makeText(this,R.string.yes,Toast.LENGTH_SHORT).show();
}
else{
ShakeAnimation();
Toast.makeText(this,R.string.no,Toast.LENGTH_SHORT).show();
}
}
private void fadeView(){
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new
Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.GREEN);
}
#Override
public void onAnimationEnd(Animation animation){
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void ShakeAnimation(){
cardView.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.RED);
}
#Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
I expect the animation show as soon as I choose the answer.
you need to call alphaAnimation.start(); or shake.start(); just having animation listeners is not gonna start your animation when you want it to start.
try adding these lines exactly where you want the animations to run.
For Example:
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.start();
Also setAnimation should to be used with xml Animations if I'm not mistaking
I wanted to make a snackbar dialog on double press to exit...(java)
Requested with these
On 1st time back pressed show dialogue " press back again to exit " for 2 seconds
On pressing back again show "do you want to exit ? " with the
confirmation button for 2 seconds
As like below -
Create id for your layout in activity_main
CoordinatorLayout coordinatorLayout;
#Override
public void onBackPressed() {
coordinatorLayout= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
if (!doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = true;
Snackbar.make(coordinatorLayout, "Do you really want to exit?", Snackbar.LENGTH_LONG)
.setAction("YES", new View.OnClickListener() {
#Override
public void onClick(View view) {
//button action here
System.exit(0);
}
}).setActionTextColor(Color.YELLOW)
.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
Hope this helps
Here is the solution for you -
private final long DOUBLE_PRESS_BACK_TO_EXIT_TIME = 2000;
boolean doubleBackPressed = false;
#Override
public void onBackPressed() {
// todo: show the snackbar here.
this.doubleBackPressed = true;
utils.showToastLong(getString(R.string.press_again_to_exit));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackPressed = false;
// todo: hide the snackbar here.
}
}, DOUBLE_PRESS_BACK_TO_EXIT_TIME);
}
To customize the Snackbar you may follow this link
WhatsApp has such Toolbar:
When 'Search' menu item clicked, from the top SearchView comes down which takes whole space of toolbar:
When I tried to implement SearchView, it looks like this:
I found some libraries to implement this:
Android Material SearchView by Eugene Horan
and MaterialSearchView by krishnakapil. But they are not like in WhatsApp.
This question may seem weird, I could not find the way how to do this. So my question is how to implement WhatsApp like material design SearchView which comes from the top?
I have developed a well received library by the comunity.
Does exactly what are you looking for.
Give it a try and tell if if was usufull for you.
Here it is the Github repo for MaterialSearchView.
You can create this with android.support.v7 library
First of all create menu item in menu.xml like:
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
Extend AppCompatActivity and retrieve the SearchView in onCreateOptionsMenu like:
import android.support.v7.widget.SearchView;
...
public class YourActivity extends AppCompatActivity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
// Retrieve the SearchView and plug it into SearchManager
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
...
}
Thats it. Hope this helps you. Accept, if you find it useful.
Sorry, I misread the question.
This answer should solve your issue.
Answer taken from :- Creating a SearchView that looks like the material design guidelines
After a week of puzzling over this. I think I've figured it out.
I'm now using just an EditText inside of the Toolbar. This was suggested to me by oj88 on reddit.
I now have this:
First inside onCreate() of my activity I added the EditText with an image view on the right hand side to the Toolbar like this:
// Setup search container view
searchContainer = new LinearLayout(this);
Toolbar.LayoutParams containerParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
containerParams.gravity = Gravity.CENTER_VERTICAL;
searchContainer.setLayoutParams(containerParams);
// Setup search view
toolbarSearchView = new EditText(this);
// Set width / height / gravity
int[] textSizeAttr = new int[]{android.R.attr.actionBarSize};
int indexOfAttrTextSize = 0;
TypedArray a = obtainStyledAttributes(new TypedValue().data, textSizeAttr);
int actionBarHeight = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
a.recycle();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, actionBarHeight);
params.gravity = Gravity.CENTER_VERTICAL;
params.weight = 1;
toolbarSearchView.setLayoutParams(params);
// Setup display
toolbarSearchView.setBackgroundColor(Color.TRANSPARENT);
toolbarSearchView.setPadding(2, 0, 0, 0);
toolbarSearchView.setTextColor(Color.WHITE);
toolbarSearchView.setGravity(Gravity.CENTER_VERTICAL);
toolbarSearchView.setSingleLine(true);
toolbarSearchView.setImeActionLabel("Search", EditorInfo.IME_ACTION_UNSPECIFIED);
toolbarSearchView.setHint("Search");
toolbarSearchView.setHintTextColor(Color.parseColor("#b3ffffff"));
try {
// Set cursor colour to white
// https://stackoverflow.com/a/26544231/1692770
// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(toolbarSearchView, R.drawable.edittext_whitecursor);
} catch (Exception ignored) {
}
// Search text changed listener
toolbarSearchView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Fragment mainFragment = getFragmentManager().findFragmentById(R.id.container);
if (mainFragment != null && mainFragment instanceof MainListFragment) {
((MainListFragment) mainFragment).search(s.toString());
}
}
#Override
public void afterTextChanged(Editable s) {
// https://stackoverflow.com/a/6438918/1692770
if (s.toString().length() <= 0) {
toolbarSearchView.setHintTextColor(Color.parseColor("#b3ffffff"));
}
}
});
((LinearLayout) searchContainer).addView(toolbarSearchView);
// Setup the clear button
searchClearButton = new ImageView(this);
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics());
LinearLayout.LayoutParams clearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
clearParams.gravity = Gravity.CENTER;
searchClearButton.setLayoutParams(clearParams);
searchClearButton.setImageResource(R.drawable.ic_close_white_24dp); // TODO: Get this image from here: https://github.com/google/material-design-icons
searchClearButton.setPadding(px, 0, px, 0);
searchClearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toolbarSearchView.setText("");
}
});
((LinearLayout) searchContainer).addView(searchClearButton);
// Add search view to toolbar and hide it
searchContainer.setVisibility(View.GONE);
toolbar.addView(searchContainer);
This worked, but then I came across an issue where onOptionsItemSelected() wasn't being called when I tapped on the home button. So I wasn't able to cancel the search by pressing the home button. I tried a few different ways of registering the click listener on the home button but they didn't work. Eventually I found out that the ActionBarDrawerToggle I had was interfering with things, so I removed it. This listener then started working:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// toolbarHomeButtonAnimating is a boolean that is initialized as false. It's used to stop the user pressing the home button while it is animating and breaking things.
if (!toolbarHomeButtonAnimating) {
// Here you'll want to check if you have a search query set, if you don't then hide the search box.
// My main fragment handles this stuff, so I call its methods.
FragmentManager fragmentManager = getFragmentManager();
final Fragment fragment = fragmentManager.findFragmentById(R.id.container);
if (fragment != null && fragment instanceof MainListFragment) {
if (((MainListFragment) fragment).hasSearchQuery() || searchContainer.getVisibility() == View.VISIBLE) {
displaySearchView(false);
return;
}
}
}
if (mDrawerLayout.isDrawerOpen(findViewById(R.id.navigation_drawer)))
mDrawerLayout.closeDrawer(findViewById(R.id.navigation_drawer));
else
mDrawerLayout.openDrawer(findViewById(R.id.navigation_drawer));
}
});
So I can now cancel the search with the home button, but I can't press the back button to cancel it yet. So I added this to onBackPressed():
FragmentManager fragmentManager = getFragmentManager();
final Fragment mainFragment = fragmentManager.findFragmentById(R.id.container);
if (mainFragment != null && mainFragment instanceof MainListFragment) {
if (((MainListFragment) mainFragment).hasSearchQuery() || searchContainer.getVisibility() == View.VISIBLE) {
displaySearchView(false);
return;
}
}
I created this method to toggle visibility of the EditText and menu item:
public void displaySearchView(boolean visible) {
if (visible) {
// Stops user from being able to open drawer while searching
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Hide search button, display EditText
menu.findItem(R.id.action_search).setVisible(false);
searchContainer.setVisibility(View.VISIBLE);
// Animate the home icon to the back arrow
toggleActionBarIcon(ActionDrawableState.ARROW, mDrawerToggle, true);
// Shift focus to the search EditText
toolbarSearchView.requestFocus();
// Pop up the soft keyboard
new Handler().postDelayed(new Runnable() {
public void run() {
toolbarSearchView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
toolbarSearchView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));
}
}, 200);
} else {
// Allows user to open drawer again
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Hide the EditText and put the search button back on the Toolbar.
// This sometimes fails when it isn't postDelayed(), don't know why.
toolbarSearchView.postDelayed(new Runnable() {
#Override
public void run() {
toolbarSearchView.setText("");
searchContainer.setVisibility(View.GONE);
menu.findItem(R.id.action_search).setVisible(true);
}
}, 200);
// Turn the home button back into a drawer icon
toggleActionBarIcon(ActionDrawableState.BURGER, mDrawerToggle, true);
// Hide the keyboard because the search box has been hidden
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(toolbarSearchView.getWindowToken(), 0);
}
}
I needed a way to toggle the home button on the toolbar between the drawer icon and the back button. I eventually found the method below in this SO answer. Though I modified it slightly to made more sense to me:
private enum ActionDrawableState {
BURGER, ARROW
}
/**
* Modified version of this, https://stackoverflow.com/a/26836272/1692770<br>
* I flipped the start offset around for the animations because it seemed like it was the wrong way around to me.<br>
* I also added a listener to the animation so I can find out when the home button has finished rotating.
*/
private void toggleActionBarIcon(final ActionDrawableState state, final ActionBarDrawerToggle toggle, boolean animate) {
if (animate) {
float start = state == ActionDrawableState.BURGER ? 1.0f : 0f;
float end = Math.abs(start - 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ValueAnimator offsetAnimator = ValueAnimator.ofFloat(start, end);
offsetAnimator.setDuration(300);
offsetAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
offsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float offset = (Float) animation.getAnimatedValue();
toggle.onDrawerSlide(null, offset);
}
});
offsetAnimator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
toolbarHomeButtonAnimating = false;
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
toolbarHomeButtonAnimating = true;
offsetAnimator.start();
}
} else {
if (state == ActionDrawableState.BURGER) {
toggle.onDrawerClosed(null);
} else {
toggle.onDrawerOpened(null);
}
}
}
This works, I've managed to work out a few bugs that I found along the way. I don't think it's 100% but it works well enough for me.
EDIT: If you want to add the search view in XML instead of Java do this:
toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
contentInsetLeft="72dp"
contentInsetStart="72dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp"
app:popupTheme="#style/ActionBarPopupThemeOverlay"
app:theme="#style/ActionBarThemeOverlay">
<LinearLayout
android:id="#+id/search_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="#+id/search_view"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="center_vertical"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="2dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textColorHint="#b3ffffff" />
<ImageView
android:id="#+id/search_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:src="#drawable/ic_close_white_24dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
onCreate() of your Activity:
searchContainer = findViewById(R.id.search_container);
toolbarSearchView = (EditText) findViewById(R.id.search_view);
searchClearButton = (ImageView) findViewById(R.id.search_clear);
// Setup search container view
try {
// Set cursor colour to white
// https://stackoverflow.com/a/26544231/1692770
// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(toolbarSearchView, R.drawable.edittext_whitecursor);
} catch (Exception ignored) {
}
// Search text changed listener
toolbarSearchView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Fragment mainFragment = getFragmentManager().findFragmentById(R.id.container);
if (mainFragment != null && mainFragment instanceof MainListFragment) {
((MainListFragment) mainFragment).search(s.toString());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
// Clear search text when clear button is tapped
searchClearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toolbarSearchView.setText("");
}
});
// Hide the search view
searchContainer.setVisibility(View.GONE);
I've just post my open source which imitates exactly what whatsapp toolbar does (Include circular animation).
Code
Full example
I am using this library this library.I used the code described in its description on github. looks like this I have event for the menu buttons, not its main Button. i want to get its main button's click event for make rest layout blur when its clicked/expended. even want to hide or set its default state when user click or touch somewhere else on screen.
FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
actionC.setIcon(R.drawable.notes);
actionC.setTitle("Add note");
actionC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
}
});
FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu)findViewById(R.id.multiple_actions);
floatingActionsMenu.addButton(actionC);
// ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).addButton(actionC);
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setIcon(R.drawable.event);
actionA.setTitle("Make life Event");
actionA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
and the answer is .
floatingActionsMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
Toast.makeText(Launcher.this, "Fdf", Toast.LENGTH_LONG).show();
}
#Override
public void onMenuCollapsed() {
}
});
this is my first Question, i hope i do it right.
I need to change the color of some buttons in a specific interval.
I decided to do it with the Colorfilter, because the setBackground method makes the Button look ugly.
The Problem is the following:
If i set the ColorFilter within a Runnable, it is not working.
But:
Setting the ColorFilter in the onCreate or click method is working.
And:
Setting the BackgroundColor with setBackgroundColor within the Runnable is working.
I forgot to mention, that it is all working fine if i run it on an emulator with android 4.1 but not with 2.3.3.
Any ideas? Here is the code
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
int counter = 0;
Button b = null;
final Handler handler = new Handler();
final Runnable doit = new Runnable() {
public void run() {
if ( counter % 2 == 0) {
b = (Button) findViewById(R.id.button1);
b.setBackgroundColor(0xffff0000); // working
//b.getBackground().setColorFilter(0xFFD2691E, PorterDuff.Mode.MULTIPLY); // doesnt work
}
else {
//b.getBackground().clearColorFilter();
b.setBackgroundColor(0xff00ff00);
}
counter++;
}
};
public void click(View view) {
// configure timer 1
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(doit);
}
};
timer.schedule(task, 1111, 1111);
}
}
I had the same problem with imageviews.
I found a solution, you have to call the invalidate()-methode:
ImageView img =(ImageView) findViewById(R.id.myimageview);
img.getDrawable().clearColorFilter();
img.invalidate();