android: how to select other view in onItemClick - java

I try to create an Android tic-tac-toe application which I use grid view as an arena. This is the game rule:
The arena is 3x3 scale that mean 9 box.
When I click a box, the cpu player select the other box.
The box that have clicked will change its image.
The problem is I only can change my selected view, and I want the CPU player to change his selected view. That mean on one call of onItemClick, there must be two views involved and changed its image. How can I do this? Can I change CPU ImageView based on position?
This is my code:
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// my selected view
ImageView myBox = (ImageView) view;
myBox.setImageResource(R.drawable.mybox_img_after_clicked);
//cpu selected view
//ImageView cpuBox = ???;
//cpuBox.setImageResource(R.drawable.cpubox_img_after_clicked);
}

Related

How can I set background color of first Row in RecyclerView "Android Application"

I used RecyclerView for displaying items as rows, and all the rows having background color is white. But i want to change first row background color as silver and rest will be white. Please help me.
In the onBindViewHolder
if( position == 0){
holder.view.setBackgroundColor(whatever_color);
}
In viewHolder class
View view;
public MyViewHolder(View itemView){
view = itemView;
Would suggest to have a look at ViewType, based on that you can inflate the correct view.
See https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType(int)
some references:
http://code2concept.blogspot.de/2015/10/android-multiple-row-layout-using.html
or
https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView
In your onBindViewHolder method of recycleView adapter class, you can use position parameter to perform required functionality.
In your case, it will be position = 0
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {

How to get the value of a TextView that is part of a ListView row in all checked items

As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});

Android - Change color of multiple choice listview background

I have a multiple choice ListView which defaults as having a white background but when selected, the background of the item changes to blue (defined here by a hex code).
mItemState = new boolean[list.length];
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mItemState[position] = !mItemState[position];
if (mItemState[position]){
view.setBackgroundColor(Color.parseColor("#33b5e5"));
}else{
view.setBackgroundColor(Color.WHITE);
}
}
});
Right now, everything seems to be working fine. However, if I select the very first element, the very last element also changes background color (it does not get ticked however). Also, if I choose the very last element, the same thing happens to the very first element. What seem to be the reason that this happens?
Well you must be familiar with ListView recycling mechanism and convertView model. The last elements view is composed using first elements view as convertView . Consider this blog http://android.amberfog.com/?p=296.

Custom listview onclicklistener on inner view

i currently create an app that needs an custom listview. Everything with my listview is fine, but now i neet to know, how to set an onClickListener to an view, defined in my list_row.xml. i just want the onclicklistener on the whole item, and on this one inner view. I attach a picture to demonstrate my problem, because it is so hard to describe >.<
Picture (dropbox): https://www.dropbox.com/s/72xdxuwz47vl7s5/problem.png
I need a function that is called when clicking into the view [my Problem] indicates. its an ImageView filled with an image.
Here's something I've done before that seems pretty similar to what you want to accomplish.
First, you declare an onItemClickListener for your ListView. This will handle standard list item taps (that is, taps inside a list item but outside the inner view region that you're concerned about). You can do this in a variety of places in your code, but onCreate() is a common one.
Example:
mListView.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
// Handle standard list item tap
// ...
}
} );
Then, you can just declare whatever onClickListeners you need for your inner view(s) inside your adapter's getView() method to handle click/tap events on your inner view.
Example:
#Override
public View getView( int position, View convertView, ViewGroup parent ) {
LinearLayout itemView;
// Inflate layout XML, etc.
// ...
// Find subviews in layout
ImageView innerView = (ImageView) itemView.findViewById( R.id.myInnerViewId );
// ...
// Set up onClickListener for inner view
innerView.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
// Handle inner view tap
// ...
}
} );
// ...
}
To set an OnClickListener in each row simply extend your current Adapter and override the getView() method. In there you can define specific listeners as you normally would.
This is discussed in great detail in this Google Talk by Romain Guy.

How to set the background color of a specific item in listview by position?

I want to set the background color of a specific item in the listview.
My listview is generated by ArrayAdapter using a ArrayList.
I have a specific item in the listview that I plan to change the background color.
I know the item's position in the list.
This is my code for generating the listview.
respondMessageListView = (ListView) findViewById(R.id.respondMessageListView);
respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages()));
Thank you!
[edit]
According to this post, using setSelection makes no effect if is used in onCreate(), the work around is "remove the method onAttachedToWindow in PullToRefreshListView". I am not quite understanding the solution. May I ask how should I accomplish this? I am a subclass of Activity, so I cannot subclass any other class anymore.
You will have to subclass ArrayAdapter and override the getView(...) method. For simplicity's sake you could just call through to the base class implementation and set the background color for the returned View.
Edit:
The following example colors the items' backgrounds alternating black and white.
private class MyAdapter extends ArrayAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(position % 2 == 0 : 0xff000000, 0xffffffff);
}
}
This code is for the when you select the listitem.
Try this code...
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
if( pos == 1) {
// to change the listview background
listview.setBackgroundColor(getResources().getColor(R.color.your_color_id));
// to change the selected item background color
myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
}
}
});
Good luck.

Categories