Android gridview change background by position - java

I have Gridview with images. above the image I have text(hello) and I can to show images(without text) to my gridview .now I want if I click gridview's 5th element I want to hide 5th image's text.meybe problem solution is gridview.setOnItemClickListener but I do not know how I can do this
I wrote some code witch can to show toast message by position, but I do not know how I can to hide text by position
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "Cliked" + position,
Toast.LENGTH_SHORT).show();
}
});

You could try setVisibility along with an array to sort through the GUI identifiers:
int textId = this.getResources().getIdentifier(position, "id", this.getPackageName());
TextView picText = (TextView) findViewById (R.id.TextID);
picText.setVisibility(TextView.INVISIBLE);
Or something like that. INVISIBLE makes it invisible, but take up the same space - GONE will make the space disappear too - so depending on what you want.
That should at least get you in the right direction.
For more: http://developer.android.com/reference/android/view/View.html#setVisibility(int)

First Find the textView that you want INVISIBLE.
Then set it INVISIBLE.
By below Code
((TextView) v).setVisibility(TextView.INVISIBLE);
if above Line dont work use below
TextView textview=(TextView)v.findViewById(R.id.id_of_textview);
textview.setVisibility(TextView.INVISIBLE);

Related

How to refresh layout/context from list adapter (I think this is what i need)

Im having a bug that i cant understand the reason and how to resolve it. I belive that is a problem of layout/view/context refresh but i dont know.
I have a cell from a listView(I prefer recyclerView but the project has years) and in the corner of the cell i have a button to show more options. Programatically it just make an element View.GONE and another element View.VISIBLE.
I will attach code in a moment
To this button i setted too a listener that when i tap on it it do the opposite of below mentioned. It shows some elements and hide an entire LinearLayout from the cell. The elements are showed BUT the LinearLayout keeps on the screen like bugged. If i tap anywhere it disappears and if i try to tap on it it disappears too. Its like the view of that linear got bugged and keep in there like a ghost view. I will shop some pictures.
The cell normally at the beginning: https://imgur.com/1BjK0KP
The cell after i press the entire view to show the LinearLayout at the bottom of the cell: https://imgur.com/eONSptW
The cell after i press the arrow of the corner to hide the LinearLayout. Here it shows the view bugged https://imgur.com/jrT0qxV
The cell after i tap anywhere else https://imgur.com/XD1jN7U
public void expandView(View view){
final View cellView = view;
final LinearLayout editLinear = (LinearLayout) view.findViewById(R.id.cart_edit);
editLinear.setVisibility(View.VISIBLE);
final TextViewFont countText = (TextViewFont) view.findViewById(R.id.itemCount);
countText.setVisibility(View.GONE);
final TextViewFont total = (TextViewFont) view.findViewById(R.id.itemTotal);
final ImageView imageViewArrow = (ImageView) view.findViewById(R.id.cart_edit_image);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_up));
//notifyDataSetChanged();
imageViewArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countText.setVisibility(View.VISIBLE);
editLinear.setVisibility(View.GONE);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_down));
}
});
First of all, you don't need to store context because in View you have the method view.getContext().
I recommend this article:
https://possiblemobile.com/2013/06/context/
On the other hand, ensure that you don't have a ghost view that is overlapping your image view.

Set the color of particular element on item in custom adapter

For example I have Custom Adapter List View, and my list item is a layout that contains different elements: textview, imageView and so on. How can I set the color of imageview of selected list item? Suppose I want to add this item in favourite and I want to change the color of star favourite for yellow color. Thank you)
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), PlaceName[position] + " in favourite",
Toast.LENGTH_SHORT).show();
//Do smth here, set the color of element on item, add to favourite and something else
return true;
}
});
Well, you have this line:
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html
According to the docs, view is the layout that you actually clicked, so you can get the child of that View using something like this:
ImageView favorite = view.findViewById(R.id.yourFavoriteImageView);
Note that if you scroll through your list, the layout might render again, and your change won't be visible again.
view.setBackgroundColor(Color.parseColor("#222222"));

Change the View's background of a ListView initially

I'm now implementing an application that contains an Activity includes ListView, when the user selects any item in the list view, the background & text color of this view are changed, So, i placed the code of this changes in the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) method ..but,this is not the problem.
The problem is, when i open the activity, i need to make an initial selection "before the user selects any thing"..so i made listView.setSelection(index);, but, unfortunately, this code doesn't invoke the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)..So, the view doesn't changed "background and text color".
Any solution for that..?!!!
Thanks,
try onItemSelected for selection
selecting a iem in list view does not fires onItemClick
Create a method in yout Adapter to set the selection (or do it in the constructor):
public myAdapter (COntext context, int initialSelectedPos){
setSelectedPos(initialSelectedPos);
}
public void setSelectedPos(int pos){
mSelectedPos = pos;
}
Then check in your getView is given pos is the same than mSelectedPos.
#Override
public View getView(..., int pos){
/*convertView stuff*/
if (pos == mSelectedPos){
//Put the background as it is selected
}else{
//...
}
return view;
}
In your OnItemClick method from your OnItemClickListener call the setSelectedPos method of your Adapter.
You will solve the issue you commented and also when your selected view is no longer visible on screen and comes back to screen, will still be marked as selected ( I am pretty sure it was appearing with the original background).
after setting selection
listView.setSelection(index);
and then after call the
listView.getAdapter().notifyDatasetChanged();
i think this will solve your problem

Click is not working on the Listitem Listview android

I implemented the android listview with the ListActivity. Here I have the problem that when i click on the list item no action is performed when the flash color is also not coming that is the orange color. So do you have any idea about this kindly answer to my question.
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT)
.show();
}
I put this code also into the Main ListActivity.
The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.
What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.
Try this,
<Button android:id="#+id/textsize_increaser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/back_button"
android:focusable="false"
android:text=" A + "/>
Here I have added this android:focusable="false" and it works fine. try it.
Have you set the choice mode of ListView to SINGLE :
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And if you have any clickable imageview or textview or button in the list item, then make them not focusable (in your Adapter class):
yourButton.setFocusable(false);
yourButton.setFocusableInTouchMode(false);
Are you using custom Adapter? and inflating layout with button or any view that eats away the list list view focus as child, then it won't work obviously. make sure to set
android:focusable="false"
to such view in xml file. hope this works for you.
Set this in your listactivity java file
listview1.setFocusable(false);
Actually there is a parameter meant for that to prevent children views from getting focus, just add the following in the parent layout:
android:descendantFocusability="blocksDescendants"
As the documentation explains:
The ViewGroup will block its descendants from receiving focus.
Eclipse suggested me to add textIsSelectable="true" to my TextViews in the layout xml which was used for list view.
Well, if you want to click the items in the list then you should not add those tags.
make sure that you are
Not using Scroll View with List View
Not using Scroll View in your row item layout for List View
If Scroll View is present at any of above place remove it
refer to this post for a solution:
Click is not working on the Listitem Listview android
View v = parent.getChildAt(position);
parent.requestChildFocus(v,view);
v.setBackground(res.getDrawable(R.drawable."Some drawable for clicked row"));
int count = parent.getChildCount();
for(int i=0; i<count; i++)
{
if(i!=position)
{
v = parent.getChildAt(i);
v.setBackground(res.getDrawable(R.drawable."some drawable for not clicked row));
}
}
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
Toast.makeText(v.getContext(), exm.get(pos).getDefinition(),
Toast.LENGTH_SHORT).show();
}
});
listItemButton.setFocusable(false);
listItemButton.setFocusableInTouchMode(false);
Set the above in your adapter. It's not working in XML

How to output text result in Android?

In an android website, I found an article about a widget similar to a drop-down list for selecting items. (Following is the link to the site; and it shows all the codes).
http://developer.android.com/resources/tutorials/views/hello-spinner.html
It uses the following code to display a message once you have selected a planet.
Toast.makeText(parent.getContext(), "Planet is Selected", Toast.LENGTH_LONG).show();
But this message "Planet is Selected" is only going to display for about 3 seconds and then it disappears. Can you please tell me how can I make the message stay on the screen for a longer time. Or how can I output the "Planet is Selected" message as a text layout in to the screen(So that it will stay on the screen permanently till I select another item from the list)? How can I use addView(tv) instead of setContentView(tv) Any help would be greatly appreciated.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if (parent.getItemAtPosition(pos).toString().equals("Mars"))
{ TextView tv = new TextView(HelloSpinner.this);
tv.setText(parent.getItemAtPosition(pos).toString() + "Planet is Selected");
setContentView(tv); //How can I use addView(tv); here?
//Toast.makeText(parent.getContext(), "Planet Selected", Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
} }
If you want it to stay permanently on the screen, why not use a TextView and set your value to that instead of a Toast.
If you have any problems with not being able to use TextView, ie undefined. You should take a look at the textview documentation, as it is very well described there.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView tv = new TextView(this);
tv.setText(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
third parameter Toast.LENGTH_LONG is time. so you can set any integer value ( not sure second or millisecond) ,. then on specific event call toast.hide() ;
toast is good choice for show message for some times only . so use textView if possible
create TextView with activity context :
TextView tv = new TextView(ActrivityName.this)
else if not an activity
TextView tv = new TextView(parent.getContext())

Categories

Resources