Reputation: 952
I have an Activity
called MainActivity.java
, with an activity_main.xml
file. But I don't know how to create a link that will launch another activity called DirectionActivity
. Below is a sample of my code.
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EEMA</string>
<string name="action_settings">Settings</string>
<string name="hello_world">The Emergency Evacuation Mobile App!</string>
<string name="directions">\n\nTo view directions click here!</string>
<string name="title_activity_direction">DirectionActivity</string>
<string name="title_activity_main">MainActivity</string>
</resources>
activity_main.xml textview
<TextView android:id="@+id/txtView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:textColorLink="#FFFF00"
android:textStyle="bold"
/>
MainActivity.java
public class MainMenuActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
String str = "Please click here to view Directions";
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setText(str);
Linkify.addLinks(txtView, Linkify.ALL);
}
@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, menu);
return true;
}
}
DirectionActivity.java
public class DirectionActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenuActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_direction);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
So what is it I need to do to get the Please click here to view Directions to appear as a link that will open the DirectionActivity?
Upvotes: 3
Views: 13329
Reputation: 6052
You want to do two things. First, you want to have something happen upon the click of a TextView
. Second, you want to launch a new Activity
.
To accomplish the first thing, in your activity class' onCreate
method, you want something like:
final TextView txtView = this.findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
To then accomplish the second thing, if the Activity
you want to launch is called DirectionActivity
:
final TextView txtView = this.findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainMenuActivity.this.startActivity(new Intent(MainMenuActivity.this, DirectionActivity.class));
}
});
Upvotes: 5