Mukunda
Mukunda

Reputation: 1623

how to have a single activity class for many Image Buttons

I have a footer in all the layouts for a android application. The footer will have Image buttons like "Help", "Home", this Image buttons directly link to Help class and Home class. Can I have a one single activity class for all the footer Image buttons. I tried with

public class FooterItems extends Activity implements OnClickListener {

@Override
public void onClick(View view) {
    if(view.getId() == R.id.footerBtnHome)
     { 
        Intent myIntent = new Intent(view.getContext(), MainActivity.class);
        startActivityForResult(myIntent, 0);
       return;
     }

     if(view.getId() == R.id.footerBtnFeedback)
     { 
         Intent myIntent = new Intent(view.getContext(), Feedback.class);
         startActivityForResult(myIntent, 0);
       return;
     }      
   }
}

but I am not getting how to call these in a class... for example the project is having MainActivity class in which I have

 ImageButton buttonFeedback = (ImageButton) findViewById(R.id.btnFeedback);
    buttonFeedback.setOnClickListener(new View.OnClickListener() {               
        public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Feedback.class);
                startActivityForResult(myIntent, 0);
            }
    });

When I call Feedback.class with onClick from one Image Button... layout and same footer items appears. I want to use the generalised FooterItems class so I can have one class for footer and use in every other layout. I am also using android:onClick="onClick" in xml for Image Buttons for footer only. But how to call those generalised class FooterItems and make it work. Looking forward to the reply. thanks.

Footer image

Upvotes: 0

Views: 842

Answers (3)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

What you are asking is not much clear.

You want to have Help, Home, etc. Image Buttons as common to all layouts in your application correct ?

If you click any Image Button, you have to show the layout on top and these buttons also has to appear on screen??

If yes my answer may help you.

You told i will create footer items as one activity, but its not good. I will prefer ViewFlipper in this case. See the layout.

<LinearLayout vertical>
    <ViewFlipper id=vf>
    <include layout1 />
    <include layout2 />
    <include layout3 />
    </ViewFlipper>

    <LinearLayout horizontal>
    <ImageButton button1 />
    <ImageButton button2 />
    <ImageButton button3 />
    </LinearLayout>
</LinearLayout>

Initially you will get layout1 and all image buttons on screen. If you want to show layout2 when you click button3 write onClickListener as below.

ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);

The variable vf is used to change layouts.

button3.setOnClickListener(new View.OnClickListener() {
    public void onClick() {
        vf.setDisplayChild(1);
    }
});

I hope it may help you. Bye.

Upvotes: 1

Anton
Anton

Reputation: 941

I can suggest another variant: I think, you can add to XML android:onClick, for example:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

And when user click this button, android programm call method selfDistruct. You just have to implement this method. Android tutorial: http://developer.android.com/reference/android/widget/Button.html

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128458

I am sure you want to implement Footer view with 2 buttons: Help and Home, this should be at bottom of every activities.

If you want to implement a code for once then follow the below steps:

  1. Define a footer layout with 2 buttons, define android:onClick="btnHelp" for help button and android:onClick="btnHome" for home button.
  2. Now you can include this layout inside any activities by using <include>.
  3. Define a base activity with below 2 methods.
  4. Now extends this base activity wherever you implements this footer layout.
public void btnHelpClick(View v)
{
  // do your task for Help
}

public void btnHomeClick(View v)
{
  // do your task for Home
}

Upvotes: 0

Related Questions