Is something wrong with my layout code using RelativeLayout in xamrin android? - android

This layout has three controls:two toolbars and a framelayout.I want two toolbars one at the bottom of the layout and another at the top of the layout.The framelayout just lies between them.I have the code like this,but it always has the error saying "No resource found that matches the given name (at 'layout_above' with value '#id/bottomToolBar')".I'm pretty sure I have set the ID of the bottom toolBar. I am so desperate. Many thanks in advance.
<RelativeLayout 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">
<android.support.v7.widget.Toolbar
android:id="#+id/mainToolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="#FFFFFF"
app:title="Musics">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/mainToolBar"
android:layout_above="#id/bottomToolBar"
android:id="#+id/fragmentsContainer"
/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/bottomToolBar"
android:minHeight="?attr/actionBarSize"
android:background="#EFEFEF">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Yes. When your FrameLayout is parsed the parser tries to resolve all the IDs. So it also tries to look up bottomToolBar. However, at this point, bottomToolBar does not exist in your R.java / Resource.cs file and therefore can't be found.
This ID is created later in your second Toolbar in this line:
android:id="#+id/bottomToolBar"
The + indicates, that a new id must be created if no id exists for this reference. It will then be added to your R.java file or Resource.cs file for Xamarin.
So the fix would be: Add a + to your reference from FrameLayout to bottomToolBar, so that an id for bottomToolBar is created when you try to reference it:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/mainToolBar"
android:layout_above="#+id/bottomToolBar"
android:id="#+id/fragmentsContainer"
/>
Source: https://developer.android.com/guide/topics/ui/declaring-layout#id

Related

Problems with reusing IDs of nested elements in <include> layout in multiple fragments?

Being new to android development and working on approach of navigating user to multiple fragments within an activity, I came across a doubt regarding including a layout file within multiple(not all) fragments using as:
<include layout="#layout/toolbar_layout" />
toolbar_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/appBarLayout"
android:id="#+id/inboxAppBar"
android:elevation="2dp"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
app:menu="#menu/actionbar_icon_menu"
android:id="#+id/inboxToolbar"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<androidx.appcompat.widget.SearchView
app:queryHint="Search here..."
app:iconifiedByDefault="false"
app:commitIcon="#drawable/ic_launcher_foreground"
android:theme="#style/searchView"
style="#style/searchViewStyle"
app:queryBackground="#color/transparent"/>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#color/white"
android:id="#+id/InboxTabLayout"
app:tabMode="scrollable"
app:tabIndicatorFullWidth="false"
app:tabTextAppearance="#style/TabTextStyle"
/>
</com.google.android.material.appbar.AppBarLayout>
Which gives output shown below, after adding NavigationButton programatically:
Now that I had to use Ids of Toobar and TabLayout for adding NavigationUp button and setting up ViewPager2 respectively in all fragments, I had to include above layout.
Its working absolutely fine but I needed to know if reusing Ids for setting up eventListeners and other things separately in all fragments code could cause any problem since we are referencing same Id in all fragments for different tasks.
Also, kindly mention the right approach of doing what I intend to do.

How to find menu height

i want to create fixed navigation layout, but there is a problem when i rotate device, margin-top of this layout is more than it has to be, so the question - how can i find height of this dark-blue header ( to use it as margin ), or maybe there is another way
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="55dp"
android:layout_height="80dp"
android:background="#drawable/panel_border"
android:layout_alignParentTop="true">
If I were you, I would go with this:
android:layout_marginTop="?attr/actionBarSize"
This attribute indicates correct height of the ActionBar / Toolbar.
The Toolbars height may be different for different APIs and that's the reason you should use ?android:attr or ?attr (when using support library).
? means that it comes from internal Android resources.
Let the android support library do it for you. Like this
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<! -- Your View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/panel_border"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
take a look at this

Toolbar title not changing in Android

I am trying to set the title of Toolbar in Android but the app crashes (null pointer exception). I have tried many stackoverflow same question but none seemed to work for me. I used the code below
toolbar = (Toolbar) findViewById(R.id.toolBarDataProvider);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Notice");
Logcat
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference at com.akapoor.kiittimetabletest1.Notice.onCreate(Notice.java:31)
XML
<?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"
android:orientation="vertical"
tools:context="com.akapoor.kiittimetabletest1.Notice">
<include
android:id="#+id/include"
layout="#layout/toolbar_data_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<TextView
android:id="#+id/tvServerText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</ScrollView>
<Button
android:id="#+id/bServer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Refresh" />
</LinearLayout>
toolbar_data_provider.xml
<?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/toolBarDataProvider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
so you don't have Toolbar... maybe show us your code for style or xml layout for this Activity
edit:
maybe there is some id overwritting, when you using
<include
android:id="#+id/include"
this is your new id for your Toolbar (first view in included in separated files). try to use merge (wrap whole Toolbar) or just do not set new id in include
more HERE
In fact, you did not set your toolbar as a SupportActionctionBar, because you included it. To acces included layout you need to:
1) Get included layout:
View icludedLayout = findViewById(R.id.include);
2) Get Toolbar from it:
View toolbar = findViewById(R.id.toolBarDataProvider);
EDIT
If you haven't mistaken in question, and your tollbar xml file is named as Toolbar.xml you need to change include tag to:
<include
android:id="#+id/include"
layout="#layout/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You must set the xml file name for layout attribute (Which in your case the xml file name is Toolbar)
EDITED
Replace this:
<include
android:id="#+id/include"
layout="#layout/toolbar_data_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
With the following code (In this code, I set the id of include tag to toolBarDataProvider instead of include):
<include
android:id="#+id/toolBarDataProvider"
layout="#layout/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And the Toolbar.XML file should contains the following code (The toolbar id attribute is not needed):
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
Though setSupportActionBar you pass null parameter. Thus you toolbar is null. In this way we can make a conclusion that findViewById cannot find view with id toolbarDataProvider. I suggest you need to check validity of your id. Or provide information about your xml.
EDITED
So, as I said you don't have id toolbarDataProvider. When you use include tag you import view described in other xml, but you can override attributes to it. In your case you override id attribute from toolbarDataProvider to include. Try to change your code to see if it works correctly:
toolbar = (Toolbar) findViewById(R.id.include);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Notice");
This is a very very late answer but for anyone still looking for help on this question... assign your toolbar the id of include. This worked for me, also be sure to set your toolbar in styles to NoActionBar.
toolbar = (Toolbar) findViewById(R.id.include);

Spinner in Toolbar Example in Xamarin for Android

I am trying to implement a spinner in the toolbar of my navigation drawer.
My initial toolbar layout included the following xml code:
<?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/languagetoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<Spinner
android:id="#+id/toolbar_languageSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/array_languageSelect"
android:gravity="right" />
</android.support.v7.widget.Toolbar>
The thing is that there does not seem to exist to be an example that seem to show the implementation method using Xamarin. I have tried to use the following resources :
https://dabx.io/2015/01/02/material-design-spinner-toolbar-style-fix/ &
http://android-pratap.blogspot.co.za/2015/01/spinner-in-toolbar-example-in-android.html
x
The short comings of the two examples is that using Xamarin I have found that I cannot use .setAdapter as it only exists as an interface
The look that i want in the tool bar is similar to the image in the first link but having the spinner to the extreme right.
The solution that I finally found is explained perfectly by prathap in http://android-pratap.blogspot.co.za/2015/01/spinner-in-toolbar-example-in-android.html. The solution involves adding the spinner to the toolbar layout like below:
<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/languagetoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<Spinner
android:id="#+id/toolbar_languageSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/array_languageSelect"
style="#style/MyTheme"
android:gravity="right" />
</android.support.v7.widget.Toolbar>
Then the code that follows will require the user to populate the spinner in the code from their activity.

Can I Use The INCLUDE LAYOUT To ScrollView In My Activity?

Good day, I'm having a problem with my xml layout. I create an another xml file share_app_scroll(ScrollView Layout) and included it in my ShareApp.xml .
The Problem is, my toolbar/action bar don't appear. All I can see, is my scrollview layout that I created. Is this possible to include the scrollView layout?. Here's my Code.
<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:fitsSystemWindows="true"
tools:context=".activities.ShareAppActivity">
<!--Include ActionBar/Toolbar-->
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:elevation="8dp"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
<include
android:id="#+id/share_scrollview"
layout="#layout/share_extend_scrollview"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
Sorry for the blurred picture.
Thank you in advanced.
You are using relative layout as your parent layout therefore you have to align your include layout below toolbar.Like this
<include
android:id="#+id/share_scrollview"
layout="#layout/share_extend_scrollview"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar"/>
The problem will be in the manifest file.
Check if the java file for your xml has a android:theme="#style/AppTheme.NoActionBar" in the manifest. remove the no action bar and it will appear. if you are talking about during the design view. next to the api level there is app theme. change that to any except .NoactionBar

Categories

Resources