Android change setContentView - android

I want to dynamically set the contentView in my activity.
Because one time i use a xml as contentView, but at anothertime i use a custom view as contentView.
But how do i change the contentView?
I read about that the ViewFlipper can do this, but a viewFlipper is implemented in a xml file. And within this ViewFlipper you can add your different views.
But i dont know them at the beginning, so i cant write them all in my xml file.
Do you have any idea?
Thank you

You can call setContentView at any time*, not just in onCreate. Just define all the views you want in separate XML files and pass the relevant id when it's time to switch. If you want to define the new layout dynamically in code, then do that and call setContentView and pass the root view of your new layout.
* Technically, you can call setContentView any time you are executing on the event thread. Otherwise you need to use a Handler to call it.

Related

<include/> set to default XML values

Hi I have a layout which has <include/> in it. I can access this include and change the visibility of some views in it or modify the edittext text.
So what I want is to create a method so that when I press a button the layout inside the <include/> would turn into its original XML state hiding what was hidden in XML and showing what was shown and to return the edittext to their default values from the original XML layout file.
Note that I am not using any fragments or changing the setContentView in my application.
Thank You So Much.
I found the best solution for this problem what I did is to copy the OnCreate code to another method thus it is callable then what I will do is setContentViewto be the layout that I want to turn it back to its default case then calling the method that contains the OnCreate code to return all the ids then using the include method I can show the layout inside another layout.

Reason for NullPointerException if setContentView() is not used

I know that we need to place setContentView() in the onCreate() method before initializing any view otherwise it will throw a null pointer exception.
But what is the reason for it?Is the setContentView() similar to the inflate() method?
before initializing any view
I do not know for certain what you mean by "initializing any view". Given the rest of your question, I am going to interpret this as meaning "call findViewById() on the activity".
You need to call setContentView() before calling findViewById(), because otherwise there are no widgets to find.
Is the setContentView() similar to the inflate() method?
setContentView() will use a LayoutInflater and inflate() under the covers, if you pass a layout resource ID into the setContentView() method.
If no content View is set then from where you will reference the views like EditText,TextView,ListVIew and all other components which you have used in your layout.
It is like you have items in your bucket and its cover is locked for safety, you came in house without bucket and forgot it in the car and your mom asked you to put items 1 by 1 on Kitchen counter , but you don't have bucket?? so first you will get bucket then you will take out items from it.
Simply first you have to have a Container in your activity so that you can reference its items by using their ID which are assigned in layout xml.
Hope it is clear to you.!
Ok. Agree with #CommonsWare. In some details, let say if you have some views defined in your xml layout file and you want to use those views in you activity, so in this case you have to call setContentView(<R.layout.xml_layout_name>) and after that to inititlalize view using findViewById(R.id.<view_name>) with resource name as your xml layout defined name.
Ok but why we have to call setContentView() ?
So when you call setContentView() application activity means android nutshell will render views and prepare view hierarchy for your activity from layout file. Just remember you have defined views in layout using xml file and you are using those views in Java code, so for that all works to preparing views for you will do setContentView() that's why when you call findViewById() without setContentView() then your application can not find views from view hierarchy and it will throw NullPointerException.
setContentView() similar to the inflate() method ?
Some how, because both are doing the same thing, rendering views from given xml layout files but scope of works is different. setContentView() provides views throughout your activity scope while inflate() will only gives you a view from the layout file, that's why whenever you have used inflate() you have to always use reference of return view to call findViewById() like
pseudo code only for your understanding,
View view = infalter.inflate(<R.layout.<file_name>>);
TextView mextView = view.findViewById(R.id.textView);
And yes, setContentView() uses the same inflater.inflate() method too.
And when setContentView() and inflate() not required?
If you are creating dynamically views, in java code then you don't have to required call either setContentView() or inflate().
Note: In old android version when you create a dynamically views using java code and you pass it to some ListView's header or footer it won't work. For this views must be inflated before set to the ListView.

Is it possible to add another view to an Android listactivity?

i want to add a waiting circle to my listactivity. I used the answer in this post:
Using the "animated circle" in an ImageView while loading stuff
However i think i have to add setContentView of the waiting circle layout in order to be able to use findViewById. The problem is i don't know if i can use setContentView twice in the same activity.
Right now when i try it, i get an error that i most use setContentView with the id of the listview layout i want to use.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);}
public void showCallList() // show call list on screen
{
getListView().setVisibility(View.GONE);
findViewById(R.loadingPanel).setVisibility(View.VISIBLE);
CallListArrayAdapter adapter = new CallListArrayAdapter(this,
arrayListCalls);
setListAdapter(adapter);
findViewById(R.id.loadingPanel).setVisibility(View.GONE);
getListView().setVisibility(View.VISIBLE);
}
the exception i get is :
E/AndroidRuntime(4320): Caused by:
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
If you're trying to render a custom view of your own(The animated circle), and add it to an activity you have to use addContentView(View view, ViewGroup.LayoutParams params), wich adds an additional content view to the activity. Added after any existing ones in the activity -- existing views are NOT removed.
These are the parameters:
view The desired content to display.
params Layout parameters for the view.
But the right way to do this(depending of course in the amount of data you're going to load) is using AsyncTask wich enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
For example,you can call a progress dialog(Function as your waiting circle) on the onPreExecute method and list your files in doInBackground method.
Good Luck!!!!
you can't call setContentView again. instead, you should add your additional layout into your main XML that also contains your ListView. you can then find it there.
Actually you don't need a ListActivity to have a ListView. You can just as well use a normal Activity with a normal layout.xml and just put a <ListView> in there and all the other views you want.
Instead of getListView(), which is defined by ListActivity use the normal findViewById() with the id you gave your ListView in your layout.xml.
Yes, it can be done easily.
Create a layout xml like you would for a normal activity, and inside this activity create an empty listview with android:id=#android:id/list.
When you start the ListActivity, setContentView to your custom layout. The ListActivity will automatically find the listView with the id android:id/list, and will use that listview for all of the list-related functionality.
Relevant text from ListActivity:
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)

When to use getparent()

In my custom view I need to use the getParent method and set the visibility on some of it's child views depending on my custom view's state. The problem is that I want to instantiate the child views just once. Where is the best place to do this?
I'm not exactly sure what you want to do. But if all of this is occurring in the same Activity screen why don't you just assign ids (e.g. android:id="#+id/someId" to your elements in the layout.xml file.
This way you can reference any element programatically in your code by calling:
View someView = findViewById(R.id.someId);
I am unclear why you would need to call getParent. If you are trying to manipulate views in a different activity then I think you will need to use a Handler.

Adding a view on runtime to a ViewFlippers

I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);

Categories

Resources