Template Navigation Drawer - Android Studio

I created an main activity with the template "navigation drawer activity" in Android Studio. How can I set the nav drawer for every activity?
activity_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
content_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:app="://schemas.android.com/apk/res-auto"
xmlns:tools="://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello stranger!"
android:id="@+id/textView3" />
</LinearLayout>
nav_header_main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:weightSum="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="54dp"
android:layout_height="54dp"
android:maxWidth="10dp"
android:maxHeight="1dp"
android:src="@drawable/library"
android:layout_weight="0.24"
android:paddingLeft="0dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Home Library"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="app" />
</LinearLayout>
Main activity:
Code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {
Intent intent = new Intent(getApplicationContext(), NewBookActivity.class);
startActivity(intent);
}
});
DrawerLayout 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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@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.main, 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();
if (id == R.id.nav_book) {
Intent intent = new Intent(this, BookListActivity.class);
startActivity(intent);
}
/*else if (id == R.id.nav_gallery) {
}
*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

Related

[Q] WebView - Please help

Hello. I want to make webview app, but with buttons like in the image that I have created. Back - Refresh - Forvard.
So maybe someone can help, I'm green with it...
Hi there,
So first thing you need to do is grant INTERNET permission to your app by edditing AndroidManifest.xml
For example (I've made all my additions in bold),
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.application.my.webviewtest" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
[B]<uses-permission android:name="android.permission.INTERNET" />[/B]
</manifest>
Then you need to edit your View so that you have a WebView widget and some buttons, i'm just showing a back and forward button here, so in my activity_main.xml I have,
Code:
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
[B] <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<WebView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/myWebView"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="64dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<"
android:id="@+id/backButton"
android:onClick="OnBackButtonClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:id="@+id/forwardButton"
android:onClick="OnForwardButtonClick"/>
</LinearLayout>
</LinearLayout>[/B]
</RelativeLayout>
You'll notice I've put it in a vertical linearlayout, and then inside of that a horizontal linearlayout to host the buttons, now for the code in MainActivity.java,
Code:
package com.application.my.webviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
[B]import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;[/B]
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[B] WebView webview = (WebView) this.findViewById(R.id.myWebView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.co.uk");[/B]
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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);
}
[B] public void OnBackButtonClick(View view) {
WebView webview = (WebView) this.findViewById(R.id.myWebView);
webview.goBack();
}
public void OnForwardButtonClick(View view) {
WebView webview = (WebView) this.findViewById(R.id.myWebView);
webview.goForward();
}[/B]
}
I've zipped up the app folder of my AndroidStudio project for you
http://npsoftware.s3.amazonaws.com/android/app.zip

[Q] Button not creating new textview

I am trying to get my button to create a new textview but its not doing it. The code works but its doesnt create the textview.
public class SampleActivity extends MainActivity implements View.OnClickListener {
int counter = 0;
private LinearLayout linearLayout;
private Button button;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(this);
}
@override
public void onClick(View view) {
if(view.getId() == button.getId()){
counter++;
TextView textView = new TextView(SampleActivity.this);
textView.setText("TextView " + counter);
linearLayout.addView(textView);
}
}
}
and in my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical">
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add TextView" />
</LinearLayout>
Have you tried setting the layout by calling setLayoutParams on the textview?
1nstan7riod said:
I am trying to get my button to create a new textview but its not doing it. The code works but its doesnt create the textview.
public class SampleActivity extends MainActivity implements View.OnClickListener {
int counter = 0;
private LinearLayout linearLayout;
private Button button;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(this);
}
@override
public void onClick(View view) {
if(view.getId() == button.getId()){
counter++;
TextView textView = new TextView(SampleActivity.this);
textView.setText("TextView " + counter);
linearLayout.addView(textView);
}
}
}
and in my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical">
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add TextView" />
</LinearLayout>
Click to expand...
Click to collapse
Try this:
TextView textView = new TextView(SampleActivity.this);
textView.setText("TextView " + counter);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_WRAP_CONTENT));
linearLayout.addView(textView);

help with progress bar.

trying to make the progress bar be an actual progress bar. i dont want the spinning circle loading graphic. im looking for a bar that would load and go to the right while loading.
here is some code.
Code:
package com.example.syn.testing;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setprogressbar();
}
public void setprogressbar(){
ProgressBar pb = (ProgressBar) findViewById(R.id.pbloading);
for(int x = 1; x <= pb.getMax(); x++){
pb.setProgress(x);
}
}
public void second_page(View view)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
public void gotobattery(View view)
{
Intent intent = new Intent(MainActivity.this, BatteryActivity.class);
startActivity(intent);
}
public void gotoText(View view){
Intent intent = new Intent(MainActivity.this, Text.class);
startActivity(intent);
}
}
Code:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.syn.testing.MainActivity"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_alignTop="@+id/textView"
android:background="#393939"
android:textColor="#00ff00"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
android:onClick="second_page"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/batbtn"
android:id="@+id/button3"
android:onClick="gotobattery"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="71dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button6"
android:onClick="gotoText"
android:layout_above="@+id/button"
android:layout_alignEnd="@+id/button3"
android:layout_alignParentStart="true" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="400px"
android:layout_height="wrap_content"
android:id="@+id/pbloading"
android:layout_below="@+id/textView"
android:layout_marginTop="28dp"
android:max="100"/>
</RelativeLayout>

first completed project, looking for efficiency.

below is my project code. Its a truth or dare sex game i made for my inner friends. I am posting it here because i am looking for suggestions in improving functionality and efficiency. Meaning less lines. I also want to know if there is anything i could do better.
MainActivity.java
Code:
package com.example.syn.secretproject;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.msg);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "Aladin-Regular.ttf");
tx.setTypeface(custom_font);
}
public void tf(View view){
Intent myIntent = new Intent(this, TruthFemale.class);
startActivity(myIntent);
}
public void df(View view){
Intent myIntent = new Intent(this, DareFemale.class);
startActivity(myIntent);
}
public void tm(View view){
Intent myIntent = new Intent(this, TruthMale.class);
startActivity(myIntent);
}
public void dm(View view){
Intent myIntent = new Intent(this, DareMale.class);
startActivity(myIntent);
}
}
Activity_main.xml
Code:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@drawable/both"
tools:context="com.example.syn.secretproject.MainActivity"
android:id="@+id/rellay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome Press Start"
android:layout_marginTop="51dp"
android:id="@+id/msg"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="24dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Truth"
android:id="@+id/btnTruthM"
android:layout_below="@+id/msg"
android:layout_alignParentStart="true"
android:layout_marginTop="89dp"
android:onClick="tm"
android:background="#FF2F48EB" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dare"
android:id="@+id/btnDareM"
android:background="#FF2F48EB"
android:onClick="dm"
android:layout_alignTop="@+id/btnTruthM"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Truth"
android:id="@+id/btnTruthF"
android:layout_marginTop="89dp"
android:background="#e52feb"
android:onClick="tf"
android:layout_below="@+id/btnTruthM"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dare"
android:id="@+id/btnDareF"
android:background="#e52feb"
android:onClick="df"
android:layout_alignTop="@+id/btnTruthF"
android:layout_alignParentEnd="true" />
</RelativeLayout>
And a copy of one of the activities...all the other activities are similar.
Code:
package com.example.syn.secretproject;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class DareFemale extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dare_female);
String comments[] = {"message 1","message 2","message 3"};
Random rand = new Random();
int number = rand.nextInt(comments.length);
TextView t = (TextView) findViewById(R.id.msg);
t.setText(comments[number]);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "Aladin-Regular.ttf");
t.setTypeface(custom_font);
}
public void tm(View view){
Intent myIntent = new Intent(this, TruthMale.class);
startActivity(myIntent);
}
public void dm(View view){
Intent myIntent = new Intent(this, DareMale.class);
startActivity(myIntent);
}
}
xml code
Code:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@drawable/woman"
tools:context="com.example.syn.secretproject.MainActivity"
android:id="@+id/rellay">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Welcome Press Start"
android:layout_marginTop="51dp"
android:id="@+id/msg"
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="24dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Truth"
android:id="@+id/btnTruthM"
android:onClick="tm"
android:background="#FF2F48EB"
android:layout_below="@+id/msg"
android:layout_alignStart="@+id/msg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dare"
android:id="@+id/btnDareM"
android:background="#FF2F48EB"
android:onClick="dm"
android:layout_alignTop="@+id/btnTruthM"
android:layout_alignEnd="@+id/msg" />
</RelativeLayout>
any advice or criticism is welcomed.
nothing? i hardly think this is as efficient as possible...

ListView ArrayAdapter doesn't display list items in Navigation Drawer Activity

I have some problems with the list items of a ListView. I've implemented a Navigation Drawer Activity and want to display a simple ListView with some list items. I haven't changed the implementation of the navigation bar yet, just tried to display an activity I've already implemented. So in the automatically generated app_bar_start.xml I just replaced in the <include layout="@layout/activity_main_list" /> section the example activity by my own activity. So far so good, but I figured out that the list items, I want to add by an ArrayAdapter, doesn't appear in the app.
On the other hand, when I implement an string-array in strings.xml and include this items via android:entries in the xml file of my activity, it works fine.
Also when I display my activity directly, so without the Navigation Drawer Activity, everything works fine too.
So does anybody have an idea why it just don't work included in the Navigation Drawer Acitivity?
MainList.java (this is my own written activity):
Code:
public class MainList extends AppCompatActivity {
private ListView mListView;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
mListView = (ListView) findViewById(R.id.mainlist);
List<String> list = new ArrayList<String>();
list.add("Dies ist ein Test");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
mListView.setAdapter(adapter);
}
}
activity_main_list.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="..."
xmlns:app="..."
xmlns:tools="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_start"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<ListView
android:id="@+id/mainlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
StartActivity.java:
Code:
public class StartActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout 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 = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
}
activity_start.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="..."
xmlns:app="..."
xmlns:tools="..."
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_start"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_start"
app:menu="@menu/activity_start_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_start.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="..."
xmlns:app="..."
xmlns:tools="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="martinfactory.archibook.StartActivity">
<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/activity_main_list" />
</android.support.design.widget.CoordinatorLayout>
Problem solved
Okay I've solved the problem, or actually kind of avoided it. I had to change the structure of my files because I have to implement some fragments and after that all the list items are displayed.

Categories

Resources