My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.
Related
Good evening
I am trying to make users can change font in other layout called row.XML from the main activity class everything seems okay but there's no change of the font or color it seems like look not linking
Any idea guys
Edit:
I used include method row.XML in main XML but all in vain
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
/* We inflate the xml which gives us a view */
view = inflater.inflate(R.layout.row, null);
/* Get the widget with id name which is defined in the xml of the row */
textfont = (TextView) view.findViewById(R.id.textItemmain);
(https://i.stack.imgur.com/GN6UQ.png)
That is not how views work, views are not explicitly bound to an activity so all you are doing by inflating that view is inflating a view that isn't associated to anything.
To send data back to an activity you need to use startActivityForResult then listen for data coming back to it when the activity is resumed
First of all you need to know that you can't change the views properties of another layout activity directly from your current activity it is not possible, what you can do is that before going to the next activity you can pass an intent with a Boolean flag that will be check in the targeted activity to see if a boolean condition is true or false if it is true change the font of textview in target activity if it is false dont do anything, This is the only way to achieve what you are trying to do. If you can't create an intent and pass boolean extra in it post in comments I'll update the answer.
I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now
I am trying to make an app(http://pastebin.com/uWkP6XNY) that when you press a button, creates a custom sms message. The user can go to a second activity (http://pastebin.com/MK2NPV5R) thats full of edit-texts that when saved, will bring back strings to be used to change the custom sms.
The issues I am facing is how I initalize my variables with whats in savedpreferences. I put this in my onCreate method.
smsintroduction = (sp.getString("intro", "")); //these are both strings initalized at the top
smsbody = (sp.getString("body", ""));
On start up, since it can't get "intro" from the dictionary, it goes to a null string. I want to be able to use my save() function in my second activity to save, which I Think I already do, but be able to change my two strings above.
I put the code above to set the strings in a method that completes the finalized textbody, but it keeps giving me emptystrings.
The only thing that gets created is "!", as shown in finishedtext().
It looks like in your MainActivity in onCreateOptionsMenu you are overriding the sp member previously set in onCreate with
sp = getSharedPreferences("prefs", 0);
Try removing that line or setting those shared prefs to a different instance member.
I'm new to Android development.
I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.
When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.
I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.
All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.
Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.
Thanks,
SonCoder
Not exactly sure what you want but either way..
-You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.
-Write a custom list adapter (extend from base adapter).
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
In the getView method of this class you load the the String field of the model that you want in the textview.
-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.
-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from
parent.getAdapter().getItem(position);
in the on click method) into the detail activity.
-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.
-if you want to set the titile in the actionbar set:
this.getActionBar().setTitle(title)
This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.
setTitle(String title) can be called from anywhere using the activity by the way.
So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.
In the listener you say something in the ways of this:
Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);
Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.
Continue in your MySubActivity's onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String key = getString(R.string.my_title_key);
String title = intent.getString(key);
setTitle(title);
}
NOTE: I'm not sure of all method names are correct and such but something like this.
I have a log in page that pulls information from a data base, I then want to use this some of this information to populate different textviews on a new page/activity. I can get a textview to change on the activity where I have my submit button, but when I try to change the textview on my second activity, it just crashed (The application has stopped unexpectedly).
Here's my code for changing the textview (where txtID is my textview on a separate activity)
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
my xml for seperate activity
<TextView android:text="TextView" android:id="#+id/txtID"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
Oh, I'm using a tableview for my login page, then tabs for my the rest of my pages. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
You don't want to directly touch the UI elements of another Activity. You can make use of bundles to pass information back and forth. Here is an example:
Say we have Activity A, and it has some information as a String it wants to pass to become the text of a TextView in Activity B.
//Setup our test data
String test = "Some text";
//Setup the bundle that will be passed
Bundle b = new Bundle();
b.putString("Some Key", test);
//Setup the Intent that will start the next Activity
Intent nextActivity = new Intent(this, ActivityB.class);
//Assumes this references this instance of Activity A
nextActivity.putExtras(b);
this.startActivity(nextActivity);
So now in the onCreate method for Activity B, we can get that String and assign it as the text to the TextView like you have
public void onCreate(Bundled savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //Setup some layout, set to your own
String test = getIntent().getExtras().getString("Some Key");
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
}
I'd probably let each separate Activity take care of its own display, and not try to have Activity 1 directly update the display of Activity 2, which is kind of what it looks like you were doing.
The Notepad Tutorial demonstrates an application with two Activities, where one Activity calls another, passing in data. (Take a look at onListItemClick in Notepadv3.) You could maybe follow this model to pass data from Activity 1 to Activity 2, where Activity 2 then takes care of its proper display, using the data it received.
If you're still having problems (like your application crashing), then please post the complete minimal code necessary to replicate your problem. Note the Notepad Tutorial and the Hello, World Tutorial include steps for debugging, which might help you isolate the exact problem.
Andy... If you try to directly touch a UI widget in another activity, your app will crash. Been there, done that accidentally. Instead, consider passing an immutable stateful object between the activities. This can be done using startActivityForResult for instance. I have some sample code here.