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);
...
Related
I have created a MainActivity, it has a layout which has different elements; e.g: TextBox, EditBox, Button.
I have created a ChildActivity that is extending from MainActivity, ChildActivity also has a Layout.
My question is, can i use the layout elements of MainActivity and display them in my ChildActivity
The elements you can use depend on what layout file you pass to setContentView(R.layout.my_layout_file); in the onCreate. So yes you can use them in both if you give both activities the same layout file, but they will be treated like separate layouts. E.g. if you set some text in a textview in Main, it will not show in Child.
yes you can access the parent-activity element through add child-activity layout in parent-activity layout.this way you can inflate both layout in child-activity.
ViewGroup viewGroup; is child layout container in parent-activity.
like
parent activity or NormalActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ViewGroup viewGroup;
protected void onCreate(Bundle savedInstanceState,int res) {
onCreate(savedInstanceState);
viewGroup = (ViewGroup) findViewById(R.id.childContainer);
viewGroup.addView(LayoutInflater.from(this).inflate(res, viewGroup,false));
}
}
Child Activity
public class ChildActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.child_layout);
}
}
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
When changing the text of TextView of the first view, the textView's text of the second view shows the text of both TextViews, one on top of the other.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout rl=(FrameLayout) findViewById(R.id.mainLayout);
View v1=LayoutInflater.from(this).inflate(R.layout.text_view, null);
View v2=LayoutInflater.from(this).inflate(R.layout.text_view, null);
TextView myTextView1= (TextView) v1.findViewById(R.id.myTextView);
TextView myTextView2= (TextView) v2.findViewById(R.id.myTextView);
myTextView1.setText("str1");
myTextView2.setText("str2");
rl.addView(v1);
rl.addView(v2);
}
I would guess the issue is because you are using a framelayout so the subviews will overlap. If you switch that framelayout to a linearlayout for example I imagine the results will be as you expect according to the textviews.
very simple.. 1. you have to use append property rather than settext in second textview
2nd you can use extras to pass using intent or use getter setter method like
public static void setstr_name(String str_name) {
GlobalVariable.str_name = str_name;
}
public static String getstr_name() {
return str_name;
}
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");
}
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);