Hope this points you in the right direction.
Updated - Working Code & Description
Application min sdk is 11, and target sdk is 19.
My Previous Answer was a little miss leading because in fact you cannot define a ViewGroup in your XML. So you have to create a basic LinearLayout with a resource Id. Here is the following XML which I have come up with for you.
I hope this is exactly what you were looking for I forgot to note for you that you can't define a ViewGroup in XML, but you have to cast your layout to a ViewGroup.
<RelativeLayout 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=".MainActivity" >
<LinearLayout
android:id="@+id/textViewHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/buttonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/buttonPrevious"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_Previous"
style="?android:attr/buttonBarButtonStyle" />
<Button
android:id="@+id/buttonNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_Next"
style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>
My strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextViewSwitcher</string>
<string name="text_Previous">Previous</string>
<string name="text_Next">Next</string>
</resources>
And Finally my MainActivity.class:
public class MainActivity extends Activity {
// ------------------------------------
// Member Variables
private LinearLayout m_textViewHolder = null;
private Button m_btnPrevious = null;
private Button m_btnNext = null;
private final String m_nothingText = "Nothing Pressed";
private final String m_previousText = "Previous Pressed";
private final String m_nextText = "Next Pressed";
// ------------------------------------
// Class Overrides
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a reference to the UI Elements
m_textViewHolder = (LinearLayout) findViewById(R.id.textViewHolder);
m_btnPrevious = (Button) findViewById(R.id.buttonPrevious);
m_btnNext = (Button) findViewById(R.id.buttonNext);
// Add Listeners to the Buttons
m_btnPrevious.setOnClickListener(PreviousListener);
m_btnNext.setOnClickListener(NextListener);
// Populate the Initial Layout with a new TextView, & set the Text
TextView nothingTextView = new TextView(getBaseContext());
nothingTextView.setText(m_nothingText);
((ViewGroup) m_textViewHolder).addView(nothingTextView);
}
// -----------------------------------------
// Private Methods
// Creates a new TextView based on the Text Passed in, and returns the new TextView
private TextView createNewTextView(String text)
{
TextView newTextView = new TextView(getBaseContext());
newTextView.setText(text);
return newTextView;
}
// Removes the Child Views of the Parent ViewGroup
private void removeChildViewsFromParent()
{
((ViewGroup) m_textViewHolder).removeAllViews();
}
// -------------------------------------
// Listeners
private OnClickListener PreviousListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
removeChildViewsFromParent();
((ViewGroup) m_textViewHolder).addView(createNewTextView(m_previousText));
}
};
private OnClickListener NextListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
removeChildViewsFromParent();
((ViewGroup) m_textViewHolder).addView(createNewTextView(m_nextText));
}
};
}
Sorry for the Quick Answer before but this produces the following results when the buttons are pressed.
