I'm starting with android studio
reading some things and watching some videos on youtube, basically what I want to do is a calculation program to make my life easier on my service, some simple accounts I've already been able to do (I only got the calculations ), but with some awful layouts.
I discovered this tabHost slide
with fragments
, and I really liked the result, simple and practical.
I then created from a tutorial this app below that creates the tabs, and by clicking on each one (or using the "slide") it shows me the tab ID, but what I would need would be instead of showing just one text, load another activity
and keep the tab for easy access to the others.
I put IF
to compare if the ID
of the tab is "such", loads "such" activity
, until it worked, but in that activity
was without the tabs, so I copied the entire tabs from the first activity
to that second, and clicking the second tab, I see that it loads the new activity
(text only, no colors and layout) but then crashes the app.
I honestly did not understand what I did wrong, I even tried to put a FOR
and a WHILE
to ensure that it would load only once (I thought I was stuck loading the second activity
several times) but nothing has changed ...
Would anyone have any light please?
activity_main.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:scrollbars="none"
android:scrollbarSize="0dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
MainActivity.java
package com.example.tbarata.myapplication;
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost;
public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1111", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2222", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3333", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab44").setIndicator("Tab 44", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab5").setIndicator("Tab 55", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab6").setIndicator("Tab 66", null),
FragmentTab.class, null);
}
}
activity_fragment_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#eaecee">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium" />
FragmentTab.java
package com.example.tbarata.myapplication;
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
public class FragmentTab extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment_layout, container, false);
if (this.getTag() == "tab2") {
Intent intent = new Intent(getActivity(), fragment_layout2.class);
startActivity(intent);
}
} else {
TextView tv = (TextView) v.findViewById(R.id.text);
tv.setText(this.getTag() + " Content");
}
return v;
}
}
and fragment_layout2.java
public class fragment_layout2 extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_layout2);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1111", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2222", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3333", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab44").setIndicator("Tab 44", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab5").setIndicator("Tab 55", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab6").setIndicator("Tab 66", null),
FragmentTab.class, null);
}