Why navigation drawer is not working with my project? - android

I'm new android programmer and i have a little project to make dashboard with navigation drawer, here is my code, need help. tq
Modern Dashboard
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toolbar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class ModernAcivity extends AppCompatActivity {
CardView cardView1,cardView2,cardView3,cardView4,cardView5;
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.modern_dashboard);
/*------slider------- */
drawerLayout = findViewById(R.id.drawerlayout);
navigationView = findViewById(R.id.nav_view);
Toolbar = findViewById(R.id.menu_toolbar);
/*------toolbar------- */
setSupportActionBar(toolbar);
/*------navigation------- */
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
cardView1 =(CardView)findViewById(R.id.card1);
cardView2 =(CardView)findViewById(R.id.card2);
cardView3 =(CardView)findViewById(R.id.card3);
cardView4 =(CardView)findViewById(R.id.card4);
cardView5 =(CardView)findViewById(R.id.card5);
cardView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ModernAcivity.this,InfoActivity.class);
startActivity(i);
}
});
cardView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ModernAcivity.this,KuesActivity.class);
startActivity(i);
}
});
cardView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ModernAcivity.this,ReportActivity.class);
startActivity(i);
}
});
cardView4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ModernAcivity.this,MainActivity.class);
startActivity(i);
}
});
cardView5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ModernAcivity.this,VidioActivity.class);
startActivity(i);
}
});
}
}
I am doing a project in the android studio, and I need a navigation drawer, I made a navigation but the problem I can't slide it left and right its block without moving.
Here is the layout
<?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:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
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/drawermenu"
android:layoutDirection="ltr"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/menu_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:navigationIcon="#mipmap/menu" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:fontFamily="#font/baloo"
android:text="Sadari"
android:textSize="30sp"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/baloo"
android:text="Periksa Payudara Sendiri"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:srcCompat="#mipmap/pita" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="175dp"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/card1"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/informasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Informasi"
android:textSize="18sp"/>
<ImageView
android:id="#+id/image1"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#mipmap/info"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/card2"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/ceksadari"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Cek Sadari"
android:textSize="18sp" />
<ImageView
android:id="#+id/image2"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#mipmap/cek" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="175dp"
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/linearLayout">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/card3"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/laporanku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Laporanku"
android:textSize="18sp" />
<ImageView
android:id="#+id/image3"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:contentDescription="TODO"
app:srcCompat="#mipmap/report" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:id="#+id/card4"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/pengingat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Pengingat"
android:textSize="18sp" />
<ImageView
android:id="#+id/image4"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#mipmap/reminder" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="175dp"
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/linearLayout2">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card5"
android:layout_margin="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/videoku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image5"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#font/aclonica"
android:text="Video"
android:textSize="18sp" />
<ImageView
android:id="#+id/image5"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
app:srcCompat="#mipmap/video" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

Related

Moving from one activity to another takes a long time

Within my application I could notice that each time I went from one activity to another it took a long time (1 second or more), when it should be almost instantly. I did the test on my own cell phone and it takes about 2 seconds. I tested on a newer (more powerful) one, it takes less but still takes longer than it should.
I don't know if my activities are very loaded or if I am calling them in the wrong way.
Call to activities from my MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Toolbar toolbar;
private Anuncio anuncio;
private TextView titulo;
private CardView cardView1, cardView2, cardView3, cardView4;
private View include_1, include_2, include_3, include_4, include_5, include_6, include_7, include_8, include_9;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contenedor_principal);
anuncio = new Anuncio();
anuncio.cargarAnuncios(this, R.id.adView_alone);
toolbar = findViewById(R.id.toolbar_alone);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) getSupportActionBar().setTitle(null);
titulo = findViewById(R.id.textview_toolbar_titulo_alone);
titulo.setText(R.string.app_name);
inicializar();
}
private void inicializar() {
include_1 = findViewById(R.id.include_objetivos);
include_1.setVisibility(View.GONE);
include_2 = findViewById(R.id.include_compras);
include_2.setVisibility(View.GONE);
include_3 = findViewById(R.id.include_porCuentas);
include_3.setVisibility(View.GONE);
include_4 = findViewById(R.id.include_porQuincena);
include_4.setVisibility(View.GONE);
include_5 = findViewById(R.id.include_porInteres);
include_5.setVisibility(View.GONE);
include_6 = findViewById(R.id.include_lista_ahorros);
include_6.setVisibility(View.GONE);
include_7 = findViewById(R.id.include_ingresos);
include_7.setVisibility(View.GONE);
include_8 = findViewById(R.id.include_deudas);
include_8.setVisibility(View.GONE);
include_9 = findViewById(R.id.include_home_menu);
include_9.setVisibility(View.VISIBLE);
cardView1 = findViewById(R.id.menu_cardView_1);
cardView2 = findViewById(R.id.menu_cardView_2);
cardView3 = findViewById(R.id.menu_cardView_3);
cardView4 = findViewById(R.id.menu_cardView_4);
cardView1.setOnClickListener(this);
cardView2.setOnClickListener(this);
cardView3.setOnClickListener(this);
cardView4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.menu_cardView_1:
intent = new Intent(this, Menu1.class);
this.startActivity(intent);
break;
case R.id.menu_cardView_2:
intent = new Intent(this, Menu2.class);
this.startActivity(intent);
break;
case R.id.menu_cardView_3:
intent = new Intent(this, Menu3.class);
this.startActivity(intent);
break;
case R.id.menu_cardView_4:
intent = new Intent(this, Menu4.class);
this.startActivity(intent);
break;
}
}
#Override
public void onPause() {
anuncio.pausar();
super.onPause();
}
#Override
public void onResume() {
super.onResume();
anuncio.resumir();
}
#Override
public void onDestroy() {
anuncio.destruir();
super.onDestroy();
}
}
XML from my MainActivity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorAccent">
<TextView
android:id="#+id/menu_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#color/blanco"
android:textStyle="bold"
android:textSize="30sp"
android:shadowColor="#color/negro_semi_transparente"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="6"
android:text="MenĂº Principal"
android:padding="15dp"
android:layout_marginTop="30dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/menu_titulo"
android:rowCount="2"
android:columnCount="2"
android:padding="20dp">
<androidx.cardview.widget.CardView
android:id="#+id/menu_cardView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/regalo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regalos"
android:textStyle="bold"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/menu_cardView_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/carro"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carros"
android:textStyle="bold"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/menu_cardView_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/casa"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Casas"
android:textStyle="bold"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/menu_cardView_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/cine"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cine"
android:textStyle="bold"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
Activity Regalo XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/menu_fondo_3"
android:orientation="vertical">
<TextView
android:id="#+id/titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#color/blanco"
android:textStyle="bold"
android:textSize="30sp"
android:shadowColor="#color/negro_semi_transparente"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="6"
android:text="Regalos"
android:padding="15dp"
android:layout_marginTop="30dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/titulo"
android:rowCount="3"
android:columnCount="1"
android:padding="20dp">
<androidx.cardview.widget.CardView
android:id="#+id/regalo1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="15dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="15dp">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/regalo"
android:layout_marginEnd="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regalo 1"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/regalo2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="15dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="15dp">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/regalo"
android:layout_marginEnd="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regalo 2"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/regalo3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="20dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="15dp">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/regalo"
android:layout_marginEnd="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regalo 3"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
All these XML are inside each other through an 'include'. Here below I leave the code of the main_container.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Inicio de la Appbar -->
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<!-- Inicio de la Toolbar-->
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_alone"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview_toolbar_titulo_alone"
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textStyle="bold"
android:textColor="#color/blanco" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<!-- Fin de la Toolbar-->
</com.google.android.material.appbar.AppBarLayout>
<include
android:id="#+id/include_menu"
layout="#layout/menu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_1"
layout="#layout/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_2"
layout="#layout/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_3"
layout="#layout/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_4"
layout="#layout/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_5"
layout="#layout/layout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_6"
layout="#layout/layout6"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_7"
layout="#layout/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_8"
layout="#layout/layout8"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<include
android:id="#+id/include_9"
layout="#layout/layout9"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="90dp"
android:visibility="gone"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView_alone"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_anchorGravity="bottom|center" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Thanks in advance.
translated by google translate.
I removed all the 'include' and used a single ViewStub to load the layouts depending on which one the user wanted to see.
Main_container.xml:
<ViewStub
android:id="#+id/viewStub"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
MainActivity:
viewStub = findViewById(R.id.viewStub);
viewStub.setLayoutResource(R.layout.home_layout);
viewStub.inflate();

Layout not showing up when using intent to start new activity

I have a fragment class with 6 hard-coded cardviews. I want the user to be able to click on them and it open up another activity. Each opens to different activity. Currently, when the user clicks on the cardview it opens the new activity, but doesn't show the layout. For example, when the user clicks on the science cardview it just shows a blank page, but doesn't show the layout with the button.
Here is my fragment page code:
public class show_class extends Fragment implements
View.OnClickListener {
private CardView science_cv, math_cv, social_studies_cv, english_cv,
tech_cv, other_cv;
private DatabaseReference myRef ;
//List<show_class> list;
// private RecyclerView recycle;
// Button view;
public show_class() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_show_class, container, false);
//find cardview's by ids
science_cv = (CardView) view.findViewById(R.id.science);
math_cv = (CardView) view.findViewById(R.id.math);
social_studies_cv = (CardView) view.findViewById(R.id.social_studies);
english_cv = (CardView) view.findViewById(R.id.English);
tech_cv = (CardView) view.findViewById(R.id.tech);
other_cv= (CardView) view.findViewById(R.id.other);
//set onclicklistener to cardviews
science_cv.setOnClickListener(this);
social_studies_cv.setOnClickListener(this);
math_cv.setOnClickListener(this);
english_cv.setOnClickListener(this);
tech_cv.setOnClickListener(this);
other_cv.setOnClickListener(this);
return view;
}
public void onClick (View v){
Intent i;
switch (v.getId()) {
case R.id.science:
i = new Intent(getContext(), Science_classes.class);
case R.id.math: i = new Intent(getContext(), Math_classes.class);startActivity(i);break;
case R.id.social_studies: i = new Intent(getContext(), Social_studies_classes.class);startActivity(i);break;
case R.id.English: i = new Intent(getContext(), English_classes.class);startActivity(i);break;
case R.id.tech: i = new Intent(getContext(), Technology_classes.class);startActivity(i);break;
case R.id.other: i = new Intent(getContext(), Other_classes.class);startActivity(i);break;
default:break;
}
}
}
Here is my xml for the fragment page:
<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">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#a4a4a4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:selectableItemBackground"
android:id="#+id/science"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/ic_launcher_background"
android:padding="10dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Science"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show2"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/math"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Math"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:onClick="show3"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/English"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show4"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/social_studies"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Social Studies"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:onClick="show5"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/tech"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:background="#color/colorSecondary"
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/ic_launcher_background"
android:padding="10dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technology"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show6"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/other"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView></FrameLayout>
Here is the code for the science class i am trying to go to(it is the same for all):
public class Science_classes extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_classes);
}
}
Here is the xml, just a simple button:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Science_classes">
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
there is a missing startActivity(i); and break; statement in the first case case R.id.science: that is the reason that after clicking science you are ending up with maths case and starting a maths activity. Will have to check if that is the problem of the blank screen. Is you maths activity layout empty?
'in your show_class fragment in onClick() you are not starting the Science_classes activity like.'
'in first case of R.id.science'
startActivity(i);

Clicking on navigation drawer listview item opens unclicked activity every time

I'm implementing a service and receiver in SettingsActivity and when i open the app for the first time there is no issue but once I visit SettingsActivity and come back, closing the navigation drawer from then on opens SettingsActivity everytime even if don't click any item in the listview
This is my MainActivity layout file where i have implemented navigation drawer :-
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/top_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="#00AFF0"
android:orientation="vertical">
<TextView
android:id="#+id/ship_name"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#00AFF0"
android:background="#drawable/button_background"
android:padding="10dp"
android:textSize="16sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="10dp">
<ImageButton
android:id="#+id/date_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_reset"
android:background="#android:color/transparent"
android:layout_marginEnd="10dp"
android:visibility="gone"/>
<TextView
android:id="#+id/current_date"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="16sp"
/>
<ImageButton
android:id="#+id/date_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_date"
android:background="#android:color/transparent"
android:layout_marginStart="10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tickets_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/button_border"
android:gravity="center"
android:minWidth="120dp"
android:orientation="horizontal"
android:textColor="#android:color/white">
<TextView
android:id="#+id/ticket_no_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="end|center"
android:padding="2dp"
android:text=""
android:textColor="#FFFF00"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="2dp"
android:gravity="center_vertical"
android:text="#string/tickets"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="16sp" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/white" />
<TextView
android:id="#+id/no_of_people_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="end|center"
android:padding="2dp"
android:textSize="16sp"
android:textColor="#FFFF00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="2dp"
android:text="#string/people"
android:textSize="16sp"
android:textAllCaps="true"
android:gravity="center_vertical"
android:textColor="#android:color/white" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_no_data"
android:gravity="center|center_horizontal"
android:textSize="18sp"
android:textColor="#color/textColor"
android:textStyle="bold"
android:visibility="gone"
android:paddingTop="15dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/search_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/ticket_no"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:layout_marginStart="20dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/edittextbackground"
android:imeOptions="actionDone"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:maxLength="19"
android:inputType="number"
android:digits="01234 56789"
android:maxLines="1"
/>
<Button
android:id="#+id/search_ticket_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="20dp"
android:layout_weight="0.2"
android:textColor="#android:color/white"
android:text="#string/go"
android:textSize="16sp"
android:textAllCaps="true"
android:background="#drawable/go_button_shape"/>
</LinearLayout>
<LinearLayout
android:id="#+id/or_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
android:orientation="horizontal"
android:paddingRight="30dp">
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:background="#android:color/black"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:text="#string/or"
android:textSize="20sp"
android:gravity="center"/>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:background="#android:color/black"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/scan_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.5">
<Button
android:id="#+id/scanQr"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:text="#string/scan_qr"
android:layout_gravity="center|top"
android:gravity="center"
android:textColor="#android:color/white"
android:background="#drawable/scan_button"
android:padding="10dp"
android:layout_marginTop="10dp"
android:textSize="16sp"
/>
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="#+id/scan_results"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="50dp"
android:layout_marginBottom="10dp"
android:visibility="visible"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="left"
android:layout_marginStart="20dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
/>
</LinearLayout>
<TextView
android:id="#+id/no_trips_tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="18sp"
android:text="#string/no_trips"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/layout_layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:alpha="0.5"
/>
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFF"
android:choiceMode="singleChoice"
/>
</android.support.v4.widget.DrawerLayout>
And in my MainActivity i handle item click events like this where navList is Listview and drawer is DrawerLayout :-
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
if (position == 0){
} else if(position == 1)
{
getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate);
} else if(position == 2) {
submitTickets();
} else if(position == 3) {
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
} else {
Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext());
finish();
}
}
});
drawer.closeDrawer(navList);
}
});
remove drawer.setDrawerListener and place the code directly inside onItemClick
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case:0
{
break;
}
case:1
{
getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate);
break;
}
case:2
{
submitTickets();
break;
}
case:3
{
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
break;
}
case:4{
Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext());
finish();
break;
}
default:
}
}
});
drawer.closeDrawer(navList);
}

Layout of fragment disintegrates when it dynamically replaces framelayout

I have an app with MainActivity that contains a FrameLayout which would be replaced by a fragment based on which item is clicked in the bottom layout. I have a fragment named "bookings" that must appear in the body area when "Bookings" option is clicked. The "bookings" fragment contains a spinner and a button that are placed at the center of screen inside of RelativeLayout. When the fragment's layout is placed directly inside of body of MainActivity, the spinner and button is centered, just the way I want it to be.
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<!-- The FrameLayout goes here -->
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
But when I replace the FrameLayout with the fragment, the spinner disappears and the button moves at the top.
Code in activity_main.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_frame"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code in fragment_bookings.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
Code of onCreateView of bookings.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bookings, container, false);
Spinner loc = (Spinner)v.findViewById(R.id.city_spinner);
String[] cities = new String[]{
"Select a City",
"ABC",
"DEF",
"GHI",
"JKL",
"MNO"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this.getActivity(),android.R.layout.simple_spinner_item,cities
);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
loc.setAdapter(spinnerArrayAdapter);
loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
L1 = id;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
return v;
}
Code in MainActivity.java:
package com.example.user.temporary;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements
profile.OnFragmentInteractionListener,
bookings.OnFragmentInteractionListener,
games.OnFragmentInteractionListener {
private Activity myActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myActivity = this;
LinearLayout profilebtn = (LinearLayout)this.findViewById(R.id.profile_button);
LinearLayout bookingsbtn = (LinearLayout)this.findViewById(R.id.bookings_button);
LinearLayout gamesbtn = (LinearLayout)this.findViewById(R.id.games_button);
profilebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
profile profileFragment = new profile();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, profileFragment);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
bookingsbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bookings bookings_frame = new bookings();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, bookings_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
gamesbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
games games_frame = new games();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, games_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
}
#Override
public void onFragmentInteraction(Uri uri){
}
}
Change height of FrameLayout to match_parent from wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_frame"/>
</LinearLayout>
You have match_parent in the height for fragment layout. Try changing it to wrap_content and see if it works

How to get Item Clicked in Fragment's Recycler View to activity?

i have an activity which is having about 4 fragment and fragment have a recyclerview and in each recyclerview item there is a button.. i have a textfield in activity and i want to show the no. of button clicked.
thats my activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
tools:context="com.example.khaalijeb.newlistview_module.PromoCodeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:background="#f9f9f9"
android:elevation="5dp">
<ImageView
android:id="#+id/back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="#drawable/greyback" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back"
android:text="Fancy Some Deals ?"
android:textSize="16dp"
android:textStyle="bold" />
<Button
android:id="#+id/skip"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/roundedcorneredittext"
android:text="SKIP"
android:textColor="#0277bd"
android:textSize="12dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.55"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#E12728"
app:tabMode="scrollable"
app:tabSelectedTextColor="#E12728"
app:tabTextAppearance="#style/TextAppearance.Design.Tab" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.05"
android:background="#f9f9f9"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel">
<EditText
android:id="#+id/spass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:hint="Promo Code"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textColor="#color/material_blue_grey_800"
android:textSize="14dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLY"
android:textColor="#0277bd"
android:textSize="12dp" />
<Button
android:id="#+id/applied"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLIED"
android:textColor="#0277bd"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8">
<TextView
android:id="#+id/credittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="12dp"
android:text="Credited To Jeb No." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/credittext"
android:layout_toRightOf="#+id/credittext"
android:text="C100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/credittext"
android:layout_marginRight="16dp"
android:text="Rs.100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/pickdeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/credittext"
android:layout_margin="5dp"
android:text="Haven't Picked Any Deals Yet"
android:textColor="#78909C"
android:textSize="12dp" />
<TextView
android:id="#+id/dealamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/amount"
android:text="Rs.50"
android:layout_marginRight="16dp"
android:layout_alignTop="#+id/pickdeal"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/dealamount"
android:text="+"
android:layout_alignTop="#+id/dealamount"
android:layout_marginRight="2dp"
android:textSize="12dp"
/>
<TextView
android:id="#+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pickdeal"
android:text="Discount"
android:layout_alignLeft="#+id/pickdeal"
android:textColor="#F44336"
android:textSize="12dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/discountedamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Rs.50"
android:textColor="#F44336"
android:layout_alignTop="#+id/discount"
android:layout_marginRight="16dp"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/discountedamount"
android:layout_alignTop="#+id/discountedamount"
android:text="-"
android:textColor="#F44336"
android:layout_marginRight="2dp"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0277bd"
android:text="PROCEED TO PAY" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
recyelrview item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/llContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
card_view:cardBackgroundColor="#f9f9f9"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/nike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/brandname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="hello"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="14dp"
android:typeface="sans" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/brandname"
android:layout_below="#+id/brandname"
android:layout_marginTop="10dp"
android:text="Get 50% off on Apparels"
android:textSize="12dp" />
<TextView
android:id="#+id/seemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/brandname"
android:layout_below="#+id/title"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="See More"
android:textSize="12dp" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignTop="#+id/seemore"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/seemore"
android:src="#drawable/sendgrey" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:id="#+id/free"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorneredittext"
android:text="Free"
android:textColor="#0277bd"
android:textSize="12dp"
/>
<Button
android:id="#+id/taken"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorner1"
android:text="Great!"
android:textColor="#ffffff"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Adapter.java
public class PromoCodeRecyclerViewAdapter extends RecyclerView.Adapter<PromoCodeRecyclerViewAdapter.MyViewholder> {
private ClickListener clickListener;
private LayoutInflater inflator;
private Context mcontext;
Typeface font;
public Button b1;
public ImageButton b2;
List<promocodedata> data = Collections.emptyList();
public PromoCodeRecyclerViewAdapter(Context context, List<promocodedata> y, Typeface font) {
inflator = LayoutInflater.from(context);
this.data = y;
this.mcontext = context;
this.font = font;
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflator.inflate(R.layout.promocode_item_layout, parent, false);
MyViewholder holder = new MyViewholder(view);
return holder;
}
#Override
public void onBindViewHolder(final MyViewholder holder, int position) {
promocodedata current = data.get(position);
holder.brandicon.setImageResource(current.brandicon);
holder.brandname.setText(current.brandname);
holder.title.setText(current.title);
holder.free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.VISIBLE);
holder.free.setVisibility(View.INVISIBLE);
}
});
holder.taken.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.INVISIBLE);
holder.free.setVisibility(View.VISIBLE);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public interface ClickListener {
void itemClicked(View v, int position);
}
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView brandicon;
TextView brandname;
TextView title;
Button free;
Button taken;
public MyViewholder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
brandicon = (ImageView) itemView.findViewById(R.id.icon);
brandname = (TextView) itemView.findViewById(R.id.brandname);
title = (TextView) itemView.findViewById(R.id.title);
free = (Button)itemView.findViewById(R.id.free);
taken = (Button)itemView.findViewById(R.id.taken);
}
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.itemClicked(v, getPosition());
}
}
}
}
You need to create your onitemclick because RecyclerView does not have a OnItemClick like Listview, i have an example in Github check it.

Categories

Resources