is there a android:onClick="abc", but for pressing and holding? - android

I have a single button in my app. (simplified) it looks like this:
<ImageButton
android:id="#+id/SendButton"
android:onClick="getCommand"
app:srcCompat="#drawable/circle_send" />
My java looks like this:
public void getCommand(View view)
{
//my code here
}
My question is, is there an xml tag similar to android:onClick="abc", but that works for press+holds, separately from a tap? I want to have something like this:
<ImageButton
android:id="#+id/SendButton"
android:onClick="abc"
android:onPressAndHold="xyz"
app:srcCompat="#drawable/circle_send" />
thanks for your help :)

No, sorry. In general, we do not use XML attributes for event handlers. android:onClick is the only one built into Android, and it has been obsolete for years.
If you really want to do it, you could use data binding and create a BindingAdapter to give you support for an app:onLongClick attribute. However, the syntax of the binding expression that you would use with app:onLongClick would be more complicated than what you have for android:onClick.

Related

Which is more recommendable in kotlin?

if i have this button:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button" />
which is more advisable in kotlin when it comes to calling views?
this:
val buttonVar: Button = findById(R.id.buuton)
buttonVar.setOnClickListener{
//my code
}
or:
button.setOnClickListener{
//my code
}
When it comes to performance in Kotlin , this is more advisable
button.setOnClickListener{
//my code
}
Because calling views by their ID directly will generate a local view cache.
So the when the view is called the first time kotlin plugin will execute findViewById just a single time, and the next time the view gets called, it will get recovered from the cache . So accesing that view will be faster.
You can refer to this link for more informations enter link description here
I hope this will help you, don't forget to accept the answer if it helps you.
First one is recommended now. The reason is because if you do it the second way, you would be using kotlinx synthetic which is no longer a recommended practice. Source

Is there an XML equivalent to HTML classes?

Sorry if this is a really obvious question, but I've been trying to find something like it in a number of places, and I'm not sure if it just doesn't exist or if I'm using the wrong language to describe what I need.
If, for example, I have a number of TextViews in different parts of my activity, and I want all of them to open the same activity when clicked on, how would I do this without specifying each of their IDs individually in an if statement in the Java? In HTML I would use a class attribute to group them together, but I can't find a similar feature in XML.
Thanks for your help!
If your intention is to prevent code repetition and tedious code repetition, then one way is to use ButterKnife. Something link below would work:
#OnClick({R.id.view_id1, R.id.view_id2, R.id.view_id3})
public void onClick(View view) {
// TODO: Handle click
}
Or, as the other answer suggests, you can have android:onClick attribute on each of those views which points to same method in Java code.
Write your xml code of EditText as follows
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Fourth EditText"
android:onClick="editTextClick"/>
And in Java file write method
public void editTextClick(View v) {
// does something very interesting
Log.d("MainActivity","EditText pressed");
}
I hope this will help you,
Happy Coding

How to override a button attribute

I should be able to use a #BindingAdapter in Android dataBinding so that i can override a certain attribute. I am able to do it with a cusutom attribute but with a android built-in attribute how is it accomplished?
what i have so far:
in my viewModel i have a method that is annotated with #BindingAdapter and looks like this:
#BindingAdapter({"android:text"})
public static void setText(Button view,boolean language) {//i need to pass one more variable in here for area code , its just a integer, but how ?
if("french".equals(language))//i want to test if french && area code
view.setText("si vous play");
else if ("English".equals(language)) //i want to test if french && area code
view.setText("please");
}
but i have a few problems. Lets see the xml :
<Button
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{`french,844`}"/>
See my issue, i want to pass in more then one parameter to dataBinding. But how ? Do i have to make a POJO object and pass it in ? even if i did that how do i set the object from xml ?
So if someone can just tell me the following i'll be fine:
1. how to pass multiple values to a bindingAdapter and
2. How to override a built-in android attribute in any view.
If you want to set two different values in your BindingAdapter, you should use two different attributes:
<Button
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{`french`}"
app:areaCode="#{`844`}"/>
Then have two different attributes in your BindingAdapter:
#BindingAdapter({"android:text", "areaCode"})
public static void setText(Button view, String language, String areaCode) {
...
}
But it would probably be better to set a different "app:language" as that would be more clear to the developer.

How to define a custom BindingMethod for a TextView?

I am starting to work with the new data binding API. I want to bind on a TextView a custom property where I change the text and background at once. As far I understood the api there is a #BindingMethod annotation for this, but the documentation is there a little weak.
I want something like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Important"
tools:background="#drawable/bg_badge_important"
tools:textColor="#fff"
android:id="#+id/badge"
custom:badge="#{item.badge}"/>
Where this item.badge is a string like important or notice. For i18n I cannot use this string directly but this will help me to choose the right text and the right background image.
Can you give me a small example or a reference how to use this attribute? I just found one answer on Stack Overflow, but this does not answer my question.
Just create a public static method on your codebase and annotate with #BindingAdapter.
Here is an example for fonts:
https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb
Edit by rekire I ended in using this code:
#BindingAdapter({"bind:badge"})
public static void setBadge(TextView textView, String value) {
switch(value) {
case "email":
textView.setText(R.string.badge_email);
textView.setBackgroundResource(R.drawable.bg_badge_email);
break;
// other cases
}
}

include tag in xml file of Android Programming

I have a question regarding the tag. Actually I am new to the Android Programming and I want to use the Concept of Reusability in my Application at several places. I get to know that it is possible by the tag but I don't know how to use that. I have refered some of it's examples from the net but didn't found them quite satisfactory.
Can anybody please make me understand it with a Clear and appearant example!
Thanks
john
Let's say on an activity you have several buttons, all almost doing similar stuff onClick. Now you can use an onClick method, but since you cannot pass parameters in the onClick attribute, you need to put it somewhere else, which is where tag comes in handy.
In your layout you might have:
<Button android:id="#+id/btn1"
android:tag="paramValue1"
android:onClick="myOnClick"/>
<Button android:id="#+id/btn2"
android:tag="paramValue2"
android:onClick="myOnClick"/>
Then you can use one central custom onClickListener (especially if you want to reuse amonst multiple activities) or like in my case just a method in my activity for your buttons that handle the actions for it.
public void myOnClick(View v) {
String param = (String) v.getTag();
....
}
This is especially useful for generic actions, and also if you want to reuse code (i.e. same button listener) amongst multiple classes/activities.
This way you don't rely on a switch/case and checking your button (view) id; staying more independent from your activity itself.

Categories

Resources