I want to change the Background Color Green OF the Recycler View data Whose Status IS Ture and Set Backround Color RED whose status is False.
The Round One Status is True and rest are False.
So can any one tell me how i Differentiate ...
My Recycler View code Write Below.
#Override
public void onBindViewHolder(DocRecycleAdoptor.MyViewHolder holder, int position) {
final EmpAttandance empAttandance = mDocs.get(position);
holder.txtDocID.setText(empAttandance.getDOC_ID());
holder.txtEmpID.setText(empAttandance.getEMP_ID());
holder.dateTime.setText(empAttandance.getDATE()+" "+empAttandance.getTIME());
holder.txtCoID.setText(empAttandance.getCO_ID());
holder.txtStatus.setText(empAttandance.getSA());
holder.empName.setText(empAttandance.getEmpName());
if(empAttandance.getStatus() == "True"){
holder.itemView.setBackgroundColor(Color.GREEN);
}else{
holder.itemView.setBackgroundColor(Color.RED);
}
}
Now it Shows like This.
But I want When Status is False Color Will Red.
it didit hapen.
top three Status are True And Other is false.
Showing all in same color
In your onBindViewHolder add below check
if(empAttandance.getStatus()){
//set your desired color on textview or a view for status true
}else{
//set your desired color on textview or a view for status false
}
#Override public void onBindViewHolder(DocRecycleAdoptor.MyViewHolder holder, int position) {
final EmpAttandance empAttandance = mDocs.get(position);
holder.txtDocID.setText(empAttandance.getDOC_ID());
holder.txtEmpID.setText(empAttandance.getEMP_ID());
holder.dateTime.setText(empAttandance.getDATE()+" "+empAttandance.getTIME());
holder.txtCoID.setText(empAttandance.getCO_ID());
holder.txtStatus.setText(empAttandance.getSA());
holder.empName.setText(empAttandance.getEmpName());
// this is it
holder.rootLayout.setBackgroundColor(empAttandance.getStatus() ? GREEN : RED);
}
inside your activity_docattd_list_items add linear layout.
add inside linearlayout a drawable #drawarble/selector_gray_view.
#selector_gray_view.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/gradient_gray_light"
android:state_pressed="true"/>
<item android:drawable="#color/clicked"
android:state_focused="true"/>
</selector>
and after that add drawble #gradient_gray_light.
#gradient_gray_light.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#30888888"
android:endColor="#30888888"
android:angle="90" />
</shape>`enter code here`
and after that add another drawable ,
click inside your selector_gray_view.xml,
#drawable/color.
and also intialize linearlayout in myviewholder.
use it inside onbindviewholder like as:
holder.linearlayout.setonclicklistener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
);
happy coding.
In ANdroid == are Not working. so i use empAttandance.getStatus().equal("True") instead Of == and its working Fine.
Thanks to All of you Guys for helping me out. much appreciate.
if(empAttandance.getStatus().equal("True")){
//set your desired color on textview or a view for status true
}else{
//set your desired color on textview or a view for status false
}
Related
The Image of the issue
Before clicked, the view of "setOnClickListener" is like the right picture
The action of code setOnClickListener:
card_sales.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view.startAnimation(fade);
if(m_inventory.equals("1")){
card_sales.setBackgroundColor(getResources().getColor(R.color.white_greyish));
Intent profil = new Intent(getActivity(),InventoryActivity2.class);
startActivity(profil);
}else if(m_inventory.equals("0") || m_inventory.equals("") || m_inventory == null){
Toast.makeText(getActivity(),"Access Denied",Toast.LENGTH_LONG).show();
}
}
});
then i press back button from the activity
#Override
public void onBackPressed() {
finish();
formstock.setVisibility(View.VISIBLE);
}
and then the "setOnClickListener" view is like on the left picture.
Please, give me solution for this issue :(
You shouldn't set a fixed color when button clicked with setBackgroundColor. instead use selector for background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="#color/white_greyish" />
</item>
<item>
<color android:color="#color/white" />
</item>
</selector>
Save this file in drawable folder and set this in XML for card_sales
android:background="#drawable/filename"
imho even better effect (ripple) you can achieve with default selector
android:background="?android:attr/selectableItemBackground"
btw. when you call finish() then this Activity gets destroyed, so its unnecessary to call formstock.setVisibility(View.VISIBLE);. if you really need to show something at that moment and with above line it works as you expected - you have very wrong architecture and some memory leak for sure
I've created a spreadsheet in Android to display the results of a data search using TableRow. The TableRows are dynamically created so there can be up to 30 rows displayed at a time.
I'm trying to toggle the background color of a row when the user touches the row, and can easily change the background color with an android:onClick event. However once the row is clicked and the color changes I cannot toggle the color back when another android:onClick event is initiated.
So how do I determine the color state of the TableRow so I can construct the proper control flow and toggle the color appropriately? There are plenty of examples of how to set the background color, but nothing helpful in GETTING the background color.
Thanks
Try this:
// save old view style
Drawable defaultViewStyle = view.getBackground();
// set new style
view.setBackgroundResource(R.drawable.new_view_style);
// return default view style
view.setBackgroundDrawable(defaultViewStyle);
But I think it's not the right way. It is best to override all of the possible conditions for the view:
// res drawable my_table_row_style:
<?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="#drawable/my_table_row_style_selected"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/my_table_row_style_selected"/>
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/my_table_row_style_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/my_table_row_style_default"/>
</selector>
Example:
int MyPosition=100000;
...
...
onclick()
{
if(MyPosition==100000)
{
//first time selection
MyPosition=a//a=any int position
//change bg of selected row
}
else
{
//now u r selecting second or N th time
//so first set previous bg of MyPosition
//and clear MyPosition
//Now set bg of current bg nd set MyPosition
}
}
You can save the color you set by setTag() and retrive it by getTag().
void OnClick(View v)
{
Integer color = v.getTag();
if (null == color) // not clicked yet
{
color = COLOR_NOT_TOGLED;
}
if (COLOR_NOT_TOGLED == color)
{
color = COLOR_TOGLED;
}
else
{
color = COLOR_NOT_TOGLED;
}
v.setBackGround(color);
v.setTag(color); // save
}
UPDATE
Ok, let's fix it))
void OnClick(View v)
{
Object o = v.getTag();
Integer color = (null == o) ? COLOR_NOT_TOGLED : (Integer) o;
if (COLOR_NOT_TOGLED == color) {
color = COLOR_TOGLED;
} else {
color = COLOR_NOT_TOGLED;
}
v.setBackgroundColor(color);
v.setTag(color); // save
}
If you take a look in source code of setBackgroundColor() you see how it works:
TableRow tr = (TableRow)v;
Drawable bg = tr.getBackground();
int oldColor = ((ColorDrawable) bg.mutate()).getColor();
I'm sure this is answered somewhere, but I can't find it.
How would I go about making a textview gradually brighten/highlighted during an OnLongClickListener? I have the following code:
jObjTv1.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(root,"Test", Toast.LENGTH_SHORT).show();
return true;
}
});
Which works just showing me the toast after the long click, but I'd like the textView to be highlighted while being "clicked".
I guess you will have to set a state list drawable as your background for your TextView
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/background_transition" />
</selector>
Some XML attributes of buttons (such as background, textColor, etc) can be defined with color or drawable state List like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/>
<item android:state_focused="true"
android:color="#ff0000ff"/>
<item android:state_enabled="true"
android:color="#ff00ffff"/>
<item android:color="#ff000000"/>
</selector>
When view state changes (pressed/unpressed, for example), corresponding color is changed automatically.
How can I prograqmmatically handle some kind of stateChangedEvent to perform more complicated layout change, than just changing a color (for example, change font size or set another text)?
For focus changes and touch events you can register listeners by setOnFocusChangeListener and setOnTouchListener. And changes about disabled/enabled states you can perform directly after changing your button state.
// use the selector method to pass your button and image
// you can use color also
b1=(Button)findViewById(R.id.button1);
// b2=(Button)findViewById(R.id.button2);
selector(b1, R.drawable.image_1_2, R.drawable.image_1);
// selector(b2, R.drawable.image_2_2, R.drawable.image_2);
}
public void selector(Button b,int pressed_image,int normal_image )
{
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(pressed_image));
states.addState(new int[] { },
getResources().getDrawable(normal_image));
b.setBackgroundDrawable(states);
}
Just override View.setPressed:
#Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);
...
}
Handler onTouch(View v, MotionEvent event) for specific views and perform actions in MotionEvent.DOWN / Up according to your requirement.
You have to take a reference to the View Object (button) via findViewById(<object_id>) and than use the appropriate methods from the API.
For example:
private Button aButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
aButton = (Button) findViewById(R.id.your_button_id);
//example 1
aButton.setVisibility(SomeExpressionEvaluation ? View.GONE : View.VISIBLE);
//example 2
if (SomeExpressionEvaluation) {
aButton.setText("Some Text");
}
and so on, just take a look at the API, especially inherited methods from the View class.
I must say you can use a (touch listner) this how u use a touch listner
image=(ImageView)findViewById(R.id.image);
find ur image first
Add a touch Listner to ur image
image.setOnTouchListener(image_onTouch);
//Add a touch method which is by name image_onTouch
OnTouchListener image_onTouch=new OnTouchListener(){
#Override
public boolean onTouch(View arg0,MotionEvent arg1){
int iAction=arg1.getAction();
if(iAction==0){
image.setImageResource(R.drawable.image1);
}
else{
image.setImageResource(R.drawable.image2);
}
return false;
}
};
// image 1 is ur image which u want 2 click and image 2 is the image when you touch that image you have to make an another image in which background color do u wanna show and use it in the code
I had also this kind of problem in past. I solved by putting this XML file in separate drawable folder in Res instaed of drawable-mdpi or else. And make sure that you have to give this Xml as your button's background.
hi dear i have 5 rows in my listview each listview have image and text associated with it ,i want when i click on listview at any position it changes its background color and when i click another position in list previosly selected list row comes to its former condition and selected change the color and image ...how to achive it ?thanks
my code is ..but it does not retain previously selected row...
public void onListItemClick(ListView parent, View v, int position,
long id) {
if(position == 0)
{
v.setBackgroundColor(Color.WHITE);
}
if(position == 1)
{
v.setBackgroundColor(Color.WHITE);
}
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
You can use the concept of selector. Create a selector xml and use it as the background of the row. it may help you.like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/selector_s" />
<item android:state_pressed="true" android:drawable="#drawable/selector_s" />
<item android:drawable="#drawable/selector_d" />
</selector>
you can use this link
http://android-journey.blogspot.com/2009/12/android-selectors.html