change textview background until select another textview in android - android

I have to develop an android application.
I have using below textview:
<TextView
android:id="#+id/deals"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:background="#drawable/best_deal_bg"
android:text="Deals"
/>
This is background of these textview:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/menu_active" android:state_enabled="true"
android:state_pressed="true" />
</selector>
Here .,If i have to click these textview means that tiime ly shows background.
But i wish to need the output like.have to shows background until select another textview.
How can i do ?? please give me solution for these ???
EDIT:
i have set that background on android textview onclik functionality:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
deals.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
*07-11 10:07:50.593: E/AndroidRuntime(816): java.lang.NoSuchMethodError: android.widget.TextView.setBackground
*
EDIT:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
View lastclickedview=null;
if(v != lastclickedview) {
v.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
lastclickedview.setBackground(getResources().getDrawable(R.drawable.message_icon));
}
lastclickedview = v;
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
E/AndroidRuntime(883): java.lang.NoSuchMethodError: android.view.View.setBackground
What's worng in my code.pls give me solution for these ???

Dynamically change it in your code on selection of another TextView use
yourTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.best_deal_bg));
and if you want remove the background use
yourTextView.setBackground(null);

first change your textview's background color from your onclickListener and create a view object to save last clicked text view, in second time click on the texview check change the backgound of last clicked view to deafault color
textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v != lastclickedview) {
v.setBackground("new color");
lastclickedview.setBackground("default");
}
//your Action
lastclickedview = v;
}
});

Related

Layout as selector item in Android

I need to use text view as CheckBox background and set different color for each state. Some how I managed to get it with Java. But I don't think it is the
proper way. Is there any other method to achieve this?
Implement onClickListener to your textview, something like:
int ispressed = 0;
TextView txtview = (TextView) findViewbyId(R.id.textview1);
txtview.setOnClickListener(new new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ispressed=0){
//CHANGE BACKGROUND COLOR
ispressed=1;
}else{
//RESTORE BACKGROUND COLOR
}
}
});

Android: Change image on many imageviews when clicked/active

I have noticed many similar answers but unfortunately I'm having trouble using any of them. I am a few different imageviews as buttons:
ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
ImageView filterSports = (ImageView) kati.findViewById(R.id.filter_sports);
filterSports.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
ImageView filterEconomy = (ImageView) kati.findViewById(R.id.filter_economy);
filterEconomy.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
Each of them is using an xml selector as resource, such as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/world_active" android:state_selected="true">
</item>
<item android:drawable="#drawable/world_active" android:state_focused="true">
</item>
<item android:drawable="#drawable/world_active" android:state_pressed="true">
</item>
<item android:drawable="#drawable/filter_white_world">
</item>
And the layout xml:
<ImageButton
android:id="#+id/filter_world"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:background="#drawable/world"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
But the problem is that every button remains on "active" state only when it is clicked, and not when spesific content is shown. Also I would like each button to return to its default state when another button is clicked.
Thank you in advance!
Edit:
with your help I have managed to make the button change on every click, but now the content does not change. There seems to be a colfict between the onclick listeners. Here is my code:
final ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
final ImageView filterSports = (ImageView) kati.findViewById(R.id.filter_sports);
filterSports.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
filterWorld.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.world_active);
filterSports.setImageResource(R.drawable.filter_white_sports); }
});
filterSports.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.filter_white_world);
filterSports.setImageResource(R.drawable.sports_active); }
});
You can do this without using selecter..
simply implement the click listner on each imageview.
and change the backround resource on all imageview.
filterWorld.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterWorld.setImageResource(R.drawable.world_active);
filterSport.setImageResource(R.drawable.filter_white_world);
filterEconomy.setImageResource(R.drawable.filter_white_world);
}
});
filterSport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterSport.setImageResource(R.drawable.world_active);
filterWorld.setImageResource(R.drawable.filter_white_world);
filterEconomy.setImageResource(R.drawable.filter_white_world);
}
});
filterEconomy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
filterEconomy.setImageResource(R.drawable.world_active);
filterSport.setImageResource(R.drawable.filter_white_world);
filterWorld.setImageResource(R.drawable.filter_white_world);
}
});
When you press a button, it will go to active state and you have to set it to default state when an another button is pressed.
So set buttons other than which is currently pressed to default state by using this
ImageView filterWorld = (ImageView) kati.findViewById(R.id.filter_world);
filterWorld.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this)
filterSports.setSelected(false);
filterSports.setPressed(false);
filterEconomy.setSelected(false);
filterEconomy.setPressed(false);
) ;

How to change color of button when being click,and revert back to default color in next click?

I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hit the like button,the button will change back to default color which is white.
I checked for few SO question,but still havent get what I want.So far my solution is like below,doesnt produce any error but when clicked the button,nothing happen.
likeButton =(Button) view.findViewById(R.id.likeButton);
//here for user like the post
holder.likeButton.setOnClickListener(new View.OnClickListener() {
boolean clicked = true;
#Override
public void onClick(View v) {
if(!clicked){
holder.likeButton.setBackgroundColor(Color.RED);
clicked = true;
//here i will update the database
}else{
holder.likeButton.setBackgroundColor(Color.WHITE);
clicked = false;
//here i will update the database
}
}
});
I checked this SO answer too,so I modified my code as below,but still nothing happens when the button is clicked.
holder.likeButton.setBackgroundColor(Color.WHITE);
holder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null;
#Override
public void onClick(View v) {
if(buttonColorAnim != null){
buttonColorAnim.reverse();
buttonColorAnim = null;
//here i will update the database
}else{
final Button button = (Button) v;//here is the line I dont undestand
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.WHITE);
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
//here i will update the database
});
buttonColorAnim.start();
}
}
});
Somebody please point out what I'm missing,what I want is change button color programmatically when being click for 1st time,and change back to default for next click(which avoid multiple like from a same user).
You should create a selector file. Create a file in drawable folder like color_change.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#color/button_focused"/> <!-- focused -->
<item android:drawable="#color/button_default"/> <!-- default -->
</selector>
and declare it in the button like this
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/color_change"
android:text="Click Me" />
Hi try to this hope this can help you...
In XML
<Button
android:id="#+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="click"/>
In Adapter Class
boolean click = true;
holder.btnClick.setTag(position);
holder.btnClick.setId(position);
holder.btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (click) {
holder.btnClick.setBackgroundColor(Color.RED);
click = false;
} else {
holder.btnClick.setBackgroundColor(Color.WHITE);
click = true;
}
notifyDataSetChanged();
}
});
Instead of clicked or not condition, make and use condition from you update database for like and dislike operation. So, In click-listener get data previously user like or not then change background and update database as per new click.
Try adding this line to your row.xml file in the main layout :
android:descendantFocusability="blocksDescendants"
Have a look at this. Here, I changed the button text color on click. First time, all buttons appear white and after click it toggles between red and white as you expected. --
//LikeAdapter.java
public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeHolder> {
public LikeAdapter() {
}
#Override
public LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
return new LikeHolder(view);
}
#Override
public void onBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {
holder.red_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.red_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
} else {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
}
}
});
holder.white_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.white_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
} else {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
}
}
});
}
#Override
public int getItemCount() {
return 5;
}
public class LikeHolder extends RecyclerView.ViewHolder {
private Button red_btn, white_btn;
public LikeHolder(View itemView) {
super(itemView);
red_btn = (Button) itemView.findViewById(R.id.red_btn);
white_btn = (Button) itemView.findViewById(R.id.white_btn);
red_btn.setBackgroundColor(Color.RED);
white_btn.setBackgroundColor(Color.WHITE);
}
}
}
//like_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button
android:id="#+id/red_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Red"/>
<Button
android:id="#+id/white_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="White"/>
</RelativeLayout>

Android Set button color on first click, unset on second click

please i need help on this.
I have searched here but the answers i have seen are not working for me, the posts being old, the functions are mostly deprecated.
I am trying to set the color of buttons on a single click in order to highlight them and unset the color on a second click. It's like making some choice from a number of buttons, and if I click on a selected button again maybe after changing my mind on my selection, the color should revert to the default. So that i am only left with the selected buttons highlighted.
The buttons are generated with an adapter in gridview and the onclicklistener applies to all of them.
The code i'm using is as shown:
public class ButtonAdapter extends BaseAdapter {
private Context context;
public View getView(int position, View convertView, ViewGroup parent)
{
final Button btn;
if (convertView == null) {
btn = new Button(context);
btn.setLayoutParams(new GridView.LayoutParams(40, 40));
btn.setPadding(2, 2, 2, 2);
}
else {
btn = (Button) convertView;
}
//exus
btn.setText(Integer.toString(gridNumbers[position]));
btn.setTextColor(Color.BLACK);
btn.setId(position);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//btn.setBackgroundColor(Color.GREEN);
//Toast.makeText(getBaseContext(), "Button clicked", Toast.LENGTH_LONG).show();
if (v.getSolidColor()!=Color.GREEN)
{
btn.setBackgroundColor(Color.GREEN);
}
else
{
btn.setBackgroundColor(Color.GRAY);
}
}
});
return btn;
}
}
}
My XML:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="8"
android:columnWidth="20dp"
android:stretchMode="columnWidth"
android:gravity="center" />
You can use a list of boolean properties instead of doing this.
Set a public boolean list in your class (it should be public and outside of any functions otherwise the onclicklistener will have error)
List<boolean> blist=new Arraylist<boolean>(Size);
//Size is maximum number of buttons
int index;
Then whenever you create a new button add this:
blist.add(index,false);
index++;
in the onclicklistener; find the index of the button from its position and save the index in an integer named pos.
if(blist.get(pos)==false)
{
//not clicked yet
blist.remove(pos);
blist.add(pos,true);
//here write the code u need for this if
}
else
{
blist.remove(pos);
blist.add(pos,false);
//todo: ur code for else
}
I tried this way and it worked for me,if you want to change on click
counter = 1;
//By Default set color
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1)
{
// Default color
counter = 2;
}
else
{
//your color
counter = 1;
}
}
});
View reused in GridView. So, you should define state for your buttons in your base adapters.
Take an ArrayList that will hold your selected index and remove it when grid is not selected.
Ex:
ArrayList<Integer> selectedItems;
In Construtor
selectedItems = new ArrayList<Integer>();
In OnClickListener
public void onClick(View v) {
if (selectedItems.contains(new Integer(position))) {
selectedItems.remove(new Integer(position));
notifyDataSetChanged();
} else {
selectedItems.add(new Integer(position));
notifyDataSetChanged();
}
}
In getView():
if (selectedItems.contains(new Integer(position))) {
btn.setBackgroundColor(Color.GREEN);
} else {
btn.setBackgroundColor(Color.GRAY);
}
Use toggle button insted of normal button. Like
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/check" //check.xml
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
and then make an xml file check.xml in drawable folder something like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/unselected_image"
android:state_checked="false"/>
</selector>
refrence.

how to make android chronometer invisible and visible when clicked

I am working on app which has a titlebar with chronometer on the left and a textview centered in a RelativeLayout.
RelativeLayout take height of textview and fill screen width
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/titlecontainer"
android:orientation="vertical"
android:padding="5dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="#color/titlebckgrnd">
I want to hide the chornometer when user clicks on it and unhide it user clicks again.
How this can be achieved?
EDIT, further to the answers and comments:
Here is the color file code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebckgrnd">#FFD55A2E</color>
<color name="titletext">#FFFFFFFF</color>
</resources>
I used the following code as suggested
final TextView chron = (TextView)findViewById(R.id.chronometer);
((Chronometer) chron).start();
chron.setOnClickListener(new OnClickListener() {
private boolean mToggle = true;
#Override
public void onClick(View v) {
Log.d("Gaurav", "Invisible");
if(mToggle) {
Log.d("Gaurav", String.valueOf(chron.getCurrentTextColor()));
chron.setTextColor(R.color.titlebckgrnd);
mToggle = false;
}
else {
chron.setTextColor(R.color.titletext);
mToogle = true;
}
//chronImage.setVisibility(View.VISIBLE);
//v.setVisibility(View.INVISIBLE);
}
});
but the result is
and it does not respond to any more clicks.
LogCat Results
Even the debugger breakpoints show change in Textcolor value but color change in display does not happen.
If I properly understood your question you can do that way to hide the chronometer:
// We are in your Activity:
final View chronometer= findViewById(R.id.chronometer);
chronometer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
chronometer.setVisibility(View.GONE);
// Use View.INVISIBLE if you would like to keep the room for this view
}
});
But there will be no view displayed anymore. So depending of where the user should click to have it displayed again, you may have to give your view and/or its text the same color as the background instead of making them invisible:
// should chronometer be a TextView that displays the time:
final TextView chronometer = (TextView) findViewById(R.id.chronometer);
chronometer.setOnClickListener(new View.OnClickListener() {
private boolean mToggle = true;
public void onClick(View v) {
if (mToggle ) {
chronometer.setTextColor(Color.BLACK);
mToggle = false;
}
else {
chronometer.setTextColor(Color.WHITE);
mToggle = true;
}
}
});

Categories

Resources