0

I am new in Android development. At the moment I am trying figure out how Android user interface is created.

I remember, previously, user interface & layout was designed in activity_main.xml files.

I have created a new activity, which generated me two files for user interface: activity_main.xml & content_main.xml. It seems, that content_main.xml contains layout and user interface elements. In this case, what activity_main.xml. Do I have to use it?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            }
        });
    }
}
Daumantas Versockas
  • 797
  • 1
  • 10
  • 29
  • Open your new created activity class, search for "setContentView". The layout used here is most likely used for your layout. You should post your activitys source code to get a more specific answer. – Jörn Buitink Nov 19 '15 at 09:57
  • File `MainActivity.java` has link to `activity_main.xml` which has also link to `content_main.xml`. I just can't understand it... – Daumantas Versockas Nov 19 '15 at 09:59
  • can you post your code in your MainActivity – Linh Nov 19 '15 at 10:00
  • may be the possibility your activity_main.xml includes content_main.xml to render ui . check this in activity_main.xml – Anjali Nov 19 '15 at 10:01
  • ok, post your MainActivity.java, your activity_main.xml and your content_main.xml – Jörn Buitink Nov 19 '15 at 10:01
  • 1
    it is just default best practice to separate layout and main content xmls, you can actually delete content_main.xml and use the old approach, be sure to delete the reference from activity_main.xml too – cw fei Nov 19 '15 at 10:02
  • Hi welcome to android development, please check this: http://stackoverflow.com/a/6812066/4503373 – Luca Ziegler Nov 19 '15 at 10:03
  • Refer to http://stackoverflow.com/a/32880945/363262 and http://stackoverflow.com/questions/33183107/difference-between-content-main-xml-and-activity-main-xml – Juan Cortés Nov 19 '15 at 10:03
  • So, as far as I understand, `activity_main.xml` should contain appearance of user interface elements, who are provided in `content_main.xml`? – Daumantas Versockas Nov 19 '15 at 10:10

2 Answers2

1

you should use it . you use include layout to keep clean and understandable your xml..most of the time your xml is to big which is very difficult to find any view(Button,Image etc) ..rather than including entire view into one xml we seprate them and include them ....when you setContentView your layout only one view hierarchy is created..

Vishal Gaur
  • 658
  • 6
  • 18
  • So, lets get back to my question, what kind of things should be in `activity_main.xml` and what should be provided in `content_main.xml`. Is that correct? – Daumantas Versockas Nov 19 '15 at 10:08
  • one situation may be where you can put NavigationView in to your activity_main.xml and you fragments layout which change based on your selected navigation item should be in content_main.xml...it depends on you what you want to add – Vishal Gaur Nov 19 '15 at 10:13
  • I want to create a simple screen without navigation bar with user interface elements inside. – Daumantas Versockas Nov 19 '15 at 10:17
  • you can put your user interface elements inside in content_main.xml or remove content.xml and put your user interface elements inside in activity_main.xml...you are free to what you want to do..android does not force you to include layout ..it is a feature of IDE which generate you a include layout tag – Vishal Gaur Nov 19 '15 at 10:21
  • And what are the advantages of not removing it? If I want not to remove, what should I do with `activity_main.xml`? I suppose it is Google recommendation to use multiple files... – Daumantas Versockas Nov 19 '15 at 11:47
0

you have to use activity_main.xml because it is the root layout. As per my knowledge content_main.xml is for fab. So if you want a new button or anything you can directly add it to activity_main.xml. Many people recommend to use multiple files because now if you want same layout as content_main.xml in another layout file you can directly include that. This is the advantage which multiple file usage provides.

aravindkanna
  • 663
  • 1
  • 7
  • 25