I have this code:
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
{
//Se aggiungo un fragment A al container X e aggiungo il fragment B allo stesso container, il fragment B andrĂ sopra
//il fragment A
#Override
public void onClick(View v)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(ko==0)
{
MyFragment fragment = MyFragment.createFragment(0);
fragmentTransaction.add(R.id.formazione3,fragment);
ko++;
}
else if(ko==1)
{
MyFragment fragment = MyFragment.createFragment(1);
fragmentTransaction.add(R.id.formazione2,fragment);
ko++;
}
else if(ko==2)
{
MyFragment fragment = MyFragment.createFragment(2);
fragmentTransaction.add(R.id.moduli2,fragment);
ko++;
}
else if(ko==3)
{
MyFragment fragment = MyFragment.createFragment(1);
fragmentTransaction.add(R.id.moduli5,fragment);
}
fragmentTransaction.commit();
}
});
Well, I'm experiencing the order of addition of fragments. I have 4 fragments and I don't understand why some fragments, when I add them, go under the bigger fragment, while other ones go on the bigger fragment in their container.
formazione3>formazione2 (formazione2 is contained in formazione3)
moduli2>moduli5 (moduli5 is contained in moduli2, moduli2 is contained in part in formazione3)
When I add the second fragment, it doesn't show, I think that it goes under the previous fragment, so it goes under the bigger. When I add the third fragment, it goes in part under the first fragment, the first is bigger. When I add the fourth fragment, it goes on the third, but the third is bigger than the fourth and above all the the fourth is contained in the third. How does it work? I don't understand at all!
This is the layout:
<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="com.example.utente.fantacalcio.FormazioniActivity"
android:weightSum="1"
android:id="#+id/activity_formazioni_layout">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="#+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="107dp"
android:layout_height="match_parent"
android:id="#+id/moduli">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="92dp"
android:layout_height="match_parent"
android:id="#+id/moduli1">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione1">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="71dp"
android:layout_height="match_parent"
android:id="#+id/moduli2">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione2">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="51dp"
android:layout_height="match_parent"
android:id="#+id/moduli3">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione3">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="29dp"
android:layout_height="match_parent"
android:id="#+id/moduli4">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione4">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="11dp"
android:layout_height="match_parent"
android:id="#+id/moduli5">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione5">
</RelativeLayout>
</LinearLayout>
Your parent layout is a RelativeLayout , the default behavior for this layout is that first child view is under the last child view.
In your xml this child :
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="51dp"
android:layout_height="match_parent"
android:id="#+id/moduli3">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione3">
</RelativeLayout>
</LinearLayout>
is under this child view :
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="71dp"
android:layout_height="match_parent"
android:id="#+id/moduli2">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formazione2">
</RelativeLayout>
</LinearLayout>
so when the views are inflating , this is the opposite formazione3 is over formazione2.
Moreover your LinearLayout have these attributes :
android:layout_width="match_parent"
android:layout_height="match_parent"
so they take all the place available.
Try to set fixe size in dp instead of match_parent to see what's really happen.
Hope this helps.
Sorry for my poor english.
Related
Good day
I am new to android development and I am trying to add 2 containers for fragments in the main xml file, that will each use half of the screen in landscape, but everything I try does not work, please help.
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.newProject.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="221dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/fragments_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
thank you!
I hope this will work for you
For landscape mode
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="2"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="#+id/fragments_container1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
In xml add
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:orientation="vertical"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:weightSum="2"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<FrameLayout
android:id="#+id/frame2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
in activity class:
public void loadFragments(){
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
loadFragment(this,fragment1);
loadFragmentTwo(this,fragment2);
}
public static void loadFragment(AppCompatActivity activity, Fragment newFragment) {
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame1, newFragment, getTagForFragment(newFragment)).commitAllowingStateLoss();
}
public static void loadFragmentTwo(AppCompatActivity activity, Fragment newFragment) {
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame2, newFragment, getTagForFragment(newFragment)).commitAllowingStateLoss();
}
public static String getTagForFragment(Fragment fragment) {
Log.d("fragment name", "fragment name " + fragment.getClass().getSimpleName());
return fragment.getClass().getSimpleName();
}
Try this xml code:
<android.support.constraint.ConstraintLayout
android:id="#+id/main_content"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<FrameLayout
android:id="#+id/frameLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/frameLayout2"/>
</android.support.constraint.ConstraintLayout>
So when I launch my app it just crashes because the button in my first fragment class cannot be found for some odd reason.
When I removed the
case R.id.button_accept : the program ran fine, so I was thinking it's most likely something I did with the XML in the register_user_fragment. So is there a specific way I need to call the button because there are multiple layouts in one class?
Here's the error.
Caused by: java.lang.IllegalStateException: Required view 'button_accept' with ID 2131296297 for field 'button_accept' was not found. If this view is optional add '#Nullable' (fields) or '#Optional' (methods) annotation.
Here's my baseactivity
public class BaseActivity extends AppCompatActivity implements View.OnClickListener {
//Fragments
Fragment usernameFragment;
Fragment passwordFragment;
//
FragmentManager fragmentManager;
#BindView(R.id.button_login)
Button button_login;
#BindView(R.id.button_accept) //CRASHES HERE.
Button button_accept;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//check if user is logged in or not
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
usernameFragment = new UsernameFragment();
passwordFragment = new PasswordFragment();
fragmentManager = getFragmentManager();
button_login.setOnClickListener(this);
button_accept.setOnClickListener(this);
//setcontentview register page or whatever use is logged into
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_login :
Toast.makeText(this, "testds", Toast.LENGTH_SHORT).show();
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.content_login, usernameFragment)
.commit();
break;
case R.id.button_accept :
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.register_user_fragment, passwordFragment)
.commit();
break;
}
}
XML containing the "button_accept"
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</LinearLayout>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Because the "button_accept" is in your fragment layout , not in your activity layout :).
Your binding a UI component that doesn't exist on your activity layout.
You just made your layout mixed. just remove the Button accept_button from frame layout .
The problem is here you have button in a layout parent in which you are placing fragment dynamically. so remove the button to out side or to upper parent and out side the fragment container.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
</LinearLayout>
</FrameLayout>
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</android.support.design.widget.CoordinatorLayout>
I have a hidden layout.
add_event_price.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/add_event_price_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText android:id="#+id/add_event_wording"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="#string/event_wording_hint" />
<EditText android:id="#+id/add_event_price"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/event_price_hint" />
<ImageButton android:id="#+id/add_event_cross"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/cross"
android:background="#null"
android:layout_gravity="center_vertical"
android:contentDescription="#string/delete" />
</LinearLayout>
When clicking on a button in the main layout, the hidden layout appears.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/app_background_color">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/add_event_dynamic"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<Button android:id="#+id/add_event_add_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:text="#string/add_event_add_field" />
</LinearLayout>
</ScrollView>
add_event_add_field.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout priceLayout = (LinearLayout) rootView.findViewById(R.id.add_event_price_layout);
LinearLayout dynamicLayout = (LinearLayout) rootView.findViewById(R.id.add_event_dynamic);
View hidden = inflater.inflate(R.layout.add_event_price, priceLayout, false);
dynamicLayout.addView(hidden);
}
});
Adding the hidden layout dynamically works but I don't know how to retrieve the values of the EditText in the hidden layout, and delete a view when clicking on the corresponding add_event_cross ImageButton.
First, you can call findViewById on dynamicLayout:
EditText et = (EditText) dynamicLayout.findViewById(R.id.add_event_wording);
Second, I suggest not to show/hide layout by adding view. Just put the dynamicLayout inside your main layout and use something like:
dynamicLayout.setVisibility(LinearLayout.GONE); //makes the layout hidden
I have not tested the code but you can build upon it.
I have two layouts, this is the parent layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="90"
android:background="#FF0000"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="10"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time: 00:00:00"
android:textColor="#FF0000"
android:textSize="25dip" />
</LinearLayout>
</LinearLayout>
And this is the child layout:
<?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:background="#00FF00"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Hello World" />
</LinearLayout>
</LinearLayout>
However, when I put the child view in id/layoutTop, it doesn't fill the parent like it's supposed to. This is the result:
This is the code where the id/layoutTop's content is replaced: (This gets called via an event from the child be removed)
public void OnVictory(GamePanel sender, GamePanel next) {
if(next != null){
gamePanel.removeView(sender);
gamePanel.addView(next);
next.triggerOnDisplay();
}else{
Toast.makeText(getBaseContext(), "Victory!", Toast.LENGTH_LONG).show();
}
}
sender is the old view there, next is the one to be put in place, and gamePanel is id/layoutTop
And this is how the inner layout is created:
private void initialize() {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.appetizerfakeout,this);
}
Why is my inner view not filling the parent?
I am getting the following error when I try to launch an activity containing a tabhost.
08-25 16:51:42.551:
ERROR/AndroidRuntime(27863):
java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.paratransit/com.paratransit.jobDialog}:
java.lang.RuntimeException: Could not
create tab content because could not
find view with id 2131165185
This is my code. Can anyone help?
Java
public Class jobDialog extends TabActivity {
TabHost tabs;
int jobCurrentTab = -1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
setContentView(R.layout.job_dialog);
tabs = getTabHost();
tabs.setup();
setupTabs();
}
public void setupTabs()
{
TabSpec tspec1 = tabs.newTabSpec("First Tab");
tspec1.setIndicator("Summary", getResources().getDrawable(R.drawable.tab_main)).setContent(R.id.jobDetail1);
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec("Second Tab");
tspec2.setIndicator("Details", getResources().getDrawable(R.drawable.tab_message)).setContent(R.id.jobDetail2);
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec("Third Tab");
tspec3.setIndicator("Notes", getResources().getDrawable(R.drawable.tab_jobs)).setContent(R.id.jobDetail3);
tabs.addTab(tspec3);
XML
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
<LinearLayout
android:id="#+id/jobDetail1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/jobDetail2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/jobDetail3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
</TabHost>
I think the 3 linear layouts, jobDetail1->3 should be within the FrameLayout xml element. The point of the FrameLayout is that all of the views are on top of each other, and thus the tab manager can decide which view to show.
I think you're getting this error because you are not including the LinearLayout inside the FrameLayout, besides I think you need to put some element view inside the LinearLayout's.
Something like this
<?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"
android:id="#+id/establecimientoView">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent" >
<LinearLayout
android:id="#+id/tabIdEst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/identificacion_establecimiento"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tabRefGeo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/referencia_geografica"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tabUbicEstablecimiento"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/ubicacion_establecimiento"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tabVialidades"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/entre_vialidades"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>