how to check which image view i have added in xml programmatically? - android

I added some imageviews in xml layout. Now programmatically i need to check which imageView i have added from the xml file and if same image then on click of that image i need to replace new image and visa versa.
Is the way i implemented it good or can I find a better solution for this?
This is my code so far:
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.bankTickImg.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.black_untick))) {
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}else{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});
Imageview Code
<ImageView
android:id="#+id/defaultBankTick"
android:layout_width="#dimen/TwentyFour"
android:layout_height="#dimen/TwentyFour"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/FiftyFive"
android:layout_marginRight="#dimen/Thirty"
android:layout_marginTop="#dimen/FiftyFive"
app:srcCompat="#drawable/black_untick" />

If i got your question.
Why don't you use a integer/boolean variable to save the state. eg.
lets say, you start with
int clicked = 0;
then in on click check if its value it 0 then treat it as fresh and change the value to 1.
and if the value is already 1 then treat it as it clicked 2nd time and change it to 0 .

There are many ways to check imageView which you added
holder.bankTickImg.setImageResource(R.drawable.black_untick); //default image , you can also set in xml code
holder.bankTickImg.setId(0);//you also do with string setTag();
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int istick=holder.bankTickImg.getId();
if (istick==1)
{
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}
else
{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});

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
}

Trying to get if click on one radion button the other is unclicked

I am trying to find how if you click on one radio button the other is unclicked using java not XML. I have both of my radioButtons in a tableRow. When I put them in a radioGroup is messes up the rest of my XML layout. Basically,if one is clicked make sure the other in not clicked.
public void drivable(View v) {
if (notDrivable.isChecked()) {
notDrivable.toggle();
}
}
public void notDrivable(View v) {
if (drivable.isPressed()) {
drivable.toggle();
}
}
I figured it out. I needed to change it from toggle() to notDrivable.setChecked(false); Here is the code that works.
public void drivable(View v) {
if (notDrivable.isChecked()) {
notDrivable.setChecked(false);
}
}
public void notDrivable(View v) {
if (drivable.isChecked()) {
drivable.setChecked(false);
}
}
as a suggestion you can give it a try.... get the id's for your radio buttons and in their onclick's method make another one unchecked.
I think you can put an OnClick Listener to each button and with a boolean and if clause determine when a button is clicked if it is selected or not and act accordingly. I hope I helped you.

Android - ImageView On Click

Im trying to trace a click when my image view is clicked, but I cannot seem to pick up the touch, any ideas?
imgView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.v(TAG, " click");
}
});
Images normally aren't clickable, therefore I would suggest that you put the definition
android:clickable="true"
android:focusable="true"
in your layout.xml to the imageView.
If this is not helping, please edit+update your question and show us the part of your layout, where you define the image to have a look at it.
EDIT:
Tried it, and your code worked with me too - even with the upper case id name.
Can you please have a close look at your LogCat?
Mine sometimes doesn't really update, as long as I don't choose the device again.
To do so in Eclipse, go to the View "Devices" (or show it first via "Window") and click once on your device / virtual device.
If you still don't find your log entry in the LogCat-View, try to create a filter (via the green plus and giving it the String you defined with TAG as "by Log Tag").
Have a look at android developers > Using DDMS at the section "Using LogCat"
You forgot to override the onClick method. This works for me and should do for you as well :-)
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Log.v(TAG, " click");
}
});
Here's to get the X and Y coordinates:
imgView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// X = event.getX()
// Y = event.getY()
return false;
}
});
1) First make your image view clickable by adding-> android:clickable="true" .
2) Then go to your java file and add declare a variable
for example-> private static ImageView imgview;
3) Then add this functionality:
public void OnclickButtonListener() {
imgview = findViewById(R.id.imageView3);
imgview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(timetable.this,MapsActivity.class);
startActivity(intent);
}
});
}
and call this function in the constructor.
Hopefully this would work.

change button image in android

I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});

Categories

Resources