currently i have:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15:22:33 \nApr 13 1999"
android:textColor="#FFFFFFFF"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="#drawable/incoming_message_buble"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/primary_text_light" />
and it work fine.
here is adapter code:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_sms_row_layout, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
OneComment coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.comment);
countryName.setBackgroundResource(coment.left ? R.drawable.incoming_message_buble : R.drawable.outgoing_message_buble);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
i have to change order, for gravity left, i need shown textview comment first, and textView1 second
for gravity right, keep xml setting.
please advise.
I suggest using multiple layouts rather than constantly reorganizing each row's layout. Override these two methods:
#Override
public int getItemViewType(int position) {
return getItem(position).left ? 0 : 1;
}
#Override
public int getViewTypeCount() {
return 2;
}
And modify your getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType == 0) {
// Use the left layout
} else {
// Use the right layout
}
}
Related
I'm trying to make an OnClick listener for an imagebutton, that is supposed to remove a bitmap (set it to null) on click. However, nothing happens when I press the button, except it does print my "Log.d()" message, and the bitmap IS actually null after the click, however changes doesn't show. The code is written inside my adapter. I even tried inserting it inside a runOnUIThread to see if that makes a difference, however there's no change.
I also tried setImageDrawable(null) and setImageResource(0) with no luck. I even tried changing other UI elements, and that doesn't work either. Only thing I can make it do is print the log message...
Here's my getView with my onClick method:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
//Set up viewholder
viewHolder = new ViewHolder();
viewHolder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
viewHolder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
viewHolder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);
viewHolder.productImageIv.setImageBitmap(images[position]);
checkForNullImages(viewHolder, position);
//Store the holder with the view
convertView.setTag(viewHolder);
viewHolder.removeImageIb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread (new Thread(new Runnable() {
public void run() {
images[position] = null;
viewHolder.productImageIv.setImageBitmap(images[position]);
notifyDataSetChanged();
//Does print this message, item is null, but image is still shown
Log.e("LogMSG", "item should be null " + getItem(position));
}
}));
}
});
}
return convertView;
}
And if relevant, here's my corresponding 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:id="#+id/android_custom_gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="10dp"
app:cardBackgroundColor="#color/white">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:cardCornerRadius="10dp">
<ImageView
android:id="#+id/android_gridview_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/gradient_background"
android:scaleType="centerCrop" />
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
<ImageButton
android:id="#+id/addImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:elevation="#dimen/standard_12"
android:background="#drawable/icon_add" />
<ImageButton
android:id="#+id/removeImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:background="#drawable/icon_remove"
android:elevation="#dimen/standard_12" />
This is because your row view recycling is incorrect. You're not recycling the view with the following code:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
// ...
}
return convertView;
}
because you're not using the previous convertView if it's not null.
So, change your code to something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
//Set up viewholder
holder = new ViewHolder();
holder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
holder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
holder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);
// set click listener here.
// don't use runOnUiThread for click listener.
// bind the view here.
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// set the value for view here
holder.productImageIv.setImageBitmap(images[position]);
return convertView;
}
Here's my data structure class:
public class FirstAidSteps implements Parcelable {
//These are the fields
private final String stepName;
private final ArrayList<String> steps;
.
.
.
}
Here's the adapter I've written:
public class StepsAdapter extends ArrayAdapter<FirstAidSteps> {
public StepsAdapter(Context context, ArrayList<FirstAidSteps> users) {
super(context, 0, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FirstAidSteps steps = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView stepName = (TextView) convertView.findViewById(R.id.step_heading);
stepName.setText(steps.getStepName());
return convertView;
}
}
Here's the XML for List Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
</LinearLayout>
I want to add the contents of ArrayList steps to the ListView. And I can't predefine the XML for steps because they can have any size.
I want the output something like below:
___________________
| StepName |
| Step |
| Step |
|-------------------|
| StepName |
| Step |
|-------------------|
| StepName |
|___________________|
So how to do this?
Assuming the number of steps is fairly small, I usually use a LinearLayout here, and add the necessary views.
To do this, you simply need to add a LinearLayout to your item view to contain the steps.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
<LinearLayout
android:id="#+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then you add steps to your item view via the adapter. Make sure you clear the previous steps in the view (also making sure you use the ViewHolder pattern).
public class StepsAdapter extends ArrayAdapter<FirstAidSteps> {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.stepName = (TextView) v.findViewById(R.id.step_heading);
holder.steps = (LinearLayout) v.findViewById(R.id.steps);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
final FirstAidSteps steps = getItem(position);
holder.stepName.setText(steps.getStepName());
holder.steps.removeAllViews();
for (String step : steps.getSteps()) {
holder.steps.addView(createStepView(step));
}
return v;
}
private static class ViewHolder {
TextView stepName;
LinearLayout steps;
}
}
In this case, createStepView() is an exercise for the reader.
Depending on your layout and possible number of steps, this may not be the best solution for you. If you will potentially have a lot of steps you may want to look into some sort of recycler view, or other recycling container.
cahnge your xml file to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
<LinearLayout
android:id="#+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
change steps to this
private final ArrayList<ArrayList<String>> steps;
private final ArrayList<String> stepsName;
and in adapter
public StepsAdapter(Context context, ArrayList<ArrayList<String>> steps, ArrayList<String> stepsName) {
super(context, 0, steps, stepsName);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView stepName = (TextView) convertView.findViewById(R.id.step_heading);
LinearLayout mLinearLayout = (LinearLayout) v.findViewById(R.id.steps);
stepName.setText(stepsName.get(position));
mLinearLayout.removeAllViews();
ArrayList<String> single_step= steps.get(position);
if(single_step.size() > 0){
for (int size =0; size <single_step.size(); size++) {
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
text.setGravity(Gravity.CENTER);
text.setPadding(5, 2, 5, 2);
text.setText(single_step.get(size));
mLinearLayout.addView(text);
}
}
return convertView;
}
I've never worked with GridView on Android. I try to inflate my layout in a GridView, and i want to have different background for every layout. I can't set background color for each layout separately, everytime i get gray color for all layouts.
My XML code :
<RelativeLayout
android:layout_width="200dp"
android:layout_height="150dp"
android:id="#+id/back">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_marginTop="10dp"
android:src="#drawable/splashscreen"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Science"
android:textSize="15sp"
android:layout_marginTop="10dp"
android:id="#+id/categoryTextView"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Grid adapter :
public class CellAdapter extends BaseAdapter {
private Context context;
private LinkedList<CellGrid> list;
public CellAdapter(Context context, LinkedList<CellGrid> list){
this.context = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(
R.layout.grid_cell_layout, null);
}
convertView.setBackgroundColor(list.get(position).getBackground());
TextView categoryTextView = (TextView)convertView.findViewById(R.id.categoryTextView);
ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
categoryTextView.setText(list.get(position).getText());
imageView.setImageResource(list.get(position).getImageId());
RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
return convertView;
}
}
If any specific color is not satisfied then you can set random bg to every view as :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// generate the random integers for r, g and b value
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int randomColor = Color.rgb(r,g,b);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(
R.layout.grid_cell_layout, null);
convertView.setBackgroundColor(randomColor);
}
Please try following code it helpful for you.
Replace your adapter code with following code
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(
R.layout.grid_cell_layout, null);
}
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
convertView.setBackgroundColor(color);
TextView categoryTextView = (TextView)convertView.findViewById(R.id.categoryTextView);
ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
categoryTextView.setText(list.get(position).getText());
imageView.setImageResource(list.get(position).getImageId());
RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
return convertView;
}
}
Feel free for comment if you have any issue
Use resources().getColor() to get color from list and set it as background
relativelayout.setBackgroundColor(getResources().getColor(list.get(position).getBackground()));
Here is my sample code..
**<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:background="#color/dark_gray" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="13dp" />
**
Do you set color (AARRGGBB int value) or color resource (R.color.*)?
convertView.setBackgroundColor(list.get(position).getBackground());
There should be color in AARRGGBB format.
To get value from resources use:
context.getResources().getColor(R.color.your_color);
I have a conversation page with bubble and thumbnail user image, I have problem with thumbnail position: when I send text thumbnail must show on the right and when my friend sent text his thumbnail must show on the left
My XML item is :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0.5dp" >
<ImageView
android:id="#+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="#drawable/user_four"
android:padding="0dp" />
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:textColor="#2e2e2e"
android:paddingLeft="10dp" />
</LinearLayout>
</RelativeLayout>
and my Java code is :
public class ChatArrayAdapter extends ArrayAdapter<Comment> {
private TextView countryName;
private List<Comment> countries = new ArrayList<Comment>();
private LinearLayout wrapper;
#Override
public void add(Comment object) {
countries.add(object);
super.add(object);
}
public ChatArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount() {
return this.countries.size();
}
public Comment getItem(int index) {
return this.countries.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_chat, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
Comment coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.comment);
countryName.setBackgroundResource(coment.left ? R.drawable.other_bubble : R.drawable.own_bubble);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
If you know in getView() method that the text is your's or your friend's you can change gravity attribute to left or right, depending on the condition, of the linear layout.
Use seperate layout for left and right then inflate it with the correct layout in your get view
I want a simple snippet of a ListView which is composed of items taken from a dataset array which also makes use of getView() method. ListView only contains text entries unlike text and images both.
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
<TextView android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
return row;
}