Is there an XML equivalent to HTML classes? - android

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

Related

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

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.

Is there an Android design pattern to handle multiple fragments in a single activity?

I inherited some code at work and I have a question about some implementation. The application I'm working on has an Activity that contains about 15 different fragments. The logic in the Activity handling these fragments can roughly be summarized with the following pseudocode:
if (button_1 selected) {
load fragment_1;
} else if (button_2 selected) {
load fragment_2;
} else if (button_3 selected) {
load fragment_3;
} ...and so on x15ish
My question is: does there exist some kind of Android design pattern to handle situations like this? The code works; however, I don't feel too comfortable with a giant if/else or case statement. I saw this question and it seems very similar to the problem that I'm having. I did quite a bit of searching on the internet but I haven't found examples or best practices for this kind of scenario.
If someone can point me in the right direction or have some suggestions; that'd be awesome. Thanks!
For each button in your layout you can assign a method in your activity:
<Button
...
android:onClick="startFragmentOne" />
Then implement those methods:
public void startFragmentOne(View view) {
//TODO
}
You should not check which button has been selected but rather use the button's onClickListener to select the correct fragment.
buttonForFragment1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// select fragment 1 here
}
});
In regards to the question, this is not a level of design patterns but rather implementation details (idioms) and you correctly recognized your code as a smell and I think one possible solution which does not qualify as a pattern is the code above.

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
}
}

how to exchange data between activity and layout xml in android

I need to exchange data between activity and it's layout xml in Android. But I do not find a way to do this in Android. For example, views and controller in mvc pattern always has a way to exchange data between. So I am wondering is there any way to exchange data between them to should I refresh my mind and realize there is no such way?
use below code to get value from layout in your activity
String value;
EditText editText= (EditText) findViewById(R.id.editText);
value=editText.getText();
code in xml example:
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
in java class (like onCreate()):
ImageView image = (ImageView) findViewById(R.id.image_view);
Then you can do what you want with image
I believe you're unsure exactly what you're asking. If you want to exchange information, such as ID or text entered into a textfield then any good android tutorial should be-able to demonstrate this. Considering your last comment I think you're talking about GET and POST based technologies which can be done usingREST andSOAP, or both if you want.
This questions answer has a good implementation and definition of what both of these webservices are.
P.S. If this is what you're looking for then upvote that answer.
As some additional info, the "view" (XML Layout file) gets set by your activity initially on your activity's onCreate method. Right after you call it's parent method (super.onCreate()).
To maintain scope throughout the activity I tend to declare all the layout widgets that I need to the activity to interact with outside of any methods and within the class.
TextView textWelcomeMessage;
public void MyActivity(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// before calling setContentView() we have the option to change
// window features ex requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my_activity);
// Now to set the textview
textWelcomeMessage = (TextView) findViewById(R.id.textWelcomeMessage);
// Set some data
textWelcomeMessage.setText("Hello, welcome to my activity");
}
It's not exactly like traditional php style mvc since static typing changes thing up a bit and we have to worry about scope. But the core principles can still apply as far as data abstraction and separation go. Hope this helps =)

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