Detect which child is clicked on click of the parent - android

I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?

You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}

One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.

If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

how to add onClickListener to buttons inside CardLayout

I am using Gabrielemariotti's Cardslib library to implement card layout in my android application. I am using a custom layout for my cards. Below is the code for creating custom cards:
Card card = new Card(getActivity().getApplicationContext(), R.layout.status_card);
card.setTitle("sample title");
I have three buttons at the bottom of my card (like buttons in Facebook android app). I want to set onClickListener for these buttons. But I am not sure how to do that.
Please help me here.
Thanks,
You have to define your layout.
Then create a Card with this layout, and override the setupInnerViewElements method.
Inside this method you can define your OnClickListener on your buttons, and you can access to all card's values.
public class CustomCard extends Card {
/**
* Constructor with a custom inner layout
*
* #param context
*/
public CustomCard(Context context) {
super(context, R.layout.carddemo_mycard_inner_content);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve button
Button myButton = (Button) view.findViewById(R.id.myButton);
if (myButton != null) {
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Click Listener card=" + getId(),
Toast.LENGTH_LONG).show();
}
});
}
}
}
I have an easy solution for this.
So another way to add onClick listeners, which is a bit easier, is through the XML.
In the xml for the button, you add this line:
android:onClick="methodName"
Where 'methodName' is obviously the name of a method. This will call the method whenever the button is clicked. The next step is obvious - just go into your java activity and create the method that you want called, making sure to take the View as a parameter. So you'd have something like this in your activity class:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
//whatever you want to do here
}
This is a shortcut to creating a whole onClickListener.
Hope that helps. Good luck :)
EDIT:
Remember, you're passing in a view here, so you can get whatever you want off of that view. Since you commented that you need to get the text off of your card, I'll show you how to do that.
Here is your method for this case:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
TextView textFromCard = view.findViewById(R.id.THE_ID_YOU_GAVE_YOUR_TEXTVIEW_IN_THE_XML);
String textFromTextView = textFromCard.getText().toString();
//do whatever you want with the string here
}

I have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

Updating arrayIndex onClick of button is causing problem

I am implementing a quiz and here am having a method for my button as
public void playquiz(final int arrayIndex) {
setContentView(R.layout.quiz);
next = (Button) findViewById(R.id.nextBtn);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) {
int totalpoints = correctAnswerCount*10;
Intent scoreintent = new Intent(TriviaQuiz.this,ScoreBoard.class);
startActivity(scoreintent);
}
else
{
playquiz(arrayIndex+1);
}
}
What I am trying to do is, inside the method I am loading another layout and assigning an onclick for the button in that layout.
Now my problem is, the arrayIndex which I get initially, I have to update this on click of the next button and based on this I have some other conditions to check.
But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?
And even then it is not behaving in the exact way as it supposed to be.
The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening
Any suggestion?
But if I do like playquiz(arrayIndex+1);, it asks me to declare the arrayIndex as final, why is this?
This is because you are using it inside another class - OnClickListener() and arrayIndex is probably a local variable. There are two ways to getting around this.
declare it in the global declaration area.
your class should implement OnClickListener and override OnClick method within which you must include your codes.
The if (arrayIndex == TourDescription.currentTour.getTriviaArray().size()) inside onClick is not happening
I am not sure if you have provided enough code here but by just looking at it i cannot see arrayIndex being incremented and hence it will never get to TourDescription.currentTour.getTriviaArray().size(). You need to increment arrayIndex
it in the else clause.

How do I go from one page to another without using Intent?

I want to create an application based on quiz which consists of 14 questions.
Can anyone tell me how do I need to go from one question to another by clicking on next button.If I use an Intent then I am afraid that I will be creating 14 Activities :(
I don't think that is the programmatic procedure too.
You can stay in the same Activity and keep track of the question.
You might want to use a TextSwitcher to add a fade in/fade out animation when swapping the question's text.
You could have the button click just update the questions text to be the next question.
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
questionTextView.setText(questionTwo);
}
});
#Vivek you can use view flipper
this might help you
http://www.bogotobogo.com/Android/android9TabWidgetFlipper.html#SlidingDrawer
http://www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/
Change the text of your textviews where the questions are.
#Override
protected void onCreate(Bundle savedInstanceState) {
Button nextButton = (Button) findViewById(R.id.yourButtonId);
TextView questionTextView = (TextView) findViewById(R.id.yourTextViewId);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
questionTextView.setText("Your Next Question");
}
});
}
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.
Has its own lifecycle;
Receives its own input events;
Can be added or removed while the activity is running;
Believe me that is the best method. I have been introduced to fragments having an application with quotes. The list of the quotes in left and the quotes that were changing on left when an author have been selected.
I'm using android.widget.ViewFlipper that contains views where one view contains TextView (question) and "input control" for answer (selectboxes, date/time widget, radiogroup, etc).

Categories

Resources