Add linebreaks to XML layout template - android-layout

Is it possible to modify the XML layout template in Android Studio so that the namespace and attributes appear on separate lines?
Generated by default template:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Preferred:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
The default template (below) shows each attribute (not including the namespace) on separate lines, but these line breaks don't translate to the generated resource file.

The problem is that Android Studio isn't respecting line breaks in the XML templates.
Solution
Enable Keep line breaks in
Settings > Editor > Code Style > XML > Other (tab)
Add a line break before the namespace in the relevant (see Notes) code template(s) in
Settings > Editor > File and Code Templates > Other (tab)
Result
XML layouts will now generate the namespace and attributes on separate lines, like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Notes
The relevant code templates are:
layoutResourceFile.xml
layoutResourceFile_vertical.xml
To ensure that Keep line breaks persists on subsequent runs of
Android Studio, make sure to modify the Project scheme rather than
the Default scheme.

Related

App on phone looks very different to android studio rendering

I'm trying to get a fragment to appear, as rendered in android studio on my phone. I have a Samsung S7 which has 1440 x 2560 pixels, so I've selected the Nexus 6 rendering option in Android Studio.
I have setup my fragment so that it is contained in a content file called content_add_edit_recipe.xml. My activity, activity_add_edit_recipe.xml includes this file, shown below.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:orientation="vertical"
android:name="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_add_edit_recipe"/>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jcg.grocerylistconverter.AddEditRecipeActivity">
here is activity file:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_add_edit_recipe"/>
here is the fragment xml for the layout tag (I don't think the rest is relevant right now):
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_add_edit_recipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
tools:showIn="#layout/activity_add_edit_recipe">....
And here is how the fragment looks in the preview:
while here is how it appears on the phone:
I suspect that this is to do with my actual java code in starting the fragment, but I can't figure out for the life of me where/how this is occurring, after tracing through my code the xmls that are inflated.
If nothing is evident from the xmls I will post the code initiating the activity/fragment, but first can someone please point out if something appears off with my xml?
I think you didn't consider text size and length of text inside while designing layout. You should test your layout with different text length and size inside spinner

How to remove extra padding at top of window?

I have this strange padding-top when I run my App on Android. On iOS it looks as expected.
When I check the padding is 0 for all directions. My last thought is that it could be related to some of my layouts, but I don't understand what that could be.
Toolbar layout:
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
Tabs layout
<?xml version="1.0" encoding="UTF-8"?>
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />
I am using Xamarin.Form's MasterDetailPage with my pages being ContentPage.
Does this combination of pages create this error on Android? Please help me fix it.
Here is a screenshot of what I see:
]1
You're able to change lots of settings in Android Solution (MainActivity.cs) just underneath your namespace.
For example:
[Activity(Theme = "#style/AppStyle.Light", Label = "ProjectName", MainLauncher = false,
ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

Error inflating class fragment with class using FragmentActivity

I have coded a Google Maps application in Xamarin, and am getting this error:
Android.Views.InflateException: Binary XML file line #1: Error
inflating class fragment
I am getting this error when the following line of code is run:
SetContentView(Resource.Layout.MapWithOverlayLayout);
Here is my MapWithOverlayLayout.axml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment class="SimpleMapDemo.MapWithMarkersActivity"
android:id="#+id/mapWithOverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The namespace is called: SimpleMapDemo
Here is my class declaration:
public class MapWithMarkersActivity : Android.Support.V4.App.FragmentActivity
I am using a SupportMapFragment object to display a Google Map
Can I please have some help to get this code working?
I think the problem is with the class attribute you set on the fragment element. Have you tired inflating the layout without it?
I have figured it out myself. I did not have the correct class name.
Here is the correct layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
I had the same problem and fixed it by deleting the AndroidManifest in the obj/Debug/android directory, in the VS or XS and rewriting it so that it only works on me
That is because the app can't distinguish between google play services manifest and the app's one.

Positioning a view partially outside the screen in Android

How do I place a view partially outside the screen in Android?The view that needs to slide in is placed inside a RelativeLayout as of now and here is the entire layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webViewLink"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
The second webview is the one which needs to be partially outside the screen.
You could use android:layout_marginRight="-30dp" as #user1490103 suggested. Or you could use android:translationX. The translation would make more sense if you are planning on animating this View. Remember to set android:clipChilren="false" on the RelativeLayout.

Having some problems trying to put views in the right places

I'm generating dynamic tablerows in this XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutPrincipal1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
</TableLayout>
</ScrollView>
The problem is: I would like to have some views in a specific position using gravity, but when a define for example, button1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL)); it doesn't work, but if I do this directly in the xml It works(with layout_gravity), someone knows what to do?

Resources