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.
Related
Here is the XML code for a simple TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:id="#+id/test"/>
Then, in MainActivity, it is used like this:
TextView test = (TextView) findViewById(R.id.test);
test.setText("test");
I want to know how the view is accessed in Main class, which is defined in xml layout.
Can anyone explain how it happens?
how the view accessed in Main class
To be honest, i dont know the process in detail. Let me help you on what i know :
The Activity will search the layout XML in setContentView method.
After the layout has been found, we can use findViewById to link the instance (test - in your case) we created to the layout XML.
If the ID is found, the instance (Java) and XML will be linked.
Of course, you can do something like :
TextView test2 = new TextView(this);
Which means the instance is not must exist in XML.
Sorry English is not my native language.
On your Activity, you have:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//...
}
According to Android documentation, setContentView is responsible for linking your XML layout to your Activity:
Set the activity content to an explicit view. This view is placed
directly into the activity's view hierarchy. It can itself be a
complex view hierarchy.
After the Activity's content view is set, you can use findViewById in order to access the views of the XML linked to your Activity:
Finds a view that was identified by the id attribute from the XML that
was processed in onCreate(Bundle).
Hope it helps you to understand this process! :)
I think this is the method
1.when you create or declare a text view or something like that a reference is generated in your R file(JAVA file).
2.which is what you access through
R.id.test
this is how your linking works
I am making an app using two tabs with different layouts, which means I have three total layouts at the moment:
activity_main,
fragment_receive,
fragment_send
In my onCreate method in my main activity, the following line sets my content view to activity_main. (If anyone could explain why this layout appears to be blank and yet my app still shows both tabs, that would be great.)
setContentView(R.layout.activity_main);
Then I use the findViewById method to set a TextView to a view that appears in both fragment_receive.xml and fragment_send.xml.
currentExchangeRate = (TextView) findViewById(R.id.exchangeRateView);
Then I attempt to use the setText method on this TextView.
currentExchangeRate.setText(Double.toString(lastPrice));
This line gives me the NullPointerException.
Can anyone explain this to me?
findViewById() called from the Activity, looks for the id in the hierarchy of views of the Activity.
if you want to perform that on the Fragment hierarchy, you have to call it from the Fragment view, for example view.findByById() from the Fragment onCreateView, after you inflated the layout.
When you call:
currentExchangeRate = (TextView) findViewById(R.id.exchangeRateView);
The compiler finds that the static int ID does exist within your R.id because you declared them in your fragments, and therefore doesn't throw a compiler error. But at runtime, the current view layout (the one you set with setContentView(R.layout.activity_main);) doesn't hold that ID and therefore returns null.
To access your textview that exists within your Fragment from the Controlling Activity, you use the Fragment Manager (add an id property to your fragments in activity xml, or set tags when you declare your Fragments in your code):
ReceiveFragment receiveFragment = (ReceiveFragment) getSupportFragmentManager().findFragmentById(R.id.receive_fragment);
TextView tv = (TextView) receiveFragment.getView().findViewById(R.id.exchangeRateView);
tv.setText(Double.toString(lastPrice));
findViewById retrieves the view with the requested id from your activity's layout
To get your fragment's textview, make the view that your fragments onCreateView is returning a static variable and initialize it in the onCreateView.
Then you can retrieve the textview like this:
TextView textview = (TextView) YourFragmentTitle.yourFragmentViewTitle.findViewById(R.id.yourId);
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.
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.
I don't seem to understand if it's possible to inflate (include) an activity into another activity. I know i can inflate a layout xml, this works, but i am wondering if i can inflate an activity. For instance , i have class A that extends Activity and another class B that extends ListActivity. Can i include and use in class A, my class B?
THis is what i have tried:
Class A:
LayoutInflater inflater = (LayoutInflater) MyActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate list
BActivity list = new BActivity();
Class B:
public class BActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
List<Model> models= new ArrayList<Model>();
models.add(new Model("John"));
models.add(new Model("Cage"));
setListAdapter(new MyAdapter(this, models));
ListView list = getListView();
}
}
and in xml (the class A xml): (for where i want to see the list)
<view class="com.test.BActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </view>
All of this throws errors :
Error inflating class BActivity
The activities are declared in the manifest.
Do you know what i am doing wrong? this is not the correct way to inflate another activity? I am using Android 2.2 api 8.
Thank you for your time.
Your question title and your issue are not actually the same thing. For completeness, I will answer both.
What is the difference from inflating an activity and inflating a view in android?
The answer is there is no difference. Ultimately, they are the same in process and logic. However, an Activity may have many different Views and you may setContentView() several times to several different Layouts or Views based on your need. An Activity requires a Layout resource, and a View may or may not be a Layout.
Do you know what i am doing wrong?
Yes. Absolutely.
Your code: BActivity list = new BActivity(); is not actually inflating an Activity. You are constructing the Activity, but not starting it.
Your XML defines BActivity as a View, but your code defines it as an ListActivity. These are two different things entirely. A ListActivity has a ListView (extended or otherwise); A ListActivity is not a ListView.
Activity and its subclasses are Contexts that have a Life Cycle that is managed by the OS. They contain and speak to Views of all types, but are not themselves Views.
this is not the correct way to inflate another activity?
No sir, but fear not! The answer is not too far away.
FAKE ANSWER (for completeness) -
First, to start another Activity so that it is inflated, you must call startActivity() from a Context. A Context may be an Application, Activity, Broadcast Reciever or any other app component (Component = declared object in your Android project manifest). So, if you really wanted to start a new Activity, you would change BActivity list = new BActivity(); to:
Intent _listActivity = new Intent();
_listActivity.setClass(BActivity.class);
startActivity(_listActivity);
REAL ANSWER -
However, since you want to see your List in class A, BActivity is not an Activity, it is a View. That means what you REALLY want is to make it recognize your View and this is a different solution. Change public class BActivity extends ListActivity to public class BActivity extends ListView and now all of a sudden you have a custom View!! Now all we have to do is get the List to work.
Constructing the View - Views are different from Activities in that they do not have a public void onCreate(Bundle bundle). All of your stuff from BActivity.onCreate() would instead be placed in the constructor. But, you don't have a proper constructor... hmmm. Well, there are three constructors to choose from -- add one or all of the following (You will probably want either option 1 or 2, at first. But you won't use both at the same time hint hint, read the comments:
//This constructor is used when the View is created from code (not XML!!)
public BActivity(Context context)
{
}
//This constructor is used when the View is created from XML (not code!!)
public BActivity(Context context, AttributeSet attr)
{
}
//This constructor is used when the View is created from XML with a Style defined in separate XML.
public BActivity(Context context, AttributeSet attr, int defStyle)
{
}
Inflating the Activity = Inflating the View
You have a choice here, you can either add the View, or you can inflate the View. There are many options for both. Based on your question, I shall assume you want to inflate the View. Simply change BActivity list = new BActivity(); to setContentView(R.id.MyXML). MyXML, of course, would be the name of your XML Layout file. SetContentView will then open the appropriate View for you (BActivity) using the 2nd constructor from the list above.
Understanding the difference between View and Activities is important. The processes between them are very similar, but they themselves have a intertwined but separate purpose.
An Activity MUST have a View.
A View MUST be in a Context.
An Activity is a Context, but a Context may also be one of several other possible classes.
Both may be inflated using a LayoutInflater
An Activity has a convenience method called setContentView which can inflate an entire XML file.
A View must inflate each View manually using LayoutInflater object.inflate().
An Activity has a Life Cycle. A View has a draw cycle instead.
For more information, certainly read more on the Android Developers Resources. However, some of these things are only learned by experimentation.
Hope this all helped!
FuzzicalLogic
Inflating means parsing an XML description of an object and building a Java object with the described attributes. Activities do not have a detailed description in XML and thus no point to inflate them.
You can dispatch activity A from activity B or you can use the new concept of Fragment to combine multiple sub activities into activities.
BTW, you can define custom views (by subclassing one of the view classes) and use it in your XML layouts, just put the class full path e.g. in the layout instead of let say .