Android Starting an Activity Introduction - Defining text_message - android

I am working on the Android guide here: http://developer.android.com/training/basics/activity-lifecycle/starting.html and there is reference to a variable that has never been created
mTextView = (TextView) findViewById(R.id.text_message);
Where am I supposed to define text_message?
Thanks for your help!
Update: I believe the chunk of code from which this is taken is merely an example, and not to be incorporated with the application we created in the previous part of the tutorial.

It is declared here
TextView mTextView; // Member variable for text view in the layout // Right here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Declaring it outside of a method, generally right after class declaration, makes it a member variable so it can be used anywhere in the class, including inner classes and listeners. You just can't initialize it in the same place because you haven't inflated your Layout yet by calling setContentView()
Short example
public class MyActivity extends Activity
{
TextView mTextView; // Member variable for text view in the layout
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface layout for this Activity
// The layout file is defined in the project res/layout/main_activity.xml file
setContentView(R.layout.main_activity);
// Initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_message); // findViewById) looks for the id you set in the xml file, here text_message
}
R.id.text_message is defined in the main_activity.xml which could look something like this
<?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">
<TextView
android:id="#+id/text_message" <!-- this is where the R.id.text_message comes from -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
That documentation is definitely good to go through but you may want to start Here and go through a short example starting from creating a project

use this way define the xml layout inside the folder of res/layout/main.xml and set in setconentView.
private TextView mTextView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
stContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text_message);
mTextView.setText("Hello");
}

Related

Android: Button not showing on view

I'm completely new to Android and am trying to add a simple button to my view, but my view is completely blank. Here is my setup:
EDIT: I have now changed my code to write this programmatically since I'm used to that with iOS.
I'm now getting the following error:
Error: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOrientation(int)' on a null object reference
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Create main view
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
}
}
You need to setContentView which takes a View or a layout resource id.
Also, you can only call findViewById if you provide a layout resource id, and call setContentView with it. Alternatively, you could inflate it and call view.findViewById on the resulting inflated view.
Here's your code with some changes that should hopefully fix your new issue:
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// Create main view
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
setContentView(linearLayout);
}
}
I cannot pinpoint the error but maybe you can.
Try checking after every line you type and see if the button appears. Also check if the problem is with button only or does it extend to other layouts as well.
Just type and see what happens.
Follow it up with dimensions.
Don not forget to save after each step. Usually all such problems can be solved by detailed debugging.
If none of the layout appears, try and restart the software.
You didn't callsetContentView(R.layout.activity_main) , in your code there, you commented it out, so your layout wasnt inflated so you got a blank white screen. Uncomment that line and you will be fine.

How to set text in a textview which is in a layout, that has bee referenced in 'Preferences' file in android?

I have created a preferences settings xml file, in which I have referenced another layout. (android:layout="#layout/things") . Now I want to add a text to the textview that is present in "things" layout from the activity that extends preference activity. how to do it? I tried like this,
LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.things,null);
TextView name = (TextView) view.findViewById(R.id.t_name);
name.setText("furniture");
But the text is not set, its just empty? How to do it?
You should be able to get a reference to that button inside your activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//.........
TextView name = (TextView) findViewById(R.id.t_name);
name.setText("furniture");
Assuming you are referencing the android:layout="#layout/things" inside the <Preference> tag, all views inside "things" should be accessible from this scope

how to set ImageView visible through out the android application for header

I have a header design which contains an ImageView and it is common for all the layouts in my application. I want to set the ImageView visible onclick of some button. The ImageView must visible in all the activities. I am using .setVisibility(View.VISIBLE); but its not working for all the activities.
There are many ways to implement this task. One of the easy task is to implement a common header by using ViewStub. Follow below steps:
Define header.xml with ImageView or any other widgets.
Now take ViewStub inside the layouts of other activity's xml layout for example: main.xml
<!-- Included header.xml here -->
<ViewStub android:id="#+id/vsHeader"
android:inflatedId="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="#layout/header" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/txtDemo" />
Define one BaseActivity class from where you can control the visibility of ImageView.
abstract public class BaseActivity extends Activity{
protected View header;
protected View footer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
#Override
public void setHeader(Activity activity, boolean visible) {
if (visible) {
activity.findViewById(R.id.vsHeader).setVisibility(View.VISIBLE);
header = findViewById(R.id.header);
} else
activity.findViewById(R.id.vsHeader).setVisibility(View.GONE);
}
}
Now extends this BaseActivity to all other your Activity, so that you can access those methods.
So in 3rd step above, you can call setHeader() method with true/false as a visible value. You can check detailed example here: Android – ViewStub example
You can create a class say "MyHeaderVisibility" that holds a static boolean variable say "imagevisibility". Set visibility of the header image for all activities dependent on this boolean variable. You should be good to go.
class MyHeaderVisibility{
static boolean imagevisibility = false;
}
On a button click event' you can change the visibility by:
Button b = (Button) findViewById(R.id.yourbuttonid);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyHeaderVisibility.imagevisibility = true;
}
}

Android: How to get a view given an activity inside a normal class?

I have a normal class (not an activity). Inside that class, I have a reference to an activity.
Now I want to access a view (to add a child) contained in the layout xml of that activity.
I don't know the name of the layout file of that activity. I only know the ID of the view, which I want to access (for example: R.id.my_view).
How can I do that?
Regarding the NullPointerException (which you should add to the question), always make sure you've called setContentView() in your Activity before trying to access a View defined in XML. Example usage:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
...
}
Then, somewhere,
ViewGroup group = (ViewGroup) context.findViewById(R.id.group); // In your example, R.id.my_view
The reason you need to have called setContentView() is that before it's called, your View(Group) doesn't exist. Because findViewById() is unable to find something that doesn't exist, it returns null.
As simple as that!
View view = activity.findViewById(R.id.my_view);
In case of the Layout:
LinearLayout layout = (LinearLayout) activity.findViewById(R.id.my_layoutId);
And to add the Views:
layout.addView(view);
You could make your method accept an Activity parameter and then use it to find the view by id.
Ex:
public class MyClass{
public void doSomething(Activity context){
TextView text=(TextView)context.findViewById(R.id.my_textview);
}
}
Then in your activity:
obj.doSomething(YourActivity.this);

Get access to Views in another layouts

How can I get reference to the TextViews, for example, if I didn't set layout as a content view?
I'm using TabHost to switch between Activities:
public class TimerResultsActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initTabs();
TextView resultsText = (TextView)findViewById(R.id.textView1);
resultsText.setText("test");
}
...
"textView1" has added to one of tabs, but hasn't declared in the main.xml.
How can I change a text on it?
You'll need a reference to the base of the layout that it is declared in. Then you can use the findViewById() method of the ViewGroup it belongs to.
TextView resultsText = (TextView)myTab.findViewById(R.id.textView1);
...

Categories

Resources