Reputation: 3274
I have an strange behavior of TextView it beats me how can I figure out a solution to this issue. I have a layout
<?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:gravity="start"
android:orientation="horizontal"
android:visibility="visible">
<View
android:id="@+id/them_avatar_spacer"
android:layout_width="@dimen/avatar_size"
android:layout_height="0.0dip"
android:visibility="gone"/>
<org.slabs.fc.chatstarter.ui.CircularImageView
android:id="@+id/them_avatar"
android:layout_width="@dimen/avatar_size"
android:layout_height="@dimen/avatar_size"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:src="@drawable/default_avatar"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4.0dip"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/chat_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2.0dip"
android:layout_marginLeft="6.0dip"
android:layout_marginTop="6dp"
android:includeFontPadding="false"
android:text="Title"
android:textColor="@color/black_light"
android:textSize="@dimen/chat_name_fontsize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal">
<TextView
android:id="@+id/chat_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="63.0dip"
android:autoLink="all"
android:background="@drawable/selectable_balloon_left"
android:clickable="true"
android:lineSpacingExtra="@dimen/text_body_line_spacing"
android:linksClickable="true"
android:text="Every message text comes here"
android:textColor="@color/black_heavy"
android:textIsSelectable="false"
android:textSize="@dimen/chat_text_msg_fontsize"
android:visibility="visible"/>
<TextView
android:id="@+id/chat_time"
android:layout_width="60.0dip"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginLeft="-60.0dip"
android:text="12:15"
android:textColor="@color/black_lightest"
android:textSize="@dimen/chat_timestamp_fontsize"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It gives me desired layout in a Design option of XML editor, as shown
But when I run the App the TextView with id chat_text
is not showing and I get a result like below,
Then I tried to create a custom TextView I simply created one as
public class ChatThemTextView extends TextView {
public ChatThemTextView(Context context) {
super(context);
}
public ChatThemTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ChatThemTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
ViewGroup.MarginLayoutParams margins = ViewGroup.MarginLayoutParams.class.cast(getLayoutParams());
int margin = 5;
margins.topMargin = margin;
margins.bottomMargin = margin;
margins.leftMargin = margin;
margins.rightMargin = margin;
setLayoutParams(margins);
}
}
At this point other issue came out which is when I add the first message I don't get anything but when I add the second message I get the background but TextView is still not showing as shown here
You all see what is expected how the output comes, any help is appreciated...
ViewHolder is just simple
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
adapter class goes here
public class ChatMessageAdapter extends RecyclerView.Adapter<ViewHolder> {
private List<ChatMessage> mMessageList;
private Context mContext;
private boolean isMe = false;
public ChatMessageAdapter(Context mContext, List<ChatMessage> mMessageList){
this.mContext = mContext;
this.mMessageList = mMessageList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = View.inflate(mContext, R.layout.chat_them_container, null);
return new ThemMessageHolder(rootView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ThemMessageHolder messageHolder = (ThemMessageHolder) holder;
messageHolder.mMessageTextView.setText(mMessageList.get(position).getMessage());
messageHolder.mSentAtTextView.setText(mMessageList.get(position).getSentAt());
Log.e("MSG_TEXT", mMessageList.get(position).getMessage());
}
@Override
public int getItemCount() {
return mMessageList.size();
}
public void clearData() {
mMessageList.clear(); //clear list
this.notifyDataSetChanged();
}
}
I get the Sting value at
Log.e("MSG_TEXT", mMessageList.get(position).getMessage());
but TextView is not showing that String...
.gitignore is
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
and settings.gradle is just
include ':app'
Upvotes: 4
Views: 1383
Reputation: 7761
You may change:
View rootView = View.inflate(mContext, R.layout.chat_them_container, null);
To:
View rootView = LayoutInflater.from(mContext).inflate(R.layout.chat_them_container, parent, false);
It will respect the LayoutParams
of its parent, and hence fix your issue.
Upvotes: 4