Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: id never declared anywhere but exist?

Tags:

android

I examine code example from FragmentBasics.zip

http://developer.android.com/training/basics/fragments/communicating.html

There was this code (MainActivity.java):

ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

But I never find *article_fragment* declared/set anywhere in the whole classes or layouts(xml) or values. Where does it come from??

like image 267
Andreas Avatar asked Feb 17 '26 13:02

Andreas


2 Answers

This is the XML code used in res\layout-large\news_articles.xml:

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.example.android.fragments.ArticleFragment"
          android:id="@+id/article_fragment"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

The fragment with the android:name="com.example.android.fragments.ArticleFragment attribute has the id set as: @+id/article_fragment

The JAVA code where you find the id article_fragment being used is on line 55 for this method: public void onArticleSelected(int position). It checks if your are using a two-pane layout. That is why the XML file mentioned at the top, is in the layout-large foldr (In the example).

like image 153
Siddharth Lele Avatar answered Feb 20 '26 05:02

Siddharth Lele


R.id.article_fragment layout is used for large screen device in current example you can find out this layout inside res/layout-large/news_articles.xml

like image 41
ρяσѕρєя K Avatar answered Feb 20 '26 07:02

ρяσѕρєя K