Android: Array loops return null elements - java

I have been trying to use getResources().getIdentifier() methods to assign ids to an array of images. However, when I consoled the array, I only got 2 valid elements and the rest was null. Could somebody please explain to me why after the onCreate method is executed, the program only consoles the values of the first 2 loops and returns null values afterward?
Currently: [ImageView0, ImageView1, null, null, null, null, null, null, null]
What should I do to make it fully loop through the array and return an array with valid values of [ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8] instead ?
Many thanks! I am appreciated any help!
INPUT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
ImageView[] computerImages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
computerImages = new ImageView[9];
for (int i=0; i<computerImages.length; i++){
String computerImageID = "ImageView"+i;
int resID = getResources().getIdentifier(computerImageID,"id",getPackageName());
computerImages[i] = findViewById(resID);
} Log.i("ON CREATE", Arrays.toString(computerImages));
}
OUTPUT
I/ON CREATE: [android.support.v7.widget.AppCompatImageView{fe40fe0 VFED..C.. ......ID 0,0-0,0 #7f070003 app:id/ImageView0}, android.support.v7.widget.AppCompatImageView{8313299 VFED..C.. ......ID 0,0-0,0 #7f070004 app:id/ImageView1}, null, null, null, null, null, null, null]
XML LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:id="#+id/button"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Computer" />
<Button
android:id="#+id/button2"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Player 02" />
</LinearLayout>
<GridLayout 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/gridLayout"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3">
<ImageView
android:id="#+id/imageView0"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="0" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="4" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="5" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="6" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="7" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="8" />
</GridLayout>
<Button
android:id="#+id/playAgainButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:onClick="clickReset"
android:text="Play Again"
android:textSize="20sp" />
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/holo_orange_dark"
android:padding="5dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="25sp"
android:visibility="invisible" />
</RelativeLayout>

It returning null because "ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8" views are not present in resource file. Only ImageView0, ImageView1 are present in resource file.

Related

Android Card View Activity is laggy

I have this fragment with 8 card views and 8 images but when I use the app on my phone it lags when i'm on this fragment and this fragment alone, anyone know why that is?
Each card directs to a different activity on click so maybe that is what is causing the lag?
Xml:
<?xml version="1.0" encoding="utf-8"?>
\<ScrollView 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:background="#color/backgroundBlack"
xmlns:app="http://schemas.android.com/apk/res-auto"\>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Inventario"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
<GridLayout
android:columnOrderPreserved="false"
android:alignmentMode="alignMargins"
android:layout_marginTop="17dp"
android:rowCount="1"
android:columnCount="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardMaquinas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="#+id/receiptImage"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/maquina"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Maquinas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardTintas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/ink"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/black"
android:text="Tintas"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardVasos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/inkcup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_gravity="center"
android:text="Vasos de tinta"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardAgujas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/agujas"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agujas y cartuchos"
android:layout_gravity="center"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardFuentes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/fuentes"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fuentes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPiel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/piel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Piel sintetica"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPuntas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/cartuchos" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tubos y puntas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardGuantes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/guantes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Guantes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:visibility="invisible">
</Button>
</LinearLayout>
Fragment:
package com.example.tattostudio.ui.home;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.example.tattostudio.R;
import com.example.tattostudio.databinding.FragmentHomeBinding;
import com.example.tattostudio.inventariostuff.AgujasInventario;
import com.example.tattostudio.inventariostuff.FuentesInventario;
import com.example.tattostudio.inventariostuff.GuantesInventario;
import com.example.tattostudio.inventariostuff.MaquinasInventario;
import com.example.tattostudio.inventariostuff.PielInventario;
import com.example.tattostudio.inventariostuff.PuntasInventario;
import com.example.tattostudio.inventariostuff.TintasActivity;
import com.example.tattostudio.inventariostuff.VasosInventario;
public class HomeFragment extends Fragment implements View.OnClickListener {
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
androidx.cardview.widget.CardView card1, card2, card3, card4, card5, card6, card7, card8;
card1 = getView().findViewById(R.id.cardMaquinas);
card2 = getView().findViewById(R.id.cardTintas);
card3 = getView().findViewById(R.id.cardVasos);
card4 = getView().findViewById(R.id.cardAgujas);
card5 = getView().findViewById(R.id.cardFuentes);
card6 = getView().findViewById(R.id.cardPiel);
card7 = getView().findViewById(R.id.cardPuntas);
card8 = getView().findViewById(R.id.cardGuantes);
card1.setOnClickListener(this);
card2.setOnClickListener(this);
card3.setOnClickListener(this);
card4.setOnClickListener(this);
card5.setOnClickListener(this);
card6.setOnClickListener(this);
card7.setOnClickListener(this);
card8.setOnClickListener(this);
}
public void onClick(View v){
Intent i;
switch (v.getId()){
case R.id.cardMaquinas:
i = new Intent(getActivity(), MaquinasInventario.class);
startActivity(i);
break;
case R.id.cardTintas:
i = new Intent(getActivity(), TintasActivity.class);
startActivity(i);
break;
case R.id.cardVasos:
i = new Intent(getActivity(), VasosInventario.class);
startActivity(i);
break;
case R.id.cardAgujas:
i = new Intent(getActivity(), AgujasInventario.class);
startActivity(i);
break;
case R.id.cardFuentes:
i = new Intent(getActivity(), FuentesInventario.class);
startActivity(i);
break;
case R.id.cardPiel:
i = new Intent(getActivity(), PielInventario.class);
startActivity(i);
break;
case R.id.cardPuntas:
i = new Intent(getActivity(), PuntasInventario.class);
startActivity(i);
break;
case R.id.cardGuantes:
i = new Intent(getActivity(), GuantesInventario.class);
startActivity(i);
break;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
I tried using smaller images but I suspect the lag comes from the code being unoptimized, i'm a begginer developer on android so i'm not really sure what can I do to optimize it and make it run smooth on my phone
Issue with your ScrollView becasuse you closed Scrollview at top without including other UI into it.It should close at the end of XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#color/backgroundBlack"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Inventario"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
<GridLayout
android:columnOrderPreserved="false"
android:alignmentMode="alignMargins"
android:layout_marginTop="17dp"
android:rowCount="1"
android:columnCount="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardMaquinas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="#+id/receiptImage"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/maquina"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Maquinas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardTintas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/ink"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/black"
android:text="Tintas"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardVasos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/inkcup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_gravity="center"
android:text="Vasos de tinta"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardAgujas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/agujas"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agujas y cartuchos"
android:layout_gravity="center"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardFuentes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/fuentes"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fuentes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPiel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/piel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Piel sintetica"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPuntas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/cartuchos" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tubos y puntas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardGuantes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/guantes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Guantes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:visibility="invisible">
</Button>
</LinearLayout>
</ScrollView>

Android studio checkbox gets unchecked when moved from one screen to next screen

I am new to android studio, I tried all possible options, but unable to understand why checkbox gets unchecked when moved from one screen to other when I come back to previous screen.
I am missing out something in onsavedInstanceState or onRestoreInstanceState
Below is my Java Code
package com.example.qualitycheckstation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Qc2_1_1 extends AppCompatActivity {
CheckBox qc2_1cb1a,qc2_1cb1b,qc2_1cb1c, qc2_1cb2a,qc2_1cb2b,qc2_1cb2c ,qc2_1cb3a,qc2_1cb3b, qc2_1cb3c;
TextView date2_1_1, time2_1_1;
Button next2_1_1,back2_1_1;
Boolean myBoolean1,myBoolean2,myBoolean3,myBoolean4,myBoolean5,myBoolean6,myBoolean7,myBoolean8,myBoolean9;
private static String pr2_1_1,pr2_1_2,pr2_1_3;
private boolean myBoolean = false;
public static String getPr2_1_1 (){
return pr2_1_1;
}
public static String getPr2_1_2 (){
return pr2_1_2;
}
public static String getPr2_1_3(){
return pr2_1_3;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qc2_1_1);
date2_1_1= findViewById(R.id.date2_1_1);
time2_1_1 = findViewById(R.id.time2_1_1);
Calendar calendar= Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = simpleDateFormat.format(calendar.getTime());
date2_1_1.setText(date);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("hh:mm:ss a");
String time0 = simpleDateFormat1.format(calendar.getTime());
time2_1_1.setText(time0);
qc2_1cb1a = findViewById(R.id.qc2_1cb1a);
qc2_1cb1b = findViewById(R.id.qc2_1cb1b);
qc2_1cb1c = findViewById(R.id.qc2_1cb1c);
qc2_1cb2a = findViewById(R.id.qc2_1cb2a);
qc2_1cb2b = findViewById(R.id.qc2_1cb2b);
qc2_1cb2c = findViewById(R.id.qc2_1cb2c);
qc2_1cb3a = findViewById(R.id.qc2_1cb3a);
qc2_1cb3b = findViewById(R.id.qc2_1cb3b);
qc2_1cb3c = findViewById(R.id.qc2_1cb3c);
next2_1_1 = findViewById(R.id.next2_1_1);
back2_1_1 = findViewById(R.id.back2_1_1);
next2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((qc2_1cb1a.isChecked())){
pr2_1_1 = "O";
}
else if((qc2_1cb1b.isChecked())){
pr2_1_1 = "X";
}
else if((qc2_1cb1c.isChecked())){
pr2_1_1 = "N/A";
}
if((qc2_1cb2a.isChecked())){
pr2_1_2 = "O";
}
else if((qc2_1cb2b.isChecked())){
pr2_1_2 = "X";
}
else if((qc2_1cb2c.isChecked())){
pr2_1_2 = "N/A";
}
if((qc2_1cb3a.isChecked())){
pr2_1_3 = "O";
}
else if((qc2_1cb3b.isChecked())){
pr2_1_3 = "X";
}
else if((qc2_1cb3c.isChecked())){
pr2_1_3 = "N/A";
}
if (
((qc2_1cb1a.isChecked()) && (qc2_1cb1b.isChecked())) || ((qc2_1cb1b.isChecked()) && (qc2_1cb1c.isChecked())) || ((qc2_1cb1a.isChecked()) && (qc2_1cb1c.isChecked())) ||
((qc2_1cb2a.isChecked()) && (qc2_1cb2b.isChecked())) || ((qc2_1cb2b.isChecked()) && (qc2_1cb2c.isChecked())) || ((qc2_1cb2a.isChecked()) && (qc2_1cb2c.isChecked())) ||
((qc2_1cb3a.isChecked()) && (qc2_1cb3b.isChecked())) || ((qc2_1cb3b.isChecked()) && (qc2_1cb3c.isChecked())) || ((qc2_1cb3a.isChecked()) && (qc2_1cb3c.isChecked()))
)
{ { Toast.makeText(getApplicationContext(),"Select Either OK, NG or N/A", Toast.LENGTH_SHORT).show(); }}
else if (
((qc2_1cb1a.isChecked()) || (qc2_1cb1b.isChecked()) || (qc2_1cb1c.isChecked())) &&
((qc2_1cb2a.isChecked()) || (qc2_1cb2b.isChecked()) || (qc2_1cb2c.isChecked()))&&
((qc2_1cb3a.isChecked()) || (qc2_1cb3b.isChecked()) || (qc2_1cb3c.isChecked()))
)
{ Intent intent = new Intent(Qc2_1_1.this, Qc2_1_2.class);startActivity(intent); }
else { Toast.makeText(getApplicationContext(),"Select all parameters", Toast.LENGTH_SHORT).show(); }
}
});
back2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Qc2_1_1.this, Qc2.class);
startActivity(intent);
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("qc2_1cb1a", qc2_1cb1a.isChecked());
outState.putBoolean("qc2_1cb1b", qc2_1cb1b.isChecked());
outState.putBoolean("qc2_1cb1c", qc2_1cb1c.isChecked());
outState.putBoolean("qc2_1cb2a", qc2_1cb2a.isChecked());
outState.putBoolean("qc2_1cb2b", qc2_1cb2b.isChecked());
outState.putBoolean("qc2_1cb2c", qc2_1cb2c.isChecked());
outState.putBoolean("qc2_1cb3a", qc2_1cb3a.isChecked());
outState.putBoolean("qc2_1cb3b", qc2_1cb3b.isChecked());
outState.putBoolean("qc2_1cb3c", qc2_1cb3c.isChecked());
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
qc2_1cb1a.setChecked(myBoolean1);
qc2_1cb1b.setChecked(myBoolean2);
qc2_1cb1c.setChecked(myBoolean3);
qc2_1cb2a.setChecked(myBoolean4);
qc2_1cb2b.setChecked(myBoolean5);
qc2_1cb2c.setChecked(myBoolean6);
qc2_1cb3a.setChecked(myBoolean7);
qc2_1cb3b.setChecked(myBoolean8);
qc2_1cb3c.setChecked(myBoolean9);
}
}
xml Code
<TableLayout
android:layout_width="match_parent"
android:layout_height="462dp"
android:layout_marginStart="15dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="15dp"
android:background="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="Sl No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Measured Item"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="199dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="OP No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text="Gauge Used"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Parameter"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="91dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="OK"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="92dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="NG"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="N/A"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="1."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Groove Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="131dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Exist"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb1a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb1b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb1c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="2."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Serial Engraving Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="30"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="41dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Numbers should be readable"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb2a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb2b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb2c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="3."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Machining Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="328dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="No rough surface, unwash surface,scratch, burr"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb3a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb3b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb3c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="360dp"
android:layout_marginTop="32dp"
android:text="QC Station 2.1"
android:textColor="#color/black"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="211dp"
android:layout_height="68dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/date2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="DD/MM/YYYY"
android:inputType="date"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/time2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="HH.MM.SS"
android:inputType="time"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="next"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/back2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="Back"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Any support to solve this would be of great help
You should get values from savedInstanceState in onCreate() method. Someting like:
if (savedInstanceState!=null){
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
}
and then assign it to your check boxes

TextView text animation is restarted if the text is changed in another TextView

In TextView1 text animation runs from right to left, and in TextView2 constantly changing text. The problem is that when I perform TextView2.setText ( "...") in the text animation TextView1 restarted. Is it possible to prevent the restart of the animation?
XML code TextView1:
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="text text text text text text text"
android:textSize="14sp" />
Java code initialization TextView1:
final TextView textView1 = (TextView) rootView.findViewById(R.id.textView1);
textView1.setSelected(true);
XML code TextView2:
<TextView
android:id="#+id/seekSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="2dp"
android:paddingStart="10dp"
android:paddingTop="12dp"
android:text="0%"
android:textSize="20sp" />
XML code:
<TextView
android:id="#+id/fullSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/background"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingStart="2dp"
android:paddingTop="12dp"
android:text="3:15"
android:textSize="20sp" />
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:id="#+id/seekDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="#id/seekSongDuration"
android:layout_toEndOf="#id/seekSongDuration"
android:layout_toStartOf="#id/fullSongDuration"
android:paddingBottom="10dp"
android:paddingTop="10dp"
app:dsb_trackHeight="3dp" />
<com.andexert.library.RippleView
android:id="#id/butPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_centerHorizontal="true"
android:background="#color/background"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgPlayPause"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_play_circle_outline_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butShufle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgShufle"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="#drawable/ic_shuffle_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butPreviosSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:layout_toEndOf="#id/butShufle"
android:layout_toStartOf="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_rewind_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butLooping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgLoopReaped"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:src="#drawable/ic_repeat_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butNextSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#+id/butPlayPause"
android:layout_toEndOf="#id/butPlayPause"
android:layout_toStartOf="#id/butLooping"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_forward_black_48dp" />
</com.andexert.library.RippleView>
<RelativeLayout
android:id="#id/currentSongLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_above="#+id/butPlayPause"
android:background="#color/backgroundCurrentTrack"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<com.andexert.library.RippleView
android:id="#+id/butSettingCurrentSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/black"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="8dp"
android:src="#drawable/ic_more_circle_vert_black_36dp" />
</com.andexert.library.RippleView>
<ImageView
android:id="#+id/iconCurrentPlaylist"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="#drawable/ic_playlist_play_black_36dp" />
<TextView
android:id="#+id/txtNumberSongs"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:background="#drawable/shape_ellipse"
android:paddingEnd="6dp"
android:paddingStart="6dp"
android:paddingTop="3dp"
android:text="1/1"
android:textAlignment="center"
android:textColor="#color/grayLite"
android:textSize="12sp" />
<TextView
android:id="#+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_toEndOf="#id/txtNumberSongs"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Title"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_below="#id/songTitle"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Artist - Album"
android:textSize="14sp" />
</RelativeLayout>
Java code:
private TextView songArtistAlbum;
private TextView txtSeek;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
songArtistAlbum = (TextView) rootView.findViewById(R.id.artistAlbum);
songArtistAlbum.setSelected(true);
txtSeek = (TextView) rootView.findViewById(R.id.seekSongDuration);
...
}
private DiscreteSeekBar.OnProgressChangeListener onDurationProgressChangeListener = new DiscreteSeekBar.OnProgressChangeListener() {
#Override
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
if (!fromUser) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
}
#Override
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
};
Thanks in advance.
Try to wrap your TextView1 with LinearLayout.
Check Activity class TextView object and its assign id.

Could not execute method for android:onClick [duplicate]

This question already has answers here:
Resource not found TextView
(2 answers)
Closed 7 years ago.
Hi i am new to android development and i couldn't execute method when a button is clicked.I retyped the code as in the tutorial but it ended up lot of errors. Check the code below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basketball Score Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="70dp"
android:id="#+id/header"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team A"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:id="#+id/score_a"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:onClick="display_score_a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:onClick="three_pts_a"
android:padding="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:text="Inside the Ring"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Free Throw"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team B"
android:layout_marginLeft="250dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="290dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:layout_marginLeft="230dp"
android:padding="10dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Inside the Ring"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:text="Free Throw"
android:padding="10dp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:background="#ff6000"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
****Mainactivity.java****
package android.mytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int total_pts1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void three_pts_a(View view)
{
total_pts1 = total_pts1 + 3;
display_score_a(total_pts1);
}
private void display_score_a(int number) {
TextView num = (TextView) findViewById(
R.id.score_a);
num.setText(number);
}
}
its because of the integer variable.you have to parse it to a string or change that line to num.setText(String.valueOf(number));

How to set list view height programatically in android

I am making an application in which my keyboard comes up on the activity and it hides may header view (layout) I have searched and tried adjust pane state hidden,adjust nothing all the attributes in manifest but nothing solved my problem then I found one the posts over stackover flow that you can calculate the spacing then set the height I implemented but the behaviour is still same this is the sanpshot when activity comes
but when keyborad pops up it hides my header view this is snapshot of what's happening
this is my manifest code
<activity
android:name="com.dd.sproutchat.ChatActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing">
</activity>
this is my layout of that activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<RelativeLayout
android:id="#+id/TopLayout"
android:layout_width="match_parent"
android:layout_height="55dp">
<ImageButton
android:id="#+id/btn_back"
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="#drawable/back_icon_2x"
android:textColor="#000000"
android:textSize="22sp" />
<LinearLayout
android:id="#+id/Image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/btn_back">
<Button
android:id="#+id/recUserImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/image_circle_shape"
android:text=""
android:textColor="#color/white"
android:visibility="gone" />
<com.dd.sproutchat.customcontrols.MLRoundedImageView
android:id="#+id/userImg"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/btn_Search"
android:layout_toRightOf="#+id/Image"
android:orientation="vertical">
<TextView
android:id="#+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="start"
android:gravity="left"
android:singleLine="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/txtOnlineStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:singleLine="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black" />
</LinearLayout>
<ImageButton
android:id="#+id/btn_Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/menu_icon_2x"
android:visibility="gone" />
<ImageButton
android:id="#+id/btn_Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/search_icon_2x" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Rl_line"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/TopLayout"
android:background="#color/chat_border"></RelativeLayout>
<RelativeLayout
android:id="#+id/Rl_Options"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#+id/Rl_line"
android:background="#color/chat_options_bg">
<ImageButton
android:id="#+id/btn_Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:background="#drawable/home_btn_active_2x" />
<ImageButton
android:id="#+id/btn_SproutesList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/chat_btn_2x" />
<ImageButton
android:id="#+id/btn_Note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="#drawable/note_btn_2x" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Rl_line2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/Rl_Options"
android:background="#color/chat_border"></RelativeLayout>
<RelativeLayout
android:id="#+id/Rl_ChatLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/Rl_MessageLayout"
android:layout_below="#+id/Rl_line2">
<!-- android:background="#drawable/chat_bg_2x" -->
<RelativeLayout
android:id="#+id/Btn_Chats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="4">
<RelativeLayout
android:id="#+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<ImageButton
android:id="#+id/btn_Sortby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:background="#drawable/icon_sortby_sprout_2x" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/btn_AddSprout"
android:layout_toRightOf="#+id/btn_Sortby"
android:weightSum="2">
<Button
android:id="#+id/btn_AllSprouts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/btn_blue"
android:paddingBottom="3dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="3dp"
android:text="#string/AllSprouts"
android:textColor="#color/white" />
<Button
android:id="#+id/btn_AllFavorites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:background="#drawable/btn_blank"
android:paddingBottom="3dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="3dp"
android:text="#string/AllFavorites"
android:textColor="#color/grey_start" />
</LinearLayout>
<ImageButton
android:id="#+id/btn_AddSprout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#drawable/icon_add_sprout_2x" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/noteLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="gone">
<EditText
android:id="#+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/imageButton4" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="#+id/imageButton5"
android:background="#drawable/sort" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:background="#drawable/add" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/Lv_Chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/Btn_Chats"
android:layout_above="#+id/Rl_MessageLayout"
android:divider="#null"
android:dividerHeight="0dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:scrollbars="none"
android:stackFromBottom="true"
android:transcriptMode="normal"></ListView>
<RelativeLayout
android:id="#+id/Rl_MessageLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/chat_screen_bottom">
<ImageButton
android:id="#+id/btn_Attachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="#drawable/attachment_icon_white_2x" />
<EditText
android:id="#+id/edt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/btn_Send"
android:layout_toRightOf="#+id/btn_Attachment"
android:background="#drawable/txt_field"
android:imeOptions="actionDone"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:singleLine="true"
/>
<ImageButton
android:id="#+id/btn_Send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/icon_send_white_2x" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
and this is my activity code for calculating height of listview
private static boolean keyboardHidden = true;
private static int reduceHeight =0;
final View decorView = this.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHeight = rect.bottom - rect.top;
int height = decorView.getHeight();
boolean keyboardHiddenTemp = (double) displayHeight / height > 0.8;
int mylistviewHeight = Lv_Chat.getMeasuredHeight();
if (keyboardHiddenTemp != keyboardHidden) {
keyboardHidden = keyboardHiddenTemp;
if (!keyboardHidden) {
reduceHeight = height - displayHeight;
RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mylistviewHeight - reduceHeight);
Lv_Chat.setLayoutParams(mParam);
Lv_Chat.requestLayout();
} else {
RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mylistviewHeight + reduceHeight);
Lv_Chat.setLayoutParams(mParam);
Lv_Chat.requestLayout();
}
}
}
});
I have also tried this link for setting xml file
http://codetheory.in/android-add-views-view-groups-listview-gridview/
any help please
To change the height of ListView you should use LayoutParams:
ViewGroup.LayoutParams param = listView.getLayoutParams();
param.height = anynumberhere;
listView.setLayoutParams(param);
listView.requestLayout();
According to your question, this is the way to change the ListView's height, but i don't think its the rite approach to make that header of yours stay in its place.
I think you should use adjustResize instead of adjustNothing and add android:fitsSystemWindows="true" in your root RelativeLayout instead of programmatically trying to resize your views.

Categories