Das Design ist zu 80% an WhatsApp orientiert. Das kannst du nicht leugnen.
Und ja, man kann bei WhatsApp die Kontaktbilder rund machen.
Es ist alles mein Code, nichts ist von WhatsApp.
Snippet
Java
package de.azcaq.ayca;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import de.azcaq.ayca.R;
import de.azcaq.ayca.adapters.ChatArrayAdapter;
import de.azcaq.ayca.lists.ChatMessage;
import de.azcaq.ayca.sql.SqlChatController;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
public class ChatActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_CROP_IMAGE = 2;
private ChatArrayAdapter chatArrayAdapter;
private ListView listView;
private EditText chatText;
private Button buttonSend;
private ImageView chatSmile;
private ImageView chatBackground;
private Bundle b;
Intent intent;
SqlChatController controller;
Session session;
Drawable profilepicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
session = MainActivity.session;
if(!session.isLoggedIn()) {
session.logoutUser(this);
}
controller = new SqlChatController(this);
b = getIntent().getExtras();
getActionBar().setTitle(b.getString("Friendpublicname"));
getActionBar().setSubtitle(b.getString("Friendonline") + " (@" + b.getString("Friendusername") + ")");
getActionBar().setDisplayHomeAsUpEnabled(true);
profilepicture = ChatsActivity.chatsArrayAdapter.getItem(b.getInt("Friendid")).profilepicture;
getActionBar().setIcon(profilepicture);
buttonSend = (Button) findViewById(R.id.ButtonSend);
listView = (ListView) findViewById(R.id.ListChat);
chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), R.layout.activity_chat_singlemessage);
listView.setAdapter(chatArrayAdapter);
listView.setDivider(null);
chatBackground = (ImageView) findViewById(R.id.ChattingBackground);
chatSmile = (ImageView) findViewById(R.id.Smyle);
chatSmile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
chatSmile.setBackgroundResource(R.drawable.smileyon);
}
});
chatBackground.setBackground(ColorDrawable.createFromPath("#E8E8E8"));
chatText = (EditText) findViewById(R.id.TextChat);
Typeface font = Typeface.createFromAsset(this.getAssets(), "Ubuntu.ttf");
chatText.setTypeface(font);
chatText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
return sendChatMessage();
}
return false;
}
});
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendChatMessage();
}
});
chatArrayAdapter.add(new ChatMessage(true, getString(R.string.youwritingwith) + " " + b.getString("Friendpublicname") + "."));
ArrayList<Map<Boolean, String>> chats = controller.getAllChats(session.username, b.getString("Friendusername"));
for(int i = 0; i < chats.size(); i++) {
for (Map.Entry<Boolean, String> entry : chats.get(i).entrySet()) {
Boolean left = entry.getKey();
String message = entry.getValue();
chatArrayAdapter.add(new ChatMessage(left, message));
}
}
listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
listView.setAdapter(chatArrayAdapter);
chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(chatArrayAdapter.getCount() - 1);
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
});
listView.smoothScrollToPosition(chatArrayAdapter.getCount() - 1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuchat, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_CROP_IMAGE && resultCode == RESULT_OK && null != data) {
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
chatBackground.setImageBitmap(selectedBitmap);
}
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
File f = new File(picturePath);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
cropIntent.putExtra("crop", "true");
//cropIntent.putExtra("scale", true);
cropIntent.putExtra("aspectX", 0);
cropIntent.putExtra("aspectY", 0);
cropIntent.putExtra("outputX", 300);
cropIntent.putExtra("outputY", 300);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, RESULT_CROP_IMAGE);
} catch (ActivityNotFoundException anfe) {
BitmapDrawable ob = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(picturePath));
chatBackground.setBackground(ob);
}
cursor.close();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.MenuBackground:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
return true;
case R.id.MenuBackgroundRemove:
chatBackground.setBackground(ColorDrawable.createFromPath("#E8E8E8"));
return true;
case R.id.MenuContact:
Intent intent = new Intent(this, ContactActivity.class);
Bundle bd = new Bundle();
bd.putInt("Id", b.getInt("Friendid"));
bd.putString("Username", b.getString("Friendusername"));
bd.putString("Publicname", b.getString("Friendpublicname"));
bd.putString("Status", b.getString("Friendstatus"));
intent.putExtras(bd);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean sendChatMessage(){
if(chatText.getText().length() > 0) {
chatArrayAdapter.add(new ChatMessage(false, chatText.getText().toString()));
controller.insertChatMessage(session.username, b.getString("Friendusername"), chatText.getText().toString());
listView.smoothScrollToPosition(chatArrayAdapter.getCount() - 1);
chatText.setText("");
}
return true;
}
}
Alles anzeigen