Android items background automatically change - android

today i was implementing a new feature in my app: showing a dialog with an EditText and requesting the soft keyboard, and i notice something really strange with my items in the sliding menu (I'm using the library from jfeinstein10)...
When the dialog shows and not the keyboard the items inside the sliding menu are okay, but when i also show the keyboard, the items change their background resource...
I tried using the log in the item click listener of the list inside the sliding menu but nothing it's called... So... Why it automatically change the background ?!
Hope you can help me :) And sorry for my english :D
The item click listener inside the sliding menu:
private AdapterView.OnItemClickListener slidingListItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Unselected items
for (int j = 0; j < adapterView.getChildCount(); j++) {
View root = adapterView.getChildAt(j);
ImageView image = (ImageView) root.findViewById(R.id.partial_sliding_menu_list_image);
TextView text = (TextView) root.findViewById(R.id.partial_sliding_menu_list_text);
image.setBackgroundResource(SLIDING_MENU_ICON_NORMAL);
text.setTextColor( getResources().getColor(SLIDING_MENU_TEXT_NORMAL) );
}
// change the drawable of the selected item
ImageView image = (ImageView) view.findViewById(R.id.partial_sliding_menu_list_image);
TextView text = (TextView) view.findViewById(R.id.partial_sliding_menu_list_text);
image.setBackgroundResource(SLIDING_MENU_ICON_SELECTED);
text.setTextColor( getResources().getColor(SLIDING_MENU_TEXT_SELECTED) );
// Load the page clicked [it's a fragment.]
loadPage(i);
}
};
Some screenshots of the "error":
Click me

Related

Activate or de activate recyclerview items

I have a fragment which contains a recyclerview and a floating action button. In recyclerview, each row contains a radio button set as disabled by default. my requirement is I need to enable these radio buttons in every row upon floating action button click event. Is there any solution?
Thanks in advance..
OK... I got a solution.
here is my code.
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i <= adapter.getItemCount(); i++) {
View childVIew = recyclerView.getChildAt(i);
if (childVIew != null) {
RadioButton radioYes = childVIew.findViewById(R.id.radio_yes_my_health_info);
radioYes.setEnabled(true);
RadioButton radioNo = childVIew.findViewById(R.id.radio_no_my_health_info);
radioNo.setEnabled(true);
}
}
fab.setImageResource(R.drawable.ic_save_white_24dp);
recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);
}
});
Now on FAB button click, all the radio buttons inside my recyclerview will be enabled and I can click to change the state of every radio button. But here I have got a problem. When I click FAB button, all the radio buttons inside the recyclerview will be activated but when I scroll the recyclerview all those radio buttons will be disabled or deactivated again. Please give me a solution for this.
You can have a flag in your adapter like
boolean isActivated = false;
public void setActivated(boolean activated) {
isActivated = activated;
}
and in your onBindViewHolder() method you can check for the flag and set the state of your flag accordingly.
In the OnClickListener of your FAB, call
adapter.setActivated(true);//true or false as per your requirement
adapter.notifyDataSetChanged();
This should work.
You can try looping through every view when you click the button, like so:
for (int i = 0; i < adapter.getItemCount(); i++) {
View view = recycler.getLayoutManager().findViewByPosition(i);
view.setSelected(true); //Select item
notifyItemChanged(i); //Notify item changed
}

The background color of the listview is not changing. How do I fix it?

I'm using this code
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView idno = (TextView) view.findViewById(R.id.idno);
TextView fullname = (TextView) view.findViewById(R.id.fullname);
TextView remark = (TextView) view.findViewById(R.id.remark);
new DisplayStudent().execute("Update", idno.getText().toString());
if(remark.getText().toString().equals("absent")) {
if(isStudentLate) {
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorLate));
fullname.setTextColor(Color.WHITE);
remark.setText("late");
}
else {
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPresent));
fullname.setTextColor(Color.WHITE);
remark.setText("present");
}
}
else if(remark.getText().toString().equals("present") || remark.getText().toString().equals("late")) {
view.setBackgroundColor(Color.WHITE);
fullname.setTextColor(Color.BLACK);
remark.setText("absent");
}
}
});
((BaseAdapter)getListView().getAdapter()).notifyDataSetChanged();
Everytime I tap on an item, the background color of the item I tapped is supposed to change. Why does it not change? I tried removing the ((BaseAdapter)getListView().getAdapter()).notifyDataSetChanged(); it does change but when I scroll down and scroll up again it goes back to its original background color.
For example. One Item's background color is colored white. When I tap that item, it changed to green. That's perfect. But when I scroll down and scroll up again, the item that I tapped changed back to white. How do I fix this?
Try setting this in your ListView tag:
android:cacheColorHint="#00000000"
And also try this:
android:background="color code here"

Android - Make ImageView Visible When ListItem is selected

I have a listview and each item has a title, some info, and a couple ImageViews I'm using as edit/delete buttons. I don't want to show these "buttons" unless the user selects the row. I can make the "buttons" invisible using:
DeleteButton.setVisibility(View.INVISIBLE);
EditButton.setVisibility(View.INVISIBLE);
in my BindView.
I can make the buttons visible in an onListItemClick:
ImageView DeleteButton = (ImageView) v.findViewById(R.id.button_delete);
ImageView EditButton = (ImageView) v.findViewById(R.id.button_edit);
DeleteButton.setVisibility(View.VISIBLE);
EditButton.setVisibility(View.VISIBLE);
What I can't do is make the "buttons" invisible when selecting another item or scrolling away.
The closest I found was to do a loop through the listitems in the current view and set them all to invisible before making the selected one visible:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
\\loop through all the items and set them back to invisible
for (int i=0;i<=l.getLastVisiblePosition();i++){
View vChild = l.getChildAt(i);
ImageView DeleteButton = (ImageView) vChild.findViewById(R.id.button_delete);
ImageView EditButton = (ImageView) vChild.findViewById(R.id.button_edit);
DeleteButton.setVisibility(View.INVISIBLE);
EditButton.setVisibility(View.INVISIBLE);
}
\\set the selected one visible
ImageView DeleteButton = (ImageView) v.findViewById(R.id.button_delete);
ImageView EditButton = (ImageView) v.findViewById(R.id.button_edit);
DeleteButton.setVisibility(View.VISIBLE);
EditButton.setVisibility(View.VISIBLE);
}
As you can guess...this approach only works if you have a few items.
I thought about adding a field to the SQLite database my list is using to keep track of the button visibility (similar to what you do for checkboxes) but that seemed silly. Please tell me there's another way.
Another way to do this would be to have an int field in your class that will remember the current position:
private int current = -1;
then in the onItemCLick() method use that field to hide/show your views:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// if it is the first click ignore this part
if (current != -1) {
View last = l.getChildAt(current); // the last one clicked
last.findViewById(R.id.button1).setVisibility(View.GONE); // kill it
}
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
current = position; // remember the new clicked position
}
If you want the views to be gone also when you scroll the list and those views aren't visible then in the bindView() method add the lines that hide the views:
//...
ImageView DeleteButton = (ImageView) view.findViewById(R.id.button_delete); // view is the view that you get as a parameter
ImageView EditButton = (ImageView) view.findViewById(R.id.button_edit);
DeleteButton.setVisibility(View.INVISIBLE);
EditButton.setVisibility(View.INVISIBLE);
//...
When the use scrolls the list all the views will have the Buttons visibility set to GONE and the onItemCLick() logic will work only for visible views.
I dont know if i understand your question correct but make the definitions of the ImageViews
ImageView DeleteButton = (ImageView) v.findViewById(R.id.button_delete);
ImageView EditButton = (ImageView) v.findViewById(R.id.button_edit);
one time at onCreate() and not every time again! Dont know if this solves your problem but try it.
Edit:
if(tempEditButton != null && tempDeleteButton != null){
tempDeleteButton.setVisibility(View.INVISIBLE);
tempEditButton.setVisibility(View.INVISIBLE);
}
EditButton.setVisibility(View.VISIBLE);
DeleteButton.setVisibility(View.VISIBLE);
tempEditButton = EditButton;
tempDeleteButton = DeleteButton;
This is an old question but I think I can still answer it. I solved this problem by creating a public static method which can be called from anywhere:
public static void hideOptions(Activity activity){
for (int i = 0; i< listviewSize; i++) {
Listview x = (Listview ) activity.findViewById(R.id.contactRecyclerView);
x.getChildAt(i).findViewById(R.id.myView).setVisibility(View.GONE);
}
}
You can then call this method whenever you want to hide your view

GridView : Get a view by position

I'm using a grid view to display a month calendar. When i touch a date on the calendar , the corresponding cell is highlighted by changing its background and text Color. Everything works like charm except the fact that i can't un-highlight a cell if i choose a new date.
When i click a date i remember the position, so when i chose a new one i know where i clicked previously :
protected OnClickListener onClicThisMonthDate(final int position)
{
return new OnClickListener() {
#Override
public void onClick(View v)
{
if(prevDateSelected > -1 )
{
//Get the view by the oldPosition
//Change its background and text color
}
prevDateSelected = position;
TextView casecal = (TextView) v.findViewById(R.id.case_cal);
casecal.setBackgroundColor(Color.rgb(93, 94, 94));
casecal.setTextColor(Color.WHITE);
}
};
}
Is there a solution to get a view by his position ? What's the most efficient way to select a cell and unselect all the other ?
Note : By "select a cell" i mean highlight it, by changing it style.
ViewGroup.getChildAt returns the view at the specified position in the group.
GridView.setSelection selects of the view at the position and unselects others views.

Showing a List View when a button is clicked: Android

I am trying to implement a drop down list when a button is clicked.
So, I have a text view and a button in a navigation bar(nav.xml) and a corresponding list view. This navigation bar is included in another page( products.xml)
when the button is clicked i get the list view right below the button(which is what i want to acheive) but its my moving all the contents on the current page downwards, even the text view which is placed in nav bar moved downwards.
I am totally new to Android, any sample examples or a way how to achieve it
???
Sounds like you need a Spinner. It's the equivalent of a drop down list for Android. You can find an example here.
So, for our need we need to use ListPopupWindow.
The link to official description:
http://developer.android.com/reference/android/widget/ListPopupWindow.html
Let's dive in the code:
we have our own method:
public void downloadBtnSelected(View anchor) {
final ListPopupWindow lpw = new ListPopupWindow(this);
String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
PopupAdapter pa = new PopupAdapter(data, this);
lpw.setAdapter(pa);
//setting up an anchor view
lpw.setAnchorView(anchor);
//Setting measure specifications. I'v used this mesure specs to display my
//ListView as wide as my anchor view is
lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
lpw.setWidth(anchor.getRight() - anchor.getLeft());
// Background is needed. You can use your own drawable or make a 9patch.
// I'v used a custom btn drawable. looks nice.
lpw.setBackgroundDrawable(this.getResources().getDrawable(
android.R.drawable.btn_default));
// Offset between anchor view and popupWindow
lpw.setVerticalOffset(3);
lpw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
and the button with an onClickListener to call this method:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
we pass the View v argument as our anchor, in order to let our PopupWindow to know where to display itself. It will be displayed in the bottom-left corner of our anchor view if there is enough room below. If not- it will displa

Categories

Resources