How to add play and stop different button song in android - android

I am making an application for Android and I have made buttons to play and pause. My play button and pause one are set up, so when I click the play or pause button and first it will run, but in button two it doesn't work.The code is given below.
MainActivity.java
package com.example.cdp.keroncong;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.media.MediaPlayer;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mplayer;
public void playAudio(View view) {
mplayer.start();
}
public void pauseAudio(View view) {
mplayer.stop();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this, R.raw.Button1);
//mplayer = MediaPlayer.create(this, R.raw.Button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thank's before

Try this Code, it will help you
public class MainActivity extends AppCompatActivity {
MediaPlayer Song;
int pause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void play(View view){
if(Song == null){
Song = MediaPlayer.create(this , R.raw.music);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
else if(!Song.isPlaying()){
Song.seekTo(pause);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
}
public void pause(View view){
if(Song!= null){
Song.pause();
pause = Song.getCurrentPosition();
Toast.makeText(MainActivity.this, "Song Pause", Toast.LENGTH_SHORT).show();
}
}
public void stop(View view){
Song.stop();
Song = null;
Toast.makeText(MainActivity.this, "Song Stop", Toast.LENGTH_SHORT).show();
}
}
xml.file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Play"
android:layout_gravity="center_horizontal"
android:background="#drawable/play"
android:onClick="play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pause"
android:layout_gravity="center_horizontal"
android:background="#drawable/pause"
android:onClick="pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stop"
android:layout_gravity="center_horizontal"
android:background="#drawable/stop"
android:onClick="stop" />
Let me know if it will help you

This my activity_main.xml
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/orange"
android:layout_below="#+id/textView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="215dp"
android:onClick="playAudio" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/playButton"
android:layout_below="#+id/playButton"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orange"
android:layout_below="#+id/orange"
android:onClick="playAudio"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/orange"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>

Try this, place it in your OnCreate method right after this mplayer = MediaPlayer.create(this, R.raw.Button1);
// Play song
Button playButton = (Button) findViewById(R.id.playButton);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.start();
}
});
// Pause song
Button pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.pause();
}
});
i modified your XML to look like this although I can't fully correct it until I see the whole thing
<Button
android:id="#+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orange"
android:layout_below="#+id/orange"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/orange"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>

Related

how to use prev button and next button change seekbar

In my TopRated.java file, I have an image view which changes when the seekbar is used. It also changes when the next and previous buttons are used. however, i want both buttons and the seeker to be in sync, that is, when the next button is pressed, i want it to also move the seeker up by one, and when the previous button is pressed, i want it to also move the seeker down by one.
please help
This is my TopRated.java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
public class TopRated extends Activity {
SeekBar imgseekbar;
ImageView iconimageview;
Button prevbutton;
Button nextbutton;
int progress=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toprated);
iconimageview=(ImageView)findViewById(R.id.imageView);
imgseekbar=(SeekBar)findViewById(R.id.seekBar);
prevbutton=(Button)findViewById(R.id.button8);
nextbutton=(Button)findViewById(R.id.button9);
iconimageview.setImageResource(R.drawable.diablo);
imgseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
updateImageResource(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progress--;
updateImageResource(progress);
}
});
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progress++;
updateImageResource(progress);
}
});
}
private void updateImageResource(int progress) {
if (progress==2) {
iconimageview.setImageResource(R.drawable.eye);
}else if (progress==3) {
iconimageview.setImageResource(R.drawable.facebook);
}else if (progress==4) {
iconimageview.setImageResource(R.drawable.google);
}else if (progress==5) {
iconimageview.setImageResource(R.drawable.house);
}else if (progress==6) {
iconimageview.setImageResource(R.drawable.mail);
}else if (progress==7) {
iconimageview.setImageResource(R.drawable.pen);
}else if (progress==8) {
iconimageview.setImageResource(R.drawable.photos);
}else if (progress==9) {
iconimageview.setImageResource(R.drawable.skype);
}else if (progress==10)
iconimageview.setImageResource(R.drawable.twitter);
}
}
This is my toprated.xml file
<?xml version="1.0" encoding="utf-8"?>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:max="10"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/seekBar"
android:layout_centerHorizontal="true"
android:minHeight="50dp"
android:minWidth="50dp"
android:background="#drawable/icon_animation"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:id="#+id/button8"
android:layout_alignBottom="#+id/imageView"
android:layout_toStartOf="#+id/imageView"
android:layout_marginRight="5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button9"
android:layout_alignBottom="#+id/imageView"
android:layout_toEndOf="#+id/button8"
android:layout_marginLeft="50dp" />
I also have another xml which holds all my images, icon_animation.xml
please help!
any help would be appreciated.
use below line to update seek bar
seekbar.setProgress(int)

Changing text in textView

I am trying to change the text in one of the textview by calling it in a function that is assigned to a button.. However it is not quite working as I hoped. I was wondering if some one could point out why isn't it working?
Here is the "MainActivity.java" file
package com.nblsoft.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
String stri = new String("Hellow");
public void game(View view) {
TextView textv = (TextView) findViewById(R.id.textView2);
textv.setText(stri);
}
}
and the "activity_main.xml" file
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Welcome"
android:id="#+id/textView"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/lower_text"
android:id="#+id/textView2"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:inputType="number"
android:text="#string/guess"
android:textSize="160dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="game"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
you want call game when click in EditText?! you put android:onClick="game" in EditText
if want call when click in Button you must add android:onClick="game" to your Button in activity_main.xml file like
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="game" />
or you can use this in MainActivity.java instead use android:onClick="game" in xml file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
game(arg0);
}
});
}
- remove android:onClick="game" from EditText
package com.nblsoft.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textv = (TextView) findViewById(R.id.textView2);
String stri = new String("Hellow");
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textv.setText(stri);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This should be working.By the way.Don't rush into android like this...you've got to learn a few things about how it works before you can play with it.Try to check the documentation and play with all that android offers.It's actually fun.It was for me and it still is as I've got to learn a lot ,still.
you call the method game() when you click the edittext "editText", and this will change the textview. If you want to change the value of the textview by clicking button "button", i think you just need to move the game() method to the Button field:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="game" />

i need to make a button that opens a custom dialogbox

I'm creating an android app for my school project.
I created the interface normaly.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/mappic"
android:layout_above="#+id/zoomControls"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zoomControls"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obtenir le PCC"
android:id="#+id/button"
android:layout_alignBottom="#+id/zoomControls"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
now I want to make it when I click on the button : Obtenir le PCC this dialog box opens up :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn"
android:layout_alignLeft="#+id/Suivantbtn"
android:layout_alignStart="#+id/Suivantbtn" />
<TextView
android:id="#+id/textdp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit de départ:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textdp"
android:layout_alignStart="#+id/textdp"
android:layout_alignRight="#+id/textdp"
android:layout_alignEnd="#+id/textdp"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
and when I click on Suivant ,, the dialog box goes and another dialogbox appears :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn2"
android:layout_alignLeft="#+id/Suivantbtn2"
android:layout_alignStart="#+id/Suivantbtn2" />
<TextView
android:id="#+id/textar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit d'arrivée:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn2"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn2"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn2"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textar"
android:layout_alignStart="#+id/textar"
android:layout_alignRight="#+id/textar"
android:layout_alignEnd="#+id/textar"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
the java code :
package artofdev.org.admaps;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context context = getApplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
Button button3 = (Button)
dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And when this appears and I type in and click on next. This dialog box dismisses and another msg box I'll create later shows a msg.
how to do it ?
Here is a fully working code for you:
Context context = getAplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_dialog_layout);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.your_second_dialog_layout);
Button button3 = (Button) dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
Hope my answer helped!
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialogue);
dialog.show();
}
});
You should add onClickListener to your buttons. For example (this code is for your Activity class):
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
}
});

Android Button Disable and enable

Hello I have an android application i want to create something like a lock button to the prevent error click on any button in application i use this method and it work great in disable all button i like but i can not re-enable it again after this all i need is that when i press lock button again it re-enable button2 and button 3 again
This is the activity code
import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
public class AboutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
addListenerOnButton();
}
Button button;
Button button2;
Button button3;
private void addListenerOnButton() {
// TODO Auto-generated method stub
button = (Button) findViewById(R.id.lock);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is layout code
<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.lock.AboutActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="66dp"
android:enabled="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lock"
android:layout_centerVertical="true"
android:enabled="true"
android:text="Button" />
</RelativeLayout>
You can use isEnabled() and use "not" operator to lock and unlock. If it's not enabled, it'll enable and vice versa.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(!button2.isEnabled()); //button which u want to disable
button3.setEnabled(!button3.isEnabled()); //button which u want to disable
}
});

How to get onClickListener() event on custom actionbar

I'm developing an application in which I have to get onClick() event on click of actionbar custom view. So far I'm able to achieve the following layout.
Here is my code for achieving this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setCustomView(R.layout.custom_image_button);
getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}
Here is my custom_image_button layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/points_yellow"
android:gravity="center"
android:paddingLeft="20dp"
android:textColor="#887141"
android:textIsSelectable="false"
android:textSize="22sp"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:src="#drawable/badge_notification" >
</ImageView>
</FrameLayout>
</RelativeLayout>
I was trying to have a click listener on the custom layout. For that I have tried the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setCustomView(R.layout.custom_image_button);
getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_image_button, null);
frameLayout = (FrameLayout) v.findViewById(R.id.frame_layout);
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(), "Clicked on 1",
Toast.LENGTH_SHORT).show();
return false;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}
}
But, I'm unable to get onClick() event on the custom image. What I'm doing wrong here, please guide.
Any kind of help will be appreciated.
after in Inflater its just like view in layout file
so, you have to add android:onClick="clickEvent" in ActionBar custom layout file
here is demo:
my mainactivity:
package com.example.testdemo;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private View viewList;
private Dialog dialogMarketList;
String a[] = { "a", "aa" };
private View header;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
View cView = getLayoutInflater().inflate(R.layout.header, null);
actionBar.setCustomView(cView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void clickEvent(View v) {
if (v.getId() == R.id.button1) {
Toast.makeText(MainActivity.this, "you click on button1",
Toast.LENGTH_SHORT).show();
}
if (v.getId() == R.id.button2) {
Toast.makeText(MainActivity.this, "you click on button2",
Toast.LENGTH_SHORT).show();
}
if (v.getId() == R.id.textView1) {
Toast.makeText(MainActivity.this, "you click on textView1",
Toast.LENGTH_SHORT).show();
}
}
}
my layout header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:text="Button 1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="My action bar"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:text="Button 2" />
</LinearLayout>
View cView = getLayoutInflater().inflate(R.layout.header, null);
cView.findViewById(R.id.btn_id).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff here.
}
});
actionBar.setCustomView(cView);

Categories

Resources