New to Android: Dynamically changing views - android

I'm trying to learn how to build apps for Android.
The first simple app, which will become a component of a bigger app I hope to build, is to have a button on the screen where, when tapped, it adds something new to the view.
For instance:
Imagine a layout that only has a button:
[Create!]
When that button is pressed, the view gets a new row added to it:
[Create!]
A Something!
Upon subsequent presses, more rows are added
[Create!]
A Something!
A Something!
And so on.
I've made a LinearLayout and placed the button in it, and have attached a click listener to it. That all works great. What I can't figure out is how to get a handle on the LinearLayout in the onClick function with which I'll addView() the new TextView that says "A Something!"
Am I on the right track? What basic thing am I missing? Thanks!

I think you are approaching this the wrong way. You should look into ListView and SimpleArrayAdapter. This will put the elements into a list format that users will be more familiar with. Google has some good examples that use this (like their Notepad example). Especially if you are new to Android, you should look at their demos to get you through the basics. you can find them here

This is from memory, so it may not be exactly right.
In your layout, you'll want to give the LinearLayout an id.
<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/namehere"
... >
Then in your code you'll use findViewById to get a reference to it.
LinearLayout ll = (LinearLayout) findViewById(R.id.namehere);
ll.addView(...);

Related

Duplicate a View Android

I would like to clone a View, graphicaly and functionaly (event). For exemple I have a button on my RelativeView, I get it with findViewById(View) and copy the info inside another button, and finaly put it (the clone) on the relativeView. Like that I could have two buttons and when I click on the copy it triggers the same event. I already did clone = findViewById(View) but the app crash. I don't know if it's possible. I would like to know also if it could be possible to make a generic class. Because the view could be a button but also a Text or An Image. I want to know it's possible to avoid to make three differents methods. But if I have to copy info by info, i will need to make three methods.I'm a begginer in Android ;).
Thanks for you answers.
you are smart Sir, unfortunately that is the way to go though, quick tip when you are implementing Button extends TextView so can always call text functions on a Button like View.setText(""), so forget about it being Button or TextView and go for a boolean attack on ImageView or TextView
hope i am smart too
I would recommend to implement custom views which have the desired properties.
Then you can do this:
LinearLayout myLayout = root.findViewByID(R.id.main);
CustomView myView = new CustomView(this);
myLayout.addView(myView);

How to handle adding/removing views in LinearLayout on Android

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 :)

Loading XML layout

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

XML dynamic creation

I want to create a XML layout dynamically but I had a question on doing so.
Say I have something like this (Looking at the layout from the "Outline" perspective):
-ScrollView
---Linear Layout(Vertical) (LL1)
-------Linear Layout(Horizontal) (LL2)
-----------Image View (IV1)
-----------Linear Layout(Vertical) (LL3)
---------------TextView
---------------TextView
So my question here is would I start with the most inner Layout (LL3) and add the 2 TextViews and then branch upwards (to LL2 then LL1 then ScrollView) with adding to the other views & layouts?
I believe you can do this several ways. I've not tried creating an entire hierarchy like this dynamically, but I've added buttons, radio-buttons, text-views and other Views this way several times. In those cases, I've just added new ones to the ones that already exist using AddView().
I think the easiest way is to just create it "top down", i.e. create the ScrollView first and add any settings, then add the other views to it downwards. I would typically do something like this:
// Call other methods to create the views first:
ScrollView myScrollView = createMainScrollView();
LinearLayout myHorizontalLayout = createLinearLayoutForAbc();
LinearLayout myOtherLayout = createLinearLayoutForXyz();
TextView myFirstTxt = (...)
(..etc..)
Now populate them in the right way:
myScrollView.addView(myHorizontalLayout);
myHorizontalLayout.addView(myOtherLayout);
(..etc..)
Note:
I do believe this should work, but I can not guarantee it; if the reference to an inner view is no longer correct after having been added to an outer view (Eg. myHorizontalLayout is no longer a valid ref to the actual view under myScrollView), you might not be able to add children to that inner view. Not sure about this, though.
(If so, you might try to fetch a new, correct reference using findViewByName() after adding each view, but I don't think that would be an optimal solution).
I would try the first way first - at least make a proof of concept, to see that you can add view in a hierarchy at least three levels deep. That should give you your answer. If it does not work, I suppose I would try adding them in the opposite order, as you suggest in your question, just to see if that works (maybe just switch the order in my second code block?).
Sorry for the imprecise answer, hope it is of some help anyway.

Android: Using my own customized layout to fill a scrollview

I've been learning and building an Android application from scratch and I've hit a wall.
I read on the android developer page about customizable UI components and figured I could use those to contain messages for a chat page in my application.
I have made the XML files but I have no idea how to call those layout files from my code.
At the moment I read my messages from an array and then go through a loop making a TextView and setting the necessary properties for it through code, then adding it to a RelativeLayout within a scrollView. This all works fine but I want my messages to display more information (such as time and if the recipient has received/read it), this is where the new XML files come in, they have the field and layout already set up, so I figured I could just call those, set some variables and be done with it.
As I've stated before, the problem is that I have no idea how to reference the layout files inside my code. I've already read all the articles on the android developer page concerning building your own UI elements and I've also googled my fair share, but apparently I'm either the only one who has thought of doing it this way, or I can't find the right keywords for it. So now I'm asking you guys. Thanks ahead for any answers you guys can give me.
Edit:
I've been doing some research, and I think what I need to do is write my own adapter to work with my own class of messages and my own XML layout for those messages. However I have no idea where to begin with this, since I can't find any good documentation on how to write your own adapters.
You could do the following, but that will create a new TextView ignoring whatever layout you had:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = new TextView(this);
textView.setText(message);
scrollView.addView(textView);
textView.setText(timeAndDate);
scrollView.addView(textView);
I've never personally used ScrollView, so I'm not 100% sure on the above functionality, but it matches my experience with LinearLayout and RelativeLayout.
Alternatively, since you have your customr layout already, you would do:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(messageTimeDate);
scrollView.addView(textView);
That's essentially how you could handle it. I'm not 100% sure if you could add textView more than once in either implementation. If that throws an exception, you could use an array list of text views, such as:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
ArrayList<TextView> textView = new ArrayList<TextView>();
textView.add((TextView)findViewById(R.id.textView));
textView.get(index).setText(messageTimeDate);
scrollView.addView(textView.get(index).getId());
Wherever I put textView for the id, you could use message or timeDate, and make two of each thing I referenced, then.
For those interested in how I did it (and if you are running in to the same problem yourself):
For every list I wanted to create with my own random set of data I created my own adapter which extends BaseAdapter. By simply overriding the getView method it's possible to inflate and fill the different parts of your listitems.
I used this tutorial to help me with creating my own adapters.

Categories

Resources