Android Studio ListView items gravity - java

I want to have my items in listview looking like a conversation in messenger.
There is simple code of recieving messages:
if (arrayList2.get(y).toString().equals(myEmail)) {
arrayList.add(arrayList2.get(k).toString());
} else if (arrayList2.get(y).toString().equals(recipientEmail)) {
arrayList.add(arrayList2.get(k).toString());
}
arrayAdapter.notifyDataSetChanged();
When array.get.....equals(myEmail), the messages are from me so, they should stay like they are. When ...equals(recipientEmail), I want text to be on the right of the screen/or listview and have an background of image, like classically in messenger

I will recommend you to add all Your messages in one Arraylist and create two layouts one of sender with left gravity and one of receiver with right gravity then make a logic like-
if(arraylist.get(position).getUserID==currentUser){
senderLayout.setvisibility(View.VISIBLE);
senderTextview.setText(arraylist.get(position).getText)
}
else{
senderLayout.setvisibility(View.GONE);
receiverLayout.setvisibility(View.VISIBLE);
recieverTextview.setText(arraylist.get(position).getText)
}
Do this in getView method of your adapter.

You may create a row with 2 views. One on left and other on right. Based on your if condition you can make one of the views gone and the other visible. At any time only one view in that will be visible.
Another way of doing is to use single view in it and setting (left/right) gravity based on your condition.
You may refer http://www.uandblog.com/How-to-Create-Chat-Application-using-Firebase-in-Android-. See MainActivity.java and item.xml

Use two TextView in raw file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<com.github.library.bubbleview.BubbleTextVew
android:id="#+id/tv_chatRecive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="35dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:textColor="#android:color/white"
android:visibility="gone"
app:angle="5dp"
app:arrowHeight="12dp"
app:arrowLocation="left"
app:arrowPosition="5dp"
app:arrowWidth="12dp"
app:bubbleColor="#color/colorPrimary" />
<com.github.library.bubbleview.BubbleTextVew
android:id="#+id/tv_chatSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="7dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:textColor="#color/text_labelColoe"
android:visibility="gone"
app:angle="5dp"
app:arrowHeight="12dp"
app:arrowLocation="right"
app:arrowPosition="5dp"
app:arrowWidth="12dp"
app:bubbleColor="#android:color/white" />
</RelativeLayout>
and in Adapter class use this kind of logic to manage textview
#Override
public void onBindViewHolder(final CustomViewHolder customViewHolder, final int position) {
if (listChat.get(position).CHAT_SEND_RECIVE.equalsIgnoreCase("right")) {
customViewHolder.tvSend.setVisibility(View.VISIBLE);
customViewHolder.tvRecive.setVisibility(View.GONE);
customViewHolder.tvSend.setText(listChat.get(position).CHAT_MESSAGE);
} else if (listChat.get(position).CHAT_SEND_RECIVE.equalsIgnoreCase("left")) {
customViewHolder.tvRecive.setVisibility(View.VISIBLE);
customViewHolder.tvSend.setVisibility(View.GONE);
customViewHolder.tvRecive.setText(listChat.get(position).CHAT_MESSAGE);
}
}
Add key in chat responce like just i add right and left

Here is complete example.
Resources under drawable:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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: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="com.example.andrii.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/chat"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Message layout will looks like so (message.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:background="#drawable/in_9">
<TextView
android:id="#+id/message"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:breakStrategy="balanced"
android:maxWidth="200dp"
android:text="test"
/>
</FrameLayout>
</LinearLayout>
And finally in MainActivity.java :
package com.example.andrii.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Message> list= new LinkedList<>();
list.add(new Message(true, "hi"));
list.add(new Message(false, "hello"));
list.add(new Message(true, "how are you"));
list.add(new Message(false, "I'm fine"));
list.add(new Message(false, "Test test test test test"));
RecyclerView view = (RecyclerView) findViewById(R.id.chat);
view.setLayoutManager(new LinearLayoutManager(this));
view.setAdapter(new ChatAdapter(this, list));
}
private class Message {
private boolean misMine;
private String mMessage;
public Message (boolean mine, String message) {
misMine = mine;
mMessage = message;
}
public boolean isMine() { return misMine; }
public String getMessage() { return mMessage; }
}
private class MessageViewHolder extends RecyclerView.ViewHolder {
private View mContainer;
private TextView mMessage;
public MessageViewHolder(View itemView) {
super(itemView);
mMessage = (TextView) itemView.findViewById(R.id.message);
mContainer = itemView.findViewById(R.id.container);
}
public void setMessage(String message) {
mMessage.setText(message);
}
public void setMine(boolean isMine) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mContainer.getLayoutParams();
params.gravity = isMine ? Gravity.RIGHT : Gravity.LEFT;
mContainer.setBackgroundResource(isMine ? R.drawable.out_9 : R.drawable.in_9 );
mContainer.setLayoutParams(params);
}
}
private class ChatAdapter extends RecyclerView.Adapter<MessageViewHolder> {
private List<Message> mMessages;
public ChatAdapter(Context context, List<Message> list) {
mMessages = list;
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message, parent, false);
MessageViewHolder holder = new MessageViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
Message message = mMessages.get(position);
holder.setMessage(message.getMessage());
holder.setMine(message.isMine());
}
#Override
public int getItemCount() {
return mMessages.size();
}
}
}
The result of code above:

Related

ListView only shows the first item from the arraylist

Previously I had crash issues due to the wrong reference to the resource files. Fixed that issue and updated this thread with the logical error that I am getting.
I am new to android and currently learning custom classes and adapter. While working I am facing a problem which is the listview shows the first arraylist item only.
I have attached the codes of the required files as well.
Working Activity
package np.com.shresthakiran.tourswoniga;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class KhowpaActivity extends AppCompatActivity {
ListView lvHeritageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_heritage);
lvHeritageList = findViewById(R.id.lvHeritage);
ArrayList<Heritages> heritageAryList = new ArrayList<>();
heritageAryList.add(new Heritages(R.drawable.ic_launcher_background,"Ngatapol", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Dattatreya", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Lu dhwakha", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "55 jhyale Durbar", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Taleju Bhawani", "Lyaaku"));
HeritageAdapter heritageAdapter = new HeritageAdapter(KhowpaActivity.this, R.layout.heritages_row, heritageAryList);
lvHeritageList.setAdapter(heritageAdapter);
}
}
Custom Adapter
package np.com.shresthakiran.tourswoniga;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class HeritageAdapter extends ArrayAdapter<Heritages> {
private Context mContext;
private int mResource;
public HeritageAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Heritages> objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater =LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
ImageView ivHeritageImage = convertView.findViewById(R.id.ivHeritage);
TextView tvHeritageName = convertView.findViewById(R.id.tvHeritageName);
TextView tvHeritageAddress = convertView.findViewById(R.id.tvHeritageAddress);
ivHeritageImage.setImageResource(getItem(position).getmImageResourceId());
tvHeritageName.setText(getItem(position).getmHeritageName());
tvHeritageAddress.setText(getItem(position).getmHeritageAddress());
return convertView;
}
}
Object Class
package np.com.shresthakiran.tourswoniga;
public class Heritages {
private int mImageResourceId;
private String mHeritageName;
private String mHeritageAddress;
public Heritages(int heritageImageResourceId, String heritageName, String heritageAddress) {
this.mImageResourceId = heritageImageResourceId;
this.mHeritageName = heritageName;
this.mHeritageAddress = heritageAddress;
}
public int getmImageResourceId() {
return mImageResourceId;
}
public String getmHeritageName() {
return mHeritageName;
}
public String getmHeritageAddress() {
return mHeritageAddress;
}
}
ListView XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="100dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lvHeritage">
</ListView>
</RelativeLayout>
List Row XML
<?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"
android:orientation="horizontal"
android:weightSum="100"
android:layout_margin="10dp">
<ImageView
android:id="#+id/ivHeritage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="33.33"
android:padding="2dp"
android:text="Ngatapol"
android:layout_marginTop="7dp"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:id="#+id/llHeritageInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="66.66"
android:padding="8dp" >
<TextView
android:id="#+id/tvHeritageName"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:text="Ngatapol"
android:textAppearance="?android:textAppearanceMedium"
android:padding="2dp"/>
<TextView
android:id="#+id/tvHeritageAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:text="Taumadi"
android:padding="2dp"/>
</LinearLayout>
</LinearLayout>
listview shows the first item only is because you have set height in heritages_row layout to match_parent which will cover the whole screen height and for the next item, you've to scroll down even if the content of the first item is not covering the whole height.
To make each row to only cover the content its displaying, use wrap_content instead of match_parent.

Custom Dialog with RecyclerView doesn't show OK Cancel buttons

I have a simple custom multi select items dialog. If number of items is small, then the dialog works fine and shows OK and Cancel buttons at the bottom. But if there are many items (so you have to scroll the list) - no buttons are shown. I've searched the SO for my problem with no luck. I've tested my dialog on Android API 27..29 - it's the same. Maybe I'm missing something important in the layout properties etc...
Here is my code and xml:
package ru.vitvlkv.myapp.gui;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import java.util.Set;
import ru.vitvlkv.myapp.R;
import ru.vitvlkv.myapp.playlists.Exercise;
public class SelectExercisesDialog extends DialogFragment {
private static final String TAG = SelectExercisesDialog.class.getSimpleName();
private final String message;
private final List<Exercise> exercises;
private RecyclerView recyclerView;
private final SelectExercisesDialog.OnOKClickListener onOKButtonClick;
private final Set<String> selectedExercisesIds;
public interface OnOKClickListener {
void onClick(Set<String> exerciseIds);
}
public SelectExercisesDialog(String message, List<Exercise> exercises, Set<String> selectedExercisesIds,
final SelectExercisesDialog.OnOKClickListener onButtonOKClick) {
this.message = message;
this.exercises = exercises;
this.selectedExercisesIds = selectedExercisesIds;
this.onOKButtonClick = onButtonOKClick;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.select_exercises_dialog, null);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new AllExercisesAdapter(exercises, selectedExercisesIds));
setRetainInstance(true);
return new AlertDialog.Builder(getActivity())
.setView(view)
.setMessage(message)
.setPositiveButton(R.string.dialog_button_ok, (DialogInterface dialog, int id) -> {
onOKButtonClick.onClick(selectedExercisesIds);
})
.setNegativeButton(R.string.dialog_button_cancel, (DialogInterface dialog, int id) -> {
})
.create();
}
private static class AllExercisesAdapter extends RecyclerView.Adapter<ExerciseViewHolder> {
private final List<Exercise> exercises;
private final Set<String> checkedExercisesIds;
public AllExercisesAdapter(List<Exercise> exercises, Set<String> checkedExercisesIds) {
this.exercises = exercises;
this.checkedExercisesIds = checkedExercisesIds;
}
#NonNull
#Override
public ExerciseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.select_exercise_item_view, parent, false);
return new ExerciseViewHolder(view, checkedExercisesIds);
}
#Override
public void onBindViewHolder(#NonNull ExerciseViewHolder holder, int position) {
Exercise exercise = exercises.get(position);
holder.setExercise(exercise);
}
#Override
public int getItemCount() {
return exercises.size();
}
}
private static class ExerciseViewHolder extends RecyclerView.ViewHolder {
private Exercise exercise = null;
private final TextView textView;
public final CheckBox checkBox;
private final Set<String> checkedExercisesIds;
public ExerciseViewHolder(#NonNull View view, Set<String> checkedExercisesIds) {
super(view);
this.checkedExercisesIds = checkedExercisesIds;
textView = view.findViewById(R.id.textView);
checkBox = view.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
if (isChecked) {
checkedExercisesIds.add(exercise.getId());
} else {
checkedExercisesIds.remove(exercise.getId());
}
});
}
public void setExercise(Exercise exercise) {
this.exercise = exercise;
textView.setText(exercise.getName());
checkBox.setChecked(checkedExercisesIds.contains(exercise.getId()));
}
}
}
layout/select_exercises_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
layout/select_exercise_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#dimen/list_item_height"
android:layout_marginLeft="#dimen/margin_medium"
android:layout_marginRight="#dimen/margin_medium"
android:gravity="center_vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:text="MyExercise"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
And the Exercise class is just a POJO:
package ru.vitvlkv.myapp.playlists;
public class Exercise {
private final String id;
private final String name;
private Exercise(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
How can I fix this and make the dialog to show OK Cancel buttons in all the cases?
Any help is appreciated.
Thank you!
P.S. The screenshot of the dialog:
If you are Using dialog fragment It's fragment in style of dialog with the some of the dialog specs now if you want to add the ok and cancel you have two chooses
extend the class from AlertDialog.Builder and on create add the positive and the negative button handler and the text
example about the code will be look like this
class ExampleDialog(context: Context) : AlertDialog.Builder(context) {
private val view by lazy {
LayoutInflater.from(context).inflate(R.layout.dialog_exmaple, null, false)
}
override fun setView(layoutResId: Int): AlertDialog.Builder {
// do the recylceview init from the view code here
return super.setView(view)
}
override fun setPositiveButton(
text: CharSequence?,
listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
return super.setPositiveButton(
context.getText(R.string.action_ok)
) { p0, p1 ->
}
}
override fun setNegativeButton(
text: CharSequence?,
listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
return super.setNegativeButton(
context.getText(R.string.action_cancel)
) { p0, p1 ->
}
}
}
note I don't like this way allot
you can just add inside you'r xml two button's to the bottom of the recycle-view and add the text to them like ok and cancel and handle the onClick by high order function or by interface it's up to you I can show you and example below
I prefer this code more clean for me and add click listener in the dialogFragment class
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listData"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/actionOk"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="#+id/actionOk"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="#string/action_ok"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/listData" />
<com.google.android.material.button.MaterialButton
android:id="#+id/actionCancel"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="#string/action_cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/actionOk"
app:layout_constraintTop_toBottomOf="#id/listData" />
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
val root = ConstraintLayout(activity)
root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(root)
dialog.window?.let {
it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
it.setWindowAnimations(R.style.DialogAnimationNormal)
}
dialog.setCanceledOnTouchOutside(true)
return dialog
}
I improvised around the solution suggested by #radesh and came to this (still it's not exactly what I need):
<?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"
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="150dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This layout at least displays the OK Cancel buttons and the list with items. But still in this case my dialog doesn't want to occupy all the available space of the device screen. The list recyclerView is very limited (it's only about 150dp in height). Of course I want the dialog to occupy as much space as available.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight= "200dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content"
android:layout_marginLeft="#dimen/margin_medium"
android:layout_marginRight="#dimen/margin_medium"
android:gravity="center_vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:text="MyExercise"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

make same fab Transformation exapnd behavior like shown at material.io

I wanna have same fab transformation behavior as shown in
Material Website in Full Screen (or morph) section.
I have tried default FabTransformationScrimBehavior and FabTransformationSheetBehavior. but scrim behavior doesn't do anything and sheet behavior don't animate just like I wanted and also it reduce the elevation of the fab to value 0f. and I haven't found any way to unexpand fab. and I wanna know how to use fab transformation behavior. Actually I didn't find any doc on how to use it. So Please help
Code
This is the code that I used:
this is the code of first activity:-
final FloatingActionButton actionButton = findViewById(R.id.add_fab);
actionButton.setOnClickListener(view -> {
actionButton.setExpanded(!actionButton.isExpanded());
});
And this is the xml of first activity:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/include_generic_toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="76dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
tools:listitem="#layout/media_list_item_card" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:includeFontPadding="false"
android:text="Add Media"
android:textAllCaps="false"
android:visibility="visible"
app:srcCompat="#drawable/ic_add_24"/>
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior"
>
<fragment
android:id="#+id/fab_transformation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.zimong.ssms.fragments.AddMediaFragment"
/>
</com.google.android.material.circularreveal.CircularRevealFrameLayout>
And the code of AddMediaFragment(second screen):-
package com.zimong.ssms.fragments;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.zimong.ssms.R;
import com.zimong.ssms.common.model.User;
import com.zimong.ssms.extended.CallbackHandlerImpl;
import com.zimong.ssms.model.Source;
import com.zimong.ssms.model.ZResponse;
import com.zimong.ssms.service.AppService;
import com.zimong.ssms.service.ServiceLoader;
import com.zimong.ssms.util.Constants;
import com.zimong.ssms.util.Util;
import java.util.Calendar;
import java.util.Date;
import retrofit2.Call;
import retrofit2.Response;
public class AddMediaFragment extends DialogFragment {
private ArrayAdapter<Source> adapter;
private TextView source;
private int checkedItem = 0;
private TextView mediaDate;
private Date currentSelectedDate;
public static AddMediaFragment newInstance() {
AddMediaFragment fragment = new AddMediaFragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AppTheme_Light);
setHasOptionsMenu(true);
}
private void getSources() {
final User user = Util.getUser(getActivity());
AppService service = ServiceLoader.createService(AppService.class);
Call<ZResponse> call = service.mediaGallerySources(Constants.DEFAULT_PLATFORM, user.getToken());
call.enqueue(new CallbackHandlerImpl<Source[]>(getActivity(), true, true, Source[].class) {
#Override
protected void failure(Throwable t) {
}
#Override
protected void failure(Response<ZResponse> response) {
}
#Override
protected void success(Source[] response) {
if (response.length > 0) {
source.setText(response[0].getName());
}
adapter = new ArrayAdapter<Source>(getContext(), R.layout.dialog_singlechoice_item, response) {
#Override
public long getItemId(int position) {
return getItem(position).getPk();
}
};
}
});
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_media, container, false);
TextView headline = view.findViewById(R.id.media_headline);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getContext()).setSupportActionBar(toolbar);
source = view.findViewById(R.id.source);
mediaDate = view.findViewById(R.id.media_date);
Calendar fromDateInstance = Calendar.getInstance();
currentSelectedDate = fromDateInstance.getTime();
mediaDate.setText(Util.formatDate(fromDateInstance.getTime(), "dd MMM yyyy"));
final DatePickerDialog fromDatePicker = new DatePickerDialog(getContext(), (datePicker, year, month, date) -> {
fromDateInstance.set(Calendar.YEAR, year);
fromDateInstance.set(Calendar.MONTH, month);
fromDateInstance.set(Calendar.DATE, date);
currentSelectedDate = fromDateInstance.getTime();
mediaDate.setText(Util.formatDate(currentSelectedDate, "dd MMM yyyy"));
}, fromDateInstance.get(Calendar.YEAR), fromDateInstance.get(Calendar.MONTH), fromDateInstance.get(Calendar.DATE));
mediaDate.setOnClickListener(v -> fromDatePicker.show());
source.setOnClickListener(v -> alertDialog());
getSources();
return view;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void alertDialog() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
builder.setTitle("Source");
builder.setSingleChoiceItems(adapter, checkedItem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
source.setText(adapter.getItem(which).getName());
checkedItem = which;
dialog.dismiss();
}
});
builder.show();
}
}
And the xml of Add Media is:-
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.circularreveal.coordinatorlayout.CircularRevealCoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:background="#color/white"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/include_toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<include
layout="#layout/content_add_media"
android:layout_width="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
android:layout_height="match_parent"/>
</com.google.android.material.circularreveal.coordinatorlayout.CircularRevealCoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.circularreveal.CircularRevealLinearLayout
android:id="#+id/dial"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_gravity="bottom|center_horizontal"
android:visibility="invisible"
android:layout_marginBottom="16dp"
app:layout_anchor="#id/fab"
app:layout_anchorGravity="top|center_horizontal"
android:background="#color/colorPrimary"
app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior">
<ImageButton
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"/>
</com.google.android.material.circularreveal.CircularRevealLinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#color/colorPrimary"
app:srcCompat="#android:drawable/ic_dialog_email"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
For the FabTransformationSheetBehavior to work, you need to change the isExpanded value.
on change of this value, expand and collapse animation will be called and visibility of the linearlayout and fab button will be handled automatically. Don't change visibility manually as this would block transformation animation.
Use back button to transform back to FAB.
val fab: FloatingActionButton = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener { fab.isExpanded = !fab.isExpanded } // this sets isExpanded as true
val back = findViewById<ImageButton>(R.id.back)
back.setOnClickListener{ fab.isExpanded = !fab.isExpanded } // this sets isExpanded as false

Can't get RecyclerView to work [duplicate]

This question already has answers here:
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
After being told to use a RecyclerView instead of a ListView, I have tried to implement it along with a SearchView. However, I get a strange error about an object being null:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
I currently have the following code:
SearchActivity.java
package com.rickteuthof.strangejourneycompendium;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import java.util.ArrayList;
public class SearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
public static ArrayList<String> demonNames;
private SearchAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView r = findViewById(R.id.recyclerView);
demonNames = MainActivity.demonNames;
adapter = new SearchAdapter(this, demonNames);
r.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
r.setAdapter(adapter);
SearchView s = findViewById(R.id.searchBar);
s.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return false;
}
}
SearchAdapter.java
package com.rickteuthof.strangejourneycompendium;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {
private LayoutInflater inflater;
private ArrayList<String> demonNames;
private ArrayList<String> results;
public SearchAdapter(Context ctx, ArrayList<String> demonNames){
inflater = LayoutInflater.from(ctx);
this.demonNames = demonNames;
this.results = new ArrayList<String>();
this.results.addAll(SearchActivity.demonNames);
}
#NonNull
#Override
public SearchAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recycler_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SearchAdapter.MyViewHolder holder, int position) {
holder.time.setText(demonNames.get(position));
}
#Override
public int getItemCount() {
return demonNames.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView time;
public MyViewHolder(View itemView) {
super(itemView);
time = itemView.findViewById(R.id.result);
}
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
SearchActivity.demonNames.clear();
if (charText.length() == 0) {
SearchActivity.demonNames.addAll(results);
} else {
for (String wp : results) {
if (wp.toLowerCase(Locale.getDefault()).contains(charText)) {
SearchActivity.demonNames.add(wp);
}
}
}
notifyDataSetChanged();
}
}
My dataset is a String array named demonNames which I try to filter to get the correct results in the RecyclerView.
Here are my xml layouts:
activity_search.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"
tools:context=".SearchActivity">
<SearchView
android:id="#+id/searchBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchBar" >
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
recycler_item.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/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
setContentView(R.layout.activity_main);
Replace this with
setContentView(R.layout.activity_search);

Right and left align in a single ListView

I am setting a listView dynamically. I will have two things shown on each line that I will be receiving from parse.com. I want the first thing to be left aligned and blue, with the second thing right aligned and red. I will have two arrays, firstArray, and secondArray. I found lots of ways to do this, but none dynamically. How should I go about doing this, or I can do it in xml?
Updated
This is my xml after that tutorial posted by #t0s
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/column1"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#0000FF"
android:gravity="left"
/>
<TextView
android:id="#+id/center"
android:text="VS"
android:textSize="15sp"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="#+id/column2"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FF0000"
android:gravity="right"
/>
</LinearLayout>
and my java
ListView list = (ListView) findViewById(R.id.mylist);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.mylist, namesArray);
list.setAdapter(listAdapter);
//find my objects off parse, and call this method
private String display(Object name, Object record,
ArrayAdapter listAdapter) {
String fightCard = (name).toString();
namesArray.add((name).toString() +" " + (record).toString());
return fightCard;
}
Well my code is a mess.
Check the tutorial here is [very simple]
Ok Chad check here the code :
Main Activity :
package org.example.chad;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
public class MainActivity extends ListActivity {
private ArrayList<DataItem> data = new ArrayList<DataItem>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create Objects
DataItem obj1 = new DataItem("name1", "record1");
DataItem obj2 = new DataItem("name2", "record2");
DataItem obj3 = new DataItem("name3", "record3");
DataItem obj4 = new DataItem("name4", "record4");
DataItem obj5 = new DataItem("name5", "record5");
//add the to ArrayList
data.add(obj1);
data.add(obj2);
data.add(obj3);
data.add(obj4);
data.add(obj5);
CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data);
setListAdapter(adapter);
}
}
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"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
DataItem :
package org.example.chad;
public class DataItem {
private String name;
private String record;
public DataItem(){
}
public DataItem(String n, String r ){
this.name = n;
this.record = r;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getrecord() {
return record;
}
public void setrecord(String record) {
this.record = record;
}
}
CustomAdapter :
package org.example.chad;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<DataItem> {
private ArrayList<DataItem> objects;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row, null);
}
DataItem i = objects.get(position);
if (i != null) {
TextView nameView = (TextView) v.findViewById(R.id.name);
TextView recordView = (TextView) v.findViewById(R.id.record);
if (nameView != null){
nameView.setText(i.getname());
}
if (recordView != null){
recordView.setText(i.getrecord());
}
}
return v;
}
}
and row.xml :
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="100sp"
android:layout_height="20sp"
android:textSize="16sp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10sp"/>
<TextView
android:id="#+id/record"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_gravity="right"
android:textSize="16sp" />
</LinearLayout>
In a couple of hours I will add some comments.
The result is here
If im not mistaken you have to extend BaseAdapter class so you have a custom Adapter for your ListView, it is not possible to add two array items to a ListView without creating a custom adapter.
here is a tutorial.
is not as hard as it looks like. Hope it helps.

Categories

Resources