I'm using TableLayout. On the click of a Button, I am supposed to hide it and show a progress bar and once the progress bar is done (e.g. downloading a file), I need to hide that and show a different button. The UI gets messed up, if I define these UI elements in the XML layout file. The Progess bar and new button shows as a new column. Any clue how I could dynamically hide and replace a button with progress bar and later a new button?
When you create your button use yourButton.setTag(Object) to tag them, later you can find your desired button with findViewByTag(Object) function.
Take a look at this: What is the main purpose of setTag() getTag() methods of View?
For example you can tag every of your buttons with string positionX + "," + positionY
Android allows the use of a setVisibility method call on Views.
Set up the xml where you want it, you are actually able to put things
on top of each other but i wouldn't recommmend it.
In your onCreate() method ensure that the fields/Buttons/Views are
allcreated but then set the visibility to
setVisibility(View.INVISIBLE); followed by setEnabled(false);
Once you have figured out your UI flow (the actual progression
through the process), determine where each view/button/field are to
be displayed or hidden and code in where this needs to happen.
To ensure that the flow works, and reduce the chance of a user
entering a state in-which your views are loading in at the wrong time,
create boolean variables and maybe getters and setters to create
logic rules that are applicable.
setVisibility(View.VISIBLE), setVisibility(View.INVISIBLE) are what you are looking for.
Related
I want to show an animation when a certain action happens in my application. This animation shall overlay a listview. From what I understand, this should be possible with a ViewOverlay, but I'm struggling with getting the basics to work currently.
What I tried is to simply display a vector drawable that I already have on top of my list view. In my activity's onCreate() I have the following code:
Drawable d = getResources().getDrawable(R.drawable.backspace);
d.setBounds(0,0, listview.getMeasuredWidth(), listview.getMeasuredHeight());
listview.getOverlay().add(d);
Shouldn't this display my drawable somewhere on top of my listview? For me it doesn't - what am I missing?
Solution: listview.post(() -> listview.getOverlay().add(d));
There was no layout pass happen yet at onCreate so your views are not measured yet. Defer overlay operation into post method - your view will have been measured at that time so you will have correct values to use.
I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.
For my project I need a functionality to dynamically add and remove views(textedit or buttons, etc).
I saw this similar functionality in Android "Add Contact" screen, where plus button add new fields and minus button delete the fields.
I found that EditContactActitivity.java is the file behind "Add Contacts".
I tried to find the methods that are called when plus or minus buttons are pressed but unable to find it, seems like "Add Contact" code is spreaded over multiple files. I am having difficulty understanding Android source code because documentation is unavailable.
Any advice?
You can add and remove views by calling .add() or .remove() on the reference to your main layout and passing the view you wish to add or remove;
Here is a simple example of an onCreate method that demonstrates adding and removing a button:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout myMainLayout = (LinearLayout)findViewById(R.id.yourMainLayout);
Button b = new Button(this);
//you can have some b.setXXX calls here to set text, view, click listeners etc...
myMainLayout.add(b);
//to remove
myMainLayout.remove(b);
}
I would consider researching Visibility of views rather than going through all this trouble. For example. I have an app where I have a 'record' entry screen that is relatively simple that appears as a Dialogs content. A few views/viewgroups are currently using visibility of gone, to not appear at all. If the user edits the record to add more detail, I launch an Activity that uses the same xml layout, but instantiates some of the currently 'gone' views and changes their visibility to 'visible'.
It is programmatically easy to toggle a view's visibility so I think it is really the way to go.
The only limitation I'm aware of here, would be the views order or position.
I have an Activity with a listView with few options and a button at the bottom of the screen. The listView is just to configurate some options so, when i click in any of the items in the list its needed to let the user choose between some options (in some cases i'll use another list to show the options, in other cases i'll let the user write in an editText view) to make the configuration.
It's recommendable to create new Activities to show this options or can i choose other way? I was thinking about loading a new .XML in the same Activity but im not sure if this is "a good practice".
Something like that:
Activity{
setContentView() --> The main XML
setOnItemClickListener{
switch between item's Id's and setContentView() depending on the item;
}
}
Thanxs!
#EDIT
I also have a question about declaring new classes. I've seen some tutorials declaring a custom Adapter class inside the main Activity. So, once again, is that a good way of doing things? :D
AFAIK, you can not use the setContentView() more than one time. It makes the conflict. But you cna achieve it using view's visibility change. That is you have invisible the current ListView and make visible the next view what you want to show.
You do not have to create a new Activity. For settings that are set through list of checkboxes or through a radio button selection, please check Android documentation for
AlertDialog
AlertDialog.Builder
Very easy and simple (and visualy acceptable) way to set some setting in your current Activity. Also, you can put some .xml in the DialogBuilder (through setView() function) and customize your Dialog that way (also possible to put EditText widget in the dialog to get some string).
I have a ListView backed by customized ArrayAdapter where each item has 2 LinearLayouts - one I call head, and the other one - body.
The body is hidden (gone) until user clicks on the row at which time it slides out. The second click (on the head) will hide the body. However if user clicks on the body it brings another activity. All of this works just fine, here comes the problem:
When user presses on body I want a visual indication of the action just the same way as regular list item will flicker an orange background when pressed. I'm not getting this by default since (my theory) the onPress event is intercepted by body's view and not processed by the list item.
The first thing I tried was to execute body.setBackground('#ff00ff') (never mind the color) in onPress event. That didn't work since (I suspect) there's no repainting after the call. Then I dig a little bit more and decided to use <selector/>-based background. I defined body_background.xml in drawable folder and assigned that to the body's background property.
There I noticed that background will only change if the even is processed by the list. For example if I set <item android:state_selected="true" android:drawable="#drawable/selected"/> then when I press on the head - the background of both elements (head and body) will change, however when I press on body - nothing.
So to summarize my question - how do I change background of the child element in the list item if I assign custom onClick handler to it? Any hints will be greatly appreciated
OK. Here's some more info I dig along the way.
I'm currently trying to switch implementation to ExpandableListView which provides the functionality I had to coded in (sliding body). The problem I may have here is that I have a fancy "endless" list implementation and I'm using ArrayAdapter#add method to dynamically add items to the list while scrolling (see my tutorial on androidguys.com) Well, the add method is missing from the BaseExpandableListAdapter so I need to see if adding items to internal array will work (it didn't for ArrayAdapter) possibly doing ExpandableListView#notifyChanged() will take care of that
The fact that I don't see anything when I'm directly using setBackgroundColor method is probably due to the subsequent call to startActivity call that halts all painting until new Activity is displayed
P.S. this has been resolved by switching to ExpandableListView. And may I add - it's beautiful! No need to invent anything