Strike out and unstrikeout in ListItem of a list view - java

I have a little functionality to strike out and unstrike out a list view when a list item is clicked. My code goes Here:
public void markComplete(View v)
{
Button b1 = (Button)findViewById(R.id.Completeit);
Button b2 = (Button)findViewById(R.id.InCompleted);
try{
TextView tv = (TextView)findViewById(R.id.textViewx);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}catch(Exception e){e.printStackTrace();}
b1.setVisibility(-1);
b2.setVisibility(1);
}
public void markInComplete(View v)
{
Button b1 = (Button)findViewById(R.id.Completeit);
Button b2 = (Button)findViewById(R.id.InCompleted);
try{
TextView tv = (TextView)findViewById(R.id.textViewx);
tv.setPaintFlags( tv.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}catch(Exception e){e.printStackTrace();}
b1.setVisibility(1);
b2.setVisibility(-1);
}
I have two buttons for checking and unchecking the list item.It works for the first list item alone. If i try to click 2nd list item, the striking & unstriking is done only on 1st item. Any help is appreciated and Thanks in advance

Your findViewById call is going to return the first element in your view that it finds. If you have multiple, then this is going to be a problem. It will always find the first table cell. The solution here is to save your buttons, and treat each cell as a separate view.

Related

The animation for my programatically created button acts weird in my application

So I am facing a weird bug I cannot explain - I cannot even reproduce it sometimes.
Basic context:
I have an application, which lists objects. Every object has a name and a point value. For every object, the addCustomSpinner function creates a "ticket" (a custom view, kind-of-spinner) and shows them in a scrollview so the user can select the one needed. There are four different 'containers' for four different kind of objects - so the layout can be populated with four kind of "ticket" package.
The data for the objects are collected from a database. The addCustomSpinner is called with a for cycle for every object in the database, and - Important - before the for method, the Layout it populates with the tickets is cleared (removeAllViews).
Inside addCustomSpinner, everything is created as "new" - like the button in question.
addCustomSpinner creates this button and adds a new onClickListener. Inside onClickListener, a new boolean is created - this is used to show a different animation when the button is clicked again. On first click (boolean = true), the arrow turns 180 degrees and faces upwards, on second click (boolean = false) the arrow turns 180 degrees and faces downwards. Works like a charm, until...
The bug I am facing:
Sometimes - as I already mentioned, not every time - if I click the button for one "ticket", then leave it 'opened' and click on an another one, and leave it 'opened' also, THEN I choose to populate the layout with a different kind of "ticket" package - The arrow faces upwards by default on every ticket in every package! Sometimes - again, just sometimes - with the same pattern I can turn it back, but it happens just "by accident".
I don't understand how the animation and state of the buttons can be connected, if every created ticket is new, every button is new, every onClickListener is new, and every boolean inside onClickListener is new. And if these are connected somehow, then why can that be that every behavior is "unique" for the buttons, nothing else shows any connection - even this is just a "sometimes" bug, a pretty rare one.
Can anybody help me why this happens?
What I tried:
Well, tried to trace the issue - but since it happens just by accident, I have no clue, I just searched if I can do anything else than the boolean to add different animation for the clicks. Sadly using ObjectAnimator is not a good solution for me - not the same result at least, since my animated arrow not only rotates, but it also changes its color. Shapeshifter seemed like a good idea to create animations easily, but now as I see it, maybe a simple rotation will be my ultimate solution.
Here's the code for the button:
customButton.setOnClickListener(new View.OnClickListener() {
boolean isCustomButtonClicked = true;
#Override
public void onClick(View v) {
if (isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue_back);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(View.VISIBLE);
isCustomButtonClicked = false;
} else if (!isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(GONE);
isCustomButtonClicked = true;
}
}
});
EDIT:
The full addCustomSpinner():
private void addCustomSpinner(Routes mRouteItemToAdd, String placeName) {
//creating a new View for my custom layout created in xml
View customRoutesView = new View(this);
LinearLayout.LayoutParams customViewParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
customRoutesView.setLayoutParams(customViewParams);
customRoutesView = LayoutInflater.from(this).inflate(
R.layout.custom_view_layout, routeLayout, false
);
//Setting up the views inside the custom view
ImageView imageViewDiffImage = customRoutesView.findViewById(R.id.routeDiffImageView);
TextView textViewRouteName = customRoutesView.findViewById(R.id.routeNameTextView);
TextView textViewRouteDiff = customRoutesView.findViewById(R.id.routeDiffTextView);
ImageButton customButton = customRoutesView.findViewById(R.id.customButton);
RadioButton climberNameOne = customRoutesView.findViewById(R.id.climberNameOne);
RadioButton climberNameTwo = customRoutesView.findViewById(R.id.climberNameTwo);
Button climbedItButton = customRoutesView.findViewById(R.id.climbed_it_button);
RadioGroup climberNameRadioGroup = customRoutesView.findViewById(R.id.climberNameRadioGroup);
RadioGroup climbingStyleRadioGroup = customRoutesView.findViewById(R.id.styleNameRadioGroup);
RelativeLayout routeWhoClimbed = customRoutesView.findViewById(R.id.routeWhoClimbedRelativeLayout);
imageViewDiffImage.setImageResource(R.mipmap.muscle);
textViewRouteName.setText(mRouteItemToAdd.name);
textViewRouteDiff.setText("Difficulty: " + (int) mRouteItemToAdd.difficulty);
climberNameOne.setText(climberName1);
climberNameTwo.setText(climberName2);
routeWhoClimbed.setVisibility(GONE);
//Here comes the button with the animated image
customButton.setOnClickListener(new View.OnClickListener() {
boolean isCustomButtonClicked = true;
#Override
public void onClick(View v) {
if (isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue_back);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(View.VISIBLE);
isCustomButtonClicked = false;
} else if (!isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(GONE);
isCustomButtonClicked = true;
}
}
});
//Button, works like an 'OK' or something, and I have no
//problem with this
climbedItButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int checkedNameButton = climberNameRadioGroup.getCheckedRadioButtonId();
int checkedStyleButton = climbingStyleRadioGroup.getCheckedRadioButtonId();
RadioButton checkedNameRadioButton = (RadioButton) findViewById(checkedNameButton);
RadioButton checkedStyleRadioButton = (RadioButton) findViewById(checkedStyleButton);
String checkedName = (String) checkedNameRadioButton.getText();
String checkedStyle = (String) checkedStyleRadioButton.getText();
addClimbToDatabase(user.getUid(), checkedName, mRouteItemToAdd, placeName, checkedStyle);
}
});
//And finally, I add this new "ticket" with the custom view to the layout i want to show it. Again, this also works like a charm, no problem here.
routeLayout.addView(customRoutesView);
}
Ultimately, I did not manage to understand the problem throughly, but I was able to eliminate it.
So during my fixing tries I narrowed down the problem to the animated drawable state - credit to #avalerio for his pro tip, but the answer wasn't addig an id to the button. I think somehow and sometime, the state of the first animation (turning the arrow 180 degrees) stuck in the end position - causing the other views using this animatedDrawable showing it in end position on start.
.reset() did not help, since it resets the animatedVectorDrawable object, not the animation xml drawable state. My solution is a kind of workaround, but it is working: when the custom-view 'ticket' is created with the animated-drawable-imagebutton, I set the imageResource of the button to a not-animated xml drawable - this drawable is basically the start position of my animated-drawable. This way, when the 'tickets' are generated, the imagebutton is 'hardcoded' in the start position.
Not elegant, but works. BUT(!) I would really appreciate if someone could explain to me how this weird behavior is possible - just sometimes, randomly, with no pattern I can reproduce intentionally.

Searching the First Character of a String to Pull Up Items From An Array

I have an app I'm working on in Android Studio. I have an array with a list of the 50 states.
The user is able to input one letter of text and then must press "search."
Once they press search, it should display all states beginning with the letter. For instance, if they search "k," they should see Kansas and Kentucky.
These results should be presented in a spinner.
I know I need to use
string_name.charAt(0);
somewhere in my code, but I'm not exactly sure where or what else needs to go with it.
Currently, when I run my app, no matter what letter I search for, the app displays "Alabama" in the spinner and then when you click the drop down for the spinner, it just shows all of the states.
Any help would be appreciated as I am fairly new to Java as well as StackOverflow, so I hope that I presented my question clearly and appropriately. Thank you to anyone who can help.
Here is part of my Java code.
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
states.add("Alabama");
states.add("Alaska");
states.add("Arizona");
states.add("Arkansas");
states.add("California");
states.add("Colorado");
states.add("Connecticut");
states.add("Delaware");
states.add("Florida");
states.add("Georgia");
states.add("Hawaii");
states.add("Idaho");
states.add("Illinois");
states.add("India");
states.add("Iowa");
states.add("Kansas");
states.add("Kentucky");
states.add("Louisiana");
states.add("Maine");
states.add("Maryland");
states.add("Massachusetts");
states.add("Michigan");
states.add("Minnesota");
states.add("Mississippi");
states.add("Missouri");
states.add("Montana");
states.add("Nebraska");
states.add("Nevada");
states.add("New Hampshire");
states.add("New Jersey");
states.add("New Mexico");
states.add("New York");
states.add("North Carolina");
states.add("North Dakota");
states.add("Ohio");
states.add("Oklahoma");
states.add("Oregon");
states.add("Pennsylvania");
states.add("Rhode Island");
states.add("South Carolina");
states.add("South Dakota");
states.add("Tennessee");
states.add("Texas");
states.add("Utah");
states.add("Vermont");
states.add("Virginia");
states.add("Washington");
states.add("West Virginia");
states.add("Wisconsin");
states.add("Wyoming");
final
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
When the button is clicked, at least you need to get the value in the input box, then choose what to add to that arrayList according to the first letter.
I didn't see your code getting the input text.
well you can also make a substring of the state string in a for loop and test with the input.The substring works with a start and end variable like
Result=states.substring(0,1), as this it will keep only the first letter

Android studio ImageButton change from one image to another

I was having some problems with the ImageButton. I have put 3 images in the drawable folder. The images are named e1, e2 and e3.
I have made it so that when you click a button the button image changes from e1 to e2. Can I make it so that the button changes to e3 if its already on e2?
Try this
int count = 0;
e1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(count == 0)
{
//Set Image 2
count++;
}
else if(count == 1)
{
// set Image 3
}
}
});
I think the easiest way is to handle this manually in the button click event.
Set the current drawble id for example in the button tag (button.setTag), and check the tag value on click (getTag()) to change it to another value.

using checkboxes to check data when button is clicked

me again, iv tried following the android checkbox example, and whilst it has worked the most part, i need the code to only be initiated when a button is clicked, there is only one button in the program and this button is also used to do the maths calcs.
I think i would need a nested if statement, but im just wondering if any of you guys could help me with the construction of this, when the checkBox is checked, i would like it to check if there is data in the text field next to the checkbox, if there is data i would like the program to bring up an error statement, although this is only to be done once the calculation button has been called on.
can anyone give me any help on constructing this, i have tried adding listeners but i already have one on the calc button and if i add more into this method, i get error messages :/
There are three checkboxes, all used to check different field, however i also only want one checkbox to be checked at any one time and only if there is no data in the text field that applies to this.
cheers guys, sorry if im a bit vague but im starting to loose my rag with my lack of knowledge.
cheers
public class Current extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
CheckBox checktime = (CheckBox) findViewById(R.id.custom1);
CheckBox checkcurrent = (CheckBox) findViewById(R.id.custom2);
CheckBox checkcharge = (CheckBox) findViewById(R.id.custom3);
public void onClick(View v) {
//i would like this part to check that checktime is checked and that there is no data in editText Time1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCurrent is checked and that there is no data in editText current1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, i mean it ", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCharge is checked and that there is no data in editText charge1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
// please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
// in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
// any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
First of all as it is onclicklistener for a button, v would always be button in your case. So ((CheckBox) v).isChecked() would give runtime error. Change that to checktime.isChecked() or any other checkbox you would like to check its checked status. As for checking the emptyness of editext it can be done by edittext.getText().toString().isEmpty()
public void onClick(View v) {
//i would like this part to check that checktime is checked and that there is no data in editText Time1
if(Time1.length()==0){
checktime.setChecked(true);
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCurrent is checked and that there is no data in editText current1
if(current1.length()==0){
checkCurrent.setChecked(true);
Toast.makeText(Current.this,
"Bro, i mean it ", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCharge is checked and that there is no data in editText charge1
if(charge1.length()==0){
checkCharge.setChecked(true);
Toast.makeText(Current.this,
"Bro, tryAndroid ", Toast.LENGTH_LONG).show();
}
// please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
// in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
// any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow
// EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
// EditText Time1 = (EditText)findViewById(R.id.number_input_3);
// TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
Just make sure that you findviewbyid all the edit texts before the button click.

Checkbox didn't work properly in ListView?

i'm having listview with some items. i placed a Checkbox to display the checkbox with that listitem. I've write onclick method to checkall the checkbox. But, it'll select the item which was in top of the list. What's the problem? Anyone clear me? Thanks in Advance. Sample Code : -
CheckBox c = (CheckBox)findViewById(R.id.checkBox1);
if (c.isChecked())
{
c.setChecked(false);
}
else
{
c.setChecked(true);
}
private OnClickListener checkAllCheckboxes = new OnClickListener(){
public void onClick(View v) {
ListView lv = getListView();
int size = getListAdapter().getCount();
boolean check = lv.isItemChecked(0);
for(int i = 0; i <= size; i++)
lv.setItemChecked(i, !check);
}
}
OR
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
android-unable-to-check-all-the-checkboxes-in-a-custom-listview-because-of-recy
Example2
with that snippet its difficult to point out the issue.
one, the listview reuses the view, maybe that while scrolling the same view is appearing else where.
here is a tutorial http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/
check it out. or post additional code. particularly, getView in the adapter code.
It happened because at calling of Adapter view it execute at the time of your list view.
So after the you clicked on check box (true) at same number of below screen will be true. for that you have to maintain the Vector.
if(tempVector.get(position)){
holder.box.setChecked(true);
}
else{
holder.box.setChecked(false);
}
try this after your clicking event.

Categories

Resources