I have a problem whith custom adapter view.
I try, change background of view on Click event.
I have AdapterView.OnItemClickListener, where i get selected item, and calling myListView.invalidate();
After invalidate, calling adapters getView(...). Here code for this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProjectAdapterData projectItem;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(R.layout.project_small_item_layout, null);
ProjectAdapterData projectAdapterData = new ProjectAdapterData();
row.setTag(projectAdapterData);
name = (TextView)row.findViewById(R.id.txtObjectName);
if (objectData[position].Name!= null)
name.setText(objectData[position].Name);
adress = (TextView)row.findViewById(R.id.txtObjectAdress);
if (objectData[position].Adress != null)
adress.setText(objectData[position].Adress);
}
else {
background = (RelativeLayout)row.findViewById(R.id.rlProjectBackground);
if (objectData[position].isSelected)
background.setBackgroundColor(context.getResources().getColor(R.color.cProjectSelected));
else
background.setBackgroundResource(R.color.cProjectUnSelected); //it's calls, but no result
row.invalidate();
}
return row;
}
My question, why background doesn't change?
My selector_list
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#color/cProjectSelected"/>
<item android:state_selected="false"
android:color="#color/cProjectUnSelected"/>
</selector>
you can use selector to highlight item
In drawable folder create a xml file
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/blue" android:state_activated="true"/>
<item android:drawable="#color/blue" android:state_selected="true"/>
<item android:drawable="#color/transparent"/>
</selector>
and set listSelector in xml for your listview like
android:listSelector="#drawable/list_selector"
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="BLACK">#000000</color>
<color name="WHITE">#FFFFFF</color>
<color name="light_grey">#a5acb0</color>
<color name="brown">#525964</color>
<color name="dark_grey">#212121</color>
<color name="aqua">#a6b1ba</color>
<color name="red_cherry">#C9282D</color>
<color name="silver">#A9A9A9</color>
<color name="black">#000000</color>
<color name="transparent">#00000000</color>
<color name="white">#FFFFFF</color>
<color name="blue">#00aceb</color>
<color name="spiritclips_bck">#8AB8E0</color>
<color name="translucent_black">#55000000</color>
<color name="grid_bck">#627583</color>
<color name="grey">#393430</color>
<color name="dark_grey_bg">#1f1c17</color>
<color name="login_font_color_1">#546778</color>
<color name="login_font_color_2">#8E8E8E</color>
<color name="blue_txt">#0f5690</color>
</resources>
for custom_list_item the layout should be
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/activatedBackgroundIndicator" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
and minimum version of your application should be 11
Related
In the above picture GAIN 3 is selected but its not visible properly , so how can i change that color to darker color.
basically i want to change the selected text background in darker color.
I'm using com.jaredrummler.materialspinner.MaterialSpinner Spinner.
Here's the java implementation.
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
text = spinner.getText().toString();
Log.e("Spinner Listener",text);
if(text.contains("GAIN 0")){
sendToDevice("F");
} else if(text.contains("GAIN 1")){
sendToDevice("G");
} else if(text.contains("GAIN 2")){
sendToDevice("H");
} else if(text.contains("GAIN 3")){
sendToDevice("I");
}
}
});
And the layout item looks like the following.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
app:ms_dropdown_max_height="350dp"
app:ms_dropdown_height="wrap_content"
android:textColorHighlight="#000000"
android:layout_width="130dp"
style="#style/spinner_style"
android:popupTheme="#android:style/ThemeOverlay.Material"
android:textColor="#color/blue"
android:layout_below="#+id/testmodetitle"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toEndOf="#+id/button1"
android:layout_marginStart="30dp" />
To change background color and other color this library has provided some attributes. To change background color of selected item use below code.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_background_selector="#drawable/selector_gray_white_spinner"
app:ms_dropdown_height="wrap_content"
app:ms_dropdown_max_height="350dp" />
create one selector in drawable having name selector_gray_white_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/darkGray"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/darkGray"/>
<item android:state_focused="true" android:drawable="#android:color/white"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/white"/>
</selector>
Add dark color in your color.xml file
<color name="darkGray">#acacac</color>
Use this way it will help you:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list) {
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item position
if (position == selectedItem) {
v.setBackgroundColor(Color.BLUE);
}
else {
// for other views
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
There are some attributes available along with the implementation of that specific library. Please have a look in the readme.md section where the attributes are listed.
I think you might consider using ms_background_selector attribute in your layout where you have declared the spinner.
So the layout declaration will look like this.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinner"
app:ms_background_selector="#drawable/your_darker_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Add a file named your_darker_selector.xml and put the following code inside the file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#android:color/darker_gray"/>
<item android:state_checked="false" android:drawable="#android:color/white" />
</selector>
Modify the color from the selector file as per your necessity.
Give html colour code for the first item of spinner.
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText),
TextView.BufferType.SPANNABLE);
I want to do this : I have a list whose colors of every two items are different and want to select the item that was clicked. The item that is selected is highlighted but the old color returns after it is released
I did this :
artists_list_background_alternate.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/sign_out_color" />
<item android:state_pressed="true"
android:drawable="#color/survey_toplist_item" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/survey_toplist_item" />
</selector>
artists_list_backgroundcolor.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/grey" android:state_pressed="false" android:state_selected="false" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="true" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="false" android:state_selected="true" />
<item android:drawable="#color/survey_toplist_item" android:state_pressed="false" android:state_selected="true" />
</selector>
Java File Code ::
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.listitem, parent, false);
}
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.artists_list_background_alternate);
}
((TextView) view.findViewById(R.id.heading)).setText(data.get(position));
return view;
}
ListView lvMain = (ListView) findViewById(R.id.list);
lvMain.setAdapter(adapter);
Layout xml ::
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:dividerHeight="1dp" />
And I don't know what I did wrong
In order to make an item highlighted, you need to provide choiceMode for your ListView. For example:
android:choiceMode="singleChoice"
Also, add a color for state android:state_activated="true" in your selector.
In my app I have created one Listview. When I click on the row, I want to change the Listview row color as Lightgray color and textcolor as a Blue color.
For this I have tried the code below but only row background color is changing not the textcolor.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int j = 0; j < parent.getChildCount(); j++) {
parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.LTGRAY);
rowText = (TextView) findViewById(R.id.rowitem);
rowText.setTextColor(Color.BLUE);
}
}
});
}
first enable the android:ListSelector attribute in your ListView
ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Tablayoutdesign"
android:cacheColorHint="#000000"
android:dividerHeight="1dip"
android:layout_marginTop="63dip"
android:layout_marginBottom="40dip"
/>
Thank create a selector (listselector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#drawable/focused"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#drawable/selected" />
</selector>
Than go for colors.xml
<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>
and with some Java magic:
listview.setSelector(R.drawable.listselector)
Thanks to #sankar-anesh
List State Pressed
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#4285f4" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#4285f4" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
List view background when not pressed
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:radius="2dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
List view Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/card_state_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/card_background" />
</selector>
First create a selector drawable like this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/red"
android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
Then
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
view.setBackgroundDrawable(selector);
Change the
rowText = (TextView) findViewById(R.id.rowitem);
to
rowText = (TextView)view.findViewById(R.id.rowitem);
since you should find the textview in the inflated view, not independently.
I am populating a ListView from a file. The ListvVewcontains checkboxes. Now I want to change the color and font of the text of individual items on checkbox click. Instead I am only able to highlight the individual item row (to blue). What am I missing?
main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3E2A9">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="390dp"
android:id="#+id/linearLayoutlist"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:choiceMode="multipleChoice"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/listviewcolor"
>
</ListView>
</LinearLayout>
</RelativeLayout>
listviewcolor.xml
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#color/red" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#color/blue" />
<!-- Default -->
<item android:drawable="#color/lightyellow" />
color.xml
<color name="blue">#ff3a8dcb</color>
<color name="red">#FF0000</color>
<color name="lightyellow">#F3E2A9</color>
main_activity
List arrlist=new ArrayList();
ArrayAdapter<String> adapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = getListView();
listView.setChoiceMode(listView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, arrlist); //arrlist is an ArrayList
//whose value is taken from a file
listView.setAdapter(adapter2);
listView.setOnTouchListener(touchListener);
listView.setOnScrollListener(touchListener.makeScrollListener());
}
You can change TextColor and TextFont Size programmatically on Checkbox onCheckedChangeListener()
tx.setTextSize(TypedValue.COMPLEX_UNIT_SP,30);
tx.setTextColor(Color.WHITE);
Please show your list item layout where you added check box and text view and in your adapter change text colour and font dynamically on Checkbox onCheckedChangeListener() like as follows :
final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
Well, I'm trying to change my drawer navigator whenever I change the fragment, I managed to change the ActionBar color and the Background color, but the thing is that with the Background is not enough... I saw that I declare a BackgroundResource with other colors, and when I try to change the color it does not work.
My MainActivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Displaying Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Displaying Drawer -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/drawer_list_selection"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
My colors.xml (I've tried to change it manually but I don't know how to do, then I've created as colors as items are on my drawable navigator).
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_title">#fff</color>
<color name="list_background">#458A79</color>
<color name="list_background2">#ffc591</color>
<color name="list_background3">#ab91ff</color>
<color name="list_background4">#f784fe</color>
<color name="list_background5">#91dfff</color>
<color name="list_background_pressed">#6FA698</color>
<color name="list_background_pressed2">#ffc591</color>
<color name="list_background_pressed3">#ab91ff</color>
<color name="list_background_pressed4">#f784fe</color>
<color name="list_background_pressed5">#91dfff</color>
<color name="list_divider">#fff</color>
<color name="counter_text_bg">#626262</color>
<color name="counter_text_color">#c5c4c4</color>
</resources>
And the thing that I've tried on my MainActivity.java is
private void displayView(int position) {
// update the main content with called Fragment
Fragment fragment = null;
LlistaGenericaFragment frag = null;
FragmentTransaction ft = null;
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#256F5C")));
switch (position) {
case 0:
fragment = new MetallsAlcalinsFragment();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffc591")));
mDrawerList.setBackground(new ColorDrawable(Color.parseColor("#ffc591"))); //set the background but not the ListView
mDrawerList.setBackgroundResource(R.color.list_background2); //Don't see any change
break;
I know I'm doing something wrong, but I don't get what... Could you explain me how can I change this each time I change the fragment?
What you would want to do then is create a drawable selector -> list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/list_background2" android:state_pressed="true"/>
<item android:drawable="#color/list_background" />
</selector>
then on your list add this as a selector:
mDrawerList.setSelector(R.drawable.list_selector);
If you want to do this every time something happens in your fragment, you would create an interface:
public iterface TalkToActivity(){
public void sendChangeEvent(int changeType);
}
in Your fragment create a local variable:
TalkToActivity m_callBack;
Then in your onAttach method of the fragment:
#Override
public void onAttach(Activity activity) {
// Call to the Super Class
super.onAttach(activity);
// Attempt to Add the Interface
try {
m_callBack = (TalkToActivity) activity;
} catch (ClassCastException e) {
// Print to LogCat if the Parent Did not implement the interface
Log.e(FRAGMENT_TAG, "Failed to implement interface in parent.", e);
}
}
and then in the event that you want to capture and communicate the change:
public void buttonWasPressed(int changeType){
if(m_callBack != null){
m_callBack.sendChangeEvent(changeType);
}
}
Finally, in your activity make your activity 'implents TalkToActivity' which will force you to override the method sendChangeEvent and in this method
#Override
public void sendChangeEvent(int changeType){
switch(changeType){
case 0:
// Update you UI like above
// ...
if(mDrawerList != null){
mDrawerList.setSelector(R.drawable.list_selector);
}
break;
default: break;
}
}
NOTE: If you have list_item.xml and an adapter:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutItem"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textViewNavItem"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout
Then get a reference to the parent item
LinearLayout linearlayoutItem = (LinearLayout) findViewById(R.id.linearLayoutItem);
and dynamically change the background of this with the above approach...
linearLayoutItem.setBackground(R.drawable.list_selector);
but create 6 different list_selector_num1.xml etc... and change this using the switch statement. but you need the adapter to have a method like
public void updateViewBackground(Drawable drawable) {}
which will be in your adapter to do this.
For a demo on this communication between Fragments & activities look at:
https://github.com/lt-tibs1984/InterfaceDemo
Fragments talk to the main activity via bundle and onPause()
Passing data between a fragment and its container activity