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.
Related
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
When I do changes to a layout resource file it's not updated/reflected when the apk file is buildt and installed from Android Studio 2.0 (Preview 3b).
Example:
I had a EditText and I added a TextInputLayout like this:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/edit_text_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_your_new_password"
android:inputType="textPassword"
android:saveEnabled="false" />
</android.support.design.widget.TextInputLayout>
The result after updating the app is the same as before I added the TextInputLayout, just the EditText without TextInputLayout.
What I've tried:
Build/Clean Project
Build/Rebuild Project
File/Invalidate Caches / Restart..
Uninstall app first
Turn off Instant Run
I suspect this is probably a bug with the Preview 3b version of Android Studio 2.0 causing this. Any ideas? Maybe it's just a settings/configuration?
Temporary solution:
If you make a copy of the layout file and inflate it instead. Then the changes are updated in the app? But this is not ideally the best solution!
Also make sure you make the changes in the layout-v<target-api> folder if you have set specific layout for specific api level. For example layout-v23 in mine case.
This issue is typically caused by only updating the xml file in one layout folder and not the other folders containing the file's compatibility variants as well. For example, only editing the xml file in the layout system folder and not the layout-v14 folder too will cause this issue.
The fix: update the instance of the layout xml file not only in your layout folder, but the instances in all other layout folders too (layout-v14, layout v-21, etc.)
I wasn't able to reproduce this issue. I tried the same layout changes with Preview 3b and the changes showed up in the emulator fine. I simply hit the run button again.
Was this working for you in a previous version of Android Studio?
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.
Just recently, my Android XML files which define my layout and etc have stopped autocompleting. When I type android: I used to get a list of options related to Android such as layout_width, etc. Now I get only four options, the first being xmlns="default namespace"
Also the tab to change to the layout designer and other tabs are gone, I am left with a 'design' tab which shows my XML structure and a 'source' tab with the actual XML. I think they are being recognised as normal XML files rather than Android XML files, is there a way to change this?
I am running Eclipse Indigo Version 3.7.0 with the ADT plugin Version 15.0.1.
Here is an example of one of the XML files it isn't working in:
<?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:padding="10dp"
android:textSize="16dp"
android:background="#FFFFFF"
android:textColor="#000000" ></TextView>
Switch to a new workspace may help.
My main working tool is IntelliJ. So I use it to create XML files and layouts for Android activities. However, if I open such XML file in Eclipse, it does not recognize it as layout file and does not load its GUI designer (I open XML file via Eclipse Layout Editor). If I create an XML file in Eclipse, the GUI designer loads properly.
The very content of Eclipse layout XML filet and IntelliJ layout XML file is 100% identical.
What am I doing wrong?
//////////////////////////////////////////////////////////////
EDIT: I have created new project in IntelliJ. Added some test elements into Main.xml. Exported project to Eclipse format (File->Export to Eclipse...). Closed IntelliJ, opened Eclipse, Imported the newly created project, opened Main.xml with Layout Editor and NOTHING again. This is structure of Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="just testing text"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test inside of layout"/>
</LinearLayout>
</LinearLayout>
And this is how Eclipse Layout Editor sees this XML file.
Try this
Right click on that xml file
Select Open With
In that select Android Layout Editor
Once you copy the file to the res/layout folder, right click on the project in the project explorer and hit refresh to let it know Eclipse the file is there. You might need to clean the project and rebuild also.
An xml file does not depend on where you created, it's just text.
Make sure the name given to your xml is just all small letters and underscore, no Caps. I had the same problem and changing the Case worked well
just give it time to generate the view. the bigger your layout file, the longer it takes. what you're seeing is a lack of a progress bar that shows it's doing something. just be patient for a while and it should come up