I have written my own shake animation to shake the EditTexts. When I tap "SHAKE USER", the first EditText shakes. When I tap "SHAKE PASS", the two EditTexts shake together, which should not happen. Screenshot is attached.Screenshot
Here is the animation file
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="2%"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:duration="50" />
</set>
Here is the Java code snippet
Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shakeView(etUser);
}
});
findViewById(R.id.b2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shakeView(etPass);
}
});
private void shakeView(final View view) {
new Thread(new Runnable() {
#Override
public void run() {
view.post(new Runnable() {
#Override
public void run() {
view.startAnimation(shake);
}
});
}
}).start();
}
Thanks!
Seems that I have found the solution myself. I called view.clearAnimation() after the animation finishes, and everything is working fine
Related
I have the following progress bar in my Android application:
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="200sp"
android:layout_height="200sp"
android:layout_marginTop="60sp"
android:visibility="gone"
android:indeterminate="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
I also have an ImageView with an image in it. What I want to do is while I'm making a call to an API, make the image invisible and show the ProgressBar in its place, then when it ends show the image again and hide the ProgressBar. However, this does not work:
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProgressBar progressBar = findViewById(R.id.progressBar);
ImageView image = findViewById(R.id.image);
image.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
makeRequest();
}
});
thread.start();
try {
thread.join();
progressBar.setVisibility(View.INVISIBLE);
image.setVisibility(View.VISIBLE);
} catch (InterruptedException e) {
}
}
});
I've tried various combinations with View.INVISIBLE and View.GONE, nothing works. I've also searched through similar questions on here but none of them answers my question.
I thought maybe it's because it disappears and reappears so quickly I don't notice it, but even if I do Thread.sleep() the progress bar still doesn't show up.
What am I doing wrong?
Calling thread.join() within OnClickListener blocks the UI thread and hence block any UI updates.
To fix your code, you can remove thread.join() and put progressBar.setVisibility(View.INVISIBLE);image.setVisibility(View.VISIBLE); within run() after makeRequest() but warp with runOnUiThread()
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProgressBar progressBar = findViewById(R.id.progressBar);
ImageView image = findViewById(R.id.image);
image.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
makeRequest();
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.INVISIBLE);
image.setVisibility(View.VISIBLE);
}
});
}
});
thread.start();
}
});
I'm very new to java and Android studio, and I'm making one card game for practice as well as to do something practical while learning (as I'm learning by my own).
So to explain in short, there will be 2 rows of 4 cards at top and bottom (I used linear layout to fit those), and these cards will be clickable, which will move them to ImageView at central area when they are clicked. Up to this I have done everything fine.
But now I want to make things such that whenever card gets clicked in one row, that whole row will be disabled till card in another row is clicked, then again remaining cards in that row will be disabled and first row would be made clickable.
In short I want to make those rows as different players which would be clickable in turns. I had one idea (which might be very dumb as I'm new to whole thing) in which I have set onclicklisteners on both linearlayouts containing 4 cards each, and in Onclick method in them I have added Onclicklisteners on each of that card. I was thinking that by doing this, if I disable Onclicklistener on linearlayout after clicking, those on all cards in that layout will be disabled too, but I don't know how to disable the button.
If my approach is correct then pls tell me how do I disable listener on layouts on clicking. If not suggest other logic. Thanks
In below code usercards are imageviews,on clicking they disappear (invisible) from there place and move on to central place as described before
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
usercard1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(0), card1ondeck);
usercard1.setVisibility(View.INVISIBLE);
}
});
usercard2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(1), card1ondeck);
usercard2.setVisibility(View.INVISIBLE);
}
});
usercard3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(2), card1ondeck);
usercard3.setVisibility(View.INVISIBLE);
}
});
usercard4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(3), card1ondeck);
usercard4.setVisibility(View.INVISIBLE);
}
});
}
});
layout2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user2card1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(4), card2ondeck);
user2card1.setVisibility(View.INVISIBLE);
}
});
user2card2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(5), card2ondeck);
user2card2.setVisibility(View.INVISIBLE);
}
});
user2card3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(6), card2ondeck);
user2card3.setVisibility(View.INVISIBLE);
}
});
user2card4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(7), card2ondeck);
user2card4.setVisibility(View.INVISIBLE);
}
});
}
});
I wanted to change the android button color every time I click on a button. Once a user clicks a button, I want it so that the color changes. Then, when the button is pressed again, the color reverts back to what it was before. Here is my attempt:
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Boolean isClickedDummy = !isClicked;
if(isClickedDummy) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
});
}
Originally I wanted it so that isClicked = !isClicked so that I would know for certain that the isClicked variable has changed and I can change the color. However, the method I have above only changes the isClicked to false and I can't seem to change it back to true. Is there any way I can figure this out? Any help would be appreciated. Thanks!
try this:
isClicked = false;
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isClicked) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
isClicked = false;
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
isClicked = true;
}
}
});
}
You can xml drawable :
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
selector_xml_name.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector
You have to change the value of "isClickedDummy" and you have to use it as global.
Boolean isClickedDummy; // global after the declaration of your class
isClickedDummy = true; // in your onCreate()
private void setupFollowButton(Button button, final Boolean isClicked) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isClickedDummy) {
v.setBackgroundColor(Color.parseColor("#FF0000"));
isClickedDummy = false;
} else {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
isClickedDummy = true;
}
}
});
}
Do not forget to remove BackgroundResource of your button if you want to change the background color.
That is, use:
btn.setBackgroundResource(0);
After that, 'usual button layout' will disappear and I will show the changes of setBackground method.
I have an xml file with videoview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back"
android:orientation="vertical"
android:screenOrientation="landscape">
<VideoView
android:id="#+id/videoview_concept"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And My java part is:
public class playvideos extends Activity implements OnCompletionListener{
public Integer index_val=0;
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
index_val++;
VideoView videoview= (VideoView)findViewById(R.id.videoview_concept);
checkdbconn();
videoview.setVideoURI(Uri.parse(prepare.txtLectureFileName[index_val]));
videoview.setMediaController(new MediaController(this));
videoview.requestFocus();
videoview.start();
}
public void playvideo(View view){
setContentView(R.layout.playvid);
VideoView videoview= (VideoView)findViewById(R.id.videoview_concept);
Log.i("Video URL",videourl_array[index_val]);
checkdbconn();
videoview.setVideoURI(Uri.parse(videourl_array[index_val]));
videoview.setMediaController(new MediaController(this));
videoview.requestFocus();
checkdbconn();
videoview.start();
}
The above code is a button click event. It works really well. If one first video finished, it should automatically play the second video. Thus I want to play all the videos in that array continuously. How to achieve this? Please help me out:)
At last I found out how to play all videos which urls are stored in an array. When I play the video sometimes it plays two videos one by one and sometimes only one video was playing.
This is how I over come this problem. Thanks to #Kirit.
public void playvideos_all(){
videoview.setVideoURI(Uri.parse(prepare.txtLectureFileName[index_val]));
videoview.setMediaController(new MediaController(this));
videoview.requestFocus();
videoview.start();
videoview.setClickable(true);
index_val++;
if(index_val>=no_of_videos){
Toast.makeText(getApplicationContext(), "Videos are finished index: "+index_val+"no of videos:"+no_of_videos, Toast.LENGTH_LONG).show();
}
else{
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion (MediaPlayer mp) {
mp.stop();
videoview.stopPlayback();
videoview.suspend();
playvideos_all();
}
});
}
}
public class MainActivity extends Activity implements OnCompletionListener{
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// play next video
}
}
I'm currently working on a project where I use a webview packed in RelativeLayout and I placed a ImageButton in the left-bottom corner:
<RelativeLayout
android:id="#+id/relativeLayout_maincontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
/>
<ImageButton
android:id="#+id/imageButton_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="2dp"
android:src="#android:drawable/ic_menu_call" />
</RelativeLayout>
This works just fine. The next step I was thinking of is that the button can sometimes disturb my users, when they can't see the stuff behind the button... So I tried to hide the button after a few seconds and when the user is scrolling/touching/or whatever (just some user input) I'll display it and after a few seconds the button should disappear again. Like the zoom-in and zoom-out buttons in a webview. The best way would be to use the same functionality as these zoom buttons, but I couldn't find a way to do this. I created a function based on the knowledge of How to show a view for 3 seconds, and then hide it? :
private void startCallButtonHidingThread() {
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
callButton.setVisibility(ImageButton.GONE);
}
});
}
};
thread.start();
}
That works great. The next step was to display the button when user input happens, so I tried things like that:
relativeLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(callButton.getVisibility() == ImageButton.GONE) callButton.setVisibility(ImageButton.VISIBLE);
startCallButtonHidingThread();
return false;
}
});
or that:
relativeLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(callButton.getVisibility() == ImageButton.GONE) callButton.setVisibility(ImageButton.VISIBLE);
startCallButtonHidingThread();
}
});
and the same I tried on the webview. Most of the time it works just one time and then there is no reaction at all.
So if you have any idea or a better solution, please let me know.
I really appreciate your help!
I think it's android restriction. Try to use handler:
private Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:{
if(callButton.getVisibility() == ImageButton.GONE) callButton.setVisibility(ImageButton.VISIBLE);
startCallButtonHidingThread();
}
}
};
And your listener:
relativeLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
handler.sendEmptyMessage(1);
return false;
}
});