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.
Related
I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,
I have one of those screens where you fill user info, some other EditTexts etc. and send it to server. But there is section something like add note (just example). User can add as many notes as he want. At the beginning there is just button add note. When user press it then new TextView spawns above and he can fill it, or remove it. Then he can touch add note again and whole process repeats. You sure know what I mean. At the end I take all filed notes and send it to server.
I know that the way to do it is - on btn press inflate my TextView + delete btn layout and add it to linear layout. On btnPress then get its parent or whatever and remove it from layout.
However this is pretty annoying thing to do as it requires inflating, children views managing and all the other stuff. What I am asking is - is there some other way to do that? Maybe some library, or possibility to set adapter to linear layout etc.
Thanks :)
I have a problem loading previously created layout. I would like to load it and change text on buttons inside, then show it to the user. It will be quiz question and I have to show it many times during one activity. I don't want to create new class for my layout.
What do I have to use? I read something about Inflate class, but I think it is used only to create new classes. I tried setContentView() method, but app stops when method doing this load starts:
LinearLayout layout = (LinearLayout) findViewById(R.id.CapitalQuestionLayout);
setContentView((View) layout);
Can someone give some hints?
Try using the layout field not the id field, when you call from the R class, like so :
LinearLayout layout = (LinearLayout) findViewById(R.layout.CapitalQuestionLayout);
setContentView((View) layout);
For creating a "Quiz" app your basic requirement is:
a layout which has a TextView for Question and 4 Buttons for options.
a set of questions; pretty obvious :-).
You can create a Custom Class - Questions that will hold Text for a question and its associated options (as Strings).
Now, whenever you want to display a new question with different text for buttons just do the following:
If user clicks on right answer then display a Right-Answer-Activity to the user (that has a next-question-Button).
When user clicks on next-question-button you can display Question-Activity and populate the layout-views with a randomly picked question-object's attributes (i.e. Question's text and options' text).
Hope this helps.
This is quite common in Android apps. Do your fields/buttons have an id? For the parent activity, you mostly do
Button someButton = (Button) findViewById(R.id.the_id_to_the_button_you_want_to_change);
This allows you to do things like
someButton.setText("What is the ultimate answer to life, the universe, and everything?");
Look up the Andorid documentation if you want to do other things like set the color. If you have a viewgroup of some sort (RelativeLayout / LinearLayout / etc), you can specify that specific one
Button someButton = (Button)viewGroup.findViewById(R.id.awesomely_named_button_identifier);
The above someOtherButton is used more often with inflated viewGroups
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 would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).
If you can look # DroidRecord, they have a similar layout as I am looking to achive.
Thanks!
Chris.
As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:
android:focusable="false"
android:focusableInTouchMode="false"
See also How to fire onListItemClick in Listactivity with buttons in list?
Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).
ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.
what is your question? How to add a button to a list row?
Very simple, just as you expect it will be added to the row's layout.
Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me
Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?
Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.
Cheers
It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?
I would like to thank BoD for its hint on removing the focusable state to the button(s), it saved my day.
But for information, since the button is no more focusable - state_focused on < selector > xml - , its design won't display anymore to the user.
Those buttons will still get the state pressed though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !
Keep that in mind, it could not be a good solution for your own case, but it does work well.
I tried this to be able to click on the buttons but it didn't work for me
android:focusable="false"
android:focusableInTouchMode="false"
so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it.
after that you can add buttons to the layout and each button will be clickable.