Case_history_review.java
This is one of my java to use the nav bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("E-care");
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Toast.makeText(Case_history_review.this, menuItem.getItemId() + " pressed", Toast.LENGTH_LONG).show();
Log.d(R.id.nav_1+"", menuItem.getItemId() + " ");
Intent intent = new Intent();
switch (menuItem.getItemId())
{
case R.id.nav_1:
break;
case R.id.nav_2:
intent.setClass(Case_history_review.this,queueshow.class);
//intent .putExtra("name", "Hello B Activity");
startActivity(intent);
break;
case R.id.nav_3:
intent.setClass(Case_history_review.this,Appointmentcreate.class);
//intent .putExtra("name", "Hello B Activity");
startActivity(intent);
break;
case R.id.nav_4:
intent.setClass(Case_history_review.this, AlarmActivity.class);
startActivity(intent);
break;
case R.id.nav_5:
intent.setClass(Case_history_review.this, PatientReport.class);
startActivity(intent);
break;
case R.id.nav_6:
intent.setClass(Case_history_review.this, TimeList.class);
startActivity(intent);
//logout
break;
}
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.drawer_open , R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
super .onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super .onDrawerOpened(drawerView);
}
};
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> dbuser = db.getUserDetails();
TextView name = (TextView) view.findViewById(R.id.drawer_name);
String username = dbuser.get("name");
Log.d("naem",username);
name.setText(username);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
CirculaireNetworkImageView photo = (CirculaireNetworkImageView) view.findViewById(R.id.drawer_thumbnail);
photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
These two lines get errors
name.setText(username);
photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);
Error message:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.yuen.e_carei.Case_history_review.onCreate(Case_history_review.java:116)
layout/drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#0097a7"
>
<com.example.yuen.CirculaireNetworkImageView
android:id="#+id/drawer_thumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="8dp"
android:elevation="8dp"
android:layout_above="#+id/drawer_name"
android:layout_alignStart="#+id/drawer_name" />
<TextView
android:id="#+id/drawer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_alignParentBottom="true"
android:text="Eddard Stark"
android:textSize="14sp"
android:textColor="#fff"
android:textStyle="bold"
android:paddingBottom="8dp"
/>
</RelativeLayout>
menu/drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_1"
android:checked="true"
android:icon="#drawable/hi"
android:title="Case Review"/>
<item
android:id="#+id/nav_2"
android:icon="#drawable/hi"
android:title="Queue state"/>
<item
android:id="#+id/nav_3"
android:icon="#drawable/hi"
android:title="Create appointemnt"/>
<item
android:id="#+id/nav_4"
android:icon="#drawable/hi"
android:title="Alarm"/>
<item
android:id="#+id/nav_5"
android:icon="#drawable/hi"
android:title="Report"/>
<item
android:id="#+id/nav_6"
android:icon="#drawable/hi"
android:title="Logout"/>
</group>
</menu>
activity_case_history_solo.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.example.yuen.e_carei.Case_history_review">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- your content layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<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>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.yuen.e_carei.Case_history_review">
<ImageView
android:src="#drawable/hi"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView2"
android:layout_alignBottom="#+id/imageView2"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textSize="25sp"
android:id="#+id/textView2"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID : "
android:textSize="25sp"
android:id="#+id/idTag"
android:paddingLeft="11dp"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chan Tai Man"
android:textSize="25sp"
android:id="#+id/nameResult"
android:paddingBottom="10dp"
android:layout_above="#+id/idResult"
android:layout_toEndOf="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="N001"
android:textSize="25sp"
android:id="#+id/idResult"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/idTag" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView_case_history"
android:layout_alignEnd="#+id/relativeLayout"
android:layout_below="#+id/imageView2" />
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I want to change the text in the drawer_header to become other string. However, it can get the id but crash in the runtime. Please give me some helps to solve this. Thank You.
Error depicts that the TextView object is null, means there is no such textview exists in the layout that is currently running.
Did the textview present in the XML layout that you are calling? Which layout did you put the TextView?
Kindly check whether your activity is correct!!
The id that you use inside findViewById(R.id....) may be wrong. Double check that.
You have called the
TextView name = (TextView) view.findViewById(R.id.drawer_name);
in the Case_history_review.java but it seems that the above textView doesn't exists in your layout that you are calling. Probably you will be calling the below activity_case_history_solo.XML in the Activity and it doesn't has it. And the above TextView lies in the drawer_header.xml
Hope this helps you..!!
Related
I removed the action bar and use the toolbar instead of that but it is not showing in my app.
I also used navigation drawer in my toolbar but as my toolbar is not showing so I can't use the navigation drawer.
when I scroll down it shows me a little bit toolbar layout from above.
So I think, I didn't set the layout properly.
Here is my Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:fitsSystemWindows="true"
tools:context="edmt.dev.androidgridlayout.MainActivity"
tools:openDrawer="start">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorAccent"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#drawable/bg"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="#+id/textGrid"
android:text="States of India"
android:textSize="34sp"
android:textColor="#android:color/white"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="callLoginActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:id="#+id/login"/>
</RelativeLayout>
<GridLayout
android:id="#+id/mainGrid"
android:columnCount="2"
android:rowCount="3"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="14dp"
>
<!-- Row 1 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:id="#+id/jnk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/jk"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Jammu and Kashmir"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:id="#+id/hp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="136dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/hpz" />
<TextView
android:text="Himachal Pradesh"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private CardView jnk,hp,up,mh,tn,rj,pb,kl,kr,hr,gj,ap,bhr,wb,goa,mp,tlg,odi,asm,cht,jkh,ngl,uk,skm,mnp,trp,arnp,mghl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FCM ,Channel created....Supported after oreo update
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel= new NotificationChannel("MyNotifications","MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager=getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
//FCM ,Specify Topic Eg.(Weather)
FirebaseMessaging.getInstance().subscribeToTopic("Weather")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
String msg = "Successfull";
if (!task.isSuccessful()) {
msg = "Failed";
}
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer=findViewById(R.id.drawer_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this, drawer,toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
jnk=(CardView)findViewById(R.id.jnk);
hp=(CardView)findViewById(R.id.hp);
up=(CardView)findViewById(R.id.up);
mh=(CardView)findViewById(R.id.mh);
tn=(CardView)findViewById(R.id.tn);
rj=(CardView)findViewById(R.id.rj);
pb=(CardView)findViewById(R.id.pb);
kl=(CardView)findViewById(R.id.kerala);
kr=(CardView)findViewById(R.id.krtk);
hr=(CardView)findViewById(R.id.hr);
gj=(CardView)findViewById(R.id.gj);
ap=(CardView)findViewById(R.id.ap);
bhr=(CardView)findViewById(R.id.bhr);
wb=(CardView)findViewById(R.id.wb);
goa=(CardView)findViewById(R.id.goa);
mp=(CardView)findViewById(R.id.mp);
tlg=(CardView)findViewById(R.id.tel);
odi=(CardView)findViewById(R.id.odi);
asm=(CardView)findViewById(R.id.assam);
cht=(CardView)findViewById(R.id.chhtt);
jkh=(CardView)findViewById(R.id.jhrk);
ngl=(CardView)findViewById(R.id.naga);
uk=(CardView)findViewById(R.id.uk);
skm=(CardView)findViewById(R.id.skm);
mnp=(CardView)findViewById(R.id.mani);
trp=(CardView)findViewById(R.id.tripura);
arnp=(CardView)findViewById(R.id.arunp);
mghl=(CardView)findViewById(R.id.megha);
jnk.setOnClickListener(this);
hp.setOnClickListener(this);
up.setOnClickListener(this);
mh.setOnClickListener(this);
tn.setOnClickListener(this);
rj.setOnClickListener(this);
pb.setOnClickListener(this);
kl.setOnClickListener(this);
kr.setOnClickListener(this);
hr.setOnClickListener(this);
gj.setOnClickListener(this);
ap.setOnClickListener(this);
bhr.setOnClickListener(this);
wb.setOnClickListener(this);
goa.setOnClickListener(this);
mp.setOnClickListener(this);
tlg.setOnClickListener(this);
odi.setOnClickListener(this);
asm.setOnClickListener(this);
cht.setOnClickListener(this);
jkh.setOnClickListener(this);
ngl.setOnClickListener(this);
uk.setOnClickListener(this);
skm.setOnClickListener(this);
mnp.setOnClickListener(this);
trp.setOnClickListener(this);
arnp.setOnClickListener(this);
mghl.setOnClickListener(this);
}
#Override
public void onBackPressed(){
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
#Override
public void onClick(View view) {
Intent i;
switch (view.getId()){
case R.id.jnk : i=new Intent(this,jammu.class);startActivity(i);break;
case R.id.hp : i=new Intent(this,himachal.class);startActivity(i);break;
case R.id.up : i=new Intent(this,uttar.class);startActivity(i);break;
case R.id.mh : i=new Intent(this,maharashtra.class);startActivity(i);break;
case R.id.tn : i=new Intent(this,tamil.class);startActivity(i);break;
case R.id.rj : i=new Intent(this,rajasthan.class);startActivity(i);break;
case R.id.pb : i=new Intent(this,punjab.class);startActivity(i);break;
case R.id.kerala : i=new Intent(this,kerala.class);startActivity(i);break;
case R.id.krtk : i=new Intent(this,karnataka.class);startActivity(i);break;
case R.id.hr : i=new Intent(this,haryana.class);startActivity(i);break;
case R.id.gj : i=new Intent(this,gujarat.class);startActivity(i);break;
case R.id.ap : i=new Intent(this,andhra.class);startActivity(i);break;
case R.id.bhr : i=new Intent(this,bihar.class);startActivity(i);break;
case R.id.wb : i=new Intent(this,west.class);startActivity(i);break;
case R.id.goa : i=new Intent(this,goaz.class);startActivity(i);break;
case R.id.mp : i=new Intent(this,madhya.class);startActivity(i);break;
case R.id.tel : i=new Intent(this,telangana.class);startActivity(i);break;
case R.id.odi : i=new Intent(this,odisha.class);startActivity(i);break;
case R.id.assam : i=new Intent(this,assam.class);startActivity(i);break;
case R.id.chhtt : i=new Intent(this,chhattisgarh.class);startActivity(i);break;
case R.id.jhrk : i=new Intent(this,jharkhand.class);startActivity(i);break;
case R.id.naga : i=new Intent(this,nagaland.class);startActivity(i);break;
case R.id.uk : i=new Intent(this,uttarakhand.class);startActivity(i);break;
case R.id.skm : i=new Intent(this,sikkim.class);startActivity(i);break;
case R.id.mani : i=new Intent(this,manipur.class);startActivity(i);break;
case R.id.tripura : i=new Intent(this,tripura.class);startActivity(i);break;
case R.id.arunp : i=new Intent(this,arunachal.class);startActivity(i);break;
case R.id.megha : i=new Intent(this,meghalaya.class);startActivity(i);break;
default:break;
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.login:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new login_fragment()).commit();
break;
case R.id.share:
Toast.makeText(this,"Share",Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void callLoginActivity(View view){
Intent i = new Intent(getApplicationContext(), loginactivity.class);
startActivity(i);
}
}
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="MyTheme" parent="AppTheme">
<item name="android:windowBackground">#android:color/white</item>
</style>
</resources>
AndroidManifest.xml
include this code line in manifest:
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
Your ScrollView hides the Toolbar. Try removing ScrollView or move ScrollView to LinearLayout below Toolbar. Check below:
<androidx.drawerlayout.widget.DrawerLayout>
<LinearLayout>
<androidx.appcompat.widget.Toolbar/>
<ScrollView/>
<FrameLayout/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>
Try this: in Style.xml, change Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.NoActionBar.
So when I launch my app it just crashes because the button in my first fragment class cannot be found for some odd reason.
When I removed the
case R.id.button_accept : the program ran fine, so I was thinking it's most likely something I did with the XML in the register_user_fragment. So is there a specific way I need to call the button because there are multiple layouts in one class?
Here's the error.
Caused by: java.lang.IllegalStateException: Required view 'button_accept' with ID 2131296297 for field 'button_accept' was not found. If this view is optional add '#Nullable' (fields) or '#Optional' (methods) annotation.
Here's my baseactivity
public class BaseActivity extends AppCompatActivity implements View.OnClickListener {
//Fragments
Fragment usernameFragment;
Fragment passwordFragment;
//
FragmentManager fragmentManager;
#BindView(R.id.button_login)
Button button_login;
#BindView(R.id.button_accept) //CRASHES HERE.
Button button_accept;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//check if user is logged in or not
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
usernameFragment = new UsernameFragment();
passwordFragment = new PasswordFragment();
fragmentManager = getFragmentManager();
button_login.setOnClickListener(this);
button_accept.setOnClickListener(this);
//setcontentview register page or whatever use is logged into
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_login :
Toast.makeText(this, "testds", Toast.LENGTH_SHORT).show();
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.content_login, usernameFragment)
.commit();
break;
case R.id.button_accept :
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.register_user_fragment, passwordFragment)
.commit();
break;
}
}
XML containing the "button_accept"
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</LinearLayout>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Because the "button_accept" is in your fragment layout , not in your activity layout :).
Your binding a UI component that doesn't exist on your activity layout.
You just made your layout mixed. just remove the Button accept_button from frame layout .
The problem is here you have button in a layout parent in which you are placing fragment dynamically. so remove the button to out side or to upper parent and out side the fragment container.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
</LinearLayout>
</FrameLayout>
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</android.support.design.widget.CoordinatorLayout>
Android newbie here.
I'm trying to set an onClick event on a LinearLayout. But I keep getting the error
Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Which, if I understand correctly means that the reference is null. Which doesn't make sense to me since I'm calling it after onCreate
Here's my code, anybody have any clues what I'm doing wrong?
MainActivity - The place with HERE! is where the crash is occuring
package ...
import ...
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);
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();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavHeaderOnClickAction();
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// ...
}
public void setNavHeaderOnClickAction(){
// HERE! Here's the problemsome area
LinearLayout navHeaderUser = (LinearLayout) findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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: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>
app_bar_main.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"
tools:context="com.example.trevorwood.biggles.MainActivity">
<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 android:id="#+id/main_content" layout="#layout/content_blank"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/search_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="top|end"
android:layout_margin="10dp"
android:background="#android:drawable/ic_menu_search">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
nav_header_main.xml (layout) included in activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#drawable/side_nav_bar"
android:gravity="center"
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">
<LinearLayout
android:id="#+id/nav_header_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:id="#+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon"
android:clickable="false"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="false">
<TextView
android:id="#+id/textView"
android:layout_width="303dp"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="JohnSmith007"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:clickable="false"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1,500 points"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/grp1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:title="Home" />
<item
android:id="#+id/nav_library"
android:icon="#drawable/ic_menu_book"
android:title="Library" />
<item
android:id="#+id/nav_create"
android:icon="#drawable/ic_menu_add"
android:title="Create" />
<item
android:id="#+id/nav_user"
android:icon="#drawable/ic_menu_user"
android:title="My Account" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_cog_wheel"
android:title="Settings" />
</group>
<group
android:id="#+id/grp2"
android:checkableBehavior="single">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="About" />
<item
android:id="#+id/nav_sign_out"
android:icon="#drawable/ic_menu_sign_out"
android:title="Sign Out" />
</group>
</menu>
content_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="content_blank"/>
</LinearLayout>
NavigationView header is usually included this way:
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/main_drawer_header"
app:itemTextColor="#color/black"
app:menu="#menu/menu_main_drawer"
app:itemIconTint="#null"/>
Since support libraries version 23.1.0 NavigationView is using a RecyclerView and the header is added as one of RecyclerView's items.
Activity's findViewById won't find the header and its internals.
To get access to it you need to get the header from NavigationView and call findViewById relative to header view:
NavigationView navigationView = (NavigationView) findViewById(R.id.nvView);
View navHeaderview = navigationView.getHeaderView(0);
LinearLayout navHeaderUser = (LinearLayout) navHeaderview.findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
Hope I got it right and it'll work for you!
Hi everyone i am working on hamburger menu in android application, where my problem is the hamburger menu is placed at wrong place(few steps down). I need the hamburger menu below my tool bar of window. please help me in this regard.
My java code:
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
// Creating Hamburger Menu
final TextView userName_ham = (TextView) this.findViewById(R.id.userName_hamburger);
ParseQuery<DriverDetails> query = new ParseQuery<DriverDetails>("driverDetails");
Log.d(TAG_NAME, "Driver Phone No" + driverPhoneNo);
query.whereEqualTo("PhoneNo", driverPhoneNo);
query.findInBackground(new FindCallback<DriverDetails>() {
#Override
public void done(List<DriverDetails> objects, ParseException e) {
if (e == null) {
if (objects.size() != 0) {
driverName = objects.get(0).getString("driverNameFirst");
Log.d(TAG_NAME, "DriverName inside" + driverName);
editor.putString("driverName", driverName);
editor.apply();
userName_ham.setText("Welcome " + driverName);
}
}
}
});
Log.d(TAG_NAME, "DriverName" + driverName);
mNavItems.add(new NavItem("Earnings", R.drawable.earnings));
mNavItems.add(new NavItem("History", R.drawable.history));
mNavItems.add(new NavItem("About", R.drawable.about));
mNavItems.add(new NavItem("Share", R.drawable.share));
mNavItems.add(new NavItem("Notification", R.drawable.notification));
mNavItems.add(new NavItem("Settings", R.drawable.settings));
mNavItems.add(new NavItem("Logout", R.drawable.logout));
// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// Populate the Navigation Drawer with options
mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
mDrawerList = (ListView) findViewById(R.id.navList);
DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(drawerAdapter);
mDrawerList.setFitsSystemWindows(true);
// Drawer Item click listeners
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItemFromDrawer(position);
}
});
My xml file code:
<!-- The navigation drawer -->
<RelativeLayout
android:layout_width="280dp"
android:layout_height="match_parent"
android:id="#+id/drawerPane"
android:fitsSystemWindows="true"
android:layout_gravity="start">
<!-- Profile Box -->
<RelativeLayout
android:id="#+id/profileBox"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary"
android:padding="8dp" >
<ImageView
android:id="#+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/user"
android:clickable="false"
android:layout_marginTop="15dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/avatar"
android:orientation="vertical" >
<TextView
android:id="#+id/userName_hamburger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/view_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="4dp"
android:text="View Profile"
android:clickable="true"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/navList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#+id/profileBox"
android:choiceMode="singleChoice"
android:background="#ffffffff" />
</RelativeLayout>
My activity xml file:
<?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=".ui.DriverScreenActivity">
<android.support.design.widget.AppBarLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:theme="#style/AppTheme.AppBarOverlay"/>
<include layout="#layout/content_customer_screen" />
<android.support.design.widget.FloatingActionButton android:id="#+id/logout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="160sp"
android:scaleType="fitXY"
android:src="#drawable/btn_logout" />
</android.support.design.widget.CoordinatorLayout>
I have an activity that correctly displays my ActionBar and it's items, however when I try to add a fragment to that activity the ActionBar disappears when that fragment is launched, or if I try to include the ActionBar it shows the bar but no title or MenuOptions.
Want I am trying to achieve is one activity, with the same ActionBar for all fragments, unless specified otherwise.
MainActivity.Java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HomeFragment homeFragment = new HomeFragment();
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.main_container, homeFragment)
.addToBackStack(null)
.commit();
}
}
#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_activity_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;
} else if (id == R.id.search_mag_icon){
// Open intent here
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
HomeFragment.java
<?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">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="110dip"
android:src="#drawable/book" />
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bookIcon"
android:layout_marginLeft="20dip"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="120dip"
android:src="#drawable/barcode" />
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/barcodeIcon"
android:layout_marginLeft="260dip"
android:paddingTop="5dip"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="350dip"
android:src="#drawable/email" />
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageIcon"
android:layout_marginLeft="55dip"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="350dip"
android:src="#drawable/list" />
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/listIcon"
android:layout_marginLeft="273dip"
android:paddingTop="5dip"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<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"
android:background="#color/colorPrimaryDark"
app:itemTextColor="#FFF"
app:itemIconTint="#FFF"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:src="#drawable/book"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "110dip"
/>
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="20dip"
android:layout_below="#+id/bookIcon" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:src="#drawable/barcode"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "120dip"
/>
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/barcodeIcon"
android:paddingTop="5dip"
android:layout_marginLeft="260dip"/>
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:src="#drawable/email"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="55dip"
android:layout_below="#+id/messageIcon" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:src="#drawable/list"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/listIcon"
android:paddingTop="5dip"
android:layout_marginLeft="273dip"/>
</RelativeLayout>
Thank you for any help in advance.
modify layout
Put you ToolBar inside LinearLayout and Change Fragment with FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
call Fragment from Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment));
fragmentTransaction.commit();