Reputation: 1249
Once I set the contentView to a layout, how can I manage the UI via java? Also, if I start a new activity, do I reset the contentView?
Upvotes: 1
Views: 103
Reputation: 2804
ContentView will only be in one activity.
Assuming you are in something that extends Activity
all you have to do, after setting content view, is access your views. Say you have a button in your layout:
Button homeButton = (Button) findViewById(R.id.homeButton);
homeButton.setText(); homeButton.setOnClickListener() etc
If you're in a dialog, or need to access certain views in specific circumstances, or you're in an intent and need to access stuff in your main activity's layout:
Button secondButton = (Button) getActivity().findViewById(R.id.secondButton);
//other methods on the button
EDITS--
Well images, you just add an ImageView into the layout:
<ImageView android:id="@+id/logoImageView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/logo" android:contentDescription="@string/app_name"
android:layout_gravity="center_horizontal"/>
You don't need to do anything with code specifically, it should just show up.
Buttons and other views can also be added via XML. You can add things programmatically as well, though it's not as common.
If I remember correctly, the constructor to create a button in Java that's not in your XML file, it would be Button aNewButton = new Button(getApplicationContext());
aNewButton.setText("whatever");
Upvotes: 2