0

I have 3 activities. Main activity which is the home screen with two buttons, each button should show up one of the 2 last activity. I tried many methods for multiple buttons both in intent mode and switch mode; but I still can have two working button. The first button starts its linked activity without any problem, but the second button still won't show up. Here is the java code:

package com.live.app; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { Button button; Button button01; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { final Context context = this; button = (Button) findViewById(R.id.buttonUrl); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(context, WoneWideo.class); startActivity(intent); } }); } public void addListenerOnButton2() { final Context context = this; button01 = (Button) findViewById(R.id.button01); button01.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(context, WebViewActivity.class); startActivity(intent); } }); } } 

The main layout file content:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" .......................... .............................. <Button android:id="@+id/buttonUrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/live_button" android:onClick="onClick"/> <Button android:id="@+id/button01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/vod_button" android:onClick="onClick"/> </LinearLayout> 

Also I have all these activities/class declared in the Manifest file.

0

3 Answers 3

2

You forget to call addListenerOnButton2(). Anyhow You can make your code simple

Try This:

 Button btn1,btn2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.buttonUrl); btn2 = (Button)findViewById(R.id.button01); } public void onClick(View v){ if(v.getId() == R.id.buttonUrl){ Intent intent = new Intent(context, WoneWideo.class); startActivity(intent); }else if(v.getId() == R.id.button01){ Intent intent = new Intent(context, WebViewActivity.class); startActivity(intent); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works. But I changed context into getBaseContext to get it work.
2

When there is a property android:onClick="onClick" in your xml, then there is no need for setting listeners. Only thing you need is, a function with signature public void onClick(View v){}

So, the Activity will be as:

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v){ switch(v.getId()){ case R.id.buttonUrl: Intent intent = new Intent(context, WoneWideo.class); startActivity(intent); break; case R.id.button01: Intent intent = new Intent(context, WebViewActivity.class); startActivity(intent); break; } } 

2 Comments

Thanks Nizam. It simply the code. I chann*ged this line to get it work : intent = new Intent(getBaseContext(), WebViewActivity.class);
You are welcome. That was a mistake while copy-paste. Actually it was Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
0

You did not call addListenerOnButton2() in onCreate(). You only called addListenerOnButton()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.