I have an android question:
I've successfully created a countdown kitchen timer activity, however my goal is to have an activity that has 3 timers on it that all work independently. I created a separate layout just for the timer itself and moved the timer code into a class and I've used layoutinflater to create the views and then added them into the linear layout for the activity. I get the layouts fine, however there's no functionality. There doesn't seem to be anything that ties the class code to the activity.
How should I approach this? Can anyone point me to some working example code?
thanks in advance!
Assuming you have inflated a view from an XML layout you could call findViewById(Int) on
the inflated view to get a hold of any view in that layout. Then you could do the wireing manually, for instance adding a onClick listener to some button:
inflatedView.findViewById(ID_OF_SOME_BUTTON_IN_THE_INFLATED_VIEW).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// DO SOMETHING WHEN BUTTON IS CLICKED
}
});
This was simply a case of not knowing enough. What I did in the end was to extend relativelayout in my class and fix my constructors to inflate the view in the class with the context of the activity. Then the view was added into the correct layout in the activity.
Thanks for all the good suggestions
Related
In my application's main layout I have a button of Search task. Now when user clicks that button I want to change the layout xml file to search_layout.xml.
I don't want to create a new activity for Search task and want to use my activity_main.java class for its coding. So how do I inflate search_layout from existing activity_main.xml .
My activity_main layout is Coordinating layout with Collapsing toolbar and Recycler view.
In my search_layout.xml I have a Simple relative layout.
So how to deal with this ? I searched for this but everywhere I get how to inflate view adding to an existing view. But in my case I want to totally change view.
Thanks
You can simply call setContentView(R.layout.your_search_layout) in the click listener of your button.
Example -
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.your_search_layout);
}
});
However, it is always good practice to make your code modular, hence you should consider using Fragments to achieve this.
You can have a look at this question, but, this is not a good practice to follow and you should consider using Fragment or any other mechanism. By doing this you are cluttering your Activity's logic with search logic.
There are two ways to do it: one is make frame layout the parent tag for your layout and then create two layouts: one that you have already created and and one for search layout framed over it and make visibility for search layout GONE and when you click search make its visibility visible.
The second method is to create the new XML layout file for search layout and inflate it like this:
View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
and elements of this layout will go like this:
Button button=(Button)v.findviewbyid(R.id.your_button_id);
Changing visibility of view will help in your case where you totally want to change the views
I think in your case, you should fragments to handle this situation.
Read this for more details on how to replace Fragments using FragmentManager.
Here's a nice example on how to change fragments in android :
This question already has answers here:
Android onClick in XML vs. OnClickListener
(8 answers)
Closed 4 years ago.
I am learning how to develop apps in Android Studio and have just begun.
I learnt 2 methods of handling button click events. One is to implement OnClickListener while the other is to go to XML and just use Android:OnClick. The second method is far easier for me. Is there any advantage of the first method or is just knowing the second method enough?
Thanks
Nigpig
Difference Between OnClickListener vs OnClick:
OnClickListener is the interface you need to implement and can be set to a view in java code.
OnClickListener is what waits for someone to actually click, onclick determines what happens when someone clicks.
Lately android added a xml attribute to views called android:onclick, that can be used to handle clicks directly in the view's activity without need to implement any interface.
Both function the same way, just that one gets set through java code and the other through xml code.
setOnClickListener Code Implementation:
Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourMethod(v);
}
});
public void yourMethod(View v) {
// does something very interesting
}
XML Implementation:
// method to be written in the class
public void yourMethod(View v) {
// does something very interesting
}
//XML file
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="yourMethod" />
Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.
onClick and OnClickListeners have the same functionality when it comes to simple programs. But when it comes to complex programs, onClick cannot provide the functionality of OnClickListeners.
There are a couple reasons why you might want to programmatically set an OnClickListener. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting an OnClickListener that doesn't do anything.
When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button's behavior from somewhere other than its host activity. This is a major part of Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.
Kindly search for the answer first before asking. Here is enough explanation what you are searching for. Have a look Here
its possible to add button to extended view class? I have new class which extends View and want to add new button. How I could solve it?
Ok, I get it. One more question, Its possible to mix activity with onDraw method? I want to create signature activity and add button to save it.
Your custom View needs to extend ViewGroup or another class that extends ViewGroup, such as LinearLayout and RelativeLayout.
One possibilty would be getting the View and adding an Element with
this.addView(.../*new View*/)
Be sure, that the pointer this is the created View.
Is that what you mean?
I have a linearlayout, which contains my TextView I want access.
In this linearlayout there is a listview, where every item is a linearlayout and contains also custom views.
Deep in there is a button with an onclickListener. After performing onClick(), I want to call a method which sets the text of my textview.
At the moment I am doing it like this:
(View)this.getParent().getParent().getParent().getParent().getParent().getParent().getParent().findViewById(R......)
It works, but it looks bad. Is there any possibility to do it a better way?
Image
Yes, its ugly, don't do that.
In your onClick(View v) get hold of TextView by asking Activity holding the layout:
#Override
public void onClick(View v) {
TextView tv = (TextView) YourActivity.this
.findViewById(R.id.your_text_view_id);
tv.setText("blabla");
}
if you do it from fragment instead of activity use :
YourFragment.this.getView().findViewById(R.id.your_text_view_id)
Or simply create a member TextView mTv, initialize it in your #OnCreate and use it everywhere as suggested in comments to your question
It looks bad, because it is generally a bad idea for a view to know something about outside of it self. So it is probably okay to know about your children but not back.
You'd better keep your views as simple as views, without any other logic. Do your logic in Activities/Fragments
See Circular dependency
Ok ive just started programming some android in Eclipse, and im a little dumbfounded about this. Here are two ways that the button programming has been written in this book, they both work fine, except the first one seems simpler and shorter. Which is better and why?
View newButton = findViewById(R.id.main_new_button);
newButton.setOnClickListener(this);
Button newButton= (Button) this.findViewById(R.id.main_new_button);
newButton.setOnClickListener(this);
The two forms are equivalent. In both cases findViewById returns a View object, the only difference is that in the second version an explicit cast is made to Button, a subclass of View.
As you can see in the documentation, View is the superclass of TextView, and TextView is the superclass of Button.
Which one is better? it depends. If you need to use functionality specific to a Button, then the second way is preferred. On the other hand, if a View object suffices, then use the first way.
I like your way of thinking.In android every widget is view.I will tell you in step--
1)
First important thing is Every Button is View but not necessarily Every View should button
View newButton = findViewById(R.id.main_new_button);
newButton.setOnClickListener(this);
you are using it that does not mean that newButton is always a Button.We can stroe any View reference (for ex. LinearLayout, ImageButton etc) to newButton
But In second case
Button newButton= (Button) this.findViewById(R.id.main_new_button);
newButton.setOnClickListener(this);
newButton is definitely should be Button if R.id.main_new_button is button ID in XML
2)-
As i said we know every Button is View but we do not know which View is button.That's it ask you to cast findViewByid.And your first case discourage as every view has its own method that can not be used if we generalize this view.So we have to use specifically Button.
Hope you got it
If you want a button, you need the latter formation. The formmer formation is ok but it's not the perfect method.Because the view's usage is not only for button.
In the second case, you have cast the result to the correct type. This allows you to access Button-specific methods. The first one returns just a View object although it is a Button. So you will not have access to Button-specific functionality.
A Button is a widget which extends a TextView. A TextView extends a View. If you go for the Button class, you will be exposed to more specific member functions rather than the aforementioned super classes. As far as what those specific methods are... you would have to go into the android.widget.Button class to explore that =). Upon doing this, all I see are three constructors (for explicitly declaring the button) -- all other member functions are identical.
public Button(android.content.Context context);
public Button(android.content.Context context, android.util.AttributeSet attrs);
public Button(android.content.Context context, android.util.AttributeSet attrs, int defStyle);
But if you don't want to confuse yourself down the line when you come back to this code after 3 months, or the other developers working along side with you, the second method is the proper method to use:
Button newButton= (Button) this.findViewById(R.id.main_new_button);
newButton.setOnClickListener(this);
The button which is built by View is a view, not a button widget.If you create a button from a View Class,you could not call the functions of Button Class. View Class and Button Class both have the "setOnClickListener" function,but that function of View Class is not the same as it from Button.