Marcela Flores Marcela Flores - 5 months ago 21
Android Question

Wrap content not working

**item_my_message.xml**

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:background="@drawable/my_message_style"
android:elevation="4dp"
android:padding="8dp">

<TextView
android:id="@+id/textView_mymessge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textView_time"
android:text="asdasd"
android:textColor="@color/colorPrimaryBackgorund" />

<TextView
android:id="@+id/textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView_mymessge"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:text="12:00 pm"
android:paddingRight="6dp"
android:textColor="@color/colorHint"
android:textSize="12dp" />
</RelativeLayout>
</RelativeLayout>


package pruebas.integra.Activites;

import android.content.Context;
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.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import pruebas.integra.Adapters.MensajeAdapter;
import pruebas.integra.Models.ConversationModel;
import pruebas.integra.R;

public class ChatConversationActivity extends AppCompatActivity {
Context context;
TextView textViewNombre;
TextView textViewEstado;
ImageView imgEstado;
EditText mensaje;
MensajeAdapter mensajeAdapter;
Button btnEnviarMensaje;
ArrayList<ConversationModel> mensajeChat;
ListView listView;
Calendar c;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_conversation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
context = this;
c = Calendar.getInstance();

imgEstado = (ImageView) findViewById(R.id.imageView_state);
String nombre = getIntent().getExtras().getString("value");
Boolean state = getIntent().getExtras().getBoolean("estado");
textViewEstado = (TextView) findViewById(R.id.textView_state);
if (state == true) {
textViewEstado.setText("Conectado");
textViewEstado.setTextColor(context.getResources().getColor(android.R.color.holo_green_light));
imgEstado.setColorFilter((context.getResources().getColor(android.R.color.holo_green_light)));

} else {
textViewEstado.setText("Desconectado");
textViewEstado.setTextColor(context.getResources().getColor(R.color.colorPrimaryBackgorund));
imgEstado.setColorFilter(R.color.colorPrimaryBackgorund);
}

final TimeZone tz = TimeZone.getDefault();
textViewNombre = (TextView) findViewById(R.id.txtchatNombre);
textViewNombre.setText(nombre);

btnEnviarMensaje = (Button) findViewById(R.id.btn_enviarmensaje);
mensajeChat = new ArrayList<ConversationModel>();
listView = (ListView) findViewById(R.id.listView_mensajes_conversacion);
mensaje = (EditText) findViewById(R.id.editText_mensaje);

mensajeAdapter = new MensajeAdapter(context, mensajeChat);
listView.setAdapter(mensajeAdapter);

btnEnviarMensaje.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mensaje.getText().toString() == null && mensaje.getText().toString() == " ") {

} else {
ConversationModel conversation = new ConversationModel();
//mensaje.getText().append("\ud83d\ude01"); //EMOJI
conversation.setMensaje(mensaje.getText().toString());
String temp = Integer.toString(c.get(Calendar.HOUR_OF_DAY)) + ":" + Integer.toString(c.get(Calendar.MINUTE));
conversation.setTime(temp);
mensajeChat.add(conversation);
mensajeAdapter.notifyDataSetChanged();
mensaje.setText("");
listView.smoothScrollToPosition(mensajeAdapter.getCount());
}
}
});

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
finish();
}


}

I've got an item which I'm using for a chat, the thing is that the wrap content is not working, the layout itself stays at a certain width not according to its content I don't seem to find why is it doing that.

This is how it looks, it is supposed to wrap the content

Answer

I did not fully understand what are you asking But I think The problem is for first textView You have

android:layout_alignParentLeft="true"

And for the second one

android:layout_alignParentRight="true"

for the second textView do not use align parent property But use

android:layout_toRightOf="@+id/textView_mymessge"

if you want time to the right of message OR use

android:layout_below="@+id/textView_mymessge"

if you want time below to the message.