Hey guys so I’m a bit new to Android programming and I have an issue and need help.
So I’ll use the first app that the official android site uses for training (http://developer.android.com/training/basics/firstapp/starting-activity.html)
and I’m confused about the creating second activity part. So after they’ve passed the intent they create a new TextView using java code (instead of XML) so I tried creating that TextView using the xml. I created a new TextView in the xml for the second activity and I give it an id like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/new_Text"/>
</LinearLayout>
and here’s the java code for the second activity:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView) findViewById(R.id.new_Text);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
All the code worked fine before I changed it. I changed
TextView textView = new TextView(this);
to:
TextView textView = (TextView) findViewById(R.id.new_Text);
but for some reason it can’t find new_Text and Eclipse only suggests id’s from my main.xml. Why is it that way? Is it because R.id.blabla only gets id’s from main.xml? So am I forced to make layouts using java code if they’re not going to be from main.xml?
Alot of confusion in this post, but for starters:
Each Activity that you intend to eventually be visable to the user gets its own XML layout. If you start a new Android project it will give u by default 1 XML layout located in the res>layout folder and 1 activity which will serve as your user facing visual activity by default.
For your purposes , some easy ways to figure out if a activity is meant to be a “visual” activity include:
*it extends activity or some other android superclass
*it has a “onCreate method (useually located towards the top of the class)
*inside that onCreate method there is a line of code called setContentView that looks something like this.
The setContentView method is important bc its kind of like the glue between your activity and your xml layout. After R.layout.___ goes the name of the XML layout you would like to use.
Only after youve set your content view to the approprate view can you link the elements or “views” from ur xml layout to your activity using the id you created. like this
if you set the content view to httpex.xml , you can only link to views inside httpex.xml and ect…
I have a spft spot for newbs bc ik what dicks this crowd can be to new blood for their ignorance , they forget how hard it was starting out and begin to feel all this stuff is common sense and obvious, but if i were you i would head over to thenewboston[dot]com and watch the entire series before posting to many questions like this around here so you dont get flamed on.