Suppose MyView is a class derived from View and mView is a variable of type MyView.
How to display mView when the android app is started?
Short answer: Like any other View, use setContentView(View v), although you will probably want to add it to a ViewGroup first (usually one of the many layout classes).
Long answer: Rather than answering the basics in each of your questions, I believe the best the community can do for you here is to point you to the sources to learn android basic programming. One of the best sources to start is the android.com website itself. Please, take your time to read the Android Developers Guide (link: http://developer.android.com/guide/topics/fundamentals.html).
Also, there are several questions here in SO that link to many resources to learn:
https://stackoverflow.com/questions/5456733/how-to-learn-android
https://stackoverflow.com/questions/949818/how-can-i-learn-android
https://stackoverflow.com/questions/2869338/where-to-start-to-learn-android
https://stackoverflow.com/questions/475152/how-can-i-learn-android
https://stackoverflow.com/questions/1114287/good-book-for-beginning-android-development
Also, in order to avoid getting close votes in your questions, please take a moment to read the FAQ and these tips
Let's say you have a LinearLayout in your main.xml.
In the onCreate method, you will have:
MyView mView = new MyView(this);
mView.setId(ID_OF_YOUR_CUSTOM_VIEW);
//modify next line as needed
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mView.setLayoutParams(layoutParams);
LinearLayout layout = (LinearLayout) findViewById(R.id.ID_OF_YOUR_LAYOUT);
layout.addView(mView);
Related
I am learning android development from a course by Udacity.While I was going through the Lesson 2,there came a situation where we had to create multiple Textviews,set the text from previously created ArrayList of strings and add those Textviews to Linear layout.
Common code:
ArrayList<String> words = new ArrayList<String>();
words.add("one");
words.add("two");
words.add("three");
words.add("four");
words.add("five");
words.add("six");
words.add("seven");
words.add("eight");
words.add("nine");
words.add("ten");
LinearLayout rootView =(LinearLayout) findViewById(R.id.rootView);
Now what they did:
for(int i=0;i<10;i++){
TextView wordView=new TextView(this);
wordView.setText(words.get(i));
rootView.addView(wordView);
}
What I did:
ArrayList<TextView> wordView = new ArrayList<TextView>();
for(int i=0;i<10;i++)
{
wordView.add(new TextView(this));
wordView.get(i).setText(words.get(i));
rootView.addView(wordView.get(i));
}
Now,my question is weather my way to approach the task has more memory overheads than their way?
I feel my code is better because I have reference to each TextView even after the loop.
No difference. In both cases TextView instances could not be garbage collected, so memory foot print is equal.
because I have reference to each TextView even after the loop.
If you don't really need these references then it's just senseless. It's not memory related advantage.
Your code looks good. But I don't see a point on having 10 the same TextViews. I would consider using a listView or recyclerView if you are using it as a list and thinking about optimising your code.
But still your code is fine.
It doesn't matter performance wise.. Just like you said:
I feel my code is better because I have reference to each TextView even after the loop.
If you need that, do it..
I will start by saying this, while I have some Java training, is my first foray into development for Android.
What I have done is created a custom ImageButton called MapCell that is basically an ImageButton that holds a few extra pieces of information, and it compiles fine.
My problem comes when I want to procedurally create a MapCell in the relevant Activity and add it to my TableLayout which is defined in the xml and has the id 'mapTable'. The relevant bit looks like this:
Random randy = new Random();
MapCell n = new MapCell(randy.nextInt(4), this); //the random number is part of my extra info
findViewById(R.id.mapTable).addView((View)n, 20, 20); //add cell to display
I get one error out of that:
The method addView(View, int, int) is undefined for the type View
Which to me sounds like utter nonsense. I put that View cast in there as desperation after I got this same error with n sitting by itself and nothing changed (Obviously my MapCell is already a View since it extends ImageButton).
I hope a new pair of eyes can tell me what this is about, since I've checked for similar problems and I didn't find any quite like this. Let me now if you need to see more code.
The method findViewById returns a View and the View class doesn't have the method addView(this method is implemented in the ViewGroup and its subclasses). Instead you should write:
((TableLayout)findViewById(R.id.mapTable)).addView(n, 20, 20);
I've cast the return of the findViewById method in a class that actually has the addView method.
You got this problem because method findViewById(R.id.mapTable) returns View object.
In android you can't add one View to another.
You can use addView function with ViewGroup, and all LinearLayout (etc.) objects.
I would like to create dynamic table in android (custom number of rows and columns). Minimum sdk is 3.0
I suppose to crate it via one of 2 ways:
1) via creating new TextView
TableRow tr = ....;
for ( i = 0; i < NumOfRows .... ) {
TextView tv = new TextView(this);
tv.setLayoutParams(...);
tv.setText("Text I wanna to see");
tr.add(tv);
}
2) via inflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for ( i = 0; i < NumOfRows .... ) {
TextView tv = (TextView) mInflater.inflate(R.layout.my_cell_layout, null, false)
.findViewById(R.id.my_cell_item);
tv.setText("Text I wanna to see");
tr.add(tv);
}
3) Your way :)
What is faster? What should I select?enter code here
It's all as per your requirement that which is better.
from link http://www.aslingandastone.com/2010/dynamically-changing-android-views/
Because layouts can be created either in XML or in code, you could probably make do without ever having to do dynamic XML layout loading. That being said, there are some clear advantages as to why I think one may want to do so:
Code cleanliness. Doing anything more than basic layouts in code can get very messy, very fast.
Code re-use. It’s extremely easy to inflate an XML layout into a specified view with one or two lines of code
Performance. Creating the objects necessary for an in-code layout leads to unnecessary garbage collection. As per the Android Designing for Performance article, “avoid creating short-term temporary objects if you can.”
Attribute availability. Defining Views in an XML layout exposes attributes that are not always available by object methods.
*
Possible disadvantages:
It make take more time to do an XML layout versus defining the layout in code, especially if there are only one or two interface elements that need to be changed.
*
To find out which is faster, implement both methods and use the method profiling utility, TraceView. That being said, its easier to maintain an XML file when making changes than it is to modify code. So, if I were you, I would prefer technique #2.
I have some problems with the TabHost and ViewFlipper.
Here are the ViewFlipper as I expect the answer to this will also do the job in the TabHost.
I would like to have a Custom Dialog shown when the user reach a certain stage, but I can not figure out which Context to hand it?
final Dialog congratsDialog = new Dialog(MyActivity.this);
congratsDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
congratsDialog.setContentView(R.layout.congrats_dialog);
TextView name = (TextView) congratsDialog.findViewById(R.id.congratsDialogName);
name.setText(player.getName());
This will result in a NullPointerException in the line were I try to setText.
I have also tried flipper.getContext(), getBaseContext(), getApplicationContext() and have also tried other crazy thing but every time I get a NullPointerException
setContentView() to dialog is trade off over android version if you are using android 2.0 or less it would not work use versions 2.0 or above for this function. Otherwise if you want to do for all version then use setContentView(View) where View is from xml layout of congrats dialog after inflating it.
Please try this and let me know if you got solution.
In my application I have 2 layouts. One's is root layout that changes dynamically and the second is just a form that opens many times. The form must be child of the root layout but I'm failing to perform so.
I assume that I should simply use:
main.AddView(formLayout)
but I can't figure out how get this formLayout object.
Will thank you for possible answers.
Sounds like you need the LayoutInflater object Android reference.
This allows you to create an object from the xml layout in your project.
With the advice of cjk I wrote piece of code that actually answers my question:
setContentView(R.layout.main);
main = ((ViewGroup)findViewById(android.R.id.content));
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup form= (ViewGroup) inflater.inflate(R.layout.formLayout, null);
main.addView(form);
Thank you all
Not sure if I understand the question properly, but something like that might work:
View myView;
myView = (View) this.findViewById(R.id.formLayout);
main.addView(myView);
By get I figure you mean you want to retrieve a field in the new opened layout, you can do it by making it a new Intent and using startActivityForResult instead of startActivity.