I want to create a new Activity in Android Studio with MainActivity as a parent. Although, when I create a blank activity (ex. NewActivity) with a fragment added I get the two expected classes
(NewActivity and NewActivityFragment) but when I check the layout resources, I can't understand why there are 3 XML files auto-generated and what's their meaning?
What is the exact meaning of each XML-file generated ? The 3 XML files are the following : activity_new.xml, fragment_new.xml and content_new.xml
For example , if i want to add a TextView in the second activity, which XML file shall i modify?
When you create a New activity with a Fragment using the wizard in Android Studio, it will generate two src files :
NewActivity.java
NewActivityFragment.java
and three res files:
activity_new.xml
content_new.xml
fragment_new.xml
The details goes as below:
The activity class NewActivity.java inflates the layout activity_new.xml in onCreate() method as below.
setContentView(R.layout.activity_new);
This layout is a CoordinatorLayout and contains the Appbarlayout, FAB and the container for your main comonents.
activity_new.xml includes another layout using include tag.
content_new.xml is a fragment xml file and contains the attributes to define its layouts.
observer that content_new.xml has an attribute as below
tools:layout="#layout/fragment_new"
fragment_new.xml is the layout which gets inflated in NewActivityFragment onCreateView() method.
inflater.inflate(R.layout.fragment_new, container, false);
fragment_new.xml is the layout file that is the place where you go for adding the components to be shown in the fragment. So as per your requirement of adding a TextView in the fragment you need to add it in fragment_new.xml.
If you check activity_new.xml, you will see an <include> tag calling the content_new.xml.
Related
I want solution of following:
I have create xml file in data/data/com.example.file/files/abc.xml so during running of app how I get this abc.xml in res/layout and should display on screen.
Thanks.
In general, the XML vocabulary for declaring UI elements closely
follows the structure and naming of the classes and methods, where
element names correspond to class names and attribute names correspond
to methods.
Courtesy goes to #Mike M : You cant create this .XML files used for layouts are pre-compiled .
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
the RelativeLayout item will be the container for your child view.
Your question needs to be a bit more specific, but here's a general answer:
If you are using fragments then you should re-create your fragment and then use:
inflater.inflate(R.layout.abc, container, false);
If you are using activities then you should use:
setOnContentView(abc);
I'm dealing now with fixed tabs and fragments. I have 2 tabs. But it happened that the 2 xml files are a copy and paste they differ slightly. I do not want to change the id name of each view there.
The first xml file name is :first.xml
the second is :second.xml
When initializing the views in my main activity extending the 2 fragments (inner classes), how can I determine the desired views if they have the same id name ?
For example:
TextView tv = (TextView)findViewById(R.id.textView1); //However, this exists in both first.xml and second.xml , how can I tell it to take it from second.xml and not from first.xml ?
Thank you!
I have activity_main which is default layout in my activity, there i have framelayout which display fragment whit different layout, is it possible to connect the different layout textview to setText() from my activity? How to connect from activity to this fragment xml layout?
activity_main
frameLayout
fragment 1 xml
fragment 2 xml
/frameLayout
Activit.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dirShow("a");
}
void dirShow(String s) {
setContentView(R.layout.fragment1);
textPath = (TextView) findViewById(R.id.path_text);
textPath.setText(s);
}
You shouldn't be calling setContentView more than once in an activity. The content view is the entire activity layout. If you want to change the layout the proper way to do it is to change the fragment or activity, and have the new activity or fragment show the new layout.
If you're not trying to change layouts, then just put the TextView you need in the first layout that you set. Posting your layout files and telling us what you're trying to do may allow us to help you achieve what you want.
EDIT:
If I understand your updated question correctly, you're using Fragments. If that's the case, call the dirShow() function in the Fragments onCreateView() function instead of the activity's onCreate() function.
If you're just incorrectly using the term fragment in your question, I suggest you switch to using them.
You want to include another xml into your main xml usin include tag, and set text on textview as you want.
I have a custom component that actually wraps another component. Its layout is:
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/text_view_background" android:textCursorDrawable="#null"
android:textColor="#android:color/black" android:inputType="textNoSuggestions"
android:paddingLeft="7dp"/>
In the component's code I'm trying to inflate it:
inflate(context,R.layout.results_auto_complete,this);
resultsAutoComplete=(AutoCompleteTextView)getChildAt(0);
But I'm getting a ClassCastException and it says the first child is a RelativeLayout! I traced all the children of this relative layout and it's actually the layout of the widget whose configuration activity contains my custom component! When I tested the component with a simple test activity everything worked!
So why is it happening and what can I do about this?
Thanks.
If your AutoCompleteTextView is a standalone xml file (your code is the root xml tag) of results_auto_complete.xml. It is the result of the inflation, NO need to use getChildAt(i).
if <AutoCompleteTextView> is a child element in an XML file, use should assign it an Id: android:id="#+id/your_view_id". Then after the inflation, use:
this.findViewById(R.your_view_id);
where this is current activity which loads the component view.
Update, try this:
LayoutInflater mInflater = (LayoutInflater)etContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resultsAutoComplete=(AutoCompleteTextView)mInflater.inflate(R.layout.your_view_id, this, true);
It seems the problem is that I have 2 layouts with the same identifiers from different projects (one project is linked to the other), so when I'm trying to inflate a layout from one of the projects, I'm getting the layout with the same identifier in the other project. Anyway, thanks for trying to help.
I want to merge between layout file and xml file.
But, my problem, is how can I do it dynamically.
I mean that I have general toolbar file that contains: my app icon, the activity name and linearlayout space for button. I want to merge this toolbar to each activity, so that all activity could put its own buttons in the linearlayout in the toolbar.
I have try to do that, I wrote toolbar file that called toolbar.xml in the layout folder.
And each activity included this toobar like that: <include layout="#layout/toolbar.xml" />, but I do not know how to insert the buttons.
Can I do that with the xml of the activity only?
To add buttons from code (which I guess is what you want to do) you just need to have a container where your buttons shall be inserted into. You can just add <LinearLayout> to your layout XML file (ensure your layout got id, i.e.:
<LinearLayout>
...
android:id="#+id/button_container"`
</LinearLayout>
Them, in your code create your button as any other object:
Button myButton = new Button( mContext );
then find your button container:
LinearLayout buttonContainer = findViewById(R.id.button_container);
and add your button to it:
buttonContainer.addView( myButton );
To find out more see ViewGroup documentation