I' new to Android development. I created a new project with automatically created 'MainActivity.class', 'activity_main.xml' and 'content_main.xml'. So at this step there was no problem.
Then I'd like to create another activity called 'MainActivity2' using basic template (the one with a fab). I expect to see two layout files: 'activity_main2.xml' and 'content_main2.xml'. But it ONLY created 'activity_main2.xml'. There is no 'content_main2.xml'.
Even worse, I tried to create MainActivity2 using other templates (beside basic and empty templates), but still I cannot get 'content_main2.xml'
Where went wrong?
It might be because of the layout type you've selected when creating an activity in Android Studio. Nevertheless, if you would like to include one layout inside another layout you could use <include> tag, for example:
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/yourlayout" />
create a new xml layout file called yourlayout.xml
Related
I want to make a menu in android studio. when I create a new android resource file in res folder the design/text tab is not show in xml file and I can't put menu item in it. but in other xml files this tab(text/design) is active. my android studio version is 2.1.3.
If you have problems on the layout(and previously the code was running), you should create a new project because sometimes the layout have some rendering problems.
If you want to add an EditText(or similar components using xml code) you can type, for example:
<EditText
android:id="#+id/edit_lastname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
So the components you added will be on the layout.
I am, at the moment, trying to make a google maps app using android studio.
Right now, everything is fine, except for one thing, when i go to the "Design" Tab, in the XML file i have this Redering message:
Rendering Problems A tag allows a layout file to
dynamically include different layouts at runtime. At layout editing
time the specific layout to be used is not known. You can choose which
layout you would like previewed while editing the layout...
And the main problem is that I cannot use any of the gui components in my layout, I searched about my problem and i understood that with this error, people couldn't see their map but they could put on textfields, widgets, layouts, etc.
But for me, my preview is completly frozen and i can't do any modification.
Picture of my android studio page.
As you can change the fragments dynamically with your code, android studio doesn't know which layout to show in design time. This is the reason of your error.
To specifically tell android which layout to show, add tools:layout="#layout/Your_layout_name" attribute to your fragment.
There is also a shortcut link below the error description which you have told. Just click on the link and android will add it for you and you will see the fragment in your layout with no rendering error messages.
For a detailed example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.insane.fragmenttest.MainActivity">
<fragment
android:id="#+id/testFragmentID"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.insane.fragmenttest.WorkOutDetails"
tools:layout="#layout/fragment_work_out_details" /> <!-- This is the line you want to add. -->
</LinearLayout>
Try using Designtime Layout Attributes. http://tools.android.com/tips/layout-designtime-attributes
These attributes guide Android Studio on how to render run-time attributes in the layout editor.
I think you should include tools:showIn="#layout/activity_maps" in your the <fragment> part of your google_maps_api.xml
Just use this design for the fragment. This solved my problem. Also, refer to this link
<fragment
android:id = "#+id/ma"
android:name = "com.google.android.gms.maps.SupportMapFragment"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "com.example.demomaps.MapsActivity"
/>
Yes I solved this easily..Simply ignore the message and click line that is showing below of the rendering messages.
Actually fragment contains layout inner and if its not include its showing warning that it must contains you can simply ignore this message below the warning then this rendering issue will be resolved.
can anyone tell me what is the main difference between main.xml and Activity_main.xml.
I am writing a sample pgm for a simple client server communication.In the tutorial that am following it says to add the following code in main.xml which the author says is inside layout folder. but in my package explorer main.xml is shown inside the menu folder and in my layout folder, i have only activity_main and fragment_main. this is the code that has been posted in the website.
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<ListView android:id=”#+id/list”
android:layout_width=”fill_parent”
android:layout_height=”0dip”
android:layout_weight=”1″
android:transcriptMode=”alwaysScroll”
android:cacheColorHint=”#00000000″
android:listSelector=”#android:color/transparent”/>
<LinearLayout android:id=”#+id/footer”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:gravity=”bottom”>
<EditText android:inputType=”textMultiLine|textNoSuggestions”
android:layout_width=”0dp”
android:layout_height=”40dp”
android:id=”#+id/editText”
android:layout_weight=”1″/>
<Button android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”#+id/send_button”
android:layout_gravity=”center_vertical”
android:text=”send” />
</LinearLayout>
</LinearLayout>
my question is where exactly am i supposed to use this code? in main.xml in menu folder or activity_main.xml? plz help
Put the code in the activity_main.xml file.
activity_main.xml and main.xml described in your tutorial are referring the same thing (a layout file). You can find it under 'res/layout' in the package explorer in ADT.
(Do not put the layout code in the .xml file in the menu folder)
You could actually name the .xml file anything you want as long as it is properly declared in your Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//<-------your xml file is declared here
I believe it is just the newer version of ADT that names the file activity_main.xml by default.
Whatever your tutorial is saying in regards to main.xml use your activity_main.xml instead.
alternatively from within ADT navigate to 'res/layout' and right click on the activity_main.xml select refactor->rename and rename the file to main.xml so everything matches your tutorial
where exactly am i supposed to use this code
This code would go inside activity_main.xml because it is a layout file.
menus
If it was a menu it would look a lot different such as having menu items instead of Views. And it would start and end with the <menu> </menu> tags. A menu file can be inflated to be used with your menu buttons whether that be a hard menu button, from the ActionBar, or inflated to be used somewhere else.
Menus
layouts
layouts will contain your Views such as ViewGroups (LinearLayout, RelativeLayout, etc..) and child Views like TextViews, Buttons, etc... The layout files can be named whatever you like (assuming they follow the naming conventions for layout file names). Then you use that name in your Activity to set the layout using setContentView(R.layout.some_layout_name)
Layouts
The code you posted is layout code; it should go into layout/activity_main.xml. The menu/main.xml file describes a menu named 'main'.
You need to add the code in layout/activity_main.xml. The tutorial main.xml corresponds to your activity_main.xml. This is the xml that is inflated in your Launcher activity (normally using setContentView(R.layout.activity_main) api in onCreate() function). This xml name can be anything. While creating the application, you get the option to give name to this xml. The default name is activity_xml.
The main.xm which is inside menu folder is used for sub menus in your applicaion. If you click the menu button inside your application, sub menu will come up. The layout of this sub menus is controlled by main.xml in menu folder.
I am new to Android development and Eclipse. I have been coding on ASP.Net and MS Visual Web Developer for years. In VWD, when you add a control to the design view, double clicking on it will automatically bring you to code view for the OnClick function of the control you have just created. You can also see the list of possible event handlers for a control from the design view.
But I can't seem to find this feature in Eclipse. Is there such a thing? I did a search on Google and the best I found is this (same question but without an answer).
http://www.techrepublic.com/forum/questions/101-341077/event-handlers-in-eclipse
Anyone to advice please?
Thanks!
What you're talking doesn't quite exist in Eclipse. You'll have to manually open your java class and add the method to the corresponding java activity there.
For example, if you set the android:onClick XML attribute to "myAwesomeMethod" in your layout XML file, in the corresponding Activity that uses that layout, you'll need to make sure you have a "myAwesomeMethod" method defined.
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myAwesomeMethod" />
<!-- even more layout elements -->
In your java Activity class:
public void myAwesomeMethod(View v) {
// does something very awesome
}
Note: you can also do this programmatically, which is what I generally do. However, defining the android:onClick method will save you a few lines of code.
For more information, check out this post. It gives a lot more detail on how to assign onClick handlers to a button and the two ways you can do so.
No, that is not how Eclipse works. You add the control in the xml file, then in the activity that you are going to load that layout in you add the onClickListener on the element you want to respond to clicks for,
I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?
Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included
You can use the <include /> tag in your XML files.
See Layout Tricks for an example.
Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.
The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API