I have two activities namely MainActivity and DisplayMessageActivity. MainActivity is a app launcher and the first UI which contains only two components : EditText and Button .
And DisplayMessageActivity has only one component TextView with default string defined. I just want to change/set the TextView's text value which was typed in EditText field and when the Button is clicked.
TextView textView = (TextView) findViewById(R.id.typed_text);
returns null pointer in DisplayMessageActivity which was navigated by MainActivity's intent call. I could able to set and get the text in MainActivity without a problem. Please help me :)
#codeMagic : Thanks :) Here is the xml
`
<TextView
android:id="#+id/typed_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/typed_text"/>
`
The onCreate method in DisplayMessageActivity :
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(MainActivity.EXTRA_MESSAGE);
// Create the text view
//TextView textView = new TextView(this); - This works
TextView textView = (TextView) findViewById(R.id.typed_text); // null pointer
textView.setTextSize(40);
textView.setText(message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
When you start DisplayMessageActivity, use intent.putExtra() to pass on the input from your EditText.
http://developer.android.com/training/basics/firstapp/starting-activity.html
This is probably exactly what you need!
Related
I have an activity in foreground.
when i press a button "OK" on the activity, the same activity must come on top with new fields in it.
I changed the button text from OK to CANCEL in onclick handler. It worked fine.
But user cant see that that a new page is loaded .
I am new to android, can someone hint me?
Sounds like you need to read the documentation
But as a quick start you should build a new intent to start your Activity. You can pass data between your Activities using putExtra methods on your Intent:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, YourActivityName.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
When you start your Activity you can retrieve the data stored inside the Intent using the following:
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
You can then display the message (or any other data field) inside your View:
textView.setText(message);
Does the following work? (Place it in your onClick method)
TextView txtView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(newText)
where newText is the new String that you need to place in textView and R.id.textView1 is the reference to your TextView in your layout.
I have a checkedbox that when, onClick (checked/unchecked), would setText to an activity.
When I run the application, it stopped and will return to the previous page.
What is wrong with my code?
My OrderActivity.java has:
public class OrderActivity extends ActionBarActivity {
CheckBox OrderMenuBiggDeal, OrderMenuCrispyChicken, OrderMenuExtremeBurger, OrderMenuTenderloinTips;
TextView ReceiptTextMenuBiggDeal, ReceiptTextMenuCrispyChicken, ReceiptTextMenuExtremeBurger, ReceiptTextMenuTenderloinTips;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
OrderMenuBiggDeal = (CheckBox) findViewById(R.id.checkBoxMenu1);
ReceiptTextMenuBiggDeal = (TextView) findViewById(R.id.textViewReceiptMenuPrice1);
and
public void onClickBiggDeal(View view){
if(OrderMenuBiggDeal.isChecked()){
ReceiptTextMenuBiggDeal.setText("" + "hello");
}
else{
ReceiptTextMenuBiggDeal.setText(R.string.default_value);
}
the application closes on the ReceiptTextMenuBiggDeal.setText("" + "hello"); line.
The setContentView is activity_order.xml.
The location of the TextView that I want to setText (ReceiptTextMenuBiggDeal) is on a different xml file, the activity_receipt.xml
You should change this
setContentView(R.layout.activity_order);
to
setContentView(R.layout.activity_receipt);
It's because your TextView with id textViewReceiptMenuPrice1 belong to activity_receipt layout and you trying to find it on activity_order layout.
You cannot set text on textview that is in other xml, just in one that is set in setcontentview
I am trying to pass EditText from Activity1 to Activity2.
Activity1 code:
public void openNextActivity()
{
Intent intent = new Intent("com.abc.xyz.ImageActivity");
EditText myEditText = (EditText)findViewById(R.id.myEditText);
int myEditTextId = myEditText.getId();
//For Test purpose ----- starts
// **Point1: next line of code works fine in this Activity1**
EditText myEditTextTest = (EditText)findViewById(myEditTextId);
//For Test purpose ----- ends
intent.putExtra("myEditText", myEditTextId);
startActivity(intent);
}
Activity2 code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_detail);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
int myEditTextId = extras.getInt("myEditText");
// Point2: next line of code displays the correct Id
Log.d("tag","myEditTextId"+ myEditTextId);
// Point 3: Next line of code not works in this Activity2
EditText myEditText = (EditText)findViewById(myEditTextId);
if(myEditText != null)
{
Log.d("tag","Not null");
}
else
{
Log.d("tag","null");// **Point4: this condition executes**
}
}
}
The problem is that the line : EditText myEditText = (EditText)findViewById(myEditTextId); works fine in Activity1 but its not working in Activity2.
EDIT:
Note: Both activities are using different layouts
Thanks for your valuable time & help.
The only views you have accessible to you are those in the layout you loaded at the start of Activity 2, i.e those in R.layout.comments_detail. I'm guessing that Activity 1 loads a different layout with its setContentView(..) and it's in that layout where 'myEditText is defined and in scope.
You can't pass a view as an extra. You can pass the string in the view (if that's your purpose).
It seems like you're trying to get the EditText id before it's been assigned:
int myEditTextId = myEditText.getId();
EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**
Try this instead:
EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**
int myEditTextId = myEditText.getId();
Edit
Does the EditText in question even exist in the set layout? (R.layout.comments_detail)
This can not be done.
If your attempt is to manipulate activity 1 from activity 2 then you should be returning something to activity 1 from activity 2. By passing the ID of view you made in activity 1 to activity 2 shouldn't resolve to anything due to nothing being created in activity 2. findViewById is invoked on the current activity. As you didn't set anything in the view there is nothing for it to find.
I think it can work if you use the same layout R.layout.comments_detail in Activity1 and Activity2 because findViewById() return the unique id and this id only belong layout comments_detail
i have two activities each contain its own textview any one help me that when i click the one textview text the text goes to the another textview of the second activity please any on help me in this problem
In activity A.
TextView aTv = findViewById(R.id.aTextView);
String aText = aTv.getText().toString();
Intent i = new Intent(bClass.class); // your other activity
i.putExtra("myText", aText);
startActivity(i);
In the onCreate of Activity B:
String aText = this.getIntent().getStringExtra("myText");
TextView bTv = findViewById(R.id.bTextView);
bTv.setText(aText);
Is that what you were looking to do? I'm into a few beers so there may be a few typo's but that's the idea. :)
Is it possible to add a button to the right corner of the app title?
e.g., adding a "refresh" button to the title of "Feed: my feeds"?
http://www.android.com/market/apps/feedr-lg-01.jpg
The simplest way to do that, IMHO, is to get rid of the standard title bar (android:theme="#android:style/Theme.NoTitleBar" in the <activity> element in the manifest) and put your own "title bar" at the top of the activity.
Note, though, that the "button in the title bar" style is more iPhone-ish. Android would typically have that in the option menu, so the UI is less cluttered (at the cost of two taps to do the refresh).
Why don't you try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitle= requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitle ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Set your layout for title here and mention your button in this layout);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
myTitleText.setBackgroundColor(Color.BLUE);
}
}
yep this solveed an issue i had... trimmed version is below...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Set your layout for title here and mention your button in this layout);
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
/* your code here */
}
}
I think a better approach would be to simply refresh the view if it is active by using a Handler. If your pulling content when the activity is resumed then any time you leave and come back to the view it will refresh. If you are expecting users to sit at the top level of the view and need to update the information then you can handle this with a delayed handler which will call your resume method and periodically refresh the view thus negating the need for a button.
Here is a link to the documentation for the handler class. I would start by looking into the basic use of handler. Then test the sendMessageDelayed method so that at the end of every call you restart the handler. Also be sure to only create a new handler if your activity is the top activity and don't bother refreshing the ui if it is not. Adding a simple isActive flag to on pause and on resume is a decent way to check for this.