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?
Related
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
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" />
I have a compound control that essentially combines a Button with a ProgressBar. It includes a background property that I have declared in attrs.xml as:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<declare-styleable name="MyControl">
<attr name="background" format="reference" />
</declare-styleable>
</resources>
In the constructor of my control, I then pull the background out of the TypedArray and apply it.
All this worked fine until recently, when I had to add in a dependency on the v4 support library. Now I get this build error in my attrs.xml file:
Error APT0000: Attribute "background" has already been defined (APT0000)
Why is this? And what can I do as an alternative so that consumers of my compound control can set the background?
It's a conflict with an internal library that also defines the 'background' attribute.
For more details visit : https://groups.google.com/forum/#!topic/actionbarsherlock/_N0hn47zx6w
The attribute "background" has already been defined in the support library. So you dont have to define that again. you just add the attribute without defining it, like this,
If you want to use app:background in the custom view, then
<attr name="background"/> is enough in the attrs file.
If you want to use android:background in the custom view, then use this line in attrs.
<attr name="android:background"/>
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.
I have a preference screen containing a preferenceList. i want the user to be able to add new friends to that list.
I have been unable to find any examples of how i can achieve such a thing doing run time.
Does anyone have an example or able to give me an example of how i would achieve this?
Here is my preference:
<PreferenceCategory android:title="Your competetion">
<PreferenceScreen
android:key="secondPrefScreen"
android:summary="click here to see your friends"
android:title="Friends" >
<ListPreference
android:key="friendList"
/>
</PreferenceScreen>
You can access the ListPreference object at runtime and use the setter and getter methods to update the entries. ListPreference Docs
Assuming you are inside a PreferenceActivity:
PreferenceScreen root = this.getPreferenceScreen();
ListPreference list = (ListPreference) root.findPreference("secondPrefScreen");
// do stuff with list