define an event for custom android toast - android-toast

I've made a custom android toast and tried to define on click listener event for the textview, but it doesn't work....any help??
LayoutInflater inflater = LayoutInflater.from(cntxt);
View layout = inflater.inflate(R.layout.toast_layout,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(msg);
Toast toast = new Toast(cntxt);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method s'tub
//didn't go here???
toast.cancel();
}
});
toast.show();

Unfortunately, a Toast and any child child views of a Toast cannot receive touch input. You will have to create a custom View that mimics a Toast and attach that View to a layout.

Related

Recylerview in fragment doesnt populate with items

Sorry for this noob questons, but I am starting to learn Firebase UI and 2 days in a row I am stuck with populating a RecyclerView in a fragment.
I tried to move my code around in onViewCreated , onCreateView methods but I think that is not the problem, in log it just throws one error:
2020-02-18 15:08:26.730 4467-4467/com.social.voteup E/RecyclerView: No adapter attached; skipping layout
Double checked everything is linking with the xml file.
Here is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
On View Created method
mRecyclerView = view.findViewById(R.id.posts_recyclerview);
mSwipeRefreshLayout = view.findViewById(R.id.refreshLayout);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
setupAdapter();
// Refresh Action on Swipe Refresh Layout
mSwipeRefreshLayout.setOnRefreshListener(() -> mAdapter.refresh());
I use the adapter inside the fragment as shown here:
private void setupAdapter() {
// Init Paging Configuration
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(2)
.setPageSize(10)
.build();
// Init Adapter Configuration
FirestorePagingOptions<Post> options = new FirestorePagingOptions.Builder<Post>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, Post.class)
.build();
// Instantiate Paging Adapter
mAdapter = new FirestorePagingAdapter<Post, PostViewHolder>(options) {
#NonNull
#Override
public PostViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.item_post, parent, false);
return new PostViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull PostViewHolder viewHolder, int i, #NonNull Post post) {
// Bind to ViewHolder
viewHolder.bind(post);
}
#Override
protected void onError(#NonNull Exception e) {
super.onError(e);
Log.e("MainActivity", Objects.requireNonNull(e.getMessage()));
}
protected void onLoadingStateChanged(#NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
mSwipeRefreshLayout.setRefreshing(true);
break;
case LOADED:
mSwipeRefreshLayout.setRefreshing(false);
break;
case ERROR:
mSwipeRefreshLayout.setRefreshing(false);
break;
case FINISHED:
mSwipeRefreshLayout.setRefreshing(false);
break;
}
}
};
// Finally Set the Adapter to mRecyclerView
mRecyclerView.setAdapter(mAdapter);
}
Here is the full source file on pastebin:https://pastebin.com/YkzKRuUD
Add this line of code in the On View Created method.
You have created an adapter but you haven't attached the adapter to the recyclerView
mRecyclerView = view.findViewById(R.id.posts_recyclerview);
mSwipeRefreshLayout = view.findViewById(R.id.refreshLayout);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setAdapter(new setupAdapter());
// Refresh Action on Swipe Refresh Layout
mSwipeRefreshLayout.setOnRefreshListener(() -> mAdapter.refresh());

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.

Add a bottom menu for RecyclerView item

I have a recyclerview that will have an option button to the right, and I want to achieve something like the animation below:
I'm using this as my reference but I have some disconnects.
First I want to know if I can even do this because every thing I find on adding a menu to a recyclerview is adding a basic menu pop up, which is not what I want.
I also have some disconnect on accessing the menu from the viewholder, I can access it from my fragment but I'd think I'd need to call it from the viewholder so I have what row clicked to fire up the menu.
If I'm overthinking the implementation and there's a simpler approach, I'm open to hearing it.
Im not sure if I am understanding what you want, but if it is to have a button that exist in the row items of the recyclerview to pop up a drawer from the bottom of the screen whenever the button is clicked, then you should just use a normal drawer setup (Navigation Drawer, Create a navigation drawer) for the activity and have an OnClickListener added to the row buttons (which can be done in onCreateViewHolder() or the holder's constructor) that triggers the drawer to open by calling openDrawer(). Note to do the latter, you have to pass the navigation drawer (DrawerLayout) into the adapter via the adaptors constructor (which means your adapter needs something to store the DrawerLayout in).
So
public class CustomAdaptor extends RecyclerView.Adapter<CustomViewHolder>{
private Context mContext;
private int mLayoutResourceId;
private ArrayList<Item> items;
private DrawerLayout drawer;
public CustomAdaptor (Context context, int resource, ArrayList<Item> itemArray, DrawerLayout drawer) {
this.mContext = context;
this.mLayoutResourceId = resource;
this.items = itemArray;
this.drawer = drawer;
}
public CustomViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(mLayoutResourceId, parent, false);
final CustomViewHolder holder = new CustomViewHolder(view);
holder.mbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Check if drawer is null and if not then call
drawer.drawerOpen();
}
});
return holder;
}
}
where mButton is the button in the holder/row. Note I havent tested this so there might be somethings im forgetting.

RecyclerView.Adapter get ItemView through position

In my Adapter, I call LayoutManager.ChildAt(position) to get the itemview, but the view I get is not the matched itemview, and when i call notifyItemChanged(position), app crashes:
E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.paicaifu.riches, PID: 8502
Java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{adb21588 position=0 id=-1, oldPos=-1, pLpos:-1}
at Android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:654)
at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
at android.support.v7.widget.RecyclerView.addAnimatingView(RecyclerView.java:1107)
at android.support.v7.widget.RecyclerView.animateChange(RecyclerView.java:3270)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3088)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2917)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
…
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
Thanks,this is my code in the adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,
final int position) {
if (holder instanceof CardHolder) {
final CardHolder cardHolder = (CardHolder) holder;
cardHolder.rootview.setOnClickListener(this);
cardHolder.rootview.setTag(position);
cardHolder.rootview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//get last itemview in adapter,but preView isnot match the
//position,it is the visibable views on screen
View preView = mLinearLayoutManager.getChildAt(preIndex);
//change the datasource
mData.getResult().getCardList().get(preIndex).setOpen(false);
//update item
notifyItemChanged(preIndex);//when the item is out of screen,
//this line will cause crashes
preIndex = position;
}
});
}
I just want find the last item and change it's state(open or close),when the item is out of screen,notifyItemChanged(int position) will appear problem.
This is probably a bit too late, but the crash "Called attach on a child which is not detached" typically happens when you do NOT create a new view in onCreateViewHolder and instead return a cached view that's already inflated and attached.

android carousel with 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.