I try to programmatically add elements in a LinearLayout which contains depending on certain conditions different elements. But I absolutely don't know how to do it.
The objective I want to achieve is a friendList for a Quizz app in which it shows the friend request the user gets, the friends he already have. The activity also contains a search bar which allows the user to search amongst his friend and if the text entered in text bar doesn't correspond to any of his friend it shows the users who have similar username.
I've already tried a lot just trying to dynamically add text to the linearLayout and I've searched containers that I could add in my linearLayout instead of TextView but I don't see any that could fit my purpose.
I've searched a bit on the net and apparently there is the listView that could fit my purpose but I don't know how to implement it.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="@+id/textView2"
android:layout_width="312dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="@string/friendList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/friends_menu_button"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/friends_edit_text"
android:layout_width="275dp"
android:layout_height="38dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName|text"
android:text="@string/findFriend"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/friends_menu_button"
android:layout_width="wrap_content"
android:layout_height="53dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="menu"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView3"
android:layout_width="415dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/friends_edit_text">
<LinearLayout
android:id="@+id/friends_Linear_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:id="@+id/f_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
Here is my Activity :
package be.uclouvain.lsinf1225.groupej2a.iqtest;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class FriendsListActivity extends AppCompatActivity {
private Button menuButton;
private Button searchButton;
private DatabaseHelper myDb;
private LinearLayout friendLayout;
private User myUser;
private EditText searchbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
myDb = DatabaseHelper.getInstance(this);
myUser = myDb.getCurrentUser();
menuButton = (Button) findViewById(R.id.friends_menu_button);
friendLayout= (LinearLayout) findViewById(R.id.friends_Linear_Layout);
searchbar = findViewById(R.id.friends_edit_text);
searchButton = findViewById(R.id.f_search_button);
//showFriends();
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String search = searchbar.getText().toString();
clearFriends();
showFriends();
}
});
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent MainMenuActivity = new Intent(FriendsListActivity.this, MainMenuActivity.class);
startActivity(MainMenuActivity);
}
});
}
private void showFriends(){
LinearLayout myLayout = this.friendLayout;
DatabaseHelper db = this.myDb;
User myUser = this.myUser;
String myUsername = myUser.getUsername();
String friendList[] = db.getFriends(myUsername);
int j=0;
for(String i : friendList){
Log.d("showFriends",i);
TextView temp = new TextView(this.getApplicationContext());
temp.setText(i);
temp.setId(j);
myLayout.addView(temp);
j++;
}
}
private void clearFriends(){
LinearLayout myLayout = this.friendLayout;
DatabaseHelper db = this.myDb;
myLayout.removeAllViews();
}
}
the actual results is (I know there is only text but I don't know how to do it differently):
I would Like something like this:
Just give me the explanation of how it works and if possible some example no need to waste your time giving me the code doing exactly what I want.
You need to use multi view recyclerview. I can see from the UI that you're using 3 types of views (i.e Already Friend, Requested Friend, Other). You need to create 3 layouts and corresponding viewholders in your custom adapter.