How do I remove an imageButton after it is clicked? - java

View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setVisibility(View.GONE);
}
};
I want the imageButton to disappear or delete after the image view is clicked. I am making this app on Android Studios and when it runs and I click the imageButton the view does not become hidden.
Thank you to all who are willing to help!!!

You are creating the onClick handler but maybe you are not setting it:
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
btn.setVisibility(View.GONE);
}
};
btn.setOnClickListener(imgButtonHandler3 ); // this sets the handler

We can disable buttons in android. Try the below code.
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setEnabled(false);
}
};

Use btn.setVisibility(View.INVISIBLE) instead of View.Gone
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setVisibility(View.INVISIBLE);
}
};

Related

How to change a text on the click of a button in android?

I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}

Issues with stopping MediaPlayer

i have a one layout and 3 buttons and textView , when i cick a button it change the textView , button3 i have a song to play and textView show lyrics, and my problem here is how to make the MediaPlayer stopping when click any other button
here is my code i used :
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles= MediaPlayer.create(Langue.this, R.raw.iles);
iles.start();
}
});
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final MediaPlayer iles= MediaPlayer.create(Langue.this, R.raw.iles);//creating iles here
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles.start();
}
});
I believe this will do., in the above code i created a iles as MediaPlayer object and used it to stop the song...

I wrote this few lines of code to change the xml file of this class but it doesn't work

Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1".
Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...
public void SwitchLayout()
{
Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);
View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
weiter_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
weiter_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
zurück_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
}
Need Help ;) Thank you!
Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.
A better way is define onclick attribute in the layouts xml file.
For instance if you have this:
<Button
android:id="#+id/w2"
android:onClick="getms3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="w2" />
then you should define
public void getms3(View v)
{
setContentView(R.layout.ms3);
}
Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
}
});

Caused by: java.lang.OutOfMemoryError

I am having a layout file and 5 buttons -- 1 for next and 2 for previous etc. Application runs fine, when I click first button, it loads the image correctly, then as I click another button, it gives the error -Caused by java lang Out Of Memory Error
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView image1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
}
}
);
}
public void addListenerOnButton3() {
final Context context =this;
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
}
}
);
}
public void addListenerOnButton4() {
final Context context =this;
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
}
}
);
}
public void addListenerOnButton5() {
final Context context =this;
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
}
}
);
}
public void addListenerOnButton6() {
final Context context =this;
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
}
}
);
}
You are changing the whole layout instead of changing the image. When you do that, garbage collector is not called and you should be calling it on your own. Since it's not called, you are going to get that error pretty soon.
Do it like this instead
int[] imageArray = {R.drawable.image1, R.drawable.image2, ... };
and when you click a button, just do
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
someImageView.setImageResource(imgageArray[0])
}
}
Also make sure you don't use too big images. The take up a lot of memory.

Dialog box content doesnot change according to that row

I am giving one button in all the row,if we click that button dialog box has to appear in that one it has to display the content according to the row.
public void bindView(View view, Context context, final Cursor cursor){
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text2);
final TextView tv1 = (TextView) view.findViewById(R.id.text1);
TextView tv2 = (TextView) view.findViewById(R.id.date);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
int col1 = cursor.getColumnIndex("expname");
final String expname = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("expcontent");
final String expcontent = cursor.getString(col2 );
int col3 = cursor.getColumnIndex("expdate");
final String expdate = cursor.getString(col3);
cb.setTag(cursor.getPosition());
cb.setChecked(mCheckStates.get(cursor.getPosition(), false));
cb.setOnCheckedChangeListener(this);
// TextView tv2 = (TextView) view.findViewById(R.id.text3);
//cursor.getColumnName(1)
tv.setText( expname);
tv1.setText( expcontent);
tv2.setText(expdate);
//tv2.setText( ""+cursor.getColumnIndex(GinfyDbAdapter.CATEGORY_COLUMN_COUNT));
// String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT}
ImageButton button = (ImageButton) view.findViewById(R.id.sms);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb2 = new StringBuffer();
sb2.append("Title:");
sb2.append(Html.fromHtml(expname));
sb2.append(",Content:");
sb2.append(Html.fromHtml(expcontent));
sb2.append("\n");
String strContactList1 = (sb2.toString().trim());
sendsmsdata(strContactList1);
}
});
ImageButton button1 = (ImageButton) view.findViewById(R.id.mail);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb3 = new StringBuffer();
sb3.append("Title:");
sb3.append(Html.fromHtml(expname));
sb3.append(",Content:");
sb3.append(Html.fromHtml(expcontent));
sb3.append("\n");
String strContactList2 = (sb3.toString().trim());
sendmaildata(strContactList2);
}
});
ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer);
button2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
typed = expcontent;
speakOut();
}
});
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
dialog.show();
}
});
dialog=new Dialog(ExperiencesActivity.this);
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
textview1 = (TextView)dialog.findViewById(R.id.tv11);
textview1.setText("Title:" + expname);
textview2 = (TextView)dialog.findViewById(R.id.tv22);
textview2.setText("Content:" + expcontent);
Button button23=(Button)dialog.findViewById(R.id.btnSubmit);
button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
But here my problem is it will not take the respective content in dialog box,whenever we click the first item in an button,it will shows the content in all the row of button click. Dialog box content has to change according to the row.
You need to create saperate classe for your textviews. Like
private class ViewHolder {
TextView textview1;
TextView textview2;
Button button23;
}
Update your dialog like...
public void showMyDialog(){
dialog=new Dialog(ExperiencesActivity.this);
ViewHolder viewholder;
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
ViewHolder.textview1 = (TextView)dialog.findViewById(R.id.tv11);
ViewHolder.textview1.setText("Title:" + expname);
ViewHolder.textview2 = (TextView)dialog.findViewById(R.id.tv22);
ViewHolder.textview2.setText("Content:" + expcontent);
ViewHolder.button23=(Button)dialog.findViewById(R.id.btnSubmit);
ViewHolder.button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
And call showMyDialog() method onclick of button.
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
showMyDialog();
}
});

Categories

Resources