This is the code I have:
package mika.actual;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordion);
final View panels[] = {
findViewById(R.id.panelProfile),
findViewById(R.id.panelSettings),
findViewById(R.id.panelPrivacy)
};
final Button buttons[] = {
(Button) findViewById(R.id.btnProfile),
(Button) findViewById(R.id.btnSettings),
(Button) findViewById(R.id.btnPrivacy)
};
for (int i = 0; i < panels.length; i++){
panels[i].setVisibility(View.GONE);
}
for (int i = 0; i < buttons.length; i++){
final int x = i;
buttons[i].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
buttons[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int y = 0; y < panels.length; y++){
panels[y].setVisibility(View.GONE);
}
panels[x].setVisibility(View.VISIBLE);
for (int y = 0; y < buttons.length; y++){
buttons[y].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
}
buttons[x].setBackgroundColor(getResources().getColor(R.color.button_pressed));
}
});
}
}
}
and XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
I want to create the two arrays buttons[] and panels[] dynamically, taking all the child views inside the LinarLayout (with the id root_layout). Any suggestions appreciated. Thanks in advance :)
Maybe iterate over the childrens of your Viewgroup and adding them in a Arraylist?:
LinearLayout rootView = (LinearLayout) findViewById(R.id.mylinearlayoutid);
ArrayList<Button> buttons = new ArrayList<>(rootView.getChildCount());
for(int i = 0; i < rootView.getChildCount(); i++){
buttons.add ((Button) rootView.getChildAt(i));
}
Related
I'm trying to build an algorithm analyzing application using android. To see value change in each step when button clicked. What happened is - It gives complete result instead of step by step giving result. For example:
Selection Sort, I give an array = {4, 8, 2, 9, 5}
When 1st operation done the value will be = {2, 8, 4, 9, 5}
2nd operation the value will be = {2, 4, 8, 9, 5} and so on. But the the application gives final result. That means it gives, {2, 4, 5, 8, 9}
But how it give complete result? I'm confused.
Java Code
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Selection_Sort_Controller extends AppCompatActivity {
TextView textView_i, textView_j, textView_n, textView_min_idx, textView_i2,arr_0,arr_1,arr_2,arr_3,arr_4;
Button start;
public int[] arr = {9, 5, 8, 4, 2};
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection_sort_controller);
textView_i2 = findViewById(R.id.i2);
start = findViewById(R.id.start_btn);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sort_selection_sort(arr);
}
});
}
public void sort_selection_sort(int[] arr){
int n = arr.length;
for(int i = 0; i<n -1; i++){
int min_inx = i;
for(int j = i+1; j<n; j++){
if(arr[j] < arr[min_inx]){
min_inx = j;
}
}
int temp = arr[min_inx];
arr[min_inx] = arr[i];
arr[i] = temp;
updateArray(i,min_inx,arr,n);
}
}
public void updateArray(int i, int min_inx, int[] arr,int n){
textView_i = findViewById(R.id.i);
textView_j = findViewById(R.id.j);
textView_n = findViewById(R.id.n);
textView_min_idx = findViewById(R.id.mini_idx);
arr_0 = findViewById(R.id.array_0);
arr_1 = findViewById(R.id.array_1);
arr_2 = findViewById(R.id.array_2);
arr_3 = findViewById(R.id.array_3);
arr_4 = findViewById(R.id.array_4);
arr_0.setText(String.valueOf(arr[0]));
arr_1.setText(String.valueOf(arr[1]));
arr_2.setText(String.valueOf(arr[2]));
arr_3.setText(String.valueOf(arr[3]));
arr_4.setText(String.valueOf(arr[4]));
textView_i.setText(String.valueOf(i));
textView_min_idx.setText(String.valueOf(min_inx));
textView_n.setText(String.valueOf(n));
}
}
Here I called to give me the change the algorithm made. But how it gives complete sorted result?
public void sort_selection_sort(int[] arr){
int n = arr.length;
for(int i = 0; i<n -1; i++){
int min_inx = i;
for(int j = i+1; j<n; j++){
if(arr[j] < arr[min_inx]){
min_inx = j;
}
}
int temp = arr[min_inx];
arr[min_inx] = arr[i];
arr[i] = temp;
updateArray(i,min_inx,arr,n);
}
}
Here is the XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Selection_Sort_Controller">
<Button
android:id="#+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:text="#string/Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:layout_width="409dp"
android:layout_height="649dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/start_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="205dp"
android:layout_height="match_parent"
android:text="n"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/n"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="205dp"
android:layout_height="match_parent"
android:text="i"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/i"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="205dp"
android:layout_height="match_parent"
android:text="j"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/j"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="205dp"
android:layout_height="match_parent"
android:text="min_idx"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/mini_idx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="205dp"
android:layout_height="match_parent"
android:text="i2"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/i2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="array"
android:gravity="center"
android:textSize="24sp"/>
<TextView
android:id="#+id/array_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:layout_weight="1"
android:textSize="24sp"/>
<TextView
android:id="#+id/array_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:layout_weight="1"
android:textSize="24sp"/>
<TextView
android:id="#+id/array_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:layout_weight="1"
android:textSize="24sp"/>
<TextView
android:id="#+id/array_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:layout_weight="1"
android:textSize="24sp"/>
<TextView
android:id="#+id/array_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:layout_weight="1"
android:textSize="24sp"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my java code:
package com.example.project_final;
import static android.content.ContentValues.TAG;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import java.util.ArrayList;
public class history extends AppCompatActivity {
FirebaseFirestore Fstore;
String userID;
FirebaseAuth fAuth ;
int times;
int heart;
int avg_heart;
String date;
int count = 0;
TextView Data;
RelativeLayout [] L = new RelativeLayout[7];
TextView [] hr = new TextView[7];
TextView [] dates = new TextView[7];
ImageView [] img = new ImageView[7];
TextView last;
ArrayList<Integer> intheart = new ArrayList<>();
ArrayList<String> intdates = new ArrayList<>();
TextView average;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Fstore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
userID= fAuth.getUid();
DocumentReference documentReference = Fstore.collection("users").document(userID);
Data = findViewById(R.id.txtNodata);
Intent intent1 = getIntent();
String signup = intent1.getStringExtra("signup");
last = findViewById(R.id.txtlast);
average = findViewById(R.id.txtaverage);
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout"+ i+1, "id", this.getPackageName());
L[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("heartrate"+ i+1, "id", this.getPackageName());
hr[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("txtdate"+ i+1, "id", this.getPackageName());
dates[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("img"+ i+1, "id", this.getPackageName());
img[i] = findViewById(resourceId);
}
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
times = Math.toIntExact(task.getResult().getLong("times"));
avg_heart = Math.toIntExact(task.getResult().getLong("average_heart_rate"));
for (int i = 0 ; i <times ;i++){
intdates.add(task.getResult().getString("date" + i));
intheart.add(Math.toIntExact(task.getResult().getLong("heart"+i)));
}
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!(intheart.isEmpty()||intdates.isEmpty())){
int count =0;
if(times>7){
times=7;
}
last.setText("Last "+ times +" Days history");
for(int i =0 ; i<intdates.size();i++){
if(1.2*avg_heart<intheart.get(i)||0.8*avg_heart<intheart.get(i)){
L[i].setBackgroundColor(R.drawable.color_gradient1);
img[i].setImageResource(R.drawable.sad);
}
else{
L[i].setBackgroundColor(R.drawable.gradient_color);
img[i].setImageResource(R.drawable.happy);
}
dates[i].setText(intdates.get(i));
hr[i].setText(intheart.get(i)+" Bpm");
L[i].setVisibility(View.VISIBLE);
}
}
else{
handler.postDelayed(this, 100);
}
}
}, 100);
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context=".history">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtlast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text=""
android:textSize="30sp"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"
/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=""
android:id="#+id/txtaverage"
android:textColor="#000"
android:layout_gravity="center"
/>
</LinearLayout>
<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="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
android:visibility="invisible"
>
<TextView
android:id="#+id/txtday1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtdate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday1"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate1"
android:id="#+id/heartrate1"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:background="#drawable/color_gradient1"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 2"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday2"
/>
<TextView
android:id="#+id/txtdate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:id="#+id/heartrate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout3"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 3"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday3"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate3"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate3"
/>
<ImageView
android:id="#+id/img3"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/color_gradient1"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 4"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday4"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate4"
/>
<TextView
android:id="#+id/heartrate4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate4"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img4"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout5"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday5"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate5"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate5"
/>
<ImageView
android:id="#+id/img5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout6"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 6"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday6"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate6"
android:id="#+id/heartrate6"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img6"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:id="#+id/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 7"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday7"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate7"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate7"
/>
<ImageView
android:id="#+id/img7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/txtNodata"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:fontFamily="#font/lovelo"
android:gravity="center"
android:text="No data Available"
android:textColor="#color/colorRed"
android:textSize="20sp"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
When I run the code I get this error on a null object reference when I try to setbackground color inside the handler:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)'
The code should run fine since I initialized all the views. I've been stuck for a while I don't know where is the problem help.
I think you are not getting the name correct, so it's not initializing it, name must be a string as a whole :
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout" + (i+1).toString(), "id", this.getPackageName());
L[i] = findViewById(resourceId);
}
Here I am trying to take user input and display output in a Text View, but checkgen and checkuser is returning -1 despite of selecting them and also both the radio buttons are getting selected not any one of them, I want to display all the details entered in the text view. I have pasted the activity_reg.xml code below too
reg.java
package com.example.bloodbank;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class reg extends AppCompatActivity {
//login button
Button btn_login;
//firstname
EditText fn;
//lastname
EditText ln;
//phone number
EditText pn;
//output
TextView op;
//registration button
Button btn_reg;
//radio group for gender
RadioGroup rggen;
//radio group for user(donor/in search of blood)
RadioGroup rguser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
btn_login=findViewById(R.id.btn_login);
fn=findViewById(R.id.txtfirstname);
ln=findViewById(R.id.txtlastname);
pn=findViewById(R.id.txtmobile);
op=findViewById(R.id.outputreg);
rggen=findViewById(R.id.rggender);
rguser=findViewById(R.id.rgreg);
btn_reg=findViewById(R.id.main_btn_reg);
btn_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fname=fn.getText().toString();
String lname=ln.getText().toString();
int pno= new Integer(pn.getText().toString());
int checkgen=rggen.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkgen= "+checkgen, Toast.LENGTH_SHORT).show();
int checkuser=rguser.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkuser= "+checkuser, Toast.LENGTH_SHORT).show();
if(((checkgen == -1)||(checkuser == -1))||((checkgen == -1)&&(checkuser == -1)))
{
//no radio button selected
Toast.makeText(reg.this, "Please select an option", Toast.LENGTH_SHORT).show();
}
else {
//radio button is checked
findRadioButton1(checkgen);
findRadioButton2(checkuser);
op.setText("Welcome " + fname + " " + lname + "Phone Number: " +pno );
}
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(reg.this,login.class);
startActivity(i);
}
});
}
private void findRadioButton2(int checkuser) {
switch(checkuser){
case R.id.rbdonor:
op.setText("\nUser: Donor\n");
break;
case R.id.rbneedy:
op.setText("\nUser: In search of blood\n");
break;
}
}
private void findRadioButton1(int checkgen) {
switch(checkgen){
case R.id.rbmale:
op.setText("\nGender: Male\n");
break;
case R.id.rbfemale:
op.setText("\nGender: Female\n");
break;
}
}
}
activity_reg.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rbmale"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".reg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="23dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtregister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Registration"
android:textColor="#color/colorPrimaryDark"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtfirstname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="First Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtlastname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Last Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:id="#+id/textView"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text=" Gender*:"
android:textColor="#A31D13" />
<RadioGroup
android:id="#+id/rggender"
android:layout_width="309dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtmobile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Mobile Number*"
android:inputType="number"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:id="#+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:hint="Password*"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="111dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:text=" Register as*:"
android:textColor="#BE202F" />
<RadioGroup
android:id="#+id/rgreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbdonor"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am a donor"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbneedy"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am in search of blood"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="1dp"
android:orientation="horizontal">
</LinearLayout>
<Button
android:id="#+id/main_btn_reg"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="20dp"
android:background="#color/colorPrimaryDark"
android:text="REGISTER"
android:textColor="#color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0.6dp"
android:layout_marginTop="16dp"
android:background="#color/colorPrimaryDark"></View>
<Button
android:id="#+id/btn_login"
android:layout_width="86dp"
android:layout_height="44dp"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="LOGIN"
android:textColor="#color/colorPrimaryDark"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/outputreg"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> ```
I want to dynamically change textviews height to fit cells in other linearlayout.
I tried to make this in this way:
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.numberCage.setText(cages.get(position).getNumberCage());
holder.eggsDescription.setText(cages.get(position).getEggsDescription());
holder.eggsFrom.setText(cages.get(position).getNumberCageFrom());
if(cages.get(position).getBasicEggs().toString().equals("true"))
holder.basicEggs.setText("Tak");
else
holder.basicEggs.setText("Nie");
holder.dateEggs.setText(cages.get(position).getDateEggs());
holder.description.setText(cages.get(position).getDescription());
holder.pigeonsInside.setText(cages.get(position).getPigeonsInside());
holder.numberCageTitle.setHeight(holder.numberCage.getHeight());
holder.eggsDescriptionTitle.setHeight(holder.eggsDescription.getHeight());
holder.eggsFromTitle.setHeight(holder.eggsFrom.getHeight());
holder.basicEggsTitle.setHeight(holder.basicEggs.getHeight());
holder.dateEggsTitle.setHeight(holder.dateEggs.getHeight());
holder.descriptionTitle.setHeight(holder.description.getHeight());
holder.pigeonsInsideTitle.setHeight(holder.pigeonsInside.getHeight());
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView numberCage,pigeonsInside,description, dateEggs, basicEggs, eggsFrom, eggsDescription, numberCageTitle, pigeonsInsideTitle, descriptionTitle, dateEggsTitle, basicEggsTitle, eggsFromTitle, eggsDescriptionTitle;
LinearLayout linearLayout, linearLayoutLeft, linearLayoutRight;
public MyViewHolder(View view){
super(view);
numberCage = view.findViewById(R.id.numberCage);
pigeonsInside = view.findViewById(R.id.pigeonsInside);
description = view.findViewById(R.id.description);
dateEggs = view.findViewById(R.id.dateEggs);
basicEggs = view.findViewById(R.id.basicEggs);
eggsFrom = view.findViewById(R.id.eggsFrom);
eggsDescription = view.findViewById(R.id.eggsDescription);
numberCageTitle = view.findViewById(R.id.numberCageTitle);
pigeonsInsideTitle = view.findViewById(R.id.pigeonsInsideTitle);
descriptionTitle = view.findViewById(R.id.descriptionTitle);
dateEggsTitle = view.findViewById(R.id.dateEggsTitle);
basicEggsTitle = view.findViewById(R.id.basicEggsTitle);
eggsFromTitle = view.findViewById(R.id.eggsFromTitle);
eggsDescriptionTitle = view.findViewById(R.id.eggsDescriptionTitle);
linearLayout = view.findViewById(R.id.linearLayout);
}
}
I have good results but my problem is that I have to scroll down and scroll up my list to "load" this heights, I don't know how to tell this so I show you screenshot.
left table is not showing
After scroll down: left table after scroll
This is my content xml:
<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="wrap_content"
android:baselineAligned="true"
android:weightSum="2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".CageList"
tools:showIn="#layout/activity_cage_list"
android:divider="#android:color/black"
android:dividerPadding="1dip"
android:showDividers="end"
android:id="#+id/linearLayout"
>
<LinearLayout
android:id="#+id/linearLayoutLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<TextView
android:id="#+id/numberCageTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Numer klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/pigeonsInsideTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gołębie w klatce:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/descriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/dateEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data złożenia jaj:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/basicEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka macierzyste?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsFromTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsDescriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis do jajek:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#color/colorPrimary"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.75"
android:orientation="vertical">
<TextView
android:id="#+id/numberCage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/pigeonsInside"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dateEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/basicEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/eggsFrom"
android:layout_width="match_parent"
android:layout_height="35.65dp" />
<TextView
android:id="#+id/eggsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
create another file named sub_layout.xml which will have below code :-
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="#+id/txtTitleStatic"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.45"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".55" />
</LinearLayout>
and then use below code to include in main layout (You Content XML File):-
<include
android:id="#+id/numberCageLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/pigeonsInsideLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/descriptionLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Now in your Java File get their definition like :-
View numberCageLayout = findViewById(R.id.numberCageLayout);
View pigeonsInsideLayout = findViewById(R.id.pigeonsInsideLayout);
View descriptionLayout = findViewById(R.id.descriptionLayout);
TextView numberCageStatic =numberCageLayout.findViewById(R.id.txtTitleStatic);
TextView numberCage =numberCageLayout.findViewById(R.id.txtTitle);
TextView pigeonsInsideStatic =pigeonsInsideLayout.findViewById(R.id.txtTitleStatic);
TextView pigeonsInside =pigeonsInsideLayout.findViewById(R.id.txtTitle);
Now like this you don't have to worry about cell height.
I'm trying to inflate a list of views in a LinarLayout directly in the onCreate() method from the activity. However, I'm having a problem somewhere in the code but I really can't see it, I'm turning around this problem for two days, I'm going crazy !
The problem is that when I run the activity, the linear layout is completely empty, there is only the title.
Here is the important part of the code, KeyItems activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyitems_layout);
initializer();
ll = (LinearLayout) findViewById(R.id.ll);
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < list.size(); i++) {
View view = inflater.inflate(R.layout.keyitem_temp, ll, false);
final TextView ind = (TextView) view.findViewById(R.id.ind);
final TextView item = (TextView) view.findViewById(R.id.item);
ind.setText(list.get(i).get("id"));
item.setText(list.get(i).get("item"));
ll.addView(view);
}
}
Here is the keyitems_layout :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
And finally, my keyitem_temp.xml which I'm inflating :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>
I have an other activity called ItemsActivity which is the same code with some little differences and it works. Any suggestion? Thanks in advance.
Try using wrap_content for all of your heights except the initial ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
and:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>