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.
Related
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.
I am wondering if I can create a class that has no layout (xml) that you don't have to set it on a setcontentview. For clarification, I would like to to have a background picture for my class without creating a layout or xml on it. I just want to have a class. I want to have a background named triviabackground.png (I want this PNG file to be my background picture).
Can you show me how to code it, or provide me with a reference to a tutorial?
public class Trivia extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
i mean like canvas?
You can have an activity without any view but you can't see anything that is not exist ;) background is a view itself and every View needs layout
Yes, you can create Activity without a Layout. Layouts can be used by your objects, but are absolutely NOT mandatory. But if you want any backgrounds then you cannot have them alone as background is part of layout. You do not need XML layout file - you can create it directly from code if you need.
EDIT*
FrameLayout layout = new FrameLayout();
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setBackgroundResource( R.drawable.background );
setContentView(layout);
I am preparing to do an android demonstration of sorts and one of the first apps that i would like to write would be a screen filled with different widgets(which of course are views) but would like to put them on the screen without any layout built to hold them. is this possible or do you have to use a layout to put more than one view(widget) on the screen at once?
So right now i can do something like:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView view1 = new TextView(this);
view1.setText("I am view one");
setContentView(view1);
}
}
In this case i really havent specified a layout but there doesnt seem to be a way to position multiple widgets on the screen without setting a layout. The purpose of this would be to show why you would want to use layouts. perhaps there is a way to display widgets on the screen without having to call the setContentView method.
You can only add multiple widgets/views to something called a ViewGroup. If you take a look at the documentation you'll see - not surprisingly - that basically all layouts extend this class. Similarly, if you look up the documentation on e.g. a TextView, you'll find that it doesn't extend ViewGroup (it does inherit from View, just like ViewGroup, which means it's on a different branch in the hierarchy tree).
In other words: you will need some sort of a layout in order to display more than a single widget/view at a time. You will also always need an explicit call to setContentView(), unless you use something like a ListActivity or ListFragment that by default creates a layout with a ListView as root.
That being said, your example is actually just a programmatical way of setting the following layout on the activity:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am view one" />
You can do it like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
TextView view1 = new TextView(this);
view1.setText("I am view one");
frameLayout.addView(view1);
// add more widgets into ViewGroup as you want
// then set the viewgroup as content view
setContentView(frameLayout);
}
want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.
I have a navigation bar in my app, the thing is that I want the navbar to be available in all activities. I suppose that I have to set contentView two times, but that doesn't work, of course.
I've been looking at and but I dont get it to work. I have a super class, can I set this second layout from my super class?
You should include the nav bar via <include> tag from the other layouts. Setting the content layout twice will not work, as Android is in the callbacks basically always using what the user has told last. So
setContentLayout(R.layout.nav);
setContentLayout(R.layout.main);
will result in only the main layout being used.
Have a look at this article which gives an example of using the include tag.
You can extend standard activities (Activity, ListActivity, etc.. if you use any others) and use them as a base for including a nav_bar.
For example:
Define a layout with nabar like this
<LinearLayout
...
android:orientation="vertical"
>
<YourNavBarComponent
...
/>
<FrameLayout
android:id="#+id/nav_content"
...
>
// Leave this empty for activity content
</FrameLayout>
</LinearLayout>
This will be your base layout to contain all other layouts in the nav_content frame.
Next, in create a base activity class, and do the following:
public abstract class NavActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout navContent;
#Override
public void setContentView(final int layoutResID) {
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.nav_layout, null); // Your base layout here
navContent= (FrameLayout) fullLayout.findViewById(R.id.nav_content);
getLayoutInflater().inflate(layoutResID, navContent, true); // Setting the content of layout your provided in the nav_content frame
setContentView(fullLayout);
// here you can get your navigation buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
}
}
And now, when you create a new activity, where you need a nav bar, just extend NavActivity instead. And your nav bar will be placed where you need it, without repeating it in every layout over and over again, and polluting the layouts (not to mention repeating a code to control navigation in every activity class).
Try merging layouts, as described on Android Developers Blog.