I have a simple MainActivity with a simple main layout like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<ImageView
... />
</androidx.constraintlayout.widget.ConstraintLayout>
I need to activate Noty on this activity. What I need to know is how to create RelativeLayout "yourLayout" in its simple exmple in Kotlin.
Noty.init(YourActivity.this, "Your warning message", yourLayout, Noty.WarningStyle.SIMPLE).show();
Thank you!
Add an android:id attribute to your ConstraintLayout tag:
android:id="#+id/root"
Now you can fetch a reference to that ConstraintLayout in code:
val root: ConstraintLayout = findViewById(R.id.root)
Noty.init(this#MainActivity, "Your warning message", root, Noty.WarningStyle.SIMPLE).show()
Related
ISSUE
java.lang.IllegalArgumentException: Navigation action/destination xxxx/action_scanFragment_to_addVehicleFragment cannot be found from the current destination xxxx/addVehicleFragment
The error occurs when I do
findNavController().navigate(R.id.action_scanFragment_to_addVehicleFragment)
in scanFragment. Which means that current destination is addVehicleFragment, but it should be scanFragment.
I am clueless on how to approach this. See my earlier question for some troubleshooting and what really goes on in scanFragment:
Kotlin - fragment lifecycle navigation issues; why does child fragment become current destination?
I suspect my navigation setup is wrong, but I can't find the solution anywhere.
I'm posting my entire navigation implementation/code below.
My nav_graph design:
My nav_graph XML:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/scanFragment"
android:name="xxxx.ui.scan.ScanFragment"
android:label="#string/tab_bar_first_item_title"
tools:layout="#layout/fragment_scan" >
<action
android:id="#+id/action_scanFragment_to_addVehicleFragment"
app:destination="#id/addVehicleFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_left" />
</fragment>
<fragment
android:id="#+id/addVehicleFragment"
android:name="xxxx.ui.scan.AddVehicleFragment"
android:label="#string/add_vehicle_fragment_title_string"
tools:layout="#layout/fragment_add_vehicle">
<action
android:id="#+id/action_addVehicleFragment_to_scanFragment"
app:destination="#id/scanFragment" />
</fragment>
<fragment
android:id="#+id/mainFragment"
android:name="xxxx.ui.main.MainFragment"
android:label="#string/tab_bar_second_item_title"
tools:layout="#layout/fragment_main" />
<fragment
android:id="#+id/profileFragment"
android:name="xxxx.ui.profile.ProfileFragment"
android:label="#string/tab_bar_third_item_title"
tools:layout="#layout/fragment_profile">
<action
android:id="#+id/actionMyVehicles"
app:destination="#id/myVehiclesFragment"
app:enterAnim="#anim/from_right"
app:exitAnim="#anim/to_left" />
<action
android:id="#+id/actionMyRooms"
app:destination="#+id/myRoomsFragment"
app:enterAnim="#anim/from_right"
app:exitAnim="#anim/to_left" />
</fragment>
<fragment
android:id="#+id/myVehiclesFragment"
android:name="xxxx.ui.profile.MyVehiclesFragment"
android:label="fragment_my_vehicles"
tools:layout="#layout/fragment_my_vehicles" >
<action
android:id="#+id/action_myVehiclesFragment_to_profileFragment"
app:destination="#id/profileFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_right" />
</fragment>
<fragment
android:id="#+id/myRoomsFragment"
android:name="xxxx.ui.profile.MyRoomsFragment"
android:label="fragment_my_rooms"
tools:layout="#layout/fragment_my_rooms" >
<action
android:id="#+id/action_myRoomsFragment_to_profileFragment"
app:destination="#id/profileFragment"
app:enterAnim="#anim/from_left"
app:exitAnim="#anim/to_right" />
</fragment>
</navigation>
My Main Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/main_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_gray"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/tabBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/tabBar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
app:elevation="2dp"
app:itemBackground="#color/light_gray"
app:itemIconSize="30dp"
app:itemIconTint="#color/tab_bar_icon_tint_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/tab_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var viewBinding: ActivityMainBinding
private lateinit var tabBar: BottomNavigationView
private lateinit var navHostFragment: NavHostFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
println("MainActivity || onCreate ||")
// Initialize viewBinding object
viewBinding = ActivityMainBinding.inflate(layoutInflater)
// Hide the default navigation bar; we implement our own
supportActionBar!!.hide()
viewBinding.loadingPageLayout.visibility = View.GONE
setContentView(viewBinding.root)
setupBottomBarNavigation()
}
private fun setupBottomBarNavigation() {
tabBar = viewBinding.tabBar
navHostFragment = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
val navigationController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(setOf(R.id.scanFragment, R.id.mainFragment, R.id.profileFragment))
setupActionBarWithNavController(navigationController, appBarConfiguration)
tabBar.setupWithNavController(navigationController)
val navHost = supportFragmentManager.currentNavigationFragment
println("Current navigation fragment: $navHost")
}
override fun onSupportNavigateUp() =
Navigation.findNavController(this, R.id.navHostFragment).navigateUp()
Please help a desperate fellow out!
If someone had the same issues due to multiple clicks on the screen. It can be resolved by checking the current destination first before navigating
For example
Fragments A, B, and C
navigating from A to B while clicking on a button in fragment A that navigates to C might lead to crashes in some cases
for that you should check the current destination first as follows:
if(findNavController().currentDestination?.id==R.id.AFragment)
findNavController().navigate(
AFragmentDirections.actionAFragmentToCFragment()
)
The problem was not in Navigation Components but instead - I think - in my ImageAnalyzer. Turns out I accidentally allowed multiple calls to findNavController.navigate(). Now that I fixed that, my navigation issue is gone.
Although everything works well in android 8 and lower, but my RecyclerView doesn't load anything in android 9.
because I used Picasso in my recyclerView Adapter, I added code bellow to my AndroidManifest but nothing changed:
android:usesCleartextTraffic="true"
this is my RecyclerView XML code which is located in a MotionLayout:
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F6F6F6"
android:elevation="10dp"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_challenge"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
note that I have two recyclerView in my layout, the first one, works well
but the second one doesn't work!
Define a xml file, then add to your manifest
->> android:networkSecurityConfig="#xml/network_security_config"
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</network-security-config>
i am a new to Kotlin Android development,i added recycler view to my project and then move the icon to the layout according to the tutorial i was watching but it not displaying anything except gray background and this black text "andriod...recycler" instead of displaying the list items samples as in the tutorial
i suspect it the api level, the android studio version is 3.1
it on Api28, here is the activity_hobbies.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
this is HobbiesActivity.tk
package org.blessedmedia.www.kotlinapp
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class HobbiesActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hobbies)
}
}
if you need any more file to provide solution.kindly comment
I've messed around as best I can but I can't find a way to embed the native xamarin.android CardView control within xamarin.forms xaml. Following is my code:
my namespaces
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:androidWidgetv7="clr-namespace:Android.Support.V7.Widget;assembly=Xamarin.Android.Support.v7.CardView;targetPlatform=Android"
xmlns:androidLocal="clr-namespace:MyApp.Droid;assembly=MyApp.Android;targetPlatform=Android"
My Xaml
<androidWidgetv7:CardView x:Arguments="{x:Static androidLocal:MainActivity.Instance}" View.HorizontalOptions="Center" View.WidthRequest="600" View.HeightRequest="300">
<androidWidget:TextView x:Arguments="{x:Static androidLocal:MainActivity.Instance}" Text="Simple Native Color Picker" TextSize="24" View.HorizontalOptions="Center"/>
</androidWidgetv7:CardView>
I get the following error
Exception is: XamlParseException - Position 32:18. Can not set the content of androidWidgetv7:CardView as it doesn't have a ContentPropertyAttribute
There are zero examples on the web showing how to do this. Anyone know?
You can't. but you can implement a similar version of it like how they did here.
you can find alternative versions here and here.
Don't expect to get android animations/touch gestures here.
<?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"
android:gravity="center_horizontal"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="245dp"
android:layout_gravity="center_horizontal">
<TextView
android:text="Basic CardView"
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</android.support.v7.widget.CardView>
</LinearLayout>
I have a custom view based on GridLayout:
package com.mycompany.myapp;
...
public class GridLayoutForceSizeCells extends GridLayout
{
}
I have defined an xml with custom attributes for this class:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="GridLayoutForceSizeCells">
<attr name="totalImages" format="integer" />
<attr name="desiredColumnCount" format="integer" />
<attr name="spacing" format="dimension" />
<attr name="autoGenerate" format="boolean" />
</declare-styleable>
</resources>
Then, I have a layout using this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mycompany.myapp.GridLayoutForceSizeCells
xmlns:grid="http://schemas.android.com/apk/res/com.mycompany.myapp"
android:padding="3dp"
android:id="#+id/cellsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
grid:desiredColumnCount="20"
grid:totalImages="0"
grid:spacing="1dp"
android:orientation="horizontal"/>
</LinearLayout>
The problem I am encountering is that when I change applicationId through a gradle flavor custom attributes are not resolved. I have read in gradlew help that the application package is the one used for R when it is decoupled using gradlew applicationId entry. So, applicationId should not affect my custom attributes package. But when I set applicationId to anything different from com.mycompany.myapp an error arises claiming that attributes can't be resolved.
Has anybody found a solution to this?
Cheers.
Finally fix it using xmlns:grid="http://schemas.android.com/apk/res-auto" to let it resolve the package for me in the layout file instead of the full package name.