1

Does anyone know how to create a button which will help me to switch between tabs?

Button button1 = (Button) findViewById(R.id.button1);    
    button1.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {{
            // Perform action on click   
                if (v.getId() == R.id.button1) {
                    setContentView(R.layout.tab3);    
                }
                else if (v.getId() == R.id.button1) {
                    setContentView(R.layout.main);  
                }
            }
        }
    });
}}

this is the code in my tab 1, this next code is the button in my tab.xml. i want to press the buy button below and i want it to go to another tab, is this possible?

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageButton1"
        android:text="Buy"  
        android:onClick="tab3" />

this is my logcat file

01-05 16:04:20.903: D/dalvikvm(215): GC freed 806 objects / 70928 bytes in 96ms
01-05 15:30:29.072: D/AndroidRuntime(213): Shutting down VM
01-05 15:30:29.082: W/dalvikvm(213): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-05 15:30:29.082: E/AndroidRuntime(213): Uncaught handler: thread main exiting due to uncaught exception
01-05 15:30:29.132: E/AndroidRuntime(213): java.lang.IllegalStateException: Could not find a method tab3(View) in the activity
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View$1.onClick(View.java:2020)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.performClick(View.java:2364)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.onTouchEvent(View.java:4179)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.dispatchTouchEvent(View.java:3709)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.os.Looper.loop(Looper.java:123)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.reflect.Method.invoke(Method.java:521)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-05 16:04:25.693: E/AndroidRuntime(215):  at dalvik.system.NativeStart.main(Native Method)
01-05 16:04:25.693: E/AndroidRuntime(215): Caused by: java.lang.NoSuchMethodException: Tab3
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.Class.getMethod(Class.java:1014)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.View$1.onClick(View.java:2017)
01-05 16:04:25.693: E/AndroidRuntime(215):  ... 25 more
01-05 16:04:25.733: I/dalvikvm(215): threadid=7: reacting to signal 3
01-05 16:04:25.733: I/dalvikvm(215): Wrote stack trace to '/data/anr/traces.txt'
01-05 16:04:27.505: I/Process(215): Sending signal. PID: 215 SIG: 9
prolink007
  • 33,872
  • 24
  • 117
  • 185
user123
  • 67
  • 1
  • 6

3 Answers3

5

+1 Midoalageb and +1 Mark Gjol have given you exactly what you need to switch the tab. What they are leaving out is the code for the event you are trying to do. What you need to do is set up a button on TAB1 that will have an onClick() listener that will call either getTabHost().setCurrentTabByTag(TAB2); or getTabHost().setCurrentTab(1);. Either one of those will work fine, just depends on how you want to do it.

Do you know how to do buttons and onClick() listeners? If not, google has some great examples. Just create a button in your tabView then set the onClick and then put your code in there for switching to the tab you want.

Another post that is very similar to this one that may help you out.


getTabHost().setCurrentTabByTag(TAB2);

This will look for the tab labeled "TAB2" and change to that tab.


getTabHost().setCurrentTab(1);

This will look for the tab with the index of "1" and change to that tab.


In regards to your xml

The xml files are mainly used to abstract defining your Views from the implementation of your Views. This allows you or other developers to provide different views to your code if needed. It helps provide a way to use the MVC pattern. However, this pattern is a little different in Android, but the xml file is most commonly considered the View in that pattern.

So Yes, if you are going to have multiple buttons they will need to be defined in your xml and implemented in your code.


In regards to your button dilema

The API for Button from google actually provides you with an example of this on the very front page. Keep in mind that Android is maturing very fast as a development platform and A LOT of this stuff is actually well documented already on line. The API documentation is also very good and gives great examples. Go by there and read it and you will most likely solve a lot of the more easy problems there.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textview = new TextView(this);
    textview.setText("This is Tab1");
    setContentView(textview);

    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            // perform the switch to tab event here for instance...
            getTabHost().setCurrentTabByTag("TAB2");
        }
    });
}
Community
  • 1
  • 1
prolink007
  • 33,872
  • 24
  • 117
  • 185
  • that is all the code i have in my xml document do i need to add the button to tab one? because i have more than one button – user123 Jan 04 '12 at 14:11
  • Add your xml code to your question, so everyone can see it. Also, that means that you have defined your button. Now you need to into your code and implement the button you defined in your xml. Then in your code, you will need to add the onClick Listener to your button and proivde the implementation for switching between tabs. – prolink007 Jan 04 '12 at 14:15
  • i have added it to the question for everyone to see – user123 Jan 04 '12 at 14:22
  • Just added a link to another question on SO that is very similar to the one you are asking. Check it out. – prolink007 Jan 04 '12 at 14:31
  • Posted the stuff regarding your button problem. – prolink007 Jan 04 '12 at 15:07
  • i have one error - getTabHost().setCurrentTabByTag("TAB2"); the setCurrentTabbyTag has an error any idea what has to be done now? – user123 Jan 04 '12 at 15:13
  • Do you have another tab created that is tagged as "TAB2"? If not, that is most likely your problem. – prolink007 Jan 04 '12 at 15:19
  • is there anyway i can send someone my project, because i have been working on this for while and the buttons still do not seem to be working – user123 Jan 04 '12 at 15:30
  • Not your project, but you could possibly post your code in your above main post and people can take a look at it. All xml files and java files. – prolink007 Jan 04 '12 at 15:49
  • I do not see anything new in the original question. – prolink007 Jan 04 '12 at 18:49
  • super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is Tab1"); setContentView(R.layout.main); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Perform action on click { if (v.getId() == R.id.button1) { setContentView(R.layout.tab3); ; } } } }); }} this is my new java file, however i still get the same error where the file has to close on my emulator – user123 Jan 04 '12 at 19:12
  • Can you place that information in your original post, this way it is much easier to read. Just keep appending it to your original question. – prolink007 Jan 04 '12 at 20:38
  • I see three extra `}`. You may want to try removing them and seeing what happens. There is an extra one after you start your `public void onClick()` and 2 extra at the very end of your code snippet. May not be the problem, but try that and see what happens. (This may be because you pasted the code wrong... but i am assuming you pasted correctly) – prolink007 Jan 05 '12 at 13:54
  • i tried removing them, nothing changed..i still received a message saying unexpected error on my emulator – user123 Jan 05 '12 at 15:52
  • Can you post your logcat information in the OP please. This will be greatly helpful. Thanks – prolink007 Jan 05 '12 at 16:01
  • 1
    You are doing the "switching to new tab" part wrong inside your `onClick()` method. I posted exactly how to do it. Take a look at the link to the examples i have given you and take a look at the example i posted. Make sure that you have actually defined and implemented the tab you are trying to switch to. – prolink007 Jan 05 '12 at 19:12
  • i was told that words like Exception help you find the problem in your logcat file, here are the exception words Caused by: java.lang.NoSuchMethodException: Tab3 java.lang.IllegalStateException: Could not find a method tab3(View) in the activity Uncaught handler: thread main exiting due to uncaught exception – user123 Jan 05 '12 at 19:12
  • if you can show me the links one more time that will be very helpful – user123 Jan 05 '12 at 19:23
  • o.O ... The links are still in the answer i gave you. – prolink007 Jan 05 '12 at 19:27
  • ive been trying to fix the problem, but havent had any luck, do you know how to get it working? – user123 Jan 05 '12 at 20:03
  • Well from what i have heard from your questions and responses asked here, i honestly think you need to start with something much easier than what you are attempting. Maybe try to get some more trivial things working in android and then move onto something a little more complicated. Your understanding of java and android currently seems very very trivial... I would suggest reading more and trying some stuff from the API website. Besides writing the code for you, i believe that we have done a very good job helping you so far. The rest is up to you. Sorry, and good luck. – prolink007 Jan 05 '12 at 20:12
3

When you create your tabs you give them a tag, let's store one such tag in the variable TAB_1. So in your onClick() method, you select that tab by:

getTabHost().setCurrentTabByTag(TAB_1);

Or, as midoalageb wrote, by index:

getTabHost().setCurrentTab(index);
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Mark Gjøl
  • 1,845
  • 12
  • 21
  • sorry im still confused, what do you mean? – user123 Jan 04 '12 at 13:47
  • I assume from your code that you have created a button and want to know what to put in the `onClick()` method. If you have a TabActivity, then you get your TabHost (the object controlling your tabs) with the method `getTabHost()`. If you do not have a TabActivity you should have your TabHost as a member variable. On your TabHost object you then execute either of the two methods. If this is not your problem, please be more specific about what is not working for you. – Mark Gjøl Jan 04 '12 at 14:05
  • regarding the button, all i have done is create the button in the xml document, nothing has been linked this is the code i have – user123 Jan 04 '12 at 14:09
  • There is a very nice resource on how to make buttons here: [link](http://developer.android.com/reference/android/widget/Button.html). You need to pass R.id.imageButton1 to your `findViewById()` method. – Mark Gjøl Jan 04 '12 at 14:20
  • this is the code in my tab1 public class Tab1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is Tab1"); setContentView(textview); } } would i add the code in the class overview to that or completely change it? – user123 Jan 04 '12 at 14:37
  • Post that type of stuff in your question, please. It is much easier to read outside of the comments. Just let people know in comments that you have updated your original post. – prolink007 Jan 04 '12 at 14:50
2

Here is an explanation of Tab example :

Create a master Activity:

public class HelloTabWidget extends TabActivity

And create an Activity for each Tab:

public class ArtistsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this);
        textview.setText("This is the Artists tab");
        setContentView(textview); // you replace this with your layout for each view
    }
}

In the master Activity (HelloTabWidget), initiate a TabHost:

TabHost tabHost = getTabHost();

And add TabSpecs for each Tab you have:

intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                  res.getDrawable(R.drawable.ic_tab_artists))
              .setContent(intent);
tabHost.addTab(spec);

Select which Tab you need to be the default one:

tabHost.setCurrentTab(int index);

And finally your Button's onClickListener should be set in the Activity of the Tab containing the Button to:

intent = new Intent().setClass(this, ArtistsActivity.class);
startActivity(intent);

This Button should be present in all Tabs if you want it to switch between them.

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • It switches to the tab specified in the index! If that is not what you need to do, please clarify. – Mohamed_AbdAllah Jan 04 '12 at 13:59
  • i have that code in my xml however i have not added anything into my tab1 java file.. i have more than one button so what isit i should do? – user123 Jan 04 '12 at 14:13
  • So where are the Tabs you need to switch between? Check this [link](http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) on how to create tabs. – Mohamed_AbdAllah Jan 04 '12 at 14:25
  • I have already created 3 tabs, the xml code is an example of one of the buttons i have in my tab1 xml document, i want it to link to my tab 2 but i dont know how to do that – user123 Jan 04 '12 at 14:28
  • Now it is clear :) If you check my link above, each tab is preferred to have a separate Activity and layout, so simply let your button fire an Intent to launch the Tab's relevant Activity and in this new activity, setContentView to the relavent layout. – Mohamed_AbdAllah Jan 04 '12 at 14:35
  • i know i might be a pain, but i really do not understand what it is i have to do :S – user123 Jan 04 '12 at 14:45
  • I edited my answer to explain the android example and at the end shown what you need to do to let the button switch between them – Mohamed_AbdAllah Jan 04 '12 at 15:13
  • sorry but what you have given doesnt do anything on my code :s – user123 Jan 04 '12 at 15:27