animate activity start in Android - android

I need to animate my Activity when it starts up. The Activity is started from a BaseAdapter class. I tried using overridePendingTransition() but I can't seem to use that in the on click event. How can I over come this?
holder.userpic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(" ", " value " + obj.get(position).get_post_id());
Intent appInfo = new Intent("android.intent.action.Profile");
appInfo.putExtra("pk", obj.get(position).get_foodie_id());
context.startActivity(appInfo);
overridePendingTransition(R.anim.full_side_up,0); // cant use this
}
});

You need to attach the Context as:
context.overridePendingTransition(R.anim.full_side_up,0);
Or you can use the transition in onResume method inside the new Activity:
#Override
public void onResume() {
super.onResume();
overridePendingTransition(R.anim.full_side_up,0);
}
Let me know if this works.

overridePendingTransition is a method of the Activity class. You would have to hold a reference to an activity.
An alternative would be specifying your listener in XML using onClick attribute as follows:
<ImageView
id="#+id/user_pic"
...
onClick="onClickUserPic" />
Then you create method onClickUserPic(View) inside your activity (which contains the ListView filled by your adapter class):
public void onClickUserPic(View view) {
int position = (Integer)view.getTag(); // here is stored position in list
Log.d(" ", " value " + obj.get(position).get_post_id());
Intent appInfo = new Intent("android.intent.action.Profile");
appInfo.putExtra("pk", obj.get(position).get_foodie_id());
context.startActivity(appInfo);
overridePendingTransition(R.anim.full_side_up,0);
}
Note that this method has to be inside this and any other activity which contains the clickable image view. If it weren't, you would get a NoSuchMethodException on click.
Finally add this line to your adapter:
holder.userpic.setTag(Integer.valueOf(position)); // save position for reference
Also make sure that you have access to obj from inside the activity.

Related

How to get specific position view in RecyclerView?

I have a recyclerView. One of recyclerView items has an onClick method. In this method, I setVisibility for one Item to Visible(default is gone), also get the position and add it to an arrayList.
onClick :
public void onClickImageView(ImageView imageView, int position) {
imageView.setVisibility(View.VISIBLE);
positionArrayList.add(position);
}
Before i go to another activity, I want to the ImageView visibility be reset to default (gone) so I use an interface to send positionArrayList to the recyclerView activity to get views by position and reset ImageViews to default.
Interface :
public interface RecyclerViewMainActivityImp {
void resetImageViewToDefault(ArrayList<Integer> positionArrayList);
}
Where I use interface method in adapter :
public void OnClickNextActivity() {
Intent intent = new Intent();
intent.setClass(context, ClassActivity.class);
context.startActivity(intent);
recyclerViewInterface.resetImageViewToDefault(positionArrayList);
}
Activity :
#Override
public void resetImageViewToDefault(ArrayList<Integer> positionArrayList) {
for (int position = 0; position < positionArrayList.size(); position++) {
View row = binding.recyclerView.getLayoutManager().findViewByPosition(positionArrayList.get(position));
ImageView imageView = row.findViewById(R.id.imageView);
imageView.setVisibility(View.GONE);
}
}
When i scroll the recyclerView, and if some items are not in view anymore their views going to null so some positions in positionArrayList no longer have their view.
My problem is some views are null after scrolling and I don't know how to get access to them.
I searched on google and found some solutions but I could not solve my problem
Note : I have used data binding
there's workaround you can try for this,
create a function in your activity
function void updateRecyclerView(position){
yourArrayListForVisibility.set(position,'hide');
mAdapter.notifyDataSetChanged();
}
#Override
public void onResume(){
super.onResume();
mAdapter.notifyDataSetChanged();
}
call this function in your adapter before intent and in your adapter check for this arraylist value to hide or show content, always set default value to 'yourArrayListForVisibility' for every position
Any Activity that restarts has its onResume() method executed first.
So, call notifyDataSetChanged() of RcyclerView from onResume()
#Override
public void onResume(){
super.onResume();
mAdapter.notifyDataSetChanged();
}

How to Get a callback for activity started from inside arrayadapter for listview in Android?

How to start an activity from inside ArrayAdapter and get a call back when the activity is finished(just like onActivityResult)?
Following code is on post execute of an asynctask that is started on the button click of a button displayed in each listview row.
((Activity) mContext).startActivityForResult(intent, AppConstants.DUMMY_CONSTANT);
If it's possible, try using this instead:
listView.setOnItemClickListener(new OnItemClickListener() { ... });
Edit. You can also create interface, implement it in your acitvity and pass it to your adapter
My Pscudoe code:-- Create call back method using interface
and make call back wherever you want
MyListener listener;
public interface MyListener {
// TODO: Update argument type and name
void onClick(View view, int position);
}
#Override
public void onClick(View view) {
listener.onCardClick(view, getPosition());
}
From Listview onitem:---

Send an event from Listview (which is in a Fragment) to an existing activity via the Fragment

I have a ListView which is in a Fragment. Each row display a set of 4 buttons.
Each button has an OnClickListener defined in the Adapter Class. Everything is working well and i can diplay the ID of the clicked button.
My problem is how the send this ID to the activity (who hold the Fragment where the ListView is defined) via this Fragment. I'm stuck since 1 week till now !!!
Please remember that this activity is already existing and she is the one who already launched the fragment.
Write one method in the Activity class,
public void setButtonClick(int buttonId){
// do your task
}
and from your adapter class
((YourActivity)fragment.getActivity()).setButtonClick(id);
But you need to pass fragment instance to the adapter using its constructor.
New Edits as per your code posted here,
class ArticleSetAdapter{
// Variables
private ArticleFragment context;
// Constructor
public ArticleSetAdapter(ArticleFragment context, ArrayList<ArrayList<Article>> articleSetArray) {
super(context.getActivity(), 0, articleSetArray);
this.context = context;
}
and in your adapter onClick,
// the OnClickListener handler method
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer artID = (Integer) ((Button) v).getTag();
((youractivity.class)context.getActivity())..setButtonClick(artID)
makeText(getContext(), "CLICK: " + artID, Toast.LENGTH_SHORT).show();
}
};
Hope this will help you.

Get the listItem ID in a onClick method of SetOnClickListener method of an adapter?

I have an activity which lists objects from an array objects through a custom adapter. The row of this adapter contains several EditText's and a layout which is clickable and does the deleting of that object selected. My intention is the object can be updated by clicking on the item (which shows another activity) and deleting by clicking on the layout. So that, I have to implement the updating and the deleting by differents setOnItemClickListener's.
I have done the updating just setting an setOnItemClickListener to the listView of objects and sending the whole object to a new activity through putExtra and getIntent.
The problem is with the deleting. I have implemented an OnClickListener directly on the adapter, like this:
holder.layoutEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here call to an Async Task to delete the object but, what about t the id object???
}
That code goes fine when I click on the layout of the row but I don't know the way to obtain the id of the object selected in the listView. Does anybody know how??
Do not hesitate to ask me for more code or details.
Please excuse my English, not native.
You can set a tag for the view on your getView:
holder.layoutEliminar.setTag(theIdOfYourObject);
Note that View.setTag(Object tag) takes an Object as parameter (documentation). I will assume that you want to set the id of the object to delete as String for the tag.
And then, on your onClick
holder.layoutEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layoutEliminar;
// Retrieve your layoutEliminar from v
// ...
// Get the id of the object to delete from the tag
String id = (String) layoutEliminar.getTag();
}
};
I already did with the help of #Antonio. I didn't use Tag's, I have used the instruction getItem(position).getId() into the method onClick to refer the id of the object (don't know if it's the best and more efficient way to do). Like this:
holder.btnEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("PedidosAdapter dd: ",String.valueOf(getItem(position).getId()));
//Async Task for deleting the object with that ID
}
});

Best way to respond to Button within listview within fragment

I have a Button in a ListViewItem in a ListView within a Fragment. I have code that successfully notifies the host Activity of the button being tapped. While the code works, I want to make sure that this is the best design pattern to use here.
Here is a summary of the code:
The Activity (MainActivity) passes a reference of itself (this) to the Fragment in a variable called mainActivityReference.
The Fragment passes this reference to the ArrayAdapter object in a variable also called mainActivityReference.
In the ArrayAdapter getView method, I set the onClickListener and call a method within the mainActivityReference with the index position of the item as a parameter as follows:
viewHolder.soundButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Sound button tapped at position " + position);
mainActivityReference.chooseSoundForIndex(position);
}
});
Does this seem kosher? Or should I use something like a LocalBroadcastManager?
The basic strategy of passing an "owner" object to an adapter is quite kosher, and very common. Often I will make a listener interface, and have the parent activity or fragment implement the interface. Since it is your activity that is responding to the click, you wouldn't even need to pass the reference to the fragment and then the adapter. You could just get the View's context, and check if it implements the interface. Like this:
public interface SoundChooser {
void chooseSoundForIndex(int position);
}
viewHolder.soundButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Sound button tapped at position " + position);
Context context = v.getContext();
if (context instanceof SoundChooser) {
((SoundChooser)context).chooseSoundForIndex(position);
} else {
Log.w(TAG, "Activity should implement SoundChooser:" + context.getClass().getName());
}
}
});

Categories

Resources