Android Card View Activity is laggy - java

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>

Related

When one object is clicked, two listeners are fired

Good day!
I made two cards using **CardView**, then each of them has a **Title** and a **body**. The body should expand when you click on the title. But when you click on "*carsCollapse*", "*socketsCollapse*" also fires. This can be seen on the video. Also, for clarity, I made a Toast, so we can see that when you click on carsCollapse, "*Cars*" and "*Sockets*" appear.
Video: THE LINK
Here is the xml code (shortened version):
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MapFilterFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:animateLayoutChanges="true"
android:id="#+id/carsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/carsCollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:src="#drawable/ic_baseline_electric_car_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Cars"
android:textSize="26sp"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:id="#+id/carsFilters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
// SOMETHING IN
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:animateLayoutChanges="true"
android:id="#+id/socketsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/socketsCollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:src="#drawable/ic_baseline_electrical_services_24" />
<TextView
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Sockets"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/socketsFilters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">
// SOMETHING
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
Here is the java code:
package com.maxet24.chargely;
import android.animation.LayoutTransition;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MapFilterFragment extends Fragment implements View.OnClickListener {
public LinearLayout carsFilters, socketsFilters, carsLayout, socketsLayout, carsCollapse, socketsCollapse;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map_filter, container, false);
// INIT
socketsLayout = v.findViewById(R.id.socketsLayout);
carsLayout = v.findViewById(R.id.carsLayout);
carsCollapse = v.findViewById(R.id.carsCollapse);
carsFilters = v.findViewById(R.id.carsFilters);
socketsCollapse = v.findViewById(R.id.socketsCollapse);
socketsFilters = v.findViewById(R.id.socketsFilters);
carsCollapse.setOnClickListener(this);
socketsCollapse.setOnClickListener(this);
carsLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
socketsLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
// INIT
return v;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.carsCollapse:
Toast.makeText(getContext(), "Cars", Toast.LENGTH_SHORT).show();
toggleFilters(carsFilters, carsLayout);
case R.id.socketsCollapse:
Toast.makeText(getContext(), "Sockets", Toast.LENGTH_SHORT).show();
toggleFilters(socketsFilters, socketsLayout);
}
}
public void toggleFilters(LinearLayout filters, LinearLayout layout) {
TransitionManager.beginDelayedTransition(layout, new AutoTransition());
if (filters.getVisibility() == View.VISIBLE) {
filters.setVisibility(View.GONE);
} else{
filters.setVisibility(View.VISIBLE);
}
}
}
Full xml code (just in case):
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MapFilterFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:animateLayoutChanges="true"
android:id="#+id/carsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/carsCollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:src="#drawable/ic_baseline_electric_car_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Cars"
android:textSize="26sp"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:id="#+id/carsFilters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView5"
android:layout_height="50dp"
android:layout_width="50dp"
app:srcCompat="#drawable/ch_type_1" />
<TextView
android:layout_marginLeft="30dp"
android:id="#+id/textView6"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Type 1"
android:layout_marginTop="4dp"
android:textSize="24sp" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="6dp"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView8"
android:layout_height="50dp"
android:layout_width="50dp"
app:srcCompat="#drawable/ch_type_2" />
<TextView
android:layout_marginLeft="30dp"
android:id="#+id/textView9"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Type 2"
android:layout_marginTop="4dp"
android:textSize="24sp" />
<Switch
android:id="#+id/switch2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="6dp"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_height="50dp"
android:layout_width="50dp"
app:srcCompat="#drawable/ch_type_3" />
<TextView
android:textAlignment="center"
android:layout_marginLeft="30dp"
android:id="#+id/textView8"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CHAdeMO"
android:layout_marginTop="4dp"
android:textSize="24sp" />
<Switch
android:id="#+id/switch3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="6dp"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView9"
android:layout_height="50dp"
android:layout_width="50dp"
app:srcCompat="#drawable/ch_type_4" />
<TextView
android:layout_marginLeft="30dp"
android:id="#+id/textView10"
android:layout_width="110dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="CCS combo type 1"
android:layout_marginTop="4dp"
android:textSize="24sp" />
<Switch
android:id="#+id/switch4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="6dp"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView10"
android:layout_height="50dp"
android:layout_width="50dp"
app:srcCompat="#drawable/ch_type_5" />
<TextView
android:layout_marginLeft="30dp"
android:id="#+id/textView11"
android:layout_width="110dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="CCS combo type 2"
android:layout_marginTop="4dp"
android:textSize="24sp" />
<Switch
android:id="#+id/switch5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="6dp"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:animateLayoutChanges="true"
android:id="#+id/socketsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/socketsCollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:src="#drawable/ic_baseline_electrical_services_24" />
<TextView
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Sockets"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/socketsFilters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/example"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
Thanks for any help!

Align elements in Linear Layout

I'm trying to recreate this layout I made with the
RelativeLayout but I'm having a lot of troubles:
I divided the new LinearLayout into two sub-LinearLayout. In the first one I would place the Image and the description on the left, and the table with the text on the right. In the second LinearLayout I would put the last table and description. This is what it looks like by doing this:
This is the code, I really don't understand what's wrong:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/slidelinearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_main2"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="640dp"
android:layout_height="438dp"
android:layout_gravity="left"
android:layout_weight="0.01"
android:gravity="left"
android:orientation="vertical">
<ImageView
android:id="#+id/slideimg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="124dp"
android:layout_marginTop="144dp"
tools:layout_conversion_absoluteHeight="380dp"
tools:layout_conversion_absoluteWidth="420dp"
tools:layout_editor_absoluteX="220dp"
tools:layout_editor_absoluteY="120dp" />
<TextView
android:id="#+id/txttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/slideimg"
android:layout_alignEnd="#+id/slideimg"
android:padding="16dp"
android:text="Title Here"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_conversion_absoluteHeight="73dp"
tools:layout_conversion_absoluteWidth="166dp"
tools:layout_editor_absoluteX="330dp"
tools:layout_editor_absoluteY="480dp" />
<TextView
android:id="#+id/t1"
android:layout_width="249dp"
android:layout_height="79dp"
android:layout_above="#+id/tl"
android:layout_alignEnd="#+id/tl"
android:layout_marginEnd="13dp"
android:layout_marginBottom="-32dp"
android:fontFamily="#font/sriracha"
android:text="Punteggio finale"
android:textColor="#DD1717"
android:textSize="30dp"
tools:layout_conversion_absoluteHeight="61dp"
tools:layout_conversion_absoluteWidth="217dp"
tools:layout_editor_absoluteX="760dp"
tools:layout_editor_absoluteY="150dp" />
<TextView
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="57dp"
android:layout_alignTop="#+id/t1"
android:layout_alignEnd="#+id/t1"
android:layout_marginTop="5dp"
android:layout_marginEnd="33dp"
android:fontFamily="#font/sriracha"
android:text="______________"
android:textColor="#020101"
android:textSize="30dp"
tools:layout_conversion_absoluteHeight="61dp"
tools:layout_conversion_absoluteWidth="210dp"
tools:layout_editor_absoluteX="765dp"
tools:layout_editor_absoluteY="153dp" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/t2"
android:layout_alignParentEnd="true"
android:layout_marginEnd="265dp"
android:layout_marginBottom="86dp"
android:background="#android:color/white"
android:padding="18dp"
tools:context=".Result"
tools:layout_conversion_absoluteHeight="260dp"
tools:layout_conversion_absoluteWidth="316dp"
tools:layout_editor_absoluteX="220dp"
tools:layout_editor_absoluteY="190dp">
<TableRow
android:id="#+id/tr_item_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Flessibilità"
android:textColor="#030101"
android:textSize="30dp" />
<TextView
android:id="#+id/f_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Fluidità"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/fl_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Originalità"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/o_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/item_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Elaborazione"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/el_item_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/item_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Titolo"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/t_item_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="408dp"
android:layout_height="279dp">
<TextView
android:id="#+id/t4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/tl_2"
android:layout_alignBottom="#+id/t1"
android:layout_gravity="right"
android:layout_marginEnd="-1dp"
android:layout_marginBottom="-478dp"
android:fontFamily="#font/sriracha"
android:gravity="right"
android:text="______________________"
android:textColor="#020101"
android:textSize="30dp"
tools:layout_conversion_absoluteHeight="115dp"
tools:layout_conversion_absoluteWidth="239dp"
tools:layout_editor_absoluteX="785dp"
tools:layout_editor_absoluteY="532dp" />
<TableLayout
android:id="#+id/tl_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/tl"
android:layout_alignParentBottom="true"
android:layout_marginTop="193dp"
android:layout_marginEnd="-248dp"
android:layout_marginBottom="26dp"
android:background="#android:color/white"
android:padding="25dp"
tools:context=".Result"
tools:layout_conversion_absoluteHeight="208dp"
tools:layout_conversion_absoluteWidth="314dp"
tools:layout_editor_absoluteX="710dp"
tools:layout_editor_absoluteY="560dp">
<TableRow
android:id="#+id/tr_item_6"
android:layout_width="222dp"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/t1_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Tempo di reazione"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/tempo_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/t2_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Tempo di completamento"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/tempo_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tr_item_8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/table_row_bg"
android:padding="2dp">
<TextView
android:id="#+id/n_cancellature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingRight="10dp"
android:text=" Numero cancellature"
android:textColor="#070404"
android:textSize="30dp" />
<TextView
android:id="#+id/n_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/table_cell_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#070404"
android:textSize="30dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/tl_2"
android:layout_marginEnd="-408dp"
android:layout_marginBottom="166dp"
android:layout_toStartOf="#+id/tl_2"
android:fontFamily="#font/sriracha"
android:text="Considerazioni aggiuntive"
android:textColor="#DD1717"
android:textSize="30dp"
tools:layout_conversion_absoluteHeight="115dp"
tools:layout_conversion_absoluteWidth="244dp"
tools:layout_editor_absoluteX="780dp"
tools:layout_editor_absoluteY="530dp" />
</LinearLayout>
Create the two separate layouts for the tables and include it in the main layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg">
<LinearLayout
android:id="#+id/upperLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="140dp"
android:orientation="vertical">
<ImageView
android:layout_width="400dp"
android:layout_height="350dp"
android:layout_marginRight="150dp"
android:background="#android:color/darker_gray"
android:src="#drawable/ic_launcher_link" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:text="Senza nome"
android:textColor="#android:color/black"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/upperTableHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:text="Table Header here"
android:textColor="#android:color/holo_red_light"
android:textSize="30sp" />
<include layout="#layout/upper_table"></include>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/upperLayout"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:layout_marginRight="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/lowerTableHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table Header here"
android:textColor="#android:color/holo_red_light"
android:textSize="30sp" />
<include layout="#layout/lower_table"></include>
</LinearLayout>
</RelativeLayout>
add the following methods to your Activity class:
public int getWidth() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
return width;
}
public void setLayoutMargins() {
View layout = findViewById(R.id.relative);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layout.getLayoutParams());
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.topMargin = 400;
layout.setLayoutParams(layoutParams);
//ImageView Layout
View image = findViewById(R.id.imgage);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(image.getLayoutParams());
image.getLayoutParams();
imageParams.width = 800;
imageParams.height = 600;
image.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
image.setLayoutParams(imageParams);
//UpperTable Layout
View upperTable = findViewById(R.id.upperTableLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(upperTable.getLayoutParams());
params.leftMargin = 250;
params.topMargin = 160;
upperTable.setLayoutParams(params);
}
public void setTextSizes() {
txt1 = findViewById(R.id.item_1);
txt2 = findViewById(R.id.item_2);
txt3 = findViewById(R.id.item_3);
txt4 = findViewById(R.id.item_4);
txt5 = findViewById(R.id.item_5);
f_item_1 = findViewById(R.id.f_item_1);
f_item_2 = findViewById(R.id.fl_item_2);
t1_item_1 = findViewById(R.id.t1_item_1);
tempo_item_1 = findViewById(R.id.tempo_item_1);
t2_item_2 = findViewById(R.id.t2_item_2);
tempo_item_2 = findViewById(R.id.tempo_item_2);
n_cancellature = findViewById(R.id.n_cancellature);
n_3 = findViewById(R.id.n_3);
txtSenza = findViewById(R.id.txtSenza);
txtUpper = findViewById(R.id.upperTableHeader);
txtLower = findViewById(R.id.lowerTableHeader);
txt1.setTextSize(30);
txt2.setTextSize(30);
txt3.setTextSize(30);
txt4.setTextSize(30);
txt5.setTextSize(30);
f_item_1.setTextSize(30);
f_item_2.setTextSize(30);
txtSenza.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
txtLower.setTextSize(30);
txtUpper.setTextSize(30);
t1_item_1.setTextSize(30);
t2_item_2.setTextSize(30);
tempo_item_1.setTextSize(30);
tempo_item_2.setTextSize(30);
n_3.setTextSize(30);
n_cancellature.setTextSize(30);
}
and then finally add the following code to onCreate:
if (getWidth() > 2000) {
setLayoutMargins();
setTextSizes();
}

How to make cardview responsive?

]2]2
this is NamazActivity.java In java class i just add cardview click listener and open a toast message. Every click listener shows the iteam ID with simple text
package com.example.shakeelmughal.assanislam;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;
public class NamazActivity extends AppCompatActivity {
GridLayout gridLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_namaz);
//back button
if(getSupportActionBar()!= null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
gridLayout = findViewById(R.id.gridlayout);
setSingleClick(gridLayout);
}
//function for going back to previous activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
private void setSingleClick(GridLayout gridLayout) {
for (int i = 0; i < gridLayout.getChildCount(); i++) {
CardView cardView = (CardView) gridLayout.getChildAt(i);
final int s = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(NamazActivity.this, "Item " + s + " Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
}
this is nazamactivity.xml In XML class i use gridlayout and inside it i use cardview with a text view and a image view. I have make 3 card in it...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shakeelmughal.assanislam.NamazActivity"
android:orientation="vertical"
android:weightSum="5">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#000"
android:layout_centerInParent="true"
android:text="نماز"/>
</RelativeLayout>
<GridLayout
android:id="#+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="4"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<!-- Row 1 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/tahart"
android:paddingRight="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= " Ùرض نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000"
android:text="5"
android:paddingLeft="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/namaz"
android:paddingRight="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="واجب نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="3"
android:textSize="25sp"
android:paddingLeft="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:src="#drawable/namaz" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ù†ÙÙ„ نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp"
android:paddingRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="25sp"
android:textColor="#000"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
Try this Make ScrollView as your parent layout it will work
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="نماز"
android:textColor="#000"
android:textSize="40sp" />
</RelativeLayout>
<GridLayout
android:id="#+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="4"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<!-- Row 1 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="70dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Ùرض نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="5"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="70dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="واجب نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="3"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
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="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:text="Ù†ÙÙ„ نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>

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.

Could not find TextView by calling findViewById method [duplicate]

This question already has answers here:
Can not find a View with findViewById()
(4 answers)
Closed 8 years ago.
My main.xml
<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:background="#drawable/background"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ua.example.eventorganizer.MainActivity" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="50dp" >
<FrameLayout
android:id="#+id/FrameLayout02"
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Счёт"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Сет 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Сет 2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Сет 3"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Сет 4"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Сет 5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout2" >
<FrameLayout
android:id="#+id/FrameLayout01"
android:layout_width="50dp"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher2" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/Score_One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="40"
android:textAppearance="?android:attr/textAppearanceLarge"
android:onClick="ScoreOne"
android:clickable="true"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="7"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="4"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="7"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout01" >
<FrameLayout
android:id="#+id/FrameLayout03"
android:layout_width="50dp"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher2" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/TextView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:onClick="ScoreTwo"
android:clickable="true"
android:text="15"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="3"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="50dp"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/TextView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="4"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
My code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class FilterActivity extends Activity {
private static final int TEXT_ID = 0;
EditText ScoreOne;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ScoreOne = (EditText) findViewById(R.id.Score_One);
}
debug say - not found in
ScoreOne = (EditText) findViewById(R.id.Score_One);
What wrong?
Your Score_One is a TextView and it cannot be cast to EditText - you get a ClassCastException.
Change
EditText ScoreOne;
to
TextVIew ScoreOne;
and the (EditText) cast to (TextView).
Score_One is the TextView. It's not EditText. You're not casting as TextView
try this
TextView ScoreOne = (TextView ) findViewById(R.id.Score_One);
TextView cannot cast to EditText
Change EditText Score_one;---->Textview Score_one;
None of the textview id having Score_one as i checked in above main.xml
main.xml
<TextView
android:id="#+id/Score_One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Счёт"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
Activity.java
TextView ScoreOne = (TextView ) findViewById(R.id.Score_One);

Categories