Reputation: 11
I am just staring off with Android app development and have a question on inflating layout in onCreate() of MainActivity. May be I am not asking the right question. But basically I have 2 Tabs. I have a calculate button on the first tab that once clicked should populate a table on the 2nd tab
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTableFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Details"));
tabLayout.addTab(tabLayout.newTab().setText("Discount"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
this.init();
}
public void onButtonClick(View view) {
EditText text = (EditText) findViewById(R.id.amount);
_amount = Integer.parseInt(text.getText().toString());
text = (EditText) findViewById(R.id.rate);
_rate = Integer.parseInt(text.getText().toString()) * 12;
EditText discount = (EditText) findViewById(R.id.discount);
discount.setText(Double.toString(_discount));
this.init();
}
public void init(){
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main);
if(tableLayout == null)
System.out.println("Table Layout is NULL");
else
System.out.println("Layout is not null");
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("Date");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Amount ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Discount ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
for (int i = 0; i < _loanTerm; ++i) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
tableLayout.addView(tbrow);
}
}
}
public class MyTableFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_table, container, false);
}
}
public class PagerAdapter extends FragmentStatePagerAdapter {
private int _numOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this._numOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return _numOfTabs;
}
Here is activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
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=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
Here is tab_fragment_1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AMOUNT"
android:id="@+id/textView"
android:layout_alignBottom="@+id/Amount"
android:layout_toStartOf="@+id/calculateButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/Amount"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RATE"
android:id="@+id/textView2"
android:layout_marginTop="32dp"
android:layout_below="@+id/Amount"
android:layout_alignEnd="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/rate"
android:layout_alignBottom="@+id/textView2"
android:layout_alignStart="@+id/Amount" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DISCOUNT"
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/calculateButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/discount"
android:editable="false"
android:inputType="none"
android:layout_alignBottom="@+id/textView4"
android:layout_alignStart="@+id/rate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/CALCULATE"
android:id="@+id/calculateButton"
android:clickable="true"
android:enabled="true"
android:onClick="onButtonClick"
android:layout_above="@+id/payment"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp" />
</RelativeLayout>
Here is tab_fragment_2.xml
<RelativeLayout 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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
Here is my my_table.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="@+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
In my init() when I try to access
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main);
it returns null
What am I missing?
Thanks
Upvotes: 1
Views: 2685
Reputation: 176
If you want to inflate a fragment you should first have a framelayout inside activity main, then replace the framelayout for the fragment choosed.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//change content_frame for the id fiven to the frame layout inside activity_main
getFragmentManager().beginTransaction().add(R.id.content_frame, new MyTableFragment()).commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Loan Details"));
tabLayout.addTab(tabLayout.newTab().setText("Amortization"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
this.init();
}
}
public class MyTableFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.my_table, container, false); } }
public class MyTableFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.my_table, container, false);
//in here you find the table using the view
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tab_layout);
return view;
}
}
Upvotes: 1
Reputation: 1647
You have to consume your fragment in your activity first
Do as follows:
First define container in your main activity layout for hosting your fragment and assign it with id, suppose this id is "fragment_container", So after the line:
setContentView(R.layout.activity_main);
Add the following line:
getFragmentManager().beginTransaction().add(R.id.fragment_container, new MyTableFragment()).commit();
Upvotes: 0