Inflating ViewPager from XML layout - android

I am following the Android Programming: The Big Nerd Ranch Guide and there is an example using ViewPager through the 2 files:
The file res/values/ids.xml defines an id in XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="viewPager" />
</resources>
And the Java source file CrimePagerActivity.java uses that id:
public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
I have tried to change the example to have a ViewPager object being inflated from an XML layout file res/layout/crime_pager.xml (instead of just using an id):
<?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" >
<ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager" />
</LinearLayout>
And then I am trying to use that object in the CrimePagerActivity.java:
public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crime_pager);
mViewPager = (ViewPager)findViewById(R.id.view_pager);
Unfortunately, this does not work and fails with a RuntimeException and little info (here fullscreen):
What is the problem here please?

change:
<ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager" />
to
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

Related

List view not getting populated by array adapter

I am new to android and have been following udacity's video lectures.
The list view is not getting populated by the array adapter and when
i ran the app on mobile the app showed only the main activity layout.
the list view is not appearing on the mobile screen ,can anyone tell me whats the error here,is it something related to frame layout or is there an
error in
array adapter.
Thanks in advance
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceHolderFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public PlaceHolderFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray={
"Today","Tommorow","wed","Thursday","Friday","Saturday","Sunday"
};//creating some fake data to show in list view
List<String> fore=new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter=new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView)rootview.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
return rootview;
}
}
}
activity_main_xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/container"
tools:context="com.saxenarc.jeetesh.layouts.MainActivity">
</FrameLayout>
list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/list_item_forecast_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
>
</TextView>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast"></ListView>
</FrameLayout>
Checked everything thrice before posting it.
Your code and list is working well but you haven't told the list where to be displayed. You can fix this in 2 ways.
First
Add the fragment inside the main activity since its not visible.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager.beginTransaction().replace(R.id.container, new PlaceHolderFragment).commit();
}
Second
Stop using the fragment and use the listview inside the Main activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast"></ListView>
</FrameLayout>
MainActivity.java
List<String> fore = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(this,R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView) findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);

Fragment in main activity returns null fragmentmanager.findviewbyid<>

I have created the MainNavigationDrawerFragment.
However, when I try to findviewbyId , both the drawerFragment and drawerlayout returns null, when I try to set up drawer.
Since this activity is the main launcher, through the fragmentmanager.findviewbyID(), I would expect it to kick off the fragment onCreateView event so that I can inflate the fragment. Howerver, it's returning null.
Any help?
public class ProfileView1 : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.ProfileView2);
// other code....
// Navigation Drawer
SetUpDrawer();
}
private void SetUpDrawer()
{
**MainNavigationDrawerFragment drawerFragment =
FragmentManager.FindFragmentById<MainNavigationDrawerFragment>
(Resource.Id.nav_drwr_fragment);
DrawerLayout drawerLayout = FindViewById<DrawerLayout>**(Resource.Id.drawer_layout);**
drawerFragment.SetUpDrawer(Resource.Id.nav_drwr_fragment, drawerLayout, toolBar);
}
/////////////////////////This is the
layout///////////////////////////////
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/ToolBarOnlyLayout"
android:id="#+id/toolbarProfile" />
<include
layout="#layout/ProfileMainPageLayout"
android:id="#+id/profileMainPageCardView" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recentSearchRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<fragment
android:id="#+id/nav_drwr_fragment"
android:name="UserProfile.Fragments.MainNavigationDrawerFragment"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/navigationdrawer_main_fragment"
tools:layout="#layout/navigationdrawer_main_fragment"/>
</android.support.v4.widget.DrawerLayout>
After some digging, I solved it. There are 2 issues here. I had to change the layout to not use android:name but class instead.
<fragment
android:id="#+id/nav_drwr_fragment"
*class="UserProfile.Fragments.MainNavigationDrawerFragment"*
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/navigationdrawer_main_fragment"
tools:layout="#layout/navigationdrawer_main_fragment"/>
The second part is that i had to use supportfragmentmanager since my fragment uses support.app.fragment
drawerFragment = (MainNavigationDrawerFragment)this.SupportFragmentManager.FindFragmentById(Resource.Id.nav_drwr_fragment);

Toolbar in Settings page built with PreferenceFragment

I built a Settings activity/screen for my app using this guide. Example code is bellow.
What am I missing to be able to add a Toolbar to this new activity with a back arrow in it's left side?
AndroidManifest.xml
....
<activity
android:name=".SettingsActivity"
android:label="Settings"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar"/>
....
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_sync"
android:title="#string/pref_sync"
android:summary="#string/pref_sync_summ"
android:defaultValue="true" />
<ListPreference
android:dependency="pref_sync"
android:key="pref_syncConnectionType"
android:title="#string/pref_syncConnectionType"
android:dialogTitle="#string/pref_syncConnectionType"
android:entries="#array/pref_syncConnectionTypes_entries"
android:entryValues="#array/pref_syncConnectionTypes_values"
android:defaultValue="#string/pref_syncConnectionTypes_default" />
</PreferenceScreen>
SettingsFragment.java
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
SettingsActivity.java
public class SettingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
You must include a toolbar in your activity layout. For example:
activity_main.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=".activities.SettingsActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/app_bar"/>
</RelativeLayout>
app_bar.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
In your Activity.onCreate() get the toolbar and set a home button (the little arrow):
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Set the SettingsActivity to be a child of parent MainActivity:
<activity
android:name=".activities.SettingsActivity"
android:label="#string/title_activity_main"
android:parentActivityName=".activities.MainActivity" />
And that will give you that little arrow.
Also, do this:
public class SettingsActivity extends AppCompatActivity
Not this:
public class SettingsActivity extends Activity
In order to use support library.

Hide action bar Android Studio in tabhost

I'm creating an Android application and there's a page like this, a weather page is held inside a tabhost:
The MainActivity class:
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//I used this but it's not working
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(
new Intent(this, FirstActivity.class)
));
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(
new Intent(this, SecondActivity.class)
));
mTabHost.setCurrentTab(0);
}
And the SecondActivity contains the weather:
public class SecondActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_weather);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}
}
The problem is that I want to hide the actionbar <> as it looks really awful in this. As you can see that I tried to request FEATURE_NO_TITLE or set FLAG_FULLSCREEN, as well as set android:theme="#style/Theme.AppCompat.NoActionBar" in AndroidManifest, but nothing works well.
(In addition, is there anyway to set the weather interface fullscreen? As it has a small gap between edges)
Thanks for your help.
Change ActionBarActivity to Activity in your SecondActivity.
public class SecondActivity extends ActionBarActivity {
to
public class SecondActivity extends Activity {
And if this not give you the expected answer then add below in AndroidManifest:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
</activity>
Or in your SecondActivity add below:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_weather);
In your activity xml there is a layout called appbarlayout please delete it it will work.
in my activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.ezybzy.ezybzy.Categorypage">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_categorypage" />
</android.support.design.widget.CoordinatorLayout>
please change to this
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.ezybzy.ezybzy.Categorypage">
<include layout="#layout/content_categorypage" />
</android.support.design.widget.CoordinatorLayout>
this will work only if you create blank activity!!

Android: can't load in a detail fragment in master detail

I have an app that currently works on a handheld device and I now want it to work on a tablet using a master/detail flow. I have an InformationFragment that is shown when you click on an item in a ListView. The app works on a handheld device, but on a tablet I get an exception that there is no view found for two_pane_information_container. Here is my code:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
twoPane = getResources().getBoolean(R.bool.isTablet);
setContentView(R.layout.main_layout);
// setting up the navigation view and listfragment etc.
}
public static class InformationListFragment extends ListFragment {
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Info o = (Info) getListAdapter().getItem(position);
Bundle b = new Bundle();
b.putParcelable("info", o);
if (twoPane) {
// load the fragment to the right of the list
// this is the part causing problems
android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
InformationFragment ia = new InformationFragment();
ia.setArguments(b);
ft.replace(R.id.two_pane_information_container, ia);
ft.commit();
} else {
// An activity that displays the inforrmationfragment
// this works
Intent i = new Intent(getActivity(), InformationActivity.class);
i.putExtra("info", b);
startActivity(i);
}
}
}
res/layout/fragment_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/table_background" >
<!-- The main content view -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_dark"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dip" />
</android.support.v4.widget.DrawerLayout>
res/layout/main_twopane.xml
<?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="horizontal" >
<include
android:layout_width="400dp"
layout="#layout/fragment_main" />
<LinearLayout
android:id="#+id/two_pane_information_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
res/values/layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<item name="main_layout" type="layout">#layout/fragment_main</item>
</resources>
res/values-large/layouts.xml and res/values-sw600dp/layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">true</bool>
<item name="main" type="layout">#layout/main_twopane</item>
</resources>
Your layout name in the final 2 XML files differs.
<item name="main" type="layout">
should be
<item name="main_layout" type="layout">

Categories

Resources