Passing values with intents - android

I have searched and searched and I just can't get this code to work.I have a main.xml layout and a setting.xml.I have some values I would like the Settings.class to change in my main apps class.Three string to be exact.
I have tried this simple test code in my main app class
settings.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Settings.class);
startActivityForResult(intent, 0);
}
});
//Then a function
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
String value = extras.getString("myKey");
if(value!=null){
Log.d("hmmm",value);
}
}
}
In my settings.class I have the following
returnHome.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("myKey", "YEAH");
setResult(RESULT_OK, intent);
finish();
}
});
Back in main app class it is not getting logged.
Like I said I have three string in the main class that I want settings class to change and send back.
Any help is greatly appreciated

I have had success with the technique used in the Notepad tutorial where the information is placed in a Bundle and then added to the intent. See Step 10:
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();

Your code works fine for me, here's the whole lot
Main.java
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button settings = (Button) findViewById(R.id.settings);
settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Settings.class);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
String value = extras.getString("myKey");
if (value != null) {
Log.d("hmmm", value);
}
}
}
Settings.java
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Button returnHome = (Button) findViewById(R.id.returnHome);
returnHome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("myKey", "YEAH");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Settings"
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="Return home"
android:id="#+id/returnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Settings"
android:label="Settings" />
</application>
</manifest>
And the logcat came out:
11-17 12:21:46.717: DEBUG/hmmm(258): YEAH

Related

onActivityResult yields null Intent

In this case two activities A and B should interact in that way that A calls B for a result.
But A never gets any data back from B.
This code looks like any tutorial or SO question I have seen.
The debugging session confirmed that setResult is called.
This problem is robbing me of my motivation, would be nice if someone could point out what I am missing.
EDIT: The code doesn't work when executed on my physical target device, but runs find on a nexus 5 device. What could I try to find out why?
MainActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent in = new Intent(this, SecondActivity.class);
in.putExtra("mau", "bar");
startActivityForResult(in, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// data is null ;(
Toast.makeText(this, String.valueOf(data), Toast.LENGTH_SHORT).show();
}
}
SecondActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Toast.makeText(this, String.valueOf(getIntent()), Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
setResult(RESULT_OK, new Intent().putExtra("foo", "bar"));
finish();
}
}
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
layout/second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iifym.apps.rnoennig.de.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Works for me. (Same layouts and Manifest).
Main
public class MainActivity extends AppCompatActivity {
public static final int MAIN_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Intent in = new Intent(MainActivity.this, SecondActivity.class);
in.putExtra("mau", "start 2nd");
startActivityForResult(in, MAIN_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MAIN_REQUEST) {
if (resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, data.getStringExtra("foo"), Toast.LENGTH_SHORT).show();
}
}
}
}
Second
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent sent = getIntent();
if (sent != null) {
Toast.makeText(SecondActivity.this, sent.getStringExtra("mau"), Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v) {
setResult(RESULT_OK, new Intent().putExtra("foo", "finish 2nd"));
finish();
}
}

intent not working when swiching from one activity to another

I have made a simple android program with two activities,in that 1st activity contains an edittext and a button,and second activity contain a textview.Now when the button in 1st activity pressed the text from Edittext should go to 2nd activity's textView.I have tried code as below,but it's not working:
MainActivity.java
package com.example.myweb;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myweb";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.edit_msg);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.myweb;
import org.w3c.dom.Text;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv.setText(msg);
setContentView(tv);
}
});
}
}
please help me.thank you
Just try this:
In MainActivity.java:
declare Button b and EditText ed as a class field (i.e. keep it outside onCreate())
class MainActivity.java
{
Button b;
EditText ed;
...
onCreate() {
...
b =(Button)findViewById(R.id.button1);
ed = (EditText)findViewById(R.id.edit_msg);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
...
In Act2.java:
...
Intent i = getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
TextView tv = (TextView)findViewById(R.id.tv1);
tv.setText(msg);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
...
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
(OR)
String message = intent.getStringExtra("EXTRA_MESSAGE");
also,
i.putExtra("EXTRA_MESSAGE", s);
Change
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
to
String msg = i.getStringExtra("EXTRA_MESSAGE");
MainActivity.EXTRA_MESSAGE would mean a static variable of MainActivity.java class. Hope you get the difference. You need the variable EXTRA_MESSAGE which you had put in intent i
Edit: For your crash, we need a logcat o/p and activity, manifest code. But possible reasons:
Activity Act2.java is not declared in manifest file.
You said you have only a textview in second activity. But you are trying to get button1 from act2.xml. So you are getting Force close.
Change Act2.java 's onCreate() as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(msg);
}
You don't need to call setContentView(tv); as it is already there in act2.xml and you are using setContentView(R.layout.act2);
Hope your problem gets solved.
Use this code
**MainActivity.java**
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
final EditText editText = (EditText)findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("EXTRA_MESSAGE", editText.getText().toString());
startActivity(intent);
}
});
}
}
**activity_main.xml**
<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=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="74dp"
android:layout_marginTop="26dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginLeft="69dp"
android:layout_marginTop="47dp"
android:text="Button" />
</RelativeLayout>
**SecondActivity.java**
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.second);
Intent intent = getIntent();
final String message = intent.getStringExtra("EXTRA_MESSAGE");
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView = new TextView(SecondActivity.this);
textView.setText(message);
setContentView(textView);
}
});
}
}
**second.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.tester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Try
Intent i = new Intent(MainActivity.this,Act2.class);
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
please review and change your code its works
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.editText1);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
Intent i =getIntent();
// String msg = i.getStringExtra(EXTRA_MESSAGE);
//String receiver = getIntent().getStringExtra(EXTRA_MESSAGE);
String message = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(message);
}
}
Value of the EXTRA_MESSAGE variable in MainActivity.java is different then the key value that you are putting message in to intent.
MainActivity.java:
public final static String EXTRA_MESSAGE = "com.example.myweb";
i.put("EXTRA_MESSAGE",s);
Act2.java: (you are accessing by extra_message variable which is not correct)
Intent i = getIntent();
String text = i.getStringExtra(MainActivity.EXTRA_MESSAGE); //WHICH IS DIFFER FROM THE KEY VALUE
So key value should be the same at the time of setting and getting the value. here while setting the value key is "EXTRA_MESSAGE" and while getting "com.example.myweb" so you will get null pointer exception.
SOLUTION:
just change the line in Act2.java
Intent i = getIntent();
String text = i.getStringExtra("EXTRA_MESSAGE");
Also check your menifest file for the activities declaration.
Hope it may help you.
Did you remember to correctly define your second activity in the manifest XML?
Check out this great guide for anything else you might have missed: http://developer.android.com/training/basics/firstapp/starting-activity.html

How do I redirect a button to a URL or website(browser) Android? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I launch a URL from my application on Android?
I can't seem to figure out how to work on this. I need to click a button that will redirect to a URL / Website. Thanks.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GoogleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button english = (Button)findViewById(R.id.google);//button name in xml file
english.setOnClickListener(google); // on button click listener
}
private Button.OnClickListener google
= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
};
}
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
load_url.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Update:
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testtone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}

Android app hangs when referencing the back button on Secondary screen

I have a small app that when button presses navigates when moving from main screen to next screen this works fine, but when I added a button on the next page (to go back) it breaks.
Fun.java
package com.forcetechnology.OptusApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Fun extends Activity {
OnClickListener backListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fun);
Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf);
backListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.OptusAppMain");
startActivity(i);
}
};
backButtonf.setOnClickListener(backListener);
}
}
Fun.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/backtoMainf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/val5" />
</LinearLayout>
Main.xml
<ImageButton
android:id="#+id/funbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/val5"
android:src="#drawable/val5" />
<ImageButton
android:id="#+id/executionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/funbutton"
android:layout_alignParentRight="true"
android:background="#drawable/val2"
android:src="#drawable/val2" />
<ImageButton
android:id="#+id/performancebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/executionbutton"
android:layout_toLeftOf="#+id/funbutton"
android:background="#drawable/val3"
android:src="#drawable/val4" />
<ImageButton
android:id="#+id/innovationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/executionbutton"
android:layout_alignParentLeft="true"
android:background="#drawable/val3"
android:src="#drawable/val3" />
<ImageButton
android:id="#+id/peoplebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/innovationbutton"
android:layout_toLeftOf="#+id/executionbutton"
android:background="#drawable/val1"
android:src="#drawable/val1" />
</RelativeLayout>
OPtusAppMain.java
package com.forcetechnology.OptusApp;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class OptusAppMain extends Activity
{
OnClickListener funListener,executionListener,innovationListener,peopleListener,performanceListener;;
TextView testView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.forcetechnology.OptusApp.R.layout.main);
ImageButton funButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.funbutton);
ImageButton executionButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.executionbutton);
ImageButton innovationButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.innovationbutton);
ImageButton peopleButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.peoplebutton);
ImageButton performanceButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.performancebutton);
funListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Fun");
startActivity(i);
}
};
executionListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Execution");
startActivity(i);
}
};
innovationListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Innovation");
startActivity(i);
}
};
peopleListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.People");
startActivity(i);
}
};
performanceListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Performance");
startActivity(i);
}
};
funButton.setOnClickListener(funListener);
executionButton.setOnClickListener(executionListener);
innovationButton.setOnClickListener(innovationListener);
peopleButton.setOnClickListener(peopleListener);
performanceButton.setOnClickListener(performanceListener);
}
}
Edit: I have traced the error to this line Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf); in fun.java.
In the onClick() of backListener, just call finish() to go back to the previous activity.
Lets take an example: You have two activities "A" & "B", You start your "B" activity from "A" that means your "A" activity is in stack so there is no need to start it again, just finish your "B" activity with "finish()" method.
public class A extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onAbuttonClick()
{
startActivity(new Intent(A.this,B.class));
}
}
}
public class B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public onBbuttonclick(View v)
{
finish();
}
}
}

How to open a website when a Button is clicked in Android application?

I am designing an app, with several button for users to click on. Once button is clicked, user is directed to appropriate website. How do I accomplish this?
If you are talking about an RCP app, then what you need is the SWT link widget.
Here is the official link event handler snippet.
Update
Here is minimalist android application to connect to either superuser or stackoverflow with 2 buttons.
package ap.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class LinkButtons extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goToSo (View view) {
goToUrl ( "http://stackoverflow.com/");
}
public void goToSu (View view) {
goToUrl ( "http://superuser.com/");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
And here is the layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="#string/select" />
<Button android:layout_height="wrap_content" android:clickable="true" android:autoLink="web" android:cursorVisible="true" android:layout_width="match_parent" android:id="#+id/button_so" android:text="StackOverflow" android:linksClickable="true" android:onClick="goToSo"></Button>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="SuperUser" android:autoLink="web" android:clickable="true" android:id="#+id/button_su" android:onClick="goToSu"></Button>
</LinearLayout>
In your Java file write the following piece of code...
ImageView Button = (ImageView)findViewById(R.id.yourButtonsId);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.yourURL.com"));
startActivity(intent);
}
});
Here is a workable answer.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.todolist"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3"></uses-sdk>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".todolist"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
todolist.java
package com.tutorial.todolist;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class todolist extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_clickme);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://www.anddev.org"));
startActivity(myWebLink);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="#+id/btn_clickme"
android:text="Click me..."
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Import import android.net.Uri;
Intent openURL = new Intent(android.content.Intent.ACTION_VIEW);
openURL.setData(Uri.parse("http://www.example.com"));
startActivity(openURL);
or it can be done using,
TextView textView = (TextView)findViewById(R.id.yourID);
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.typeyourURL.com"));
startActivity(intent);
} });
ImageView Button = (ImageView)findViewById(R.id.button);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Add this to your button's click listener:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
try {
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException exception) {
Toast.makeText(getContext(), "Error text", Toast.LENGTH_SHORT).show();
}
If you have a website url as a variable instead of hardcoded string then don't forget to handle an ActivityNotFoundException and show error. Or you may receive invalid url and app will simply crash. (Pass random string instead of url variable and see for youself )
I just need one line to show a website in my app:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://match4app.com")));
you can use this on your button click activity
Intent webOpen = new Intent(android.content.Intent.ACTION_VIEW);
WebOpen.setData(Uri.parse("http://www.google.com"));
startActivity(myWebLink);
and import this on your code
import android.net.Uri;
public class MainActivity extends Activity {
private WebView webView1;
Button google;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
google = (Button) findViewById(R.id.google);
google.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
webView1 = (WebView) findViewById(R.id.webView);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.loadUrl("http://www.google.co.in/");
}
});
}
#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;
}
}
You can wrap the buttons in anchors that href to the appropriate website.
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
When the user clicks the button (input) they are directed to the destination specified in the href property of the anchor.
Edit: Oops, I didn't read "Eclipse" in the question title. My mistake.

Categories

Resources