Reputation: 23
Very new to Android development. I have a training app that has a text input field and a button. If the entered text matches a word, then the button click
creates a new page (Intent?) which displays some text.
The problem I'm having is that the new page that is created by the button click does not have the same background image as the main page. I don't know why. Here's the relevant bits of code I have so far. I can post more if required.
**fragment_welcome_screen.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:background="@drawable/bg_pic">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
This part works fine and bg_pic is displayed.
WelcomeScreen.java
public void sendMessage(View view) {
// Do something in response to button
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
if ((message.equals("Secret"))||(message.equals("secret"))) {
Intent intent = new Intent(this,
DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I want the button to work only if "Secret" is typed in.
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
//String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
String message = "Test String";
TextView textView = new TextView(this);
textView.setTextSize(24);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
I know I'm probably doing something wrong here.
activity_display_message.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/test_pic"
tools:context="com.example.varun.myapplication.DisplayMessageActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/test_pic"/>
</RelativeLayout>
I would expect test_pic to be the background of the page that is displayed when the button is pressed. But its not. The background is just white.
I realize this is probably a trivial question but can someone please help me out? I would really appreciate it.
Thank you.
Upvotes: 0
Views: 1653
Reputation: 6973
Why you used setContentView() 2 times in DisplayMessageActivity.java. Remove the secound setcontentview() and assign some id for the textview in activity_display_message.xml. Here is the code to assign id for textview.
<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:background="@drawable/test_pic"
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.varun.myapplication.DisplayMessageActivity">
<TextView
android:id="@+id/mytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/test_pic"
android:text="@string/hello_world"/>
And get the textview with that id reference like
TextView textView = (TextView)findViewById(R.id.mytv);
And use this textview reference to show message from the previous activity like this
textView.setText(message);
I hope this will helpful for you.
Upvotes: 3
Reputation: 3322
try like this,
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/test_pic"
tools:context="com.example.varun.myapplication.DisplayMessageActivity">
<TextView android:id="@+id/txtmsg"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/test_pic"/>
</RelativeLayout>
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
//String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
String message = "Test String";
TextView textView = (TextView) findViewById(R.id.txtmsg);
textView.settext(message);
}
Upvotes: 0
Reputation: 830
Change code
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
//String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
String message = "Test String";
TextView textView = (TextView)findViewByID(R.id.textMessage)
textView.setTextSize(24);
textView.setText(message);
}
and also put id for textview
<TextView android:id="@+id/textMessage"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/test_pic"/>
Upvotes: 1
Reputation: 7439
Why you have wrote setContentView(textView); line?
Just remove above given line of the code, and you are done.
Upvotes: 0