hello guys i was trying to add tab layout
in navigation drawer activity
which i created from welcome screen in android studio. but when i try to implement the code and run the application the app is crashing and is showing the rendering issue with XML
file here is the code of .xml
file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.madhu.management.QuestionPaperActivity"
tools:showIn="@layout/app_bar_question_paper">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.design.widget.TabLayout>
</LinearLayout>`
and here is the .class file
` public class QuestionPaperActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//declaring access specifiers for tablayout
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
//declaring variables for navigation layout
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_paper);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
tabLayout = (TabLayout) findViewById(R.id.tablayout_id);
viewPager = (ViewPager) findViewById(R.id.viewpager_id);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
//adding fragments here
adapter.AddFragment(new InternalQuestionFragment(),"Internal Papers");
adapter.AddFragment(new ExternalQuestionFragment(),"External Papers");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dash_board, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id=item.getItemId();
switch (id) {
case R.id.nav_notice:
Intent h = new Intent(QuestionPaperActivity.this, NoticeActivity.class);
startActivity(h);
break;
case R.id.nav_notes:
Intent i = new Intent(QuestionPaperActivity.this, NotesActivity.class);
startActivity(i);
break;
case R.id.nav_question_papers:
Intent g = new Intent(QuestionPaperActivity.this, QuestionPaperActivity.class);
startActivity(g);
break;
case R.id.nav_update:
Intent s = new Intent(QuestionPaperActivity.this, UpdatesActivity.class);
startActivity(s);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}`
From your xml file, take android.support.v4.view.ViewPager out from the TabLayout. This is because TabLayout can only contain tabitem instances. Make it like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.madhu.management.QuestionPaperActivity"
tools:showIn="@layout/app_bar_question_paper">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With