make lottie animation setVisibility immediately - android

i have an adroid project and i want to show a lottie animation during the time that an algorithm run but it does not become visible at time.
in my xml file:
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="250dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
android:visibility="gone"
app:lottie_rawRes="#raw/loading" />
// i tried also with android:visibility="invisible"
in java:
LottieAnimationView lottieanimationview;
lottieanimationview = findViewById(R.id.animation_view);
in specific function where i want to start to see the animation, i write:
lottieanimationview.setVisibility(LottieAnimationView.VISIBLE);
then, inside this function i have a call to another function that run the algorithm (in a cpp file that in the project).
now, instead of get visible and then run the another function (i want it to be like a loading animation),
the lottie get visible just after the run of the second function done. how can i make the lottie get visible immediately when i call setVisibility?
thank you

Ok.. Here is the code which I tried :
I have some lottie views in this(Screenshot attached below).
But we will be only using toggle view and thumbsDown
When we switch Toggle on, we are setting thumbsdown view visibilty gone
and when we switch Toggle off, thumbsdown view is visible again.
activity_main.xml
<!-- Custom Action Bar -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_actionBar"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="gradient_bg.json"
app:lottie_loop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Demo Lottie"
android:textColor="#android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="#+id/lav_actionBar"
app:layout_constraintEnd_toEndOf="#+id/lav_actionBar"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/lav_actionBar" />
<!-- Thumbs Up Button -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_thumbUp"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="false"
app:lottie_fileName="thumb_up.json"
app:lottie_loop="false"
app:lottie_speed="1.25" />
<!-- Thumbs Down Button (We just rotate the previous one by 180 deg ;) )-->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_thumbDown"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="8dp"
android:rotation="180"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="false"
app:lottie_fileName="thumb_up.json"
app:lottie_loop="false"
app:lottie_speed="1.25" />
<!-- Toggle Switch -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_toggle"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lav_thumbUp"
app:layout_constraintVertical_bias="0.4"
app:lottie_autoPlay="false"
app:lottie_fileName="toggle_switch.json"
app:lottie_loop="false"
app:lottie_speed="1.75" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
LottieAnimationView thumb_up;
LottieAnimationView thumb_down;
LottieAnimationView toggle;
LottieAnimationView imprint;
int flag = 0;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thumb_up = findViewById(R.id.lav_thumbUp);
thumb_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
thumb_down.setProgress(0);
thumb_down.pauseAnimation();
thumb_up.playAnimation();
Toast.makeText(MainActivity.this, "Cheers!!", Toast.LENGTH_SHORT).show();
//---- Your code here------
}
});
thumb_down = findViewById(R.id.lav_thumbDown);
thumb_down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
thumb_up.setProgress(0);
thumb_up.pauseAnimation();
thumb_down.playAnimation();
Toast.makeText(MainActivity.this, "Boo!!", Toast.LENGTH_SHORT).show();
//---- Your code here------
}
});
toggle = findViewById(R.id.lav_toggle);
toggle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeState();
}
});
} //
private void changeState() {
if (flag == 0) {
toggle.setMinAndMaxProgress(0f, 0.43f);
toggle.playAnimation();
flag = 1;
//---- Your code here------
thumb_down.setVisibility(View.GONE);
} else {
toggle.setMinAndMaxProgress(0.5f, 1f);
toggle.playAnimation();
flag = 0;
//---- Your code here------
thumb_down.setVisibility(View.VISIBLE);
}
} }
Main thing is :
thumb_down.setVisibility(View.GONE);
thumb_down.setVisibility(View.VISIBLE);
Hope it helps.

Related

TextView Edit Text is not responding to Click Events

I have a layout with three TextViews, two TextViews are responding to Click Events but the last one is not responding. I am using Navigation to navigate to other fragments. The OnClick method is called inside an adapter which implements view.OnClickListener.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_marginStart="#dimen/defaultMargin"
android:layout_height="wrap_content">
<TextView
android:text="How CADO works"
style="#style/CircularBook"
android:drawableEnd="#drawable/arrow_right"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/howCadoWorks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/howCadoWorks"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view3"></View>
<TextView
android:text="FAQ"
style="#style/CircularBook"
android:layout_width="0dp"
android:drawableEnd="#drawable/arrow_right"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/faqs"
app:layout_constraintTop_toBottomOf="#+id/view3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/faqs"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view4"></View>
<TextView
android:text="Contact Us"
android:drawableEnd="#drawable/arrow_right"
style="#style/CircularBook"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/contactus"
app:layout_constraintTop_toBottomOf="#+id/view4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
OnClickMethod
public void onClick(#Nullable View view) {
NavController var3 = Navigation.findNavController(view);
if(view.getId()==R.id.contactus){
**var3.navigate(R.id.contactus_fragment);**
return;
}
else if (view.getId()==R.id.faqs){
var3.navigate(R.id.faqFragmnt);
return;
}
else if (view.getId()==R.id.howCadoWorks){
Toast.makeText(view.getContext(),"Feature is in Progress", Toast.LENGTH_SHORT).show();
// var3.navigate(R.id.contactus_fragment);
return;
}
else
{
}
}
I have tried multiple ways, rechecked id's as well but nothing is working.
as #Nilesh Rathod said, you need to make sure that all your text view have set an on click listener like:
textView.setOnClickListener(this);
or you can also:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you put the code you want to use for the specific textView click event
}
});

How can I change an image using a button in android studio?

I am doing a project that requires me to change an image in the same form content page with a button onclick or another button method if you have one. I normally program in C# with visual studio and I am not used to the java so I would greatly appreciate it, Thank you.
Here is a sample image changer code
Xml -
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/changeBtn"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="151dp"
android:layout_marginLeft="151dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="4dp"
android:onClick="change"
android:text="Change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="456dp"
android:layout_height="632dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cats01" />
</android.support.constraint.ConstraintLayout>
Main Activity code
public class MainActivity extends AppCompatActivity {
private int i =0;
public void change(View view)
{
ImageView imageView2 = (ImageView)findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.cats01); // this function
changes the image
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Changing a Item in a listView on a click

I want to make a ListView, when you long click on an item, the item will be added to the buttons: delete and update (only the clicked item):
So I have made a ListView in my MainLayout:
<ListView
android:id="#+id/neighborhood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/allAddresses"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/AddresSearch" />
and a Custom ListView Item lay out, which contains 3 TextViews:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".passItemView">
<TextView
android:id="#+id/num"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="28dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/house"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/house"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cvc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pass"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/num"
app:layout_constraintTop_toTopOf="parent" />
and this layout is conneted to a class "passItemView", and there, what I did is that on longClick on the layout it will add 2 button (or even just will make a toast)
But it does nothing.
public class passItemView extends AppCompatActivity {
Button delete = new Button(this);
Button update = new Button(this);
ConstraintLayout btnLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass_item_view);
btnLayout = (ConstraintLayout) findViewById(R.id.button_layout);
btnLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(passItemView.this,"Toast it",Toast.LENGTH_LONG).show();
btnLayout.addView((delete));
btnLayout.addView((update));
return false;
}
});
Thank you for your Help.
use an boolean value (ex : showButtons) and initialize set it false for all items.
when you clicked someitem make items showButtons variable true and notifydatasetchange (reload lsitview) again.

Animate LayoutChanges does not work properly

I want to animate text in my textswitcher. I went through all the stack questions and the documentation, but they didn't solve my problem.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv__inc_pre_sing__screen_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:gravity="center"
android:text="PRACTICE"
android:textAllCaps="true"
android:textColor="#color/colorAccent"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/rl__inc_pre_sing__tm_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#efff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv__inc_pre_sing__screen_title">
<ImageView
android:id="#+id/tv__inc_pre_sing__quotation_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:src="#drawable/ic_archive_black_24dp"
app:layout_constraintTop_toTopOf="parent" />
<TextSwitcher
android:id="#+id/tv__inc_pre_sing__teacher_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/tv__inc_pre_sing__quotation_mark"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_archive_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv__inc_pre_sing__teacher_message" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="16dp"
android:background="#drawable/ic_launcher_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/rl__inc_pre_sing__tm_container" />
</android.support.constraint.ConstraintLayout>
Here is my code
public class Main3Activity extends AppCompatActivity {
private TextSwitcher mSwitcher;
Button btnNext;
int currentIndex=-1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Resources resources = getApplicationContext().getResources();
final String[] textString = resources.getStringArray(R.array.teacher_messages);
btnNext = findViewById(R.id.button);
mSwitcher = findViewById(R.id.tv__inc_pre_sing__teacher_message);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(Main3Activity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button TextSwitcher will switch between texts
// The current Text will go OUT and next text will come in with specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
//
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==textString.length)
currentIndex=0;
mSwitcher.setText(textString[currentIndex]);
((ViewGroup) findViewById(R.id.rl__inc_pre_sing__tm_container)).getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
}
});
}
}
The animation on ConstraintLayout whose id is rl__inc_pre_sing__tm_container does not work properly. Suppose my TextSwitcher has text whose length is greater than 3 and then TextSwitcher shows text whose length is 1 then the ConstraintLayout does not animate, if the next text is of length 1 then the constraintlayout animates.
I am not able to figure out this weird behaviour.
Here is the issue: When the TextSwitcher transitions from X lines of text to something less than X lines then the TextSwitcher's height does not change.
The TextSwitcher inherits some behavior from ViewAnimator which considers the height of all views when determining the height of the widget. The solution is to add the following XML to the TextSwitcher definition in the layout file that will inhibit this behavior:
android:measureAllChildren="false"
Thanks to this Stack Overflow answer that states all this better than I have.

ImageSwitcher now showing image

I am trying to run a simple app which displays an image with Animation on button click.All I see is a white screen.I am using exact code from the tutorial.Can any one tell me whats the problem?
This is the tutorial https://www.tutorialspoint.com/android/android_imageswitcher.htm
This is MainActivity.java
import...
public class MainActivity extends AppCompatActivity {
ImageSwitcher imageSwitcher;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher=(ImageSwitcher)findViewById(R.id.img);
btn=(Button)findViewById(R.id.button);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView view = new ImageView(getApplicationContext());
view.setScaleType(ImageView.ScaleType.CENTER);
view.setLayoutParams(new
ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT));
return view;
}
});
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "s", Toast.LENGTH_SHORT).show();
imageSwitcher.setImageResource(R.drawable.m1);
}
});
}
}
This is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="vaibhav.com.game.MainActivity">
<ImageSwitcher
android:id="#+id/img"
android:layout_width="324dp"
android:layout_height="419dp"
android:layout_margin="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="57dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.439"
app:layout_constraintVertical_bias="0.946" />
</android.support.constraint.ConstraintLayout>

Categories

Resources