android carousel with animation - android-animation

I want to have a animation(ex:fadeout) when I touch the selected image.
I use the carousel example here:Android 3D Carousel
my codes is
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Animation fadeout = AnimationUtils.loadAnimation(this, R.anim.fadeout);
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
//Toast.makeText(MainActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
if(position==0){
String toastMessage = String.format("RFID");
Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show();
Animation anim = null;
anim = new RotateAnimation(0.0f,+360.0f);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(30000);
findViewById(R.id.carousel).startAnimation(anim);
Intent i = new Intent();
i.setClass(MainActivity.this, web_rfid.class);
startActivity(i);
}
}
});
}
}
I want the selected image to be rotated, but the result is whole screen rotated.
The problem is
findViewById(R.id.carousel).startAnimation(anim);
But I don't know how to find out the selected image in my code.
Plz hlep me.
Thanks.

Related

How to compare which drawable is in an imageview with a user spinner selection

Currently I am displaying a random image using math.random, this part works but I need to check if the users selection from the dropdown list matches this. The only part I can get to work is the incorrect selection displaying the correct name of the car displayed.
My comparison function never becomes true as Im not sure how to compare the tag of the imageview with a random int
See below my activity
public class IdentifyTheBrandActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,
AdapterView.OnItemSelectedListener { String[] Brands = { "Audi", "Bentley", "BMW" };
Button SubmitButton;
ImageView imageView;
int[] images;
int chosenCar;
int locationOfCorrectAnswer;
String[] answers = new String[3];
ArrayList<String> carBrands = new ArrayList<>();
public void brandChosen(View view) {
if (imageView.getTag().toString().equals(carBrands.get(locationOfCorrectAnswer))){
Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Wrong it was " + carBrands.get(locationOfCorrectAnswer), Toast.LENGTH_LONG).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_identify_the_brand);
imageView = findViewById(R.id.RandomImage);
imageView.setTag("bg");
carBrands.add("Audi");
carBrands.add("Bentley");
carBrands.add("BMW");
images = new int[]{R.drawable.audi_a3_2010_27_17_200_20_4_77_56_169_21_fwd_5_4_4dr_igm, R.drawable.bentley_bentayga_2017_229_21_600_60_12_78_68_202_12_awd_5_4_suv_cce,
R.drawable.bmw_6_series_2014_82_18_310_30_6_74_53_192_20_rwd_4_2_convertible_mua};
imageView.setTag(R.drawable.audi_a3_2010_27_17_200_20_4_77_56_169_21_fwd_5_4_4dr_igm);
imageView.setTag(R.drawable.bentley_bentayga_2017_229_21_600_60_12_78_68_202_12_awd_5_4_suv_cce);
imageView.setTag(R.drawable.bmw_6_series_2014_82_18_310_30_6_74_53_192_20_rwd_4_2_convertible_mua);
if (imageView.getTag() != null) {
int resourceID = (int) imageView.getTag();
}
Random random = new Random();
chosenCar = random.nextInt(3);
locationOfCorrectAnswer = chosenCar;
imageView.setBackgroundResource(images[chosenCar]);
int incorrectAnswerLocation;
//Get a random between 0 and images.length-1
//int imageId = (int) (Math.random() * images.length);
Spinner spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brands);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
Toast.makeText(getApplicationContext(), "Selected Brand: " + Brands[position]
,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
I tried comparing the imageview and the randomly assigned index to the array but it never becomes true
Essentially my issue is how to compare which drawable is actively in the imageview with the users selection

Cardview does not collapse when Visible.GONE child API<24

I have a RecyclerView that contains a list of MaterialCardView. Inside MaterialCardView, there is a RecyclerView and an Expand / Collapse button. When clicking on Collapse, the internal RecyclerView is assigned Visible.GONE, but on devices with API <24, the change in the height of the cardview is not correctly processed
[See API19|API22|API23|API29] (http://g.recordit.co/08OJWDaOja.gif)
`` `
TransitionManager.beginDelayedTransition(recyclerView, new AutoTransition());
if(state){
mProfileList.setVisibility(View.VISIBLE);
mExpand.setText(R.string.item_group_collapse);
managerProfiles.updateGroupState(group, true);
}else{
mProfileList.setVisibility(View.GONE);
mExpand.setText(R.string.item_group_expand);
managerProfiles.updateGroupState(group, false);
}
`` `
This is a bug in the foreground drawable of the MaterialCardView, i've created a bug report here: https://github.com/material-components/material-components-android/issues/537
I've created the following quick fix:
public class CardViewForegroundFix extends LayerDrawable {
public CardViewForegroundFix(Drawable foreground) {
super(new Drawable[]{foreground});
}
#Override
public int getMinimumHeight() {
return -1;
}
#Override
public int getMinimumWidth() {
return -1;
}
public static void fix(MaterialCardView cardView) {
Drawable foreGround = cardView.getForeground();
if (!(foreGround instanceof CardViewForegroundFix)) {
final CardViewForegroundFix foregroundFix = new CardViewForegroundFix(foreGround);
cardView.setForeground(foregroundFix);
// foreGround#callback is nullified in cardView#setForeground,
// reset it to the foreground fix drawable
foreGround.setCallback(foregroundFix);
}
}
}
Which you can apply by CardViewForegroundFix.fix(materialCardViewInstance)

How to hide/show a view in the Activity by clicking a button in the Android Cardview?

I am working on a commercial app as an internship. In one of its activity, I have tab view with two fragments. In each fragment, I'm using the card view to hold the views.
The card view has one image view, two text views, and a button and in the bottom of the activity below the tab view, there is a button which has its visibility mode as "GONE".
Now What I want is, Whenever I click on the button in the card view, the button at the bottom of the activity should hide/show for respective clicks.
Cardcaptionadapter.java
public CaptionedImagesAdapterMenu.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_captioned_image_menu, parent, false);
return new ViewHolder(cv);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position){
CardView cardView = holder.cardView;
ImageView imageView = (ImageView)cardView.findViewById(R.id.info_image);
Drawable drawable = ContextCompat.getDrawable(cardView.getContext(), imageIds[position]);
imageView.setImageDrawable(drawable);
imageView.setContentDescription(captions[position]);
TextView textView = (TextView)cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
TextView textView1 = cardView.findViewById(R.id.info_menu);
textView1.setText(desc[position]);
TextView textView2 = cardView.findViewById(R.id.info_price);
textView2.setText("₹ " + price[position]);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}
});
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(holder.button.getText().equals("ADD")){
holder.button.setText("ADDED");
holder.button.setBackgroundColor(Color.parseColor("#00ff00"));
SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("ADDED", 1);
editor.apply();
}
else {
holder.button.setText("ADD");
holder.button.setBackgroundColor(Color.parseColor("#ffffff"));
SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.apply();
}
}
});
}
MenuActivity.java
SharedPreferences preferences = getSharedPreferences("ADD",0);
int addvalue = preferences.getInt("ADDED",0);
if(addvalue==1){
orderbutton.setVisibility(View.VISIBLE);
}
else{
orderbutton.setVisibility(View.GONE);
}
orderbutton.setOnClickListener(view -> {
orderbutton.setVisibility(View.GONE);
paybutton.setVisibility(View.VISIBLE);
});
you should add MenuActivity.java code inside public void onBindViewHolder(ViewHolder holder, final int position){ } method also.

Automatically scrolling TextView inside a RecyclerView

My current problem is the following: I have a RecyclerView and each element of this RecyclerView has a TextView. I want that the TextView scrolls automatically. What if tried so far:
Customize TextView:
public class ScrollCustomTextView extends AppCompatTextView {
public ScrollCustomTextView(Context context) {
super(context);
}
public ScrollCustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context,attrs,defStyle);
}
public ScrollCustomTextView(Context context, AttributeSet attrs) {
super(context,attrs);
}
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if (focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
#Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
#Override
public boolean isFocused() {
return true;
}
}
and my .xml file looks like this:
<package.ScrollCustomTextView
android:id="#+id/main_idea_name"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/darkGreen"
android:textSize="27dp"
android:layout_marginTop="10dp"
android:maxLines="1"
android:textAppearance="#android:style/TextAppearance.Medium"
/>
And in RecyclerView in the constructor for the ViewHolder I set
textView.setSelected(true);
But it doesn't work. Does anyone have an idea to solve this?
Thanks.
So here is the answer:
I put my TextView inside a HorizontalScrollView and added the following code to my adapter for the RecyclerView:
private void animateTextView(TextView mTextView) {
int textWidth = getTextViewWidth(mTextView);
int displayWidth = getDisplayWidth(mContext);
/* Start animation only when text is longer than dislay width. */
if(displayWidth<=textWidth) {
Animation mAnimation = new TranslateAnimation(
0, -textWidth,
0, 0);
mAnimation.setDuration(10000); // Set custom duration.
mAnimation.setStartOffset(1000); // Set custom offset.
mAnimation.setRepeatMode(Animation.RESTART); // This will animate text back ater it reaches end.
mAnimation.setRepeatCount(Animation.INFINITE); // Infinite animation.
mTextView.startAnimation(mAnimation);
}
}
private int getDisplayWidth(Context context) {
int displayWidth;
WindowManager windowManager = (WindowManager)context.getSystemService(
Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point screenSize = new Point();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(screenSize);
displayWidth = screenSize.x;
} else {
displayWidth = display.getWidth();
}
return displayWidth;
}
private int getTextViewWidth(TextView textView) {
textView.measure(0, 0); // Need to set measure to (0, 0).
return textView.getMeasuredWidth();
}
And start the animation with:
animateTextView(txtView);
Note: there is no more need for the customized TextView, just use a normal TextView.

MediaPlayer inside an adapter playing the wrong track

I'm fairly new to Android development, and I added a MediaPlayer in a list of items so that the pronunciation is played for each word listed.
However, the wrong track is playing, I can't figure out how to link the right track to each item in the list view.
The ID changes when I'm scrolling for example as I can see in logs, so I think there's something I misunderstood.
What I fail to understand is why it works for the ImageView for example, but not for the audio file.
public class WordsAdapter extends ArrayAdapter<Words>{
private int colorview;
private View listItemView;
private Words currentWords;
public WordsAdapter(Context context, ArrayList<Words> words, int color){
super(context, 0, words);
colorview = color;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_view_black, parent, false);
}
currentWords = getItem(position);
ImageView image = (ImageView) listItemView.findViewById(R.id.imageicon);
if(currentWords.hasImage()) {
image.setImageResource((currentWords.getImageID()));
image.setVisibility(View.VISIBLE);
}else image.setVisibility(View.GONE);
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.text_default);
defaultTextView.setText(currentWords.getDefaultWord());
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.text_miwok);
miwokTextView.setText(currentWords.getMiwokWord());
View linearWords = listItemView.findViewById(R.id.linear_words);
linearWords.setBackgroundResource(colorview);
Button ButtonPlay = (Button) listItemView.findViewById(R.id.playButton);
ButtonPlay.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
MediaPlayer player = MediaPlayer.create(v.getContext(), currentWords.getAudioFile());
Log.v("Audio id : ", player.getTrackInfo().toString());
player.start();
}
});
return listItemView;
}
}
I found the solution.
The variable currentWords needs to be declared inside the method and set to final.
public View getView(int position, View convertView, ViewGroup parent) {
[...]
final Words currentWords = getItem(position);
[...]
}