android.R.id.content as container for Fragment
there is nothing wrong with it. Like you said: you dont need your extra R.id.content layout so... just don't add it with setContentView. There is even mention about it in official documentation of ActionBar: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
Alternatively, if the tab content will fill the activity layout, then
your activity doesn't need a layout at all (you don't even need to
call setContentView()). Instead, you can place each fragment in the
default root view, which you can refer to with the
android.R.id.content ID
If you develop only for 14+ (because of native ActionBar) everything should be fine with it, but if you use support lib please read the points below.
I. If you use Support Library revision lower than 19:
Important thing is: What is your min API level that you develop for?
If your app supporting API < 14 and you use AppCompat you must be aware the different behavior.
android.R.id.content is the part of screen where your application should display it's content.
On native API 14+ This is just part below ActionBar, because this part is supposed to display activity content.
In AppCompat, where there is no native support for ActionBar. android.R.id.content is the container of entire app screen. This means - including ActionBar, because ActionBar is emulated there and added as a standard view hierarchy. To solve this issue you have to check whether you are on API lower than 14 and use different id: R.id.action_bar_activity_content
You can create helper method to get correct id:
public static int getContentViewId() {
return Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH ? android.R.id.content : R.id.action_bar_activity_content;
}
So if you are developing for 14+ this is perfectly fine solution. If you use custom ActionBar implementation (like AppCompat) you have to do this trick.
II. If you use Support Library revision 19 (or greater):
It seems that this behavior was fixed in Support Library revision 19:
https://code.google.com/p/android/issues/detail?id=58108#c21
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/support/v7/app/ActionBarActivityDelegateBase.java/#228
You can see that they replacing the old R.id.action_bar_activity_content with standard android.R.id.content (and the old android.R.id.content with NO_ID) for better compatibility! So if you use Support Lib r19 or greater (or just a native framework) you can just just android.R.id.content in both <14 and 14+ variants:)
What does findViewById(android.R.id.content) do in Snackbar?
what does it really mean?
It asks the hosting activity to find a widget whose ID is android.R.id.content
. All activities have one of these, set up by the framework Activity
implementation and its associated Window
. It represents the main content area of the activity.
isn't it a little unexplanatory in code about what's really going on?
You are certainly welcome to add comments to your code to explain your choice.
The Snackbar
documentation explains the role of the View
:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.
With that in mind...
can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work?
Perhaps. It depends a bit on the UI of your app. If there is a particular CoordinatorLayout
that you want to use with the Snackbar
, pass it (or a child) to make()
. Otherwise, any widget should work.
Can findViewById(android.R.id.content) ever return null for Snackbars?
findViewById
can always be null, if you try to find a view that doesn't exist in the current layout.
The warning is just a help.
It's probably very generic and if you look inside the source of the Activity class, you will find this method:
@Nullable
public View findViewById(@IdRes int id) {
return getWindow().findViewById(id);
}
The Nullable
annotation just informs the compiler that there might be a possibility of getting a null
reference here and Lint will react to this. It doesn't know how to differentiate between a findViewById(android.R.id.content)
or some other call with findViewById(R.id.myCustomLayoutId)
. You could probably add the Lint check yourself however.
You can safely use findViewById(android.R.id.content)
whenever you're inside an Activity
.
You can safely use getView()
inside a Fragment
whenever onCreateView
has been called.
Is ViewGroup with id android.R.id.content always FrameLayout?
i would rely that android.R.id.content will be of type ViewGroup but not FrameLayout or any other specific. i checked on AppCompatActivity and on simple Activity and they return different types of ViewGroup.
What is this R.id.home?
You're probably used to using R.id.whatever
to refer to the IDs you've given to the views in your app. Giving a TextView
an ID of label
, and then you can find it by looking for the view with R.id.label
for its ID, that kind of thing.
That R
file is generated for your app, based on the resources you've added or declared. But android.R.id.home
is an ID in Android's R
file - you can think of it like a separate set of system resources. It won't conflict with anything you've given a "home" ID to, because that would be R.id.home
instead.
When you call setHomeButtonEnabled
on your toolbar, Android adds its own icon, and it gives that an internal ID of android.R.id.home
. That's always the identifier for that home button, and you can check if it's been pressed by checking for that ID when a menu item is selected.
Android: Adding fragments with android.R.id.content
I add the same problem when trying to add a PreferenceFragment
to an ActionBarActivity
: the fragment does not show at all (it works on a simple Activity
).
I solved it by defining a dummy layout xml file, with a single frame layout. Then I replace this layout with my fragment in onCreate
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.settings_layout, new SettingsFragment())
.commit();
}
activity_settings.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
What is android.R.id.text1 ?
android.R.id.text1
is just an identifier defined in the Android framework.
In the framework, android.R.id.text1
is an used for TextView
views. You can find it in many layouts from the framework (select_dialog_item
, select_dialog_singlechoice
, simple_dropdown_item_1line
, etc.). In Android framework xml, it is represented by @+id/text1
.
Hence, if you use one of these layouts and want to change the text, you will need to use this id.
// probably in a custom ListAdapter that uses
View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView textView = (textView) view.findViewById(android.R.id.text1);
textView.setText("Oh no! not hello world again");
Also, you can use this same identifier to identify a TextView
(or anything) in your custom layouts. See in the sample "Notepad", the layout file noteslist_item.xml
.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
And actually, you could use R.id.text1
as an identifier of anything else, but that would be confusing.
Related Topics
Lazy Download Images into Gridview
How to Get Available Wifi Networks and Display Them in a List in Android
What Is the Purpose of Android's <Merge> Tag in Xml Layouts
Solving "Adb Server Version Doesn't Match This Client" Error
How to Block a Mobile Number Call and Message Receiving in Android Application Development
How to Print PDF Using Android 4.4 Printing Framework
Spinner Does Not Wrap Text -- Is This an Android Bug
How to Style the Drawerarrowtoggle from Android Appcompat V7 21 Library
Error Inflating Class Android.Support.V7.Widget.Toolbar
In Androidx.Fragment.App.Fragment,Setuservisiblehint()Is Deprecated,And Not Executed,Why
How to Crop an Image in Android
Error: Could Not Find Com.Google.Gms:Google-Services:4.2.0
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
How to Clear an Imageview in Android
Onrequestpermissionsresult Not Being Called in Fragment If Defined in Both Fragment and Activity
Android Volley - How to Isolate Requests in Another Class