Accessing another view from a button's onclick event - android

I am developing a simple game and in an activity I have 2 image buttons:
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img1"
android:onClick="btn1_click" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img2"
android:onClick="btn2_click" />
And I am showing some animations when buttons are clicked:
public void btn1_click(View v) {
v.startAnimation(animLeftTranslate);
}
public void btn2_click(View v) {
v.startAnimation(animRightTranslate);
}
Of course, only the clicked button is being animated but what I want to do is to show animation for both two buttons when one of them are clicked. How can I do this?

Instead of using android:onClick, you can do it in java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.bt1);
ImageButton btn2 = (ImageButton) findViewById(R.id.bt2);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.startAnimation(animRightTranslate);
btn2.startAnimation(animRightTranslate);
}
};
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);`
`

You can accomplish this by your own approach i.e. using android:onClick attribute:
Assign one function to both ImageButton for example btns_clicked(). So add android:onClick='btns_clicked' to both your buttons. And in that function write:
public void btns_clicked(View v) {
View btn1 = findViewById(R.id.btn1);
View btn2 = findViewById(R.id.btn2);
btn1.startAnimation(animLeftTranslate);
btn2.startAnimation(animRightTranslate);
}

you can do this as describe below.
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img1"
android:onClick="btn_click" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img2"
android:onClick="btn_click" />
and in activity, you have to use
public void btn_click(View v) {
if(v.getId() == R.id.btn1)
v.startAnimation(animLeftTranslate);
else if(v.getId() == R.id.btn2)
v.startAnimation(animRightTranslate);
}

Related

How to make your parent clickable but children not?

I have LinearLayout which has 2 children which are used only as a preview. LinearLayout functionality here is same as Button. I want to disable any interaction with its children but make LinearLayoutclickable by using onClickListener. Currently if I click on Spinner it will not trigger onClickListener of LinearLayout. I also called spinner.isEnabled = false in code.
Layout:
<LinearLayout
android:id="#+id/clickableLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical">
<Spinner
android:id="#+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
</Spinner>
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textColor="#color/colorBlack"
android:textSize="14dp"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:duplicateParentState="true"
android:maxLength="9"/>
</LinearLayout>
Make on Click Listener for All of the views and then call your function, so it does not matter on which view you click. It will always trigger that function.
LinearLayout linearLayout;
TextView textView;
Spinner spinner;
linearLayout = findViewById(R.id.clickableLayout);
spinner = findViewById(R.id.picker);
textView = findViewById(R.id.text1);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
}
private void myFunction() {
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
}

android custom dialog with multiple image buttons

I am trying to implement a share action that on user's choice, he gets a popup dialog with specific share actions (over specific distinct apps - desired screen:desired layout).
I would like to have 6 ImageButtons.
Till now, I could get the popup window (but the onclick listenes - were like dead) or (on the other hand) the app would crash.
My Layout for the popup is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/viber"
android:id="#+id/viber_share_button"
android:layout_below="#+id/twitter_share_button"
android:background="#color/colorAccent"
android:layout_alignEnd="#+id/twitter_share_button"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="#+id/facebook_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/twitter"
android:id="#+id/twitter_share_button"
android:background="#color/colorAccent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/facebook_messenger"
android:id="#+id/messanger_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignTop="#+id/viber_share_button"
android:layout_alignEnd="#+id/facebook_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/instagram"
android:id="#+id/instagram_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/twitter_share_button"
android:layout_marginStart="31dp"
android:layout_marginTop="10dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/whatsapp"
android:id="#+id/whatsapp_share_button"
android:background="#color/colorAccent"
android:layout_alignTop="#+id/viber_share_button"
android:layout_alignEnd="#+id/instagram_share_button"
android:layout_toEndOf="#+id/viber_share_button"
android:layout_alignStart="#+id/instagram_share_button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/facebook"
android:id="#+id/facebook_share_button"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/twitter_share_button"
android:layout_marginEnd="39dp"
android:layout_marginTop="10dp"/>
</RelativeLayout>
the code that I use is:
private void displayIntentOptions(final Bitmap bitmap){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.share_buttons, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
alertD.setView(promptView);
alertD.show();
ImageButton facebook_button = (ImageButton)findViewById(R.id.facebook_share_button);
ImageButton twitter_button = (ImageButton)findViewById(R.id.twitter_share_button);
ImageButton intagram_button = (ImageButton)findViewById(R.id.instagram_share_button);
ImageButton messanger_button = (ImageButton)findViewById(R.id.messanger_share_button);
ImageButton viber_button = (ImageButton)findViewById(R.id.viber_share_button);
ImageButton whatsapp_button = (ImageButton)findViewById(R.id.whatsapp_share_button);
facebook_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
twitter_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
shareTwitter(v, bitmap);//just a sample of what I want to do
}
});
intagram_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
messanger_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
viber_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
whatsapp_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
This code doesn't work at all. I would be thankful, If someone could help me with this. Since on a regular dialog I cannot have all those ImageButtons, I tried to make a custom one.
Furthermore, I would like to know if this is a good practice, or I should implement a new Intent for this.
Thanks in Advance.
You need to change one little thing
Was
final AlertDialog alertD = new AlertDialog.Builder(this).create();
alertD.setView(promptView);
alertD.show();
Now
final AlertDialog.Builder alertD = new AlertDialog.Builder(this);
alertD.setView(promptView);
alertD.show();
Update
Also there is a problem in this
ImageButton facebook_button = (ImageButton)findViewById(R.id.facebook_share_button);
It will be null, because you'r looking at Activity(or fragments) views, but not in AlertDialog ones. You need to set it like this
ImageButton facebook_button = (ImageButton)promptView.findViewById(R.id.facebook_share_button);

Issue with using an image button in Android Studio

I'm creating a Soundboard app which uses buttons currently to make different sounds. However I want to change the buttons to image buttons, having tried this I get the following error:
Unexpected Cast to ImageButton: Layout tag was imageView.
I've tried looking around the internet but can't find a fix. The code in the java class looks like this.
public class SecondActivity extends AppCompatActivity {
Button b,b2;
MediaPlayer nice,burp,fancy;
ImageButton IMG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button3);
nice = MediaPlayer.create(this, R.raw.nice);
burp = MediaPlayer.create(this, R.raw.burp);
fancy = MediaPlayer.create(this, R.raw.fancy);
configureImageButton();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
nice.start();
}
}
);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
fancy.start();
}
}
);
}
private void configureImageButton() {
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
IMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
burp.start();
}
}
);
}
The XML code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.oliver.olliessoundofmusic.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
</RelativeLayout>
I just want imageButtons to work with action listeners the same way a normal button would, to make the app more visually appealing.
Thanks in advance.
In your xml change ImageView tag to ImageButton
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#android:color/transparent"
android:src="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
Please notice that I made the background to a transparent color, and made the drawable to be in src property
Cast this to an ImageView instead, then since that is what you have in the XML
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
The OnClickListener will still work as expected.
Also, beware of variable shadowing. You'd want line this instead.
IMG = (ImageButton) findViewById(R.id.IMG);
Declare IMG as ImageView in you activity or use imagebutton widget in the layout file.

onclick event in dialog preference

I have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference?
this is part of my dialog layout (time_picker) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/showTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="_ _ : _ _"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="#+id/Buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="#+id/showTimer"
android:layout_marginTop="10dp">
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="1"
android:background="#android:color/transparent" />
<Button
android:id="#+id/button2"
android:layout_width="100dp"
...
this is Setting layout that launch dialog preference:
<com.example.test.TimerForNoti
android:key = "pref_sync_noti_timer"
android:title = "#string/pref_sync_noti_timer"
android:summary = "#string/pref_sync_noti_timer_summ"
android:dialogMessage = "#string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>
and this is class of dialog preference (TimerForNoti) :
public class TimerForNoti extends DialogPreference
{
public TimerForNoti(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.time_picker);
setPositiveButtonText(R.string.ok);
setNegativeButtonText(R.string.cancel);
Dialog di = new Dialog(context);
di.setContentView(R.layout.time_picker);
OnClickListener test = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = v.getTag().toString();
if(tag == "1"){
Log.v("onOne", "ok");
}
// .....
}
};
Button btn1 = (Button) di.findViewById(R.id.button1);
btn1.setOnClickListener(test);
}
}
If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_centerInParent="true"
android:text="6"
android:textStyle="bold"
android:textSize="20dp"
android:tag="6"
android:background="#android:color/transparent" />
Code:
OnClickListener buttonsListener = new OnClickListener() {
#Override
public void onClick(View v) {
String buttonText = v.getTag().toString();
// your logic here
}
};
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(buttonsListener);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(buttonsListener);
Button btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(buttonsListener);
// your buttons here
Also you can use v.getId() in OnClickListener for determining which button was clicked
As much I understood your question. You want one method to be called on every button clicked.
For that Add this to your every button element in xml:
<Button
android:onClick="OnClick"
.. />
And this method to your .java file where you want to perform task:
public void OnClick(View v){
// do something....
}
Or you can directly call setOnClickListener at button object
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something....
}
});

how to set image as button in xml and in class

Im new to android..please guide me..
I want to set image to act as button..
How to implement this in xml and in class file?
I now the code to set button in xml and class file like this..
<Button
android:id="#+id/nxt_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/rdtxt"
android:layout_marginRight="22dp"
android:layout_marginTop="34dp"
android:text="Next" />
Button nextBtn = (Button) findViewById(R.id.nxt_btn);
I want practice.png this image want to act as button..I have put this image into drawables..
Now how to implement this by code...please guide me...
Thank a lot...
In xml you can add:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
And in Activity:
ImageView image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your stuff here
}
});
In a xml file:-
<Button
android:id="#+id/nxt_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/rdtxt"
android:layout_marginRight="22dp"
android:background="#drawable/practice"
android:layout_marginTop="34dp" />
or
In a Activity :-
Button nextBtn = (Button) findViewById(R.id.nxt_btn);
nextBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your stuff here
}
});
Use ImageButton. You can set an image to it using android:src="#drawable/myImage" and you can set OnClickListener to it.
For example :
<ImageButton
android:id="#+id/nxt_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/rdtxt"
android:layout_marginRight="22dp"
android:layout_marginTop="34dp"
android:src="#drawable/next_btn_img" />
ImageButton next = (ImageButton) findViewById(R.id.nxt_btn);

Categories

Resources