Trying to adapt my application to other devices (tablets) is giving me an error, so my layout (which I have called activity_wine_list) is adapted for different situations, I have created it according to the situation in one way or another, in this way:
This for mobile mode.
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list">
</FrameLayout>
</LinearLayout>
This is for tablet vertical mode:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/winery">
</FrameLayout>
</LinearLayout>
And the latter also for tablets but horizontal mode:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/list"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/winery"/>
</LinearLayout>
</LinearLayout>
They use the id "list" and the id "winery", well, the problem is that when I run the application it gives me an error indicating that it does not find the id "list" it does not tell me anything about "winery" I understand why My first condition is to verify the list.
Here is the code for the activity:
public class WineListActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This here, will choose one of the three options I have in R.layout.activity_wine_list.
setContentView(R.layout.activity_wine_list);
//Visible Toolbar.
setSupportActionBar((Toolbar)findViewById(R.id.toolbar));
FragmentManager fm = getSupportFragmentManager();
//I ask if they have already the listFragment
if (findViewById(R.id.list) != null) {
Fragment listFragment = fm.findFragmentById(R.id.list);
//If no exist, I created it.
if (listFragment == null) {
listFragment = new WineListFragment();
//Once is created, now add the fragmentManager to it
fm.beginTransaction().add(R.id.list, listFragment).commit();
}
}
//Them, I goona do something like that but, with the winery.
if (findViewById(R.id.winery) != null) {
Fragment wineryFragment = fm.findFragmentById(R.id.winery);
if (wineryFragment == null){
//Set newInstance because my WineryFragment have it has as an argument
wineryFragment = WineryFragment.newInstance(0);
//And them, just the same.
fm.beginTransaction().add(R.id.list, wineryFragment).commit();
}
}
}
}
Aqui el codigo Fragment:
public WineListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_wine_list, container, false);
//Reference to ListView
ListView listView = (ListView) root.findViewById(android.R.id.list);
//Acceding Winery
Winery winery = Winery.getInstance();
//List adapter
ArrayAdapter<Wine> adapter = new ArrayAdapter<Wine>(getActivity(), android.R.layout.simple_list_item_1, winery.getWineList());
//Saying the adapter to the listView
listView.setAdapter(adapter);
//What to do when the user press one item on the list.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent wineryIntent = new Intent(getActivity(), WineryActivity.class);
wineryIntent.putExtra(WineryActivity.EXTRA_WINE_INDEX, i);
startActivity(wineryIntent);
}
});
return root;
}
}
And finally the code from where I get the constants:
public class WineryFragment extends Fragment implements ViewPager.OnPageChangeListener {
public static final String ARG_WINE_INDEX ="jhon.casique.baccus.controller.fragment.WineryFragment.ARG_WINE_INDEX";
private ViewPager mPager = null;
private ActionBar mActionBar = null;
private Winery mWinery = null;
//Creating new method to access him from WineryActivity. (newInstance)
public static WineryFragment newInstance(int wineIndex){
Bundle arguments = new Bundle();
arguments.putInt(ARG_WINE_INDEX, wineIndex);
WineryFragment fragment = new WineryFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.fragment_winery, container, false);
mPager = (ViewPager) root.findViewById(R.id.pager);
mPager.setAdapter(new WineryPagerAdapter(getFragmentManager()));
mWinery = Winery.getInstance();
//Getting reference about the ActionBar
mActionBar = (ActionBar) ((AppCompatActivity)getActivity()).getSupportActionBar();
//Listener.
mPager.addOnPageChangeListener(this);
//This value is for rotate the pageView and the ActionBar
int initialWineIndex = getArguments().getInt(ARG_WINE_INDEX);
//I ask to update my ActionBar and pageView to charge the wine it is pass as a argument.
mPager.setCurrentItem(initialWineIndex);
updateActionBar(initialWineIndex);
return root;
}
//Creating method to update my data page wine (I gonna need a Winery)
public void updateActionBar (int index) {
mActionBar.setTitle(mWinery.getWine(index).getName());
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//Changing by position in the PageView name by name.
updateActionBar(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_winery, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean superValue = super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.menu_next && mPager.getCurrentItem() < mWinery.getWineCount() -1) {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
}
else if (item.getItemId() == R.id.menu_prev && mPager.getCurrentItem() > 0) {
mPager.setCurrentItem(mPager.getCurrentItem() -1);
return true;
}
else {
return superValue;
}
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuNext = menu.findItem(R.id.menu_next);
MenuItem menuPrev = menu.findItem(R.id.menu_prev);
//Setting when they gonna be enabled.
menuNext.setEnabled(mPager.getCurrentItem() < mWinery.getWineCount() - 1);
menuPrev.setEnabled(mPager.getCurrentItem() > 0);
}
}
If someone can help me, I would really appreciate it.
Thanks.
Updated 28/10
I appreciate your help but, even so, this keeps giving me an error, specifically this one: FATAL EXCEPTION: main Process: jhon.casique.baccus, PID: 2238 java.lang.RuntimeException: Unable to start activity ComponentInfo {jhon.casique.baccus / jhon.casique.baccus.controller.activity.WineListActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0b0057 (jhon.casique.baccus: id / list) for fragment WineryFragment {a50a4b98 # 0 id = 0x7f0b0057}
For some reason the id "list" does not recognize it and it does not matter if I make it a refactor or whatever it does, it does not want to work. I do not understand what the hell is going on.