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.
Related
I am new in android.I have a simple doubt,Is there any way to get id of a widget from some layout without set it as content view ?? I am looking to invisible a view from a class .I used the code
View b = findViewById(R.id.id2);
b.setVisibility(View.GONE);
But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? please help me. Thanks in advance :)
You can inflate the views separately and then use findViewById on the root of the inflated layout.
// inside of Activity, you can use 'this' for the context
LayoutInflater inflater = LayoutInflater.from(context);
View root = inflater.inflate(R.layout.some_layout);
// from your example
View b = root.findViewById(R.id.id2);
b.setVisibility(View.GONE);
At this point however these views are not part of your Activity's view hierarchy and will not be seen by the user. You will have to add them either by using setContentView(root) or by finding a ViewGroup in the current view hierarchy and calling viewGroup.addView(root).
You can do it this way
Button filterButton = new Button(YourActivity.this);
filterButton.setVisibility(filterButton.GONE);
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
See below link :-
Why findViewById() is returning null if setcontentview() is not called?
'But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? '
You have to create dynamic view then you do not have need to call setcontentview.
how to set setContentView?
Is there a way to set setContentView(int id) dynamically?
I'm new to Android and find it brutal (there seems to be an near infinite number of details and dependencies to remember).
Anywho, I got the TextSwitcher1 example app working, which uses ViewSwitcher. I'm assuming ViewSwitcher is the way to go, need to either display a map or a table, user can pick, and switch back and forth.
So I created my MapActivity in another application, seems to work. Next integrate into main app. So, call
View v = findViewById(R.layout.mapview);
and then
mSwitcher.addView(v);
except "v" is null. Why? Do I create the activity? But I don't want to show it yet. Is there such a call as "create activity but hide it until needed"? Or am I barking up the wrong tree?
Thanks for any insight.
The findViewById function returns a View based on an ID resource (R.id.something) for whatever view you have loaded in your activity (using setContentView(R.layout.main)). In your sample code, you're using a layout resource (R.layout.mapview). You should inflate the XML file, which will return a View that you can use to add to the ViewSwitcher.
Example Code:
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);
However, you should be able to define everything in your XML file and not have to manually add the pages to your ViewSwitcher. Here's some example code on how to do that: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/
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);
I am trying to register a context menu in a skeleton app's OnCreate():
/** Called with the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.skeleton_activity);
View v = findViewById(R.layout.skeleton_activity);
registerForContextMenu(v);
// Find the text editor view inside the layout, because we
// want to do various programmatic things with it.
mEditor = (EditText) findViewById(R.id.editor);
// Hook up button presses to the appropriate event handler.
((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);
mEditor.setText(getText(R.string.main_label));
}
The debugger tells me that findViewById(R.layout.skeleton_activity) returns null.
#CommonsWare solution to a similar post is to Wait until onFinishInflate(). However, in the sample project he provides, it doesn't seem that he waits until onFinishInflate.
My questions:
Can registerForContextMenu() wait
until onFinishInflate()?
If so, how do I do so?
This line is not correct its asking for id and you are providing layout
View v = findViewById(R.layout.skeleton_activity);
Instead if you want to have object of your root layout element then provide it some id and then try something like this
View v = findViewById(R.id.root_element);
I think you should use
View v = findViewById(R.id.skeleton_activity);
instead.
For the 2nd question, sorry, I 've no idea. Hope to see someone else's answer.
You shouldn't need to wait for the content to inflate in an Activity.
One problem is that findViewById takes an ID (R.id....) when you provide it with a layout (R.layout...). Can you try the following instead, to reference the Activity's root view?
setContentView(R.layout.skeleton_activity);
View content = findViewById(android.R.id.content);
registerForContextMenu(content);
i think the code you have shown is very confusing. Here is a good article http://blog.sptechnolab.com/2011/02/10/android/android-contextmenu-submenu/. In my case it works, i hope you can solve your problem.
This is a dumb question and I know the answer is sitting in front of me, I'm just having trouble searching for it in the right way.
I've got a custom view that has been set as the content view and inflated from xml. How can I access the instance to call methods on it from the activity class? I remember seeing something akin to getResourceById() a while back, but now I can't seem to find it and I'm not even sure if that's the best way to do it.
Sorry for the dumb question.
If you have used an inflater, you will be given an instance of a View class. You then use your instance like so
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.small_listview_row, null);
TextView tvItemText = (TextView)row.findViewById(R.id.tvItemText);