tools:context=".MainActivity" is failing, how can i solve it? - android

the argument: tools:context=".MainActivity" gives me error, addly the part named: .MainActivity appears in color red, what can i do?
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"

tried rebuild project or try invalidate cache restart
if still same problem you can add yourpackagename.mainactivity
like in the picture this
tools:context="com.etest.myapplicwr.MainActivity">
my packgae name is com.etest.myapplicwr
replace it with your package name

You can delete it, or you can update it to point to some activity or fragment where you will be using this layout resource.

you should find which folder save your MainActivity.
For Example:
So, Check it.
And then, you should understand what's its function.
it is used by: Lint, Android Studio layout editor .
This attribute declares which activity this layout is associated with by default.
This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insert onClick handlers when you make those from a quickfix.
you can find more information here

Related

Child items overlay-ed by parent layout in Android Studio Design

Can anybody how to remove this parent NavDrawer overlay showing whilst designing layout? (Screen 1).
The overlay is removed when running in actual device (Screen 2).
Because of this I can't design child elements right there in the studio.
Any help would be great!
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Always Visible."
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
When you see that gray block with the tag name in it, that means that Android Studio can't resolve the class from the tag in your layout, so it doesn't know what to preview. Often this will happen if you have a typo in your layout file, but then you wouldn't see it working when you actually run the app.
For me, I can make this happen quite easily by using the old support library versions of widgets, since I have AndroidX enabled in my project. For example, just pasting your code in to my IDE gives me the same gray box, but changing your tags to use AndroidX fixes it (android.support.v4.widget.DrawerLayout -> androidx.drawerlayout.widget.DrawerLayout).
Chances are good that your Android Studio just needs a kick in the pants.
I suggest doing a Gradle Sync (File -> Sync Project with Gradle Files) and, if that doesn't work, an Invalidate + Restart (File -> Invalidate Caches / Restart...).

Want Relative Layout from the beginning

Novice in Android Studio. Recently started practising basic coding. I want to have relative layout or linear layout from the beginning. Tried but could not make it possible. Could not find any option provided to select the layout theme. Automatically constraint layout getting launched. Please guide..
Change this line:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
To
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
And do check your ending tag changes to this:
</android.widget.RelativeLayout>
Also (optionally) go ahead and delete this line if it's being shown in grey:
xmlns:app="http://schemas.android.com/apk/res-auto"
And also this is an optional change to make in project,
If the constraint layout is not needed in the project remove the following dependency from build.gradle by deleting this line and then doing gradle sync:
compile 'com.android.support.constraint:constraint-layout:......'

Android Studio Rendering fragment issue

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.

No properties are generated with Android SDK

I installed packages from the Android SDK manager, but when I open an xml file to edit a layout, I get unresolved errors When I drag and drop items from the sidebar, I get a log saying:
Couldn't resolve resource #dimen/activity_horizontal_margin
Couldn't resolve resource #dimen/activity_vertical_margin
"#dimen/activity_horizontal_margin" in attribute "paddingLeft" is not a valid format.
"#dimen/activity_vertical_margin" in attribute "paddingTop" is not a valid format.
"#dimen/activity_horizontal_margin" in attribute "paddingRight" is not a valid format.
"#dimen/activity_vertical_margin" in attribute "paddingBottom" is not a valid format.
Also, no properties show up when I view the menu. When I right click on the layout to set a different layout manager, I see the following. I installed the correct packages from the SDK Manager, including all tools, the Android 19 API, Android 18 API, and the Android Support library.
Here's my xml file
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
Where can I start by fixing these errors?
This is probably caused by a mix-and-match of some large, medium and small resolutions in your project but render the layout in a view not considered large enough. In this case, just change the render parameter in the graphical layout tool. Hope it helps.
Such layout errors usually appears after you've created new Activities/Layouts using IDE build-in tools/wizards (like New->Activity->Blank Activity with Fragment in Android Studio). At this moment Java and XML code is already created, but auto-generated code (like R.java file) isn't generated yet. What's why layout editing feature is not working - it requires valid R.java file with all your resources (in your case it's #dimen/*).
To fix this errors simply compile your project (which will force auto-generated code to be generated) and refresh layout editor window.

ADT Plugin: Missing properties in layout editor

I'm having trouble with eclipse ADT plugin. Everything was ok in Eclipse's layout editor, in both graphical and XML modes. I designed the GUI mostly from the graphical editor, for convenience.
At some point the Properties view broke. Now it only shows properties for the "View" class, and every other property from View subclasses are not shown (for instance, properties of ImageView are not shown). Properties for aligning inside RelativeLayout are also missing (for instance alignParentTop).
This is a "clean" eclipse install I use only for android, there are no other plugins installed (other than subeclipse). It has never failed me before. I've updated the SDK and also the plugin to the last version but it didn't fix this problem. Eclipse is v3.7 Indigo R2.
Also tried cleaning the project to no avail.
Have you seen this behaviour before? Is it possible to get the graphical editor back and fully working as expected without reinstalling everything again?
Thanks in advance.
UPDATED:
I've traced the problem to a single layout XML. Other layout XMLs are working fine. And the problem is that I'm using the DrawerLayout as main layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android">
If I replace this layout for a regular LinearLayout everything returns back to normal. Still I've to find a solution, because every modificattion attempted on the graphical layout results in that element being removed. So this is probably a bug and not only it is inconvenient but also dangerous!
I had the same problem and managed to fix it by splitting the content of the DrawerLayout out into different files like this:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<include layout="#layout/activity_tab_content"/>
<!-- The navigation drawer -->
<include layout="#layout/navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
Now I can edit the content of the drawer successfully in a separate editor.

Categories

Resources