So here is what my app looks like so far. Every time I click the "+" button I go into another activity where I enter description, date and time and dynamically create a horizontal LinearLayout. With the X button to the left I'm deleting said layouts with this code (I know it's not the best way but it works for me so far):
final Task toBeRemoved = x;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myLayout.removeView((ViewGroup) v.getParent());
Task.tasks.remove(toBeRemoved);
}
});
..while iterating through each element in a list where I store my values in my OnCreate method.
What I want to do now is make it so I can remove them with the assistance of the checkboxes and the "Clear" button as well.
I have added each layout dynamically, though, so I can't think of any way for me to determine which one I've checked for deletion. They have no id, they can't be stored anywhere so I can iterate through them, as far as I know. What can I do in this situation?
A couple of ways. One would be to set listeners on each new checkbox and keep a Set of views with a checked state (add to the Set when checked, remove when unchecked). Then when clear is pressed, you remove all views in that Set.
The other way is to lopp through all the child views of the parent layout above the dynamically. For each one, find the checkbox child via findViewById, and see if its check. Remove it if it is. This is computationally expensive if you have lots of complex views
I prefer method 1 myself, but either works.
I have added each layout dynamically, though, so I can't think of any
way for me to determine which one I've checked for deletion
that's not true. You can call setId( ) or setTag() while you are adding each layout,
They have no id, they can't be stored anywhere so I can iterate
through them, as far as I know. What can I do in this situation?
now they have one. You can either use findViewById or findViewWithTag
Here's a solution that I consider pretty cool.
Create a widget on your own.It's easy.
So, you would have a class that extends ...probably LinearLayout.Depends on your needs.
You create the button,the editText, the textview and the check box, than you add functionality like you want.
When the Check box is pressed, I suppose you want the X circle button to get activated and when you press it, it deletes the whole "thing".This way you won't even need the clear button.You will still need the add button though, so you can add how many of those items you need.
It's easy,practical and makes the code reusable, which is something I look for always.
If you need more help, I can help you in a bit more detail ,but I can't always be around so I am sorry if I will respond a bit slow.
Related
Okay so I am making an android application, and I have a list of goals let's say to make it easy to explain. A goal consists of a string. This data will be coming from a database so in the future, the length of the list may be slightly longer or slightly shorter. Each goal is placed inside the text of a button.
I have made this loop to create the buttons at run time due to the changing length of the list depending on whats happened before and this all works perfectly, i can create the buttons and they are placed where i want etc. This is the code I used to do this.
for (int i = 0; i < goals.size(); i++){
buttons[i] = new Button(this.getContext());
buttons[i].setLayoutParams(params);
buttons[i].setPadding(0,150,0,150);
buttons[i].setGravity(Gravity.CENTER);
buttons[i].setText(goals.get(i));
buttons[i].setBackgroundResource(R.drawable.btn_back);
buttons[i].setTextSize(30);
buttons[i].setTransformationMethod(null);
linear.addView(buttons[i]);
}
Now my issue is I want the click of each button to do a slightly different thing - depending on the goal ie, the text of the button. I really am struggling with setting up the click event so that different buttons will do different things even tho i have created all of them dynamically.
I can give more detail into the setup I am in in the android studio, but I feel I've given enough insight into my issue.
I appreciate any help.
My best guess is that if you already know what the function of each button should be according to the text, couldn't you just take the text of the button and then use it in switch (or something else) to get specific functionality like:
buttons[i].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//Put whatever is common between buttons outside of the switch
switch(((Button)view).getText().toString()){
//And then just assign a case for whatever functionality you need specifically for each goal
}
}
});
I have a problem regarding the back button feature together with RecyclerView, my goal is to emulate the behavior in the Contacts application, where you press and hold (long press) an item in the RecyclerView and a check box appears behind the item(s) (all of them). Then, when you press the back button, all the check boxes disappear.
Regarding the information I provided we can break the problem in two parts:
First, we need to solve the long click problem, which I suspect we can use
NotifyDataSetChanged() together with hiding the check boxes in the XML so that we can switch between checkbox.setVisibility(VISIBLE) and checkbox.setVisibility(GONE).
Lastly, and this is the hardest part for me, I would like that, when back button is pressed, all the check boxes disappear, instead of leaving the app.
Thank you in advance and sorry if the problem is not understandable, as this is the first time I am posting a problem and English is not my native language.
What you are describing is known as Contextual Action Mode.
You'll notice, that when you long press the item the toolbar (action bar) on top changes and shows the number of selected items and a set of action you can apply to the selected items.
Pressing the back button then cancels the action mode.
It is beyond Stackoverflow's scope to explain the whole action mode system, but you can simply search for it in the internet.
Here is a tutorial for beginners, for example.
I can give you a better answer if you post your code.
You'd probably want your adapter to contain a list of selected items. If the list has any elements, you show the checkboxes and check the ones that correspond to the list. If it is empty, you don't do that. You're correct about notifying data set changed. Long press on an item would add that item to this list. Then when your adapter re-laid out your items, it would show the checkboxes since that list would not be empty.
For the second issue, you'd want to override onBackPressed() in your activity. Then you can have some code like:
public void onBackPressed() {
if (adapter.hasItemsSelected()) {
adapter.clearSelection()
} else {
super.onBackPressed()
}
}
You'll need to write these adapter methods. hasItemsSelected should check to see if there are elements in the selected items list and clearSelection should clear the list and notify data set changed.
Hey so i've got a listview with buttons on it, when one is clicked if you skip down 4 more listview items the fifth shows it's been clicked in the same place the previous one was clicked. When a button is clicked it is set to invisible and the dummy button underneath is shown (which is the one that shows up in every fifth row).
Code that declares button invisible in the onclick command
boolean processClick=true;
if(processClick == true)
{
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}
processClick=false;
}
After looking at some similar questions, I think this has to do with the views being recycled, but still not sure how to remedy this.
Any helps at all is much appreciated.
Thanks!
Yes u are having problem with your code,you need to take processClick for each listView row/item and then on that bases u need to set enabled(false) or something else.You can take reference from this example which is similar work but with checkbox. You need to look at that code and apply with button. It'll solve your problem.
link of sample ex. https://drive.google.com/file/d/0B6C9Pqrvc_CWZHFDcmxKR01rc3c/edit?usp=sharing
I faced a similar problem when I was programatically assigning background color to my listview items. I dont know what caused the problem (probably the getView() was not getting called properly). I solved my problem by using if-else. In your case something like this should work:
if(processClick == true)
{//set desired result in if
myButton.setEnabled(false);
myButton.setClickable(false);
myButton.setVisibility(View.INVISIBLE);
}else{//reset items to original in else
myButton.setEnabled(true);
myButton.setClickable(true);
myButton.setVisibility(View.GONE);
}
In my app I dynamically create a list of buttons. The user should be able to click on them. A short click would select the buttom as the current item, while a long click should enter the editor for this item.
Now I wonder how I can determine and set the colors that i should use. My first idea was to simply read the background and textcolor and switch them, however I'm not sure if this would be really the appropriate way to do this. So I was wondering if there is a an appropriate way of how to retrieve colors.
Of course I could hardcode some colors, but I don't know what color scheme the user has set and they might not be visible in a good way.
Please note, that, since I have to create the buttons dynamically, I can not set it in the XML.
You could simply use a ToggleButton, so android will take care of marking a "clicked" button as selected.
Since ToggleButton is a View, it has a setOnLongClickListener(Listener)-method, which can be used to make the ToggleButton long-clickable.
This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly