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>
Related
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.
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);
}
}
I was playing around with the Shared Element Transition and developed a testapp with 4 Fragments.
In the picture you can see, the first Fragment contains a "start now" message, when it's clicked I want to replace the container with the Fragment in the middle. As an eye candy I want an Animation by using Shared Element Transitions.
Problem
My Problem is, if I leave the first Fragment empty (without starting message) and I for example set the OnClickListener to the Icon itself, everything is working alright with a nice animation. But if the first Fragment has that message in it only the first Icon (the second Fragment, picture in mid) doesn't have an animation anymore. It's just replacing the first fragment. The curios thing is, if I change my OnClickListener and let it start the second page (right picture) the animation is working fine again. So only the first/left Icon does not provide an animation though all Methods and XML are mostly 1:1 the same.
As I couldn't develope a better solution, each "toolbar" is designed in the Fragment itself.
Main Activity
public class FirstStartupActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firststartup);
doFragmentTransaction(new MainFragment(), "TAG", false, null);
}
public void doFragmentTransaction(Fragment fragment, String tag, boolean addToBackStack, List<View> sharedElements){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.ActivityFirstStartup_fragmentContainer, fragment, tag);
if(addToBackStack){
transaction.addToBackStack(tag);
}
if( sharedElements != null && !sharedElements.isEmpty()){
for(int i = 0; i < sharedElements.size(); i++){
View view = sharedElements.get(i);
transaction.addSharedElement(view, view.getTransitionName());
}
}
transaction.commit();
}}
First Fragment with Message
public class MainFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_home, container, false);
View view = v.findViewById(R.id.relLayoutPageOne);
final List<View> listview = new ArrayList<>();
listview.add(view);
View view2 = v.findViewById(R.id.relLayoutPageTwo);
final List<View> listview2 = new ArrayList<>();
listview2.add(view2);
Button button = v.findViewById(R.id.buttonStart);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((FirstStartupActivity)getActivity()).doFragmentTransaction(new UsernameFragment(), "test", true, listview);
//((FirstStartupActivity)getActivity()).doFragmentTransaction(new CameraFragment(), "TEST2", true, listview2);
}
});
RelativeLayout rel2 = v.findViewById(R.id.relLayoutPageTwo);
rel2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((FirstStartupActivity)getActivity()).doFragmentTransaction(new CameraFragment(), "TEST2", true, listview2);
}
});
return v;
}}
Second Fragment No Animation
public class UsernameFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_pgone, container, false);
postponeEnterTransition();
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}}
Third Fragment Working
public class CameraFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_firststartup_pgtwo, container, false);
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
}}
Second Fragment (Picture in middle) No Animation when replacing)
<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">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/draw_login_edittext">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:id="#+id/relLayoutPageTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileCam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:transitionName="ProfileCam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:transitionName="ProfilePic"
android:layout_width="273dp"
android:layout_height="210dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/draw_login_edittext_rounded"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
First Fragment (Picture left)
<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">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#a8655c">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:transitionName="ProfilePic"
android:id="#+id/relLayoutPageOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileCam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:forceHasOverlappingRendering="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/draw_login_edittext_rounded"
android:padding="25dp"
android:transitionName="ProfilePic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Bevor du loslegen könnst benötigen wir noch kurz ein paar Informationen über dich! :)"
android:textAlignment="center"
android:textColor="#BFFFFFFF" />
<Button
android:id="#+id/buttonStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text"
android:layout_centerHorizontal="true"
android:layout_marginHorizontal="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/draw_rounded_edittext_dark"
android:text="Start now"
android:textColor="#BFFFFFFF" />
</RelativeLayout>
Third Fragment (right Picture) Animation when replacing
<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">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/draw_login_edittext">
</android.support.constraint.ConstraintLayout>
<RelativeLayout
android:id="#+id/relLayoutPageOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfilePic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/test" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutPageThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#drawable/draw_login_edittext_rounded"
android:transitionName="ProfileGender"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testthree" />
</RelativeLayout>
<RelativeLayout
android:layout_width="273dp"
android:layout_height="210dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#c8c8c8"
android:transitionName="ProfileCam"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
app:srcCompat="#mipmap/testtwo" />
</RelativeLayout>
Visual Description
First Icon doesn't work but the second one works like charm
For some Reason the first GIF is way too fast, its popping up normally the only Problem is the missing Animation
Well I tried to change some things and I made everything worse. Then I "repaired" it and now it works, unfortunetly I can't show you a solution for that. I try to work through the codes again
EDIT: #LieForBanana had the solution but I understood him wrong, indeed my first Fragment also had the Transition Name it (3x TransitionName ProfilePic). I am ashamed, it was just one silly mistake
I have a recyclerview adapter on which I create listener to handle click event.
I implement this listener inside activity or fragment. The recyclerview items contains lot of views inside cardview.
My problem is when I click img_download no callback occur (the log not display) but when I click on other space (not img_download) the appropriate callback occur.
How do I know which view is clicked ?
Adapter Code:
public class ItuneEpisodeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvTitle;
TextView tvDescription;
View divider;
ImageView img_download;
public ItuneEpisodeViewHolder(View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tv_podcast_title);
tvDescription = itemView.findViewById(R.id.tv_description);
divider = itemView.findViewById(R.id.divider);
img_download = itemView.findViewById(R.id.img_download);
itemView.setOnClickListener(this);
img_download.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view.getId() == img_download.getId()) {
mOnClickListener.onDownloadItemClick();
} else {
mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
}
}
}
public interface EpisodeClickListener {
void onItemClick(Episode podcast, View view);
void onDownloadItemClick();
}
Activity Implementation:
#Override
public void onItemClick(Episode episode, View view) {
PlayMediaActivity_.intent(this).extra("episode_extra", Parcels.wrap(episode)).extra("img", podcast.getCoverImage()).start();
}
#Override
public void onDownloadItemClick() {
Log.d("download", "yes");
}
Screenshot:
XML code:
<?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="wrap_content"
android:layout_margin="10dp"
android:elevation="4dp">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/md_blue_grey_100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/img_podcast"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="5dp"
android:src="#drawable/podcast_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider" />
<TextView
android:id="#+id/tv_podcast_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Les chemins de la philosophie"
android:textColor="#color/md_black_1000"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="#+id/img_podcast"
app:layout_constraintRight_toLeftOf="#+id/img_download"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/tv_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text="This is a description and should be replace with the best one let"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="#+id/tv_podcast_title"
app:layout_constraintRight_toLeftOf="#+id/img_download"
app:layout_constraintTop_toBottomOf="#+id/tv_podcast_title" />
<ImageView
android:id="#+id/img_calendar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_calendar"
app:layout_constraintLeft_toLeftOf="#+id/tv_description"
app:layout_constraintTop_toBottomOf="#+id/tv_description" />
<TextView
android:id="#+id/tv_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Fév. 14 2016"
app:layout_constraintBottom_toBottomOf="#+id/img_calendar"
app:layout_constraintLeft_toRightOf="#+id/img_calendar" />
<ImageView
android:id="#+id/img_time"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:src="#drawable/ic_time"
app:layout_constraintBottom_toBottomOf="#+id/tv_calendar"
app:layout_constraintLeft_toRightOf="#+id/tv_calendar" />
<TextView
android:id="#+id/tv_realisateur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="49:50"
app:layout_constraintBottom_toBottomOf="#+id/img_time"
app:layout_constraintLeft_toRightOf="#+id/img_time" />
<ImageView
android:id="#+id/img_download"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:tint="#color/primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_drawer_download" />
</android.support.constraint.ConstraintLayout>
Solved it, add this two code on ConstraintLayout (parent layout) and on your img_download (ImageView)
android:clickable="true"
android:focusable="true"
and instead of itemView, put an ID on your ConstraintLayout and set an onClickListener on it.
ConstraintLayout main;
TextView tvTitle;
TextView tvDescription;
View divider;
ImageView img_download;
public ItuneEpisodeViewHolder(View itemView) {
super(itemView);
main = itemView.findViewById(R.id.main); //add this
tvTitle = itemView.findViewById(R.id.tv_podcast_title);
tvDescription = itemView.findViewById(R.id.tv_description);
divider = itemView.findViewById(R.id.divider);
img_download = itemView.findViewById(R.id.img_download);
main.setOnClickListener(this); //set the onclick on the parent layout
img_download.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view.getId() == R.id.img_download) {
mOnClickListener.onDownloadItemClick();
} else if (view.getId() == R.id.main) {
mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
}
}
XML:
<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/main" //add this
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true" //add this
android:focusable="true" //add this
android:elevation="4dp">
...
...
<ImageView
android:id="#+id/img_download"
android:clickable="true" //add this
android:focusable="true" //add this
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:tint="#color/primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_drawer_download" />
</android.support.constraint.ConstraintLayout>
Well i have one button and one ImageView in my app.
What i am trying to do is when i pressing on the button then the image at the ImageView will change.
All i have are two pics file.
What i am trying to do is - if the first pic is linked to the ImageView than change it to pic2 by clicking on the button, and if pic2 is linked than a click on the button will change it back to the first pic file.
here's the onClick method i tried to use:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
Thanks for any kind of help
Rather than checking the image, I would suggest set the information tag of the ImageView each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
private ImageView ib1;
private int currentImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib1 = (ImageView) findViewById(R.id.imageView1);
currentImage = R.drawable.pic1;
}
public void onClick(View view){
currentImage = (currentImage == R.drawable.pic1) ? R.drawable.pic2 : R.drawable.pic1;
ib1.setImageResource(currentImage);
}
You can also create a Boolean variable and assign it true and then if the boolean variable is true you change the picture of image button and set the boolean variable to false:- here my code example in java:-
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton play_pause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_pause = findViewById(R.id.play_pause_button);
final boolean[] play_or_pause = new boolean[1];
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
play_pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (play_or_pause[0]) {
play_pause.setImageResource(R.drawable.ic_baseline_pause_24);
play_or_pause[0] = false;
}else {
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
}
}
});
}
}
and xml code:-
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="252dp"
android:layout_height="48dp"
android:gravity="center"
android:text="TBT Music Player"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="247dp"
android:layout_height="263dp"
android:src="#drawable/ic_baseline_library_music_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.124" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="327dp"
android:layout_height="41dp"
android:thumbTint="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:layout_constraintVertical_bias="0.163"
android:secondaryProgressTint="#color/white"/>
<ImageButton
android:id="#+id/play_pause_button"
android:layout_width="64dp"
android:layout_height="77dp"
android:backgroundTint="#color/black"
android:src="#drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar"
app:layout_constraintVertical_bias="0.213" />
</androidx.constraintlayout.widget.ConstraintLayout>