Android How to set button click actions from 2nd activity layout - java

I'm new to android and Java, so I'm having problems with on click method. Here's i have 2 activities main and sub having different layouts.
I want to know is there any method to implement on click action on button view which is not from main activity layout
I can able show button view of sub activity layout but its on click action does not works.

You need to create object of your Activity, By using this Object you are able to use activity method.
For more detail; visit this link. How to use method from another Class?
Use this to call your Button's OnClick Action.

What I normally like to do is set the onClickListener programmatically
myButton.setOnClickListener(v -> handleMyButtonClick());
Since you are new to Java you may not be familiar with lambdas. So your code could also look more like is.
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
// maybe call handleMyButtonClick();
}
};
The onClickListener has to be set after the button has been initialized but before you actually click it.

Related

How to have a global click Listener for multiple buttons in the same layout?

Suppose if we are having 50 Buttons in a layout instead of creating click listeners for each buttons.
Eg:
button1.setOnClickListener(this);
button2.setOnClickListener(this);
If we have 50 Buttons like this there will be lot of bloatware code.
Is there any other way to implement this efficiently?
You can loop the view children after setting up your layout, so, lets say that these buttons are all children of your main view then loop all view children, then check for Button instances and assign your listener.
Do I understand correctly that you want all the buttons to have a same clickListener? If yes, then you can created your own button that extends the Android Button class, and initialize clickListener there.
class MyButton(context: Context,...) : android.widget.Button(context,...) {
init {
this.setOnClickListener {
....
}
}
}
Then, in your layouts, use MyButton instead of android.widget.Button

Relation between android:onClick and View.OnClickListener s

Dabbling with Android Studio, and working through a few tutorials , but I don't understand a concept. Oh, and new to java too...haha.
Lets say I have a screen, an activity, MainActivity.xml with a button.
MainActivity.xml has an onClick attribute goForIt defined for the button.
In the MainActivity class I have a method goForIt.
This is where the button being clicked will be responded to.
Inside that goForIt method,
I build an Intent to start another activity,
and fire it off by the statement startActivity(intent)
Questions:
Why do I need a listener? (If I do)? The MainActivity.xml is an explicit directive to a specific method. Or is that a "listener"?
What's the role of the manifest in this? The activity is there... but for what purpose? Again, being able to find the class and method is pretty explicit without any need to consult a lookup like the manifest....?
I'm confused by the Activity XML having an explicit attribute to a specific method in the class, and then at the same time, the Listener saying that if the onClick happens, then do something... they are both trying to do the same thing are they not?
You can use listener or you can specify a method in xml to listen to the click on a view. In both you can the view as the parameter.
2.Manifest file helps you for many purpose:
to specify which activity is to be launched first
to get permissions for accessing internet, getting cal logs, using maps etc,
Specifying the theme or label for each activity and so and so ..
3.Both does the same thing. One is an alternative for other.
This question is waaay to wide and should be (probably will be) closed.
But, here goes:
onClick attribute in xml file is a shortcut for creating a listener (the listener in such case gets created behind your back). You either use that, or a listener done by hand.
Manifest has no role in this (pressing the button). But it is necessary to configure the activity so that it starts when the launcher icon is pressed (among many other things).
Android API looks as if it was never properly designed... it just grew and evolved. So yes, there are multiple, confusing ways to do a single thing.
Using
<Button
android:id="#+id/button"
android:onClick="goForIt"/>
and then
public void goForIt(View v) {
startActivity(...)
}
is exactly the same thing that using:
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(...)
}
});
The first option is just a workaround, a short path to have the same effect.
Internally, it is just doing things the second way - take the button, add a click listener to it, and run it as soon as the user clicks on the button.
When your layout is inflated (after you call setContentView(R.layout.my_layout)) the attribute android:onClick gets internally converted into something like this:
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goForIt(v);
}
});
The only difference is that it does it for you.
The manifest has no role in this context.
Why do I need a listener? (If I do)? The MainActivity.xml is an explicit directive to a specific method. Or is that a "listener"?
Think you have 10 buttons where click is required, so Listener can handle all those types of button click.
What's the role of the manifest in this? The activity is there... but for what purpose? Again, being able to find the class and method is pretty explicit without any need to consult a lookup like the manifest....?
From manifest Android ( application manager ) understands things about your app like, which page is 1st or launcher page.
I'm confused by the Activity XML having an explicit attribute to a specific method in the class, and then at the same time, the Listener saying that if the onClick happens, then do something... they are both trying to do the same thing are they not?
yes they are doing same thing, try to use listener for them.

Can't understand the View view parameter

I am learning android development from Android Developers
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
The sendMessage message method contains a parameter 'View view'? What is a View object and what does it do?
Why is it passed to the method as a parameter and where does it come from?
While writing a click event you might need to know which object is clicked.
In android mostly all the UI components will extend View Class.
So you are getting the instance here
public void sendMessage(View view) {
// Do something in response to button
}
In android we can handle click events by two ways
First way
Providing the method in xml itself. For example
android:onClick="sendMessage"
This is how it happens in the example provided by him.
Second way
We can extend an onClickListener in the Activity or Fragment and we should override onClick method.
See the question "existence of parameter (View view)" for further references.
A View object in Android app development is the building block for a user interface. They are used to create things onscreen for a user to interact with.
http://developer.android.com/guide/topics/ui/overview.html
Edit:
In your case, when the Send button is clicked and it calls the sendMessage function, it passed the View object of the Send button (it passes the View of the object that called the method).
The parameter is the clicked button, that's all. It actually says so in the link that you provided :/
Specifically, the method must:
Be public
Have a void return value
Have a View as the only parameter (this will be the View that was clicked)
I am sure you should have understanded the following method, because it is the basic parameter we often used.
public void sendName(String name) {
//some stuff
}
And this method will be called when the sendName() appeared next time. The only difference is the sendMessage will be called when you click the button, as button is a view exactly, so you have to use View class instead of String class.
sendMessage is a method name
View is your class
view is your parameter
check in xml there are declare Sendmessage button or imageview. and set its property to android:onClick="sendMessage";. so send message is not userdefined method but its a listner method on button or imageview click. so there are View view parameter pass in method.
in that case we con not need to create object and findviewbyid and setonclicklistener class..
thats it....

Go to the next layout when pressing the contents in a listview in Android

I've created a listview extending Activity (only). I want to go to the next layout when I press the contents in listview. What can I do for this?
Android ListView and ListActivity - Tutorial can help you to how to use the list with onitemclick.
When you create the view for each row in your ListAdapter. You can register a OnClickListener that will be called when the user clicks on the row.
View view = inflater.inflate(R.layout.favorite_row, null);
view.setOnClickListener( new View.OnClickListener() {
Override
public void onClick(View view) {
beverageSelected( ((FavoriteBeverageView)view.getTag()).getFavoriteBeverage() );
}
});
view.setTag( new FavoriteBeverageView( view ) );
Using setTag and setId can help you find the object in your list that the user selected. Personally I think it's easiest to use setTag() adding a special object that contains the UI elements within your List row (for example titleTextView, subtitleTextView, image, etc), and add a pointer to the backing object in that special object.
In the example above the FavoriteBeverageView is that special object, and within him there is a FavoriteBeverage object that is the data that backs that list. So in the OnClickListener can easily get the FavoriteBeverage back by just doing a ((FavoriteBeverageView)view.getTag()).getFavoriteBeverage().
You use the onItemClick() for that.
You will want to use OnClickListener
public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
//code here that specifies what layout to go to.
}
EDIT: I found this SO question that may contain some more information for you.
Also, if you would like to learn about intents and loading new views you can take a look at this tutorial
See Stack Overflow question ListView without ListActivity. There's some example code from my application that works well.

How can i create image and link button from code (java) in android?

Android view is in xml file, and we can drag n drop controls to it, but now i want to create image and link buttons from a while loop in java, it means we have to develop interface part from backend, how can we do so?
ImageButton inherits from ImageView and has its own properties compared to Button properties.
you can check properties here,
http://developer.android.com/reference/android/widget/ImageButton.html
Make your activity implement OnClickListener, then in OnCreate() or after that event. use ,
ImageButton btn = new ImageButton(this); or
//ImageButton btn = (ImageButton) findViewById(R.id.imgbtnid)
btn.setOnClickListener(this);
in onClick() function you can handle the click event

Categories

Resources