Is it possible to pass parameter to Fragment using Navigation?
Fragment:
class ExampleFragment(val nExampleFragmentParameter: ExampleFragmentEnum):
where ExampleFragmentEnum has "HOME, TOP_NEWS, VIDEO_NEWS"
I want to use the Navigation component as Navigation.findNavController(mNavHostFragment).navigate(R.id.exampleFragment, args);
also passing the ExampleFragmentEnum.TOP_NEWS as parameter
also the navigation xml contains
<fragment
android:id="#+id/exampleFragment"
android:name="package.ExampleFragment"
android:label="example_fragment"
tools:layout="#layout/fragment_example">
<argument
android:name="exampleFragmentEnum"
app:argType="package.ExampleFragmentEnum"
android:defaultValue="HOME" />
</fragment>
Ps. I know you can pass data with args but I wonder if it's a way with the parameter
Related
fragmentManager.beginTransaction().replace(R.id.fragment_container_view,mapFragment).commit();
As we know that, this way we can load or replace Fragment
But
In this snippet we are passing fragment container id.
Is there any way
to load fragment without passing ID or pass FragmentContainerView instead of id
you can use navigation graph action to navigate between fragments with one line of code like that :
Navigation.findNavController(this).navigate(R.id.action_fragment1_to_fragment2)
provided that :
you have a navigation graph that looks like this
<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/fragment_1">
<fragment
android:id="#+id/fragment_1"
android:name="Your.Package.Fragment_1"
tools:layout="#layout/fragment_1_layout">
<action
android:id="#+id/action_fragment1_to_fragment2"
app:destination="#id/fragment_2" />
</fragment>
<fragment
android:id="#+id/fragment_2"
android:name="Your.Package.Fragment_2"
tools:layout="#layout/fragment_2_layout">
</fragment>
</navigation>
and you've written the correct code to setup your activity with navigation components.
If you don't know what is navigation components I highly recommend taking the navigation lab on android developers where it should instruct you how to setup your project with it.
Learn Jetpack Navigation | Android Developers (Java)
for more information about navigation components and fragments follow these links and start reading Android Developers Tutorials.
Fragments| Android Developers
Navigation Components | Android Developers
As android defines attributes inside the attrs files and I have seen that Button has attribute onClick listed. Is there a way to create custom attributes referencing functions as so and how does on go about implementing it.
<Button
...
android:onClick="onClickFunction"/>
What I want to have
<CustomView
...
android:onItemClick="onItemClicked"/>
attrs.xml
<attr name="onItemClick" format="string"/>
How does one create attach this function as a listener to the view?
I create custom view and use it in some xml activity then set android:onClick="buttonOnClick". I defined buttonOnClick as public and write it in its activity and set tools:context.
Activity.java
public void buttonOnClick(View view) {
validate(view);
}
layout.xml
<LinearLayout 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"
...
tools:context="com.test.myapp.Activity">
<com.test.myapp.GifImageViewComp
....
android:onClick="buttonOnClick" />
it's working on Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP but in other OS version, custom view xml onclick attribute try to find handler from view class instead of activity.
I don't want use implementation of onClick or setOnClickListener on every view, i want use onClick in layout of activity
It's just not clear the relationship between a button in your xml and
a method in your activity that reacts to the click events unless you
explicitly see it defined in your Java file. With the android:onClick
approach you can even forget that you have a button in your layout or
which is the method that is handling its onClick event.
Source
You should use OnClickListener instead of android:onClick .
Interface definition for a callback to be invoked when a view is
clicked.
This method takes the reference to the Listener and registers a callback to be invoked when the View is clicked.
FYI
Make sure you set android:clickable="true" in XML . you should specify this attribute .
<com.test.myapp.GifImageViewComp
android:clickable="true"
android:onClick="buttonOnClick" />
In my xml file I have below list view.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:divider="#color/blue"
android:dividerHeight="2dip"
android:background="#drawable/list_divider"
>
</ListView>
And in my java class I try to access the view as follows
ListView myListView = (ListView) findViewById(R.id.list);
I get a error for this saying "list cannot be resolved or is not a field". But if I change the id as android:id="#+id/list"
How can I access the view by using the id this way android:id="#android:id/list".
Also whats the basic difference between two ways of defining ID. Thanks in advance
Assuming your Activity extends ListActivity, you can access it with the convenience method
ListView myListView = getListView();
otherwise you would access it with android.R.id.list
Also whats the basic difference between two ways of defining ID
#android:id references built-in android resources
#+id is a user defined id and will add it to the R.java file to be accessed later with #id
See this post about more on that
You have to mention your id like
android:id="#+id/list"
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
Android resources id are referenced by #android:id/list in the xml
and android.R.id.list in the Java code.
User defined id are referenced by #+id/list in the xml and
R.id.list in the Java code. The + implies that the resource id value will be generated at compile time.
id define in resource files as <item type="id" name="list" /> will
be referenced as #id/list in the xml and R.id.list in the Java
code.
Is there a way I can store/retrieve arbitrary values from a view, similar to HTML5 data attributes?
This way, I can have a view call generic onClick() methods and the method can retrieve the related data.
e.g:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_france" />
...
I would like to be able to retrieve a value from the one that got clicked.
public void setCountry(View v){
//retrieve data somehow
}
You can use the View's tag property. It is intented for that purpose.
For example:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany"
android:tag="Germany" />
...
public void setCountry(View v) {
System.out.println(v.getTag());
}
Derive from ImageView, add a member variable "country" and accessor methods. Then you can reference your class in the layout file:
<com.foo.MyImageView ... />
Or use the tag property. It lets you attach an arbitrary object to a control, but there's no type safety, and no accessor methods with appropriate names. You can specify a string tag right there in the XML file.
Or use some kind of mapping from control ID to country.