Inflate a layout in a layout Android - android

I'm trying to inflate a layout in another layout but it doesn't work...
In my activity I show activity_serie.xml which is black, and then I try to inflate on image_item.xml which is white. But I never see image_item.xml.
public class Serie extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serie);
ViewPager mainLayout = (ViewPager) findViewById(R.id.serie_pager);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.image_item, mainLayout, true);
}
#Override
protected void onResume() {
super.onResume();
}}
The activity_serie.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/serie_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
The image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Can someone tell me why?
Thanks.

findViewById(R.layout.activity_serie)
You should specify a view id here, not a layout name.

Why you wanna do that? Just put both views into one xml, and set android:visibility="gone" for one element. Than you can address layout by id and change visibility as much as you want.

Related

Layout doesn't inflate in Android Studio MainActivity when it contains a Fragment Container View

I have the same problem like in this post, but in other way:
I use a button from inside a fragment to inflate a different layout in MainActivity using the interface communication. It works when the layout contains only a TextView, but when I replace it with a Fragment Container View, it crashes. I used #ianhanniballake answer changing the context to (this) and it removed the crash, but it still doesn't inflate anything..
Here is my code:
public class MainActivity extends AppCompatActivity implements FragmentB.FragmentBListener {
ViewGroup main_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_layout = findViewById(R.id.main_layout);
}
//fullScreen method comes from a button click listener from FragmentB
#Override
public void fullScreen() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.fullscreen, main_layout,false);
main_layout.removeAllViews();
main_layout.addView(view);
}
}
this is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout"
tools:context=".MainActivity">
//some basic initial code
</RelativeLayout>
and this is the layout I want to inflate - fullscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerViewB"
android:name="com.example.testglide.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
tools:layout="#layout/fragment_b" />
</RelativeLayout>
Thanks a lot for any help! :)
try this:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout"
tools:context=".MainActivity">
<include android:id="#+id/layout_fullscreen"
layout="#layout/fullscreen"/>
</RelativeLayout>
java class
View view = findViewById(R.id.layout_fullscreen);
main_layout.removeAllViews();
main_layout.addView(view);

Adding multiple cardviews

I am trying to have dynamic number of cardviews in my layout.For test i am trying to insert 3 cardviews in my layout,but instead of three only one card is added with "hello 3" written on it.
Here's my code for activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
setContentView(R.layout.card);
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView)findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}
}
`
code for cardlayout
<android.support.v7.widget.CardView
android:id="#+id/cardv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_margin="10dp"
card_view:cardCornerRadius="10dp">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
For contentmain
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical"
android:id="#+id/mainl">
</LinearLayout>
Because in loop cycle, you set content view each loop.
Please just remove this line setContentView(R.layout.card);
So your onCreate() method will look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
LinearLayout layout=(LinearLayout)findViewById(R.id.mainl);
for(i=1;i<=3;i++)
{
LayoutInflater layoutInflater=(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView=layoutInflater.inflate(R.layout.card,null);
TextView t=(TextView) addView.findViewById(R.id.text1);
t.setText("hello "+String.valueOf(i));
layout.addView(addView);
}
}

Fragment onCreateView is never called

I noticed that onCreateView() of one of my fragments is never called and i dont know why.I need to be able to make changes in onResume() but thats is also never called.
Here is how I add the fragments.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
Tab1Fragment
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab1, container, false);
Log.v("FRAGMENT","FragmentActivityCreateView");
return V;
}
And finnaly the xml passed under R.layout.tab1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<ImageButton
android:id="#+id/searchBtn"
android:src="#drawable/waves"
android:background="#drawable/search_selector"
android:padding="40dp"
android:layout_height="200dp"
android:layout_width="200dp"
android:onClick="liveMatch"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I think I might have created the fragments the wrong way. As you see it now they are created as the xml says but when I changed tabs, the content is "reseted".Any changed I placed in onCreateView() or onResume() is not called.
EDIT
I have tried to implement the method described in the 1st comment. After the implementation the tabs have the same content. Is like all the tabs have the same content. This time I posted all my classes and their associated XML.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab2Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab3Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
activity_live_game_stats.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_live_game_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
tools:context="lucian.leagueassistant.activity.LiveGameStats">
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveGameTabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/liverealtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
I wont post the code from LiveGameTab2 and LiveGameTab3 since they are almost the same. I have also another activity that
Edit2
Seems its solved when i changed from getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); to getSupportFragmentManager().beginTransaction().add(android.R.id.tabcontent, new LiveGameTab1Fragment()).commit();
The problem is this:
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
In the first argument you have to pass a id to a container in the Activity layout where your fragment will be attached.
You are passing a entire layout R.id.tab1.
Add a container view in activity_live_game_stats xml file,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<FrameLayout
android:id="#+id/myContainer"
...(Other attrs)>
</FrameLayout>
</RelativeLayout>
and then
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.myContainer, new Tab1Fragment()).commit();
}

Fragment on top of surfaceview

Hello I have an surfaceview in my app.All my games are built like this:
Instanciating a subclass of Surfaceview which implements the SurfaceHolder.Callback interface.
Now is that I want to open a menu when I click on a Button(custom made object)
This should bring up a fragment with a scrollable textview( I thought of just a listview with one textview element inside it because I think the textview alone is not scrollable if the text is very long..any contrary arguments are welcome)
but how do I do this?
I have allready a Fragment class and a layout for the fragment
Edit 3:
Ok I've managed to set the SurfaceView programmatically like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
prefs=getSharedPreferences("myPrefs",0);
mView=new MainView(this);
loadSharedPrefs();
LinearLayout myLayout = (LinearLayout)findViewById(R.id.main);
mView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
myLayout.addView(mView);
}
}
where the layout is just a blank linearlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/main">
</LinearLayout>
So for now I just need a Fragment to be displayed when I click a Button
Here the Fragment Class so far:
public class HintListFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.hintlist, container, false);
}
}
and the layout for this fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/hintList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="sagdjsahgdhascxasbcxahsgdhsagdhasgdashgdsajsgdh"
/>
</LinearLayout>
In your layout, wrap the surfaceview and a fragment in a FrameLayout. Make sure the fragment is on top of the surfaceview and then set the fragment's visibility to GONE. When you want to show the fragment, just set the visibility to VISIBLE
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.example.MyFragment
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/myfragment"/>
</FrameLayout>

Cannot find child elements of custom view after inflated

I am trying to inflate a custom view on activity load and then do something with the child elements of that view. Below I try to inflate the view and then get a reference to a TextView within the layout XML to update the text value. However in the below code, after the last line, actionBarTitle is null - when I would expect it to be a reference to a TextView. What am I doing wrong?
Activity onCreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_userseed);
LayoutInflater inflator = LayoutInflater.from(this);
actionBarView = inflator.inflate(R.layout.fragment_actionbar_viewseed, null);
TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.action_bar_title);
}
And the layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
<android.support.v4.view.ViewPager
android:id="#+id/actionbar_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/actionbar_pagerstrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.view.ViewPager>
</LinearLayout>
Try:
TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.actionbar_title);

Categories

Resources