Ok, so this may sound like a dumb question, but I am trying to figure out how to have an Activity
(other than main) control it's layout. I have MainActivity
link (on a button click) to another layout. This is all fine.
MainActivity.java:
package com.example.stands;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView resultView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView = (TextView) findViewById(R.id.result);
button1 = (Button) findViewById(R.id.button1);
//on button click, go to next layout
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_second_screen);
}
});
}
}
Now, in my second Activity
, I want to be able to do something like display text on a button click. But when I implement this, nothing happens. I am confused as to how I can make SecondScreen.java
control second_activity_screen.xml
.
SecondScreen.java:
package com.example.stands;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class SecondScreen extends ActionBarActivity {
TextView tv;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);
StrictMode.enableDefaults();
tv = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
//this is where I want to update text field in second_screen_activity
//but nothing happens
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv.setText("Second Activity Button Pressed");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
second_screen_activity.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.stands.SecondScreen$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="142dp"
android:text="Button" />
</RelativeLayout>
So, when I click a button on activity_main
, I go to second_screen_activity
and it displays Second Screen
. On a button click, I want to change Second Screen
to Button Pressed
. What am I doing wrong? Thanks guys.
You need to use Intent
to switch between activities. Replace the following line in onClick()
method of the MainActivity
setContentView(R.layout.activity_second_screen);
to this one
startActivity(new Intent(MainActivity.this, SecondScreen.class));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments