I would like to know if there is a way to access elements from different layouts in a Fragment class, something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view1 = inflater.inflate(R.activity_layout_1, container, false);
View view2 = inflater.inflate(R.activity_layout_2, container, false);
TextView textView1= (TextView) view1.findViewById(R.id.textView1);
TextView textView2= (TextView) view2.findViewById(R.id.textView2);
textView2.setText("text changed");
}
This is what I want to achieve
Am I missing something?
To add menu items to the ActionBar, you should override onCreateOptionsMenu(). You can do this in either an Activity or a Fragment. The "listener" is actually the onOptionsItemSelected() method, also within the Activity or Fragment subclass which created the menu. For details, check out the Menus API Guide.
If you are targetting API 23 (Marshmallow), you should look at Toolbar. Even for older versions, you can use Toolbar from the appcompat library.
Your code should work as written. What it will look like will depend upon the container. And, depending upon how you are setting up the fragment, you might not have control over container
Another options is to use <include> instead, for greater control. You have one layout that uses <include> elements to reference the ones that you want to reuse and, if needed, provide right containers for it all to work.
Related
From what I understand, the LayoutInflater converts XML into Views. But when I use Buttons, TextViews or other widgets in code, I simply have to use findViewById() without having to inflate these Views first. Are these views automatically inflated? If so, when are views automatically inflated and when do you have to inflate them manually?
You just have to inflate an xml layout, and then all the viewgroups and views (buttons,textview,edittext,etc.) will automatically be shown.
So in an Activity class, oncreate method has a line SetContentview(), this inflates the xml layout.
An Activity needs to include a call to setContentView(R.layout.<your_layout_here>) in its onCreate method. That inflates the XML in the specified layout into the view hierarchy for the Activity. For a Fragment, override the onCreateView method like below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.<your_layout_here>, container, false);
}
Once you've inflated a layout (and all of its children Views) into the view hierarchy, you can then use findViewById(R.id.<your_view_id>) to get a reference to the actual View object that you've inflated into the hierarchy and play with it.
you inflate your views manually if you create your activity and xml manually.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
}
Few things are here:
1. View for your activity(UI screen), that is set by setContentView() method.
2. View for a specific UI component e.g. Button, that is either created in xml of your activity_layout or you can inflate a separate xml layout file for your specific UI component. The best example and use is like inflating an xml layout file for your customised Toast.
So, here is the thing that relates setContentView() and inflating an xml layout file for a separate view:
Both of these provides a layout for view and view components, both of them creates a binary output for layout and use them as described above.
In Activity's onCreate Method we can do the following
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(this,
R.layout.activity_product_detail,R.layout.product_details_footer);
wholeView = helper.createView(productDetailDTO.isOwner());
setContentView(wholeView);
In fragment,we can return a layout from onCreateView(), we can inflate it from a layout resource defined in XML. To help us do so, onCreateView() provides a LayoutInflater object.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
Now, I want to use
whole view i.e my custom view
as my fragments rootview.I dont want to inflate if from a layout resource defined in XML.
But, until now I have not been able to do so. I have been searching in google for past few days but searching didnot produce any result. So, here I am posting this question.
I do not get your question completely but in my opinion what you can do is create an empty xml file(with relative layout only) and inflate it then add your custom view to it. Hope this will work, sorry if i was wrong
I have followed this tutorial and its working great. In my fragment's view there are some buttons which I have some onClick assigned to. For example I have a button like:
<ImageButton
android:onClick="doSomething">
</ImageButton>
While creating the rootView I use the following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
I did some googling and found out the last parameter is for attach to parentView and I have set it to true. But the parent view is container. Where does this container point to?
Where should I write the function 'doSomething'?
I did some googling and found out the last parameter is for attach to
parentView and I have set it to true.
Don't set it to true. In the onCreateView() you'll create the view for the fragment and return it and Android will attach that view to the layout on its own(what the documentation says).
Where does this container point to?
If not null it will be the layout where the fragment's view will be added, generally used for generating the proper LayoutParams for the newly inflated view.
Where should I write the function 'doSomething'?
As the buttons using the onClick attribute will be part of the fragment layout you should remove the onClick attribute from the layout and set the OnClickListener in code on the buttons. Android will search for the doSomething() method at the Activity level so you can't receive the on click event directly in the fragment.
I am preparing to do an android demonstration of sorts and one of the first apps that i would like to write would be a screen filled with different widgets(which of course are views) but would like to put them on the screen without any layout built to hold them. is this possible or do you have to use a layout to put more than one view(widget) on the screen at once?
So right now i can do something like:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView view1 = new TextView(this);
view1.setText("I am view one");
setContentView(view1);
}
}
In this case i really havent specified a layout but there doesnt seem to be a way to position multiple widgets on the screen without setting a layout. The purpose of this would be to show why you would want to use layouts. perhaps there is a way to display widgets on the screen without having to call the setContentView method.
You can only add multiple widgets/views to something called a ViewGroup. If you take a look at the documentation you'll see - not surprisingly - that basically all layouts extend this class. Similarly, if you look up the documentation on e.g. a TextView, you'll find that it doesn't extend ViewGroup (it does inherit from View, just like ViewGroup, which means it's on a different branch in the hierarchy tree).
In other words: you will need some sort of a layout in order to display more than a single widget/view at a time. You will also always need an explicit call to setContentView(), unless you use something like a ListActivity or ListFragment that by default creates a layout with a ListView as root.
That being said, your example is actually just a programmatical way of setting the following layout on the activity:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am view one" />
You can do it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
TextView view1 = new TextView(this);
view1.setText("I am view one");
frameLayout.addView(view1);
// add more widgets into ViewGroup as you want
// then set the viewgroup as content view
setContentView(frameLayout);
}
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)