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();
Related
So basically,whenever i will click on the beginners section column ,it should redirect me to next page.I have attached the images of my xml page of exactly how it looks and also attached the the code of home fragment.java code and also attached the mainpage.xml code.Actully I am bemused how exactly should i figure out the code .
so basically when the user will click on beginners calligraphy column it should redirect to next page i.e.the next page I have attached.
this is my HomeFragment.java code
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
This is my MainPage.xml code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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=".mainpage">
<LinearLayout
android:orientation="vertical"
android:background="#color/Pink"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView
android:id="#+id/TextView"
android:layout_width="950dp"
android:layout_height="550dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:layout_marginBottom="0dp"
android:alpha="0.5"
android:background="#color/purple_200"
android:text=" BEGINNERS CALLIGRAPHY"
android:textAllCaps="false"
android:textColor="#color/DarkBlue"
android:textSize="25sp"
android:textStyle="bold|italic" />
<Button
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="210dp"
android:layout_marginBottom="7dp"
android:text="PHOTOS"
android:textSize="20dp"
android:background="#color/white"
android:textColor="#color/purple_700"/>
<Button
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="52dp"
android:layout_marginBottom="7dp"
android:text="VIDEOS"
android:textSize="20dp"
android:background="#color/white"
android:textColor="#color/purple_700"/>
</RelativeLayout>
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="949dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<!-- Row 1 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
android:background="#color/Purple">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b1" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b2" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b3" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="190dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b4" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b5" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b6" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b7" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b8" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b10" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b11" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b12" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/b1" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
Try to change onCreateView to the following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
view.findViewById(R.id.your_beginning_text_id).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), MainPageActivity.class));
}
});
return view;
}
getting an Nullpointer exception on setting up an onclick listner to my CardView.
Error is:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.jaysurya.project_scrollwithtap.MainActivity.onCreate(MainActivity.java:27)
This is my mainactivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
CardView chap1,chap2,chap3,chap4,chap5;
int chapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chap1 = findViewById(R.id.chap1);
chap2 = findViewById(R.id.chap2);
chap3 = findViewById(R.id.chap3);
chap4 = findViewById(R.id.chap4);
chap5 = findViewById(R.id.chap5);
chap1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chapter = 1;
openchapters(chapter);
}
});
chap2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chapter = 2;
openchapters(chapter);
}
});
chap3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chapter = 3;
openchapters(chapter);
}
});
chap4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chapter = 4;
openchapters(chapter);
}
});
chap5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chapter = 5;
openchapters(chapter);
}
});
}
private void openchapters(int chapter){
Intent intent = new Intent(this, Main1Activity.class);
intent.putExtra("chapter_no",chapter);
startActivity(intent);
}
}
This is my activity_main.xml
ScrollView 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="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:background="#drawable/jhyti"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="185dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:gravity="center_vertical"
android:src="#drawable/logo" />
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp">
<!--ROW 1 - -!>
<!- -CARD 1-->
<android.support.v7.widget.CardView
android:id="#+id/chap1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="#animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/p2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CHAPTER 1"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="CHAPTER 1"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
<!--CARD 2-->
<android.support.v7.widget.CardView
android:id="#+id/chap2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="#animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/p4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CHAPTER 2"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:text="CHAPTER 2"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText"/>
</android.support.v7.widget.CardView>
<!--CARD 3-->
<android.support.v7.widget.CardView
android:id="#+id/chap3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="#animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/p5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 3"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="155dp"
android:layout_height="33dp"
android:text="CHAPTER 3"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText"/>
</android.support.v7.widget.CardView>
<!--CARD 4-->
<android.support.v7.widget.CardView
android:id="#+id/chap4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="#animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/p3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 4"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TextView
android:id="#+id/textView5"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:text="CHAPTER 4"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
<!--CARD 5-->
<android.support.v7.widget.CardView
android:id="#+id/chap5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="#animator/lift_on_touch"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="191dp"
android:layout_height="74dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/p1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHAPTER 5"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</LinearLayout>
<TextView
android:id="#+id/textView"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:text="CHAPTER 5"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold|italic"
tools:ignore="HardcodedText" />
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
For java.lang.OutOfMemoryError, you can add the following line in your manifest file:
android:largeHeap="true"
like this:
<application
......
android:largeHeap="true"
......
</application>
Refer this answer for more details about large heap and when you should use it.
In my app i have a class called tasti.class where i have a simple layout with a ScrollView and some CardView's in it.
It also has a button and when i press it it's open a custom AlertDialog so my question is how could i add other CardViews after i insert in my custom AlertDialog the name to place in the TextView of the CardView and the "Prezzo" that it should be a description of the textView i mean by pressing the button "AGGIUNGI" how could i create a new CardView and add it to the bottom of the ScrollView?
Here is my XML code of the layout part:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:background="#color/blueLight"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".tasti">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/rellay_LOAD"
android:paddingBottom="2dp">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="4">
<!-- Row 1 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="4dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAFFE"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="4dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAPUCCINO"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HAMBURGER"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIRRA 50Cl"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PROFITTEROL"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIRRA 1l"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 4 -->
<!--Column 1 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="4dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIQUORI VARI"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--Column 2 -->
<android.support.v7.widget.CardView
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_columnWeight="1"
android:layout_marginBottom="4dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_rowWeight="1"
app:cardElevation="0.50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIMONCINO"
android:textAlignment="center"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/rellay_LOAD"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_weight="0.65"
android:background="#color/colorWhite"
android:clickable="true"
android:gravity="center"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
tools:ignore="NestedWeights,ObsoleteLayoutParam">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="CREATE"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</RelativeLayout>
</RelativeLayout>
While here is the code from my tasti.class
public class tasti extends AppCompatActivity {
RelativeLayout rellay_LOAD;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasti);
rellay_LOAD = findViewById(R.id.rellay_LOAD);
rellay_LOAD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(tasti.this);
#SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
final EditText bottone = mView.findViewById(R.id.etBottone);
final EditText prezzo = mView.findViewById(R.id.etPrezzo);
Button mLogin = mView.findViewById(R.id.btnADD);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
mLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!bottone.getText().toString().isEmpty() && !prezzo.getText().toString().isEmpty()) {
Toast.makeText(tasti.this,
"Tasto aggiunto con successo",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else {
Toast.makeText(tasti.this,
"Non lasciare dei campi vuoti!",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
In Kotlin, you can do something like this :
val mainLinearLayout = findViewById(R.id.constraintLayout) as LinearLayout
val cardLinearLayout = LinearLayout(this)
cardLinearLayout.orientation = LinearLayout.VERTICAL
val params = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
params.setMargins(16,16,16,16)
val cardView = CardView(this)
cardView.radius = 15f
cardView.setCardBackgroundColor(Color.parseColor("#002388"))
cardView.setContentPadding(36,36,36,36)
cardView.layoutParams = params
cardView.cardElevation = 30f
val quote = TextView(this)
quote.text = "\"My cardview.\n";
quote.textSize = 24f
quote.setTextColor(Color.WHITE)
quote.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL)
cardLinearLayout.addView(quote)
cardLinearLayout.addView(name)
cardView.addView(cardLinearLayout)
mainLinearLayout.addView(cardView)
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);
}
I'm getting the error System.NullReferenceException: Object reference not set to an instance of an object when I try to reference a TextView from my fragment. It happens at runtime.
Fragment
public class Fragment1 : Fragment
{
private int checkNumber = 0;
private string[] updateTitleArray = new string[10];
private string[] updateBodyArray = new string[10];
private TextView updatesTitle0;
private TextView updatesTitle1;
private TextView updatesTitle2;
private TextView updatesTitle3;
private TextView updatesTitle4;
private TextView updatesTitle5;
private TextView updatesTitle6;
private TextView updatesTitle7;
private TextView updatesTitle8;
private TextView updatesTitle9;
private TextView updateBody0;
private TextView updateBody1;
private TextView updateBody2;
private TextView updateBody3;
private TextView updateBody4;
private TextView updateBody5;
private TextView updateBody6;
private TextView updateBody7;
private TextView updateBody8;
private TextView updateBody9;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
fillUpdates(updateTitleArray, updateBodyArray);
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.fragment1, null);
}
public void fillUpdates(string[] updatetitlearray, string[] updatebodyarray)
{
//increment1 so if block skips here
checkNumber = 1;
//fill updatesArray
updateTitleArray = updatetitlearray;
updateBodyArray = updatebodyarray;
updatesTitle0 = View.FindViewById<TextView>(Resource.Id.update_title0);
updatesTitle1 = View.FindViewById<TextView>(Resource.Id.update_title1);
updatesTitle2 = View.FindViewById<TextView>(Resource.Id.update_title2);
updatesTitle3 = View.FindViewById<TextView>(Resource.Id.update_title3);
updatesTitle4 = View.FindViewById<TextView>(Resource.Id.update_title4);
updatesTitle5 = View.FindViewById<TextView>(Resource.Id.update_title5);
updatesTitle6 = View.FindViewById<TextView>(Resource.Id.update_title6);
updatesTitle7 = View.FindViewById<TextView>(Resource.Id.update_title7);
updatesTitle8 = View.FindViewById<TextView>(Resource.Id.update_title8);
updatesTitle9 = View.FindViewById<TextView>(Resource.Id.update_title9);
updateBody0 = View.FindViewById<TextView>(Resource.Id.update_body0);
updateBody1 = View.FindViewById<TextView>(Resource.Id.update_body1);
updateBody2 = View.FindViewById<TextView>(Resource.Id.update_body2);
updateBody3 = View.FindViewById<TextView>(Resource.Id.update_body3);
updateBody4 = View.FindViewById<TextView>(Resource.Id.update_body4);
updateBody5 = View.FindViewById<TextView>(Resource.Id.update_body5);
updateBody6 = View.FindViewById<TextView>(Resource.Id.update_body6);
updateBody7 = View.FindViewById<TextView>(Resource.Id.update_body7);
updateBody8 = View.FindViewById<TextView>(Resource.Id.update_body8);
updateBody9 = View.FindViewById<TextView>(Resource.Id.update_body9);
updatesTitle0.Text = updateTitleArray[0].ToString();
updatesTitle1.Text = updateTitleArray[1].ToString();
updatesTitle2.Text = updateTitleArray[2].ToString();
updatesTitle3.Text = updateTitleArray[3].ToString();
updatesTitle4.Text = updateTitleArray[4].ToString();
updatesTitle5.Text = updateTitleArray[5].ToString();
updatesTitle6.Text = updateTitleArray[6].ToString();
updatesTitle7.Text = updateTitleArray[7].ToString();
updatesTitle8.Text = updateTitleArray[8].ToString();
updatesTitle9.Text = updateTitleArray[9].ToString();
updateBody0.Text = updateBodyArray[0].ToString();
updateBody1.Text = updateBodyArray[1].ToString();
updateBody2.Text = updateBodyArray[2].ToString();
updateBody3.Text = updateBodyArray[3].ToString();
updateBody4.Text = updateBodyArray[4].ToString();
updateBody5.Text = updateBodyArray[5].ToString();
updateBody6.Text = updateBodyArray[6].ToString();
updateBody7.Text = updateBodyArray[7].ToString();
updateBody8.Text = updateBodyArray[8].ToString();
updateBody9.Text = updateBodyArray[9].ToString();
}
}
View
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--Cardview 1-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shownbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view2-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--cardview 3-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 4-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 5-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 6-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 7-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 8-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 9-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 10-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
I understand that what I am trying to achieve can be implemented with recycler views but I haven't fully understood that, because I am a beginner programmer.
You are calling you function too early. OnCreate gets called before OnCreateView. That means, that your views haven't been created, when you try to access the layout components. You have to move your call of fillUpdates to OnStart (or OnCreateView or another lifecycle method that gets called after OnCreateView).
Have a look at the Frament Lifecycle
public override void OnStart(Bundle bundle)
{
base.OnStart(bundle);
fillUpdates(updateTitleArray, updateBodyArray);
}