I'm creating a chat application. I want to show the time when user clicks on message
. This feature is working with this code in getView() of BaseAdapter
holder.txt_msg_user.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (holder.txt_date_user.isShown() && holder.txt_AM_user.isShown())
{
holder.usertimelayout.setVisibility(View.GONE);
holder.txt_date_user.setVisibility(View.GONE);
holder.txt_AM_user.setVisibility(View.GONE);
}
else
{
holder.usertimelayout.setVisibility(View.VISIBLE);
holder.txt_date_user.setVisibility(View.VISIBLE);
holder.txt_AM_user.setVisibility(View.VISIBLE);
}
}
});
But i want to add one more feature, like in the below image if i click on every message every message is showing its time, but i want to achieve one thing like skype, if user sees the time of any message then first it will check for the previous messages. If any previous message is showing the time then it should be hide & recently tapped on message by the user should be shown.Please help how it can be possible. This is the problem: (see image)
Since you're achieving the time already and it's being added to each list item, perhaps you could just set the last item in your listview to View.VISIBLE and the rest to View.GONE.
You keep a property like:
View selectedMessage;
Where you set when the user clicks in some message, and verify it:
...
#Override
public void onClick(View v) {
if (selectedMessage != null && v != selectedMessage) {
Holder h = (Holder) selectedMessage.getTag();
h.hideTime();
h = (Holder) v.getTag();
h.showTime();
selectedMessage = v;
}
...
}
...
It worth noting that this Holder interface would be something that you have created, implementing the methods showTime and hideTime. This is not Android built-in.
Related
I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.
Not sure if this a simple one which I have just spent too much time staring at but this is the problem.
If a user clicks a button, it changes colour this is simple and has been achieved by:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter == 0) {
button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = true;
counter++;
} else {
button.setBackgroundColor(Color.parseColor("#FFFFFF"));
Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = false;
counter = 0;
}
}
});
If the user clicks it again, it will change back to the previous colour - but...
If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.
I need to know if the button has been clicked and if not, should mark the user as present.
Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.
I need the counter to treat each button individually, any ideas how this could be achieved?
Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?
I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:
import android.content.Context;
import android.widget.Button;
public class customButton extends Button {
boolean haveIBeenClicked; //false by default
public customButton(Context context) {
super(context);
}
public void toggleHaveIBeenClicked(){
haveIBeenClicked=!haveIBeenClicked;
updateBackgroundColor();
}
void updateBackgroundColor(){
if (haveIBeenClicked){
this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{
this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
}
then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call
((customButton)v).toggleHaveIBeenClicked();
...after having created a customButton object and setting an on click listener on it.
Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out
I want to implement this
.
I want to check if the clicked item is fully visible and if it's not I would like to smoothly scroll upwards/downwards. I have a GridLayoutManager with 3 columns. The items are all of the same size, and are just ImageViews.
I am able to get the RecyclerView to scroll with:
#Override
public void onClick(View view) {
int adapterPosition = RecHolder.this.getAdapterPosition();
mRecyclerView.scrollToPosition(adapterPosition);
...
}
But it's not a "scroll", it's very laggish and way too quick.
If I try to use mRecyclerView.smoothScrollToPosition(adapterPosition); the result is the same. Exactly the same movement, there is no visible difference.
Don't bother testing to see if it's not completely visible. Just insert a command to scroll to that position. Since you didn't post any of your code I can't specifically say how you would do it. I personally have my adapter create an intent and my activity handles the intent. If that's what you're doing then you can include the getAdapterPosition() as an extra like this (vh is my ViewHolder).
vh.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SelectItemGridAdapter.ContentViewHolder vh = (SelectItemGridAdapter.ContentViewHolder) (((View)v.getParent()).getTag());
Intent intent = new Intent(ACTION_SELECT_Item);
intent.putExtra(Constants.MSG_TYPE, SELECT_ITEM_TAPPED);
intent.putExtra(SELECT_ITEM_TAPPED_ID, vh.viewModel.mItemId);
intent.putExtra(SELECT_ITEM_TAPPED_POS, vh.getAdapterPosition());
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}
});
Then the reveiver in the activity can get the SELECT_ITEM_TAPPED_POS value...
int pos = intent.getIntExtra(SelectItemGridAdapter.SELECT_ITEM_TAPPED_POS, -1);
if (pos > -1)
rv.scrollToPosition(pos);
HTH, Mike
So I have a listview that is built to have each view in the list to have 2 radio buttons. The problem is when the list gets longer than the page the list starts to recycle the views and it also takes along the checked radio buttons. I understand what is causing the problem but I don't really know how to solve this issue and the answers I've looked up online aren't really helping. My problem is a bit unique because I'm using Parse with my adapter so looking up solutions specifically for parse is pretty hard.
Here is my code for my adapter:
// Set up a customized query
ParseQueryAdapter.QueryFactory<AnywallPost> factory =
new ParseQueryAdapter.QueryFactory<AnywallPost>() {
public ParseQuery<AnywallPost> create() {
Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
ParseQuery<AnywallPost> query = AnywallPost.getQuery();
query.include("user");
query.orderByDescending("PostScore");
query.whereWithinKilometers("location", geoPointFromLocation(myLoc), radius
* METERS_PER_FEET / METERS_PER_KILOMETER);
query.setLimit(MAX_POST_SEARCH_RESULTS);
return query;
}
};
// Set up the query adapter
postsQueryAdapter = new ParseQueryAdapter<AnywallPost>(this, factory) {
#Override
public View getItemView(AnywallPost post, View view, ViewGroup parent) {
if (view == null) {
view = View.inflate(getContext(), R.layout.anywall_post_item, null);
}
//TextView DetailsView = (TextView) view.findViewById(R.id.content_view);
TextView contentView = (TextView) view.findViewById(R.id.content_view);
TextView usernameView = (TextView) view.findViewById(R.id.username_view);
TextView postscoreView = (TextView) view.findViewById(R.id.PostScore);
RadioButton upvote = (RadioButton) view.findViewById(R.id.Upvote);
RadioButton downvote = (RadioButton) view.findViewById(R.id.DownVote);
//DetailsView.setText(post.getDetails());
contentView.setText(post.getText());
usernameView.setText(post.getUser().getUsername());
postscoreView.setText(post.getInt().toString());
upvote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
post.increment("PostScore", 1);
post.saveInBackground();
}
});
downvote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//post.decrement();
}
});
return view;
}
};
postsQueryAdapter.setAutoload(false);
postsQueryAdapter.setPaginationEnabled(false);
// Attach the query adapter to the view
ListView postsListView = (ListView) findViewById(R.id.posts_listview);
postsListView.setAdapter(postsQueryAdapter);
So I need these two radio buttons to stay with the post made the entire time but I am still a beginner so I'm not entirely sure how I would go about that. Could anyone provide a solution and explain how it worked? If you need anymore code just let me know. Thank you for you time.
Essentially when you get a new cell, be sure to clear both the radio buttons, then re-check them only if it meets your condition for it to be checked (likely checking the count on the post object). Hard to tell exactly how this will look without knowing more about how you are storing data, but I'd guess something like this:
upvote.setChecked(post.getInt() > 0);
downvote.setChecked(post.getInt() < 0);
Along with the list items I'd carry a list with the state of radio buttons.
So even when the views get recycled you use your own state to check the correct radio button.
A good choice would be a list of Boolean (the boolean wrapper). Where you could keep true for upvote, false for downvote and null for neither.
My button use code that shows and hides the views:
public void onClick (View v){
if (What code you need to enter here to determine hidden views or shown)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
}
What code I need to add in the "if()", so that clicking on my button was checked condition. If the activity is hidden, it should be shown, and Vice versa. If the views is shown, hide it.
I'm guessing since you are using setVisibility, that you want to check the visibility of a View , not an Activity.
In that case you just use getVisibility()
(I used != cause the visibility could be IINVISIBLE as well, change per your needs) :
public void onClick (View v){
if (testActivity.getVisibility() != View.VISIBLE)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
} });
Don't understand why, but only that removed the answer of a man who has solved my problem. Here is his response, and this code works:
public void onClick (View v){
if ((testActivity.getVisibility() == View.VISIBLE))
{
testActivity.setVisibility(View.GONE);
}
else
{
testActivity.setVisibility(View.VISIBLE);
}
I have a button that is set to VISIBLE under certain circumstances, then once its clicked its suppose to make the button INVISIBLE again but for some reason its not working. Here is my code,
if(variable == 2){
testButton.setVisibility(View.VISIBLE);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
testButton.setVisibility(View.INVISIBLE);
test2Button.setVisibility(View.VISIBLE);
}
});
}
Have you tried displaying a toast when the button is clicked, just to see if that block of code is even executing? I don't see it, but I'm assuming you've actually declared a View associated with that button via 'findViewById'
EDIT:1
Do this
public void onClick(View view) {
view.setVisibility(View.INVISIBLE);
findViewById(R.id.<your test2Buttons ID>).setVisibility(View.VISIBLE);
}
Note: If you do View.GONE it will leave all area acquired by it and the other control will capture this area
where is with View.INVISIBLE it will maintain its acquired area