Does a dynamically-created View inherit theme attributes? - android

In Android, say if I dynamically create a TextView with:
TextView view = new TextView();
and add it to the root view of an activity. Does this view inherit theme properties such as text color?
If so when does this happen? At the time of its creation or the time of adding the view?

As #zapl said, use new TextView(context). Also, make sure you use the activity context to inherit the activity's theme/style instead of application context.

Related

How to set different themes for inflated layout in a view stub

I have a layout that I'm inflating into a ViewStub. When its within view A, I want to have one set of styles applied to the EditText fields, and when it's within view B, I want to have a different set of styles applied to the EditText fields.
First, is this possible, and second (if so), how would I go about it?
I'm not sure about ViewStubs specifically, but if you wanted to inflate a predefined layout and add it to a ViewGroup you could use a ContextThemeWrapper.
View viewOne = View.inflate(new ContextThemeWrapper(context, R.style.Theme_One), R.layout.my_layout)
View viewTwo = View.inflate(new ContextThemeWrapper(context, R.style.Theme_Two), R.layout.my_layout)

In Android, how to I edit views in a layout, when my setContentView is not in that layout?

For example, I might be in R.Layout.activity_main ,
but when I onCreate my activity, I want to change a TextView in another layout, so I would create a TextView object and findViewById from that layout, and then change it.
But this will not work unless I set my contentview to that layout, but is it possible to be able to set the TextView in that layout, without having to setContentView to that layout?
This does not work in all situations, but you could try a LayoutInflator.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);
Then to find views in that layout, just do...
layout.findViewById(R.id.your_view);
It is better to store a reference of desired view in a class as a variable when you create it's parent layout.Then change it's property when you need.
You can see more details in these two questions:
Android: How to declare global variables?
Android, how to access Activity UI from my class?

Apply style to every child view

I have the following pseudo layout:
<RelativeLayout>
<ACustomView />
<AnotherCustomView />
</RelativeLayout>
I have TextViews in both of the custom views (inflated from XML). How can i set
style properties (eg. textColor) for every TextView in the first custom view? For example i want every TextView to be red which are in the first custom view.
I don't think that this is possible.
If you don't want to much typing you can define your own style and add it to each textview or you can create your own textview class where style attributes are set.
For an example use this question/answer:
Setting global styles for Views in Android
I hope i could help you
Cu
JackZ

How to inflate another view from another view in Android

I know in my onCreate() I can inflate a view from XML by something like:
loadingScreen = (RelativeLayout) findViewById(R.id.loadingScreen);
But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView
If you want to inflate a layout the code looks like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);
Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.
Here is a tutorial about the LayoutInflater:
Layout resources in Android
Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).
What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.
After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.
Here's an example:
GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);
Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.
If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:
RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);
But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.
You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.
you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity
I hope I helped you
If I correctly understood what you wanna do:
Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:
Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);
If you meant something different let me know in the comments.
EDIT: And then you can just set the button's visibility:
button.setVisibility(Button.GONE)

What does it mean to inflate a view from an xml file?

I am new to android development and keep coming across references to Inflating views from a layout xml file. I googled and searched the development guide but still wasn't able to pick up a sense for what it means. If someone could provide a very simple example, it'd be much appreciated.
When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory. Let's call that implicit inflation (the OS will inflate the view for you). For instance:
class Name extends Activity{
public void onCreate(){
// the OS will inflate the your_layout.xml
// file and use it for this activity
setContentView(R.layout.your_layout);
}
}
You can also inflate views explicitly by using the LayoutInflater. In that case you have to:
Get an instance of the LayoutInflater
Specify the XML to inflate
Use the returned View
Set the content view with returned view (above)
For instance:
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
setContentView(theInflatedView) // 4
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances.
Inflating is the process of adding a view (.xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so:
Button but = new Button();
but.setText ="button text";
but.background ...
but.leftDrawable.. and so on...
TextView txt = new TextView();
txt.setText ="button text";
txt.background ... and so on...
Then we have to create a layout where we can add above views:
RelativeLayout rel = new RelativeLayout();
rel.addView(but);
And now if we want to add a button in the right-corner and a textview on the bottom, we have to do a lot of work. First by instantiating the view properties and then applying multiple constraints. This is time consuming.
Android makes it easy for us to create a simple .xml and design its style and attributes in xml and then simply inflate it wherever we need it without the pain of setting constraints programatically.
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like
(RelativeLayout)findViewById(R.id.relative).addView(menuLayout);
A layman definition for inflation might be to convert the XML code to Java code. Just a way to understand, e.g., if we have a tag in XML, OS has to create a corresponding Java object in memory, so inflatter reads the XMLtags, and creates the corresponding objects in Java.
I think here "inflating a view" means fetching the layout.xml file drawing a view specified in that xml file and POPULATING ( = inflating ) the parent viewGroup with the created View.
Because we make UI into XML but view objects is what we display so we somehow need to convert xml into view objects so inflating means we are converting xml into view objects so that it can be displayed, for this we need a service called layout inflator service and give it an xml and it will be convert for you.
In the iOS UIKit universe, this means getting the reference to the .Xib (which is XML, just like android) file and adding it to the current ViewController's view hierarchy.

Categories

Resources