The burger drawer functioning well, however the toolbar logo didnt work when i click it. I checked everyting however i didnt find whats wrong. i tried working it on clean acvitivity and the logo and drawer really works. the drawer only works only when i slide it to the right. please really appreciate it thank you for helping.
heres my xml file
<?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:background="#drawable/bordergreen4"
tools:context=".duashome"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
app:headerLayout="#layout/header"
app:menu="#menu/menu_drawer"
android:layout_gravity="start"
android:layout_height="match_parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bordergreen4"
tools:context=".home">
<LinearLayout
android:id="#+id/duasupperlayout"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:logo="#drawable/ic_baseline_dehaze_24"
android:id="#+id/toolbar">
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:elevation="16dp"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="#+id/duasupperlayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E856E"
android:fontFamily="#font/hallington"
android:gravity="center"
android:text="List of Duas"
android:textColor="#color/white"
android:textSize="30sp">
</TextView>
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="#+id/duaslowerlayout"
android:layout_width="match_parent"
android:layout_height="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toEndOf="parent">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/duaslowerlayout"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/duasupperlayout">
<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="Dua Tawassul">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="Dua Kumayl">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="Dua Azumal Bala">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
<Button
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="sdsds">
</Button>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
and java
package com.example.munajat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.navigation.NavigationView;
public class duashome extends AppCompatActivity {
//variablesmenudrawer
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duashome);
//definingmenudrawer
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
//toolbar
setSupportActionBar(toolbar);
//navdrawermenu
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
}
You need to add the Toolbar to the ActionBarDrawerToggle.
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Related
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This Spinner is not located in the MainActivity but in A fragment
Spinner s = (Spinner) findViewById(R.id.spinner33);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String msupplier=s.getSelectedItem().toString();
Log.e("Selected item : ", msupplier);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Main MainActivity
<?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"
tools:context=".MainActivity"
android:id="#+id/drawer">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="104dp"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
//first Fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="773dp"
android:src="#mipmap/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_700"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/watBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_baseline_looks_one_24"
android:editable="true"
android:enabled="true"
android:focusable="auto"
android:gravity="center"
android:includeFontPadding="true"
android:linksClickable="true"
android:onClick="WattMthd"
android:text="#string/WAT"
app:backgroundTint="#android:color/holo_blue_dark"
tools:layout_editor_absoluteX="35dp"
tools:layout_editor_absoluteY="129dp" />
<Button
android:id="#+id/ampBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_two_24"
android:editable="true"
android:gravity="center"
android:onClick="ampMthd"
android:text="#string/AMP"
android:textColorLink="#FFFFFF"
app:backgroundTint="#android:color/darker_gray" />
<Button
android:id="#+id/voltBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_3_24"
android:editable="true"
android:gravity="center"
android:onClick="voltMthd"
android:text="فولت"
app:backgroundTint="#android:color/holo_blue_dark" />
<Button
android:id="#+id/kWbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_4_24"
android:editable="true"
android:gravity="center"
android:onClick="kWMthd"
android:text="كيلو"
app:backgroundTint="#android:color/holo_blue_dark" />
</LinearLayout>
<Spinner
android:id="#+id/spinner33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:dropDownWidth="match_parent"
android:entries="#array/SpinerConvertFrom"
android:foreground="#drawable/ic_baseline_arrow_drop_down_circle_24"
android:foregroundGravity="left|center"
android:gravity="center"
android:textAlignment="center"
android:visibility="visible"
app:flow_verticalAlign="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner33">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/AMP"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Volt"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If you are calling the spinner initialization from the fragment but it exists in the main activity it will crash, you need to call it in the activity, or move it to the xml of the framgnet.
activity_main belongs to MainActivity. So there is a fragment_first which belongs to FirstFragment. The spinner you are trying to initialize belongs to fragment_first, so you have to use it inside FirstFragment or move it (spinner) to activity_main
Hi bro inside you activity_main.xml you didn't define spinner, so you can't get view which you didn't create in your xml file, you can get it from your fragment, or you must move Spinner to activity_main.xml.
RecycleView works fine in the first time on my "home" Activity, but when i go to another activity (through the navigation menu) and comeback to "home" Activity the recycle view don't show anything ?
the items in that my recycleview shows are a linearlayouts (user_row.xml), when i use log.e i can see that the data is there but the recyleview fails to list the items (in the second time)
The home activity :
package com.example.forum;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.navigation.NavigationView;
import java.util.HashMap;
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//Variables
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//HOOKS OF NAVIGATION MENU
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
//DATA AND RECYCLERVIEW
recyclerView = findViewById(R.id.recycler_view);
adapter = new ForumAdapter(getApplicationContext());
layoutManager = new LinearLayoutManager(this);
//TOOL BAR
setSupportActionBar(toolbar);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//NAVIGATION MENU
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout , toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_home:
Intent intent33 = new Intent(Home.this,Home.class);
startActivity(intent33);
break;
case R.id.nav_user:
Intent intent = new Intent(Home.this,Myprofile.class);
startActivity(intent);
break;
case R.id.nav_logout:
SessionManager sessionManager = new SessionManager(Home.this);
sessionManager.logout();
Intent intent2 = new Intent(Home.this,Login.class);
startActivity(intent2);
break;
}
return true;
}
}
The home 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:background="#drawable/gradient"
android:id="#+id/drawer_layout"
tools:context=".Home"
tools:ignore="ExtraText"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
app:headerLayout="#layout/header"
app:menu="#menu/main_menu"
android:layout_gravity="start"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="675dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.006"
app:navigationIcon="#drawable/ic_action_menu"
app:title="Home"
app:titleMarginBottom="10dp"
app:titleTextAppearance="#style/TextAppearance.AppCompat.Large"
app:titleTextColor="#00BCD4" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:layout_marginBottom="24dp"
android:fontFamily="#font/aldrich"
android:text="#string/ensak_business_forum"
android:textColor="#E4E4E4"
android:textColorLink="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutxxx"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationY="130dp"
android:id="#+id/recycler_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#00FFFFFF"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recycler_view">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
app:cardBackgroundColor="#00FFFFFF"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
app:cardBackgroundColor="#00FFFFFF"
app:cardCornerRadius="20dp"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
The view row (user_row.xml) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayoutxxx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounding_relative_layouts"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/firstcardxxx"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/imagelayoutxxx"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView4xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/secondcardxxx"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/infoslayoutxxx"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aldrich"
android:textColor="#000000"
android:textSize="24sp" />
<Button
android:id="#+id/button4xxx"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/textView4xxx"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/rounded_green_button"
android:fontFamily="#font/aldrich"
android:text="VIEW MORE"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF" />
<Button
android:id="#+id/button5xxx"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/button4xxx"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/rounded_red_button"
android:fontFamily="#font/aldrich"
android:text="SUBSCRIBE"
android:textColor="#FFFFFF" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
The home page :
The home page
the menu :
The menu :
when i go to another activity :
when i go to another activity :
when i come back through menu :
when i come back through menu :
This is happening because all of your logic is inside the onCreate() method of the Home activity. onCreate() is only invoked once when an activity is created.
Activities are kept on the activity stack, with the most recent activities being at the top of the stack.
In your case, the Home activity is placed into a stopped state while the other activity is active.
When you navigate back to the Home Activity, onCreate() is not called because it is already created. In this case onRestart() is called followed by onStart().
My suggestion is to move the code for the RecyclerView in onCreate() to onStart().
You should read about the Activity Lifecycle here.
And for future reference, there is also a Fragment Lifecycle which is slightly different. You can read about the Fragment Lifecycle here.
I found the response to my question, so i'll just share the answer in case any one faces the same problem, the problem i had is that the activity launches with no data in it, i thought the problem was from the recycler view or from the layout manager, but actually the problem was so simple i just have to wait for the data to download first!, if the activity life starts and the data hasn't been loaded yet it will show nothing, so i added a simple ----- WAIT() ----- function to dellay the data loading first then the data can be bined to recycler view and so on ...
I want to impose a Cardview element on the toolbar. And at the stage of previewing everything is displayed correctly. However, after compiling, Cardview is under the toolbar. What could be the problem? Thanks you!
Screenshot.
It should be.:
Screenshot.
How come.:
API 22. Here and There.
My project structure: file "activity_documents" (there are left-navigation) -> file "app_bar_document" (there are toolbar) -> file "content_documents"
My code:
"activity_documents":
<?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">
<android.support.v7.widget.ContentFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_documents_type"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.ContentFrameLayout>
<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_documents_type"
app:menu="#menu/activity_documents_type_drawer" />
</android.support.v4.widget.DrawerLayout>
"app_bar_documents":
<?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:fitsSystemWindows="true"
tools:context=".DocumentsTypeActivity">
<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="wrap_content"
android:background="#drawable/back_toolbar_documents"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextColor="#android:color/white">
<TextView
android:id="#+id/textview_documents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:text="Документы"
android:textColor="#android:color/background_light" />
</android.support.v7.widget.Toolbar>
<!--android:background="?attr/colorPrimary"-->
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_documents_type"/>
<!--<include layout="#layout/content_doc"/>-->
<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" />
</FrameLayout>
"content_documents"
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".DocumentsTypeActivity"
tools:showIn="#layout/app_bar_documents_type">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="#+id/cardView_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="65dp"
android:layout_marginEnd="20dp"
app:cardBackgroundColor="#android:color/background_light"
app:cardCornerRadius="6dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_documents_multipass" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MultiPass.One"
android:textColor="#android:color/background_dark"
android:textSize="16sp"
app:fontFamily="sans-serif-black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ваш международный \n документ"
android:textAlignment="center"
android:textColor="#android:color/background_dark" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
DocumentActivity:
package com.multipassone;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class DocumentsTypeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_documents_type);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
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();
}
});
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);
}
}
I have a problem with my textview. The textview is always hiding behind the Actionbar when the page is scrolled, but I want it to stay at the top of the screen.
There is image example: Screenshot
Here is my code in the activity_main.xml and MainActivity.java file.
activity_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" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:layout_height="280dip"
app:elevation="0dip"
android:background="#color/colorLightGreen"
app:expanded="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#76BC18"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false"
app:statusBarScrim="#color/colorLightGreen">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:background="#drawable/salmon"
android:id="#+id/profile_id"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dip"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/food"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textAppearance="#style/expandedappbar"
android:background="#color/colorLightGreen" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="30dip"
android:paddingTop="35dip"
android:paddingLeft="15dip"
android:background="#android:color/background_light">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="#string/cena_pro_studenta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_weight="1"
android:textAppearance="#style/collapsedappbar"/>
<TextView
android:text="37 Kč"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_weight="1"
android:paddingLeft="7dip"
android:textAppearance="#style/collapsedappbar" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/alergeny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:textAppearance="#style/collapsedappbar"
android:layout_marginTop="10dip" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_marginTop="15dip"
android:divider="#null"
android:dividerHeight="0dip"
android:layout_marginLeft="0dip" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dip"
android:background="#ECEFF0"
android:paddingBottom="15dip"
android:paddingLeft="15dip">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_height="match_parent"
app:srcCompat="#drawable/smile"
android:id="#+id/imageView"
android:scaleType="fitStart"
android:layout_width="45dip" />
<TextView
android:text="#string/Prumer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_weight="1"
android:textAppearance="#style/collapsedappbar"
android:layout_marginLeft="15dip" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:text="#string/hodnotenie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.Button"
android:layout_marginTop="5dip" />
<Button
android:text="#string/pridej_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_weight="1"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.Button"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="30dip"
android:paddingTop="30dip"
android:paddingLeft="15dip"
android:background="#android:color/background_light">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:srcCompat="#drawable/alert"
android:id="#+id/imageView2"
android:layout_height="25dip"
android:layout_width="45dip"
android:scaleType="fitStart"/>
<TextView
android:text="#string/OpravaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:layout_weight="1"
android:textAppearance="#style/collapsedappbar"
android:layout_marginLeft="15dip" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/nahlasit_chybu"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.Button"
android:layout_marginLeft="60dip"
android:layout_marginTop="5dip" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
private ListView listView;
String[] listItems = {"Obiloviny obsahujíci lepek" , "Podzemnice ojelná (arašidy)"};
Integer[] imgid={
R.drawable.pic1,
R.drawable.pic2,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(null);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
CustomAdapter adapter=new CustomAdapter(this, listItems, imgid);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
setListViewHeightBasedOnChildren(listView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
/* Metoda pre zobrazenie celého ListView */
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
I'm writing a calculator app for Android using the shunting yard infix2postfix method (well at least is some sort of it, since I'm relatively new to java). I have a ViewPager set for swiping between base keyboard and function keyboard. Both keyboards are fragments that contain simple buttons. The stack is a custom Object Stack class I implemented. The first time swiping the keyboards slide perfectly, but after the first fiew computations, the ViewPager starts lagging a bit, and I really don't understand why. Does anybody have any suggestions?
Here is the code for ViewPager:
public class MainActivity extends AppCompatActivity {
private static final int NUM_PAGES = 2;
private Fragment[] keypads = new Fragment[NUM_PAGES];
private ViewPager pager;
private PagerAdapter pageradapter;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
pager = (ViewPager) findViewById(R.id.keypadview);
pageradapter = new KeypadSliderAdapter(getSupportFragmentManager());
pager.setAdapter(pageradapter);
keypads[0] = new NumericKeypad();
keypads[1] = new FunctionKeypad();
}
private class KeypadSliderAdapter extends FragmentStatePagerAdapter
{
public KeypadSliderAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
return keypads[position];
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
the base keyboard:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="2"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="3"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="+"
android:onClick="keyPressed"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="4"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="5"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="6"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="-"
android:onClick="keyPressed"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="7"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="8"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="9"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="*"
android:onClick="keyPressed"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="."
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="0"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:text="="
android:onClick="equalsKeyPressed"/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:onClick="keyPressed"/>
</LinearLayout>
package com.soloinfor.calculator;
import android.os.Bundle;
import android.view.View;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class NumericKeypad extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.numeric_keypad, container, false);
return view;
}
}
the function keyboard:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="sin()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="sin("/>
<Button android:text="cos()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="cos("/>
<Button android:text="tan()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="tan("/>
<Button android:text="π"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="π"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="ln()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="ln("/>
<Button android:text="log\u2081\u2080()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="log\u2081\u2080("/>
</LinearLayout>
package com.soloinfor.calculator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
public class FunctionKeypad extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.function_keypad, container, false);
}
}
Your view hierarchy is huge. You should somehow flatten it, by avoiding nested LinearLayouts and employing RelativeLayout instead. Although you don't have heavy operations, the lag you are experiencing is due to inflation, layout and drawing the views.
Use Hierarchy Viewer and run LINT check. LINT will tell you all the possible ways to flatten the layouts.