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
Related
I have a custom spinner with an ImageView and a TextView, however the return codes do not return either one of these values rather they return it as an object.
How do I get the drawable id or the textview string separately and not the object of the spinner?
Do I have to do it in my adapter, the spinner itself or other components trying to get the data?
Tq
Here is my Spinner XML code :
<?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:orientation="horizontal">
<ImageView
android:id="#+id/spinnerImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:layout_weight="1"
android:src="#drawable/dark_red_square" /><!--Make sure image is present in Drawable folder-->
<TextView
android:id="#+id/spinnerText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:textSize="14sp"
android:gravity="center"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
This spinner is in a fragment. Honestly I don't know what codes should I sample here for reference, but here's the custom adapter
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context context, int[] flags, String[] countryNames, LayoutInflater inflter) {
this.context = context;
this.flags = flags;
this.countryNames = countryNames;
this.inflter = (LayoutInflater.from(context));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int position) {
return countryNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflter.inflate(R.layout.spinner_item, null);
TextView spinnerText= convertView.findViewById(R.id.spinnerText);
ImageView spinnerImage = convertView.findViewById(R.id.spinnerImage);
spinnerText.setText(countryNames[position]);
spinnerImage.setImageResource(flags[position]);
return convertView;
}
}
I Have created a Spinner using adapter with icons and text. Everything is displayed correctly but there is a requirement that Activity spinner should show text only. Icons should only appear in the drop-down list.
It this possible to implement?
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {
this.context = applicationContext;
this.flags = flags;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
[id3844467|#Override]
public int getCount() {
return flags.length;
}
[id3844467|#Override]
public Object getItem(int i) {
return null;
}
[id3844467|#Override]
public long getItemId(int i) {
return 0;
}
[id3844467|#Override]
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(countryNames[i]);
return view;
}
}
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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#drawable/ic_launcher" /> // for check that icon existing
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="0dp"
android:text="Demo" // for check that text existing
android:textColor="#000" />
</LinearLayout>
Set these two property in spinner hopeFully it will work
android:dropDownHeight="0dp"
android:dropDownWidth="0dp"
i'am not sure but change your getItemId like this :
#Override
public long getItemId(int position) {
return position;
}
and don't return null on getItem
I have an custom array adaptor which is handling a list view.
Each row has two checkboxes. There are five rows in total.
I want to be able to count the number of checkboxes ticked and then display this number in Stand1
So basically the project Layout is like this
Inside stand1 there is a list view and a text view.
The stand1.java calls an array adapter Called CustomArrayAdaptor.
I want to be able to count the number of checkboxes clicked and send that data back to the stand1
I am quite new to android development so if somebody can be able to push me in the right direction, it would be great.
Here is my code
Stand1.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="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="446dp"
android:id="#+id/Stand1list"
android:scrollIndicators="right"
android:smoothScrollbar="true">
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Score To Databse"
android:id="#+id/sendtodb"
android:textSize="12dp"
android:clickable="true"
android:layout_below="#+id/Stand1list"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8/10"
android:id="#+id/Score"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_alignBottom="#+id/sendtodb"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
Row_Layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textelement"
xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="30dp"
android:textStyle="bold"
android:longClickable="false" />
<CheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/Checkbox1"
android:button="#drawable/checkboxcustom"
android:layout_marginLeft="50dp" />
<CheckBox
android:button="#drawable/checkboxcustom"
android:id="#+id/Checkbox2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp" />
</LinearLayout>
Stand1.java
public class Stand1 extends Fragment {
ListView mList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair 1","Pair 2","Pair 3","Pair 4","Pair 5"};
//build adapter
ArrayAdapter<String> adapter = new CustomArrayAdaptor(getActivity(),pair);
mList.setAdapter(adapter);
}
}
CustomArrayAdaptor.java
public class CustomArrayAdaptor extends ArrayAdapter<String>{
public CustomArrayAdaptor(Context context, String [] Pairs) {
super(context, R.layout.row_layout1, Pairs);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View CustomView = inflater.inflate(R.layout.row_layout1, parent, false);
String stringelement = getItem(position);
TextView Text= (TextView)CustomView.findViewById(R.id.textelement);
Text.setText(stringelement);
return CustomView;
}
}
Thanks Folks
I added a few things to your CustomArrayAdaptor and tested it, see below:
public class CustomArrayAdapter extends ArrayAdapter<String> {
int checkAccumulator;
public CustomArrayAdapter(Context context, String [] Pairs) {
super(context, R.layout.row_layout1, Pairs);
checkAccumulator = 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.row_layout1, parent, false);
String stringelement = getItem(position);
TextView Text = (TextView) customView.findViewById(R.id.textelement);
CheckBox checkBox1 = (CheckBox) customView.findViewById(R.id.Checkbox1);
CheckBox checkBox2 = (CheckBox) customView.findViewById(R.id.Checkbox2);
CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countCheck(isChecked);
Log.i("MAIN", checkAccumulator + "");
}
};
checkBox1.setOnCheckedChangeListener(checkListener);
checkBox2.setOnCheckedChangeListener(checkListener);
Text.setText(stringelement);
return customView;
}
private void countCheck(boolean isChecked) {
checkAccumulator += isChecked ? 1 : -1 ;
}
}
Added int checkAccumulator to keep track of how many items have been clicked.
Added two CheckBox Views for the purpose of tracking checks.
Added CompoundButton.OnCheckedChangeListener that calls a new countCheck method everytime any checkBox is checked / unchecked. We pass the isChecked boolean to track whether we need to add or subtract from our checkAccumulator.
Added countCheck function that adds or subtracts 1 dependent on the boolean. Sorry for the obfuscated code, for all those who aren't used to the ? conditional it is basically saying if true add 1, else add -1.
Not sure what you wanted to do with the check count, so I added a Log.i("MAIN", checkAccumulator + ""); just so you can watch the Android Monitor to ensure that it is actually working.
Should work for you.
First of all, you should recycle your views in the adapter:
LayoutInflater inflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_layout1, parent, false);
}
After that, I think you should save the state of each checkbox in your data source or just keep a list, the same size of your pair array, and save it there using the position you get from getView.
I have one app that creates a table row from an adapter.Here the adapter:
public class TableAdapter extends ArrayAdapter<Table> {
public TableAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TableAdapter(Context context, int resource, List<Table> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_table, parent, false);
holder.position = (TextView) convertView.findViewById(R.id.position);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.points = (TextView) convertView.findViewById(R.id.points);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position != 0) {
convertView.findViewById(R.id.header).setVisibility(View.GONE);
}
holder.position.setText(String.valueOf(getItem(position).getPosition()));
holder.image.setVisibility(View.GONE);
holder.progress.setVisibility(View.VISIBLE);
holder.name.setText(getItem(position).getName());
holder.points.setText(String.valueOf(getItem(position).getPoints()));
final ViewHolder tmp = holder;
Picasso.with(getContext()).load(getContext().getResources().getString(R.string.team_logo) + getItem(position).getId() + ".png").into(holder.image, new Callback() {
#Override
public void onSuccess() {
tmp.progress.setVisibility(View.GONE);
tmp.image.setVisibility(View.VISIBLE);
}
#Override
public void onError() {
}
});
return convertView;
}
class ViewHolder {
TextView position;
ProgressBar progress;
ImageView image;
TextView name;
TextView points;
}
}
and the layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/position"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/imgHolder"
android:layout_width="50dp"
android:layout_height="50dp" >
<ProgressBar
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/points"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
So far everything is working great as expected.
But I would like to add an header to the table.
Something like:
# | Name | Pts
I tried to add this in the layout, and check if the position != 0 and set the visibility to GONE, but the problem is when I scroll down and up it disappears.
My question is: How can I add and header to this table?
Or add this header fixed, and everything else scrollable?
In your adapter, add these two method overrides:
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return position == 0 ? 0 : 1;
}
This tells the adapter that you have two types of row layout, and which rows have each type.
I have created a custom ArrayList which is working as it should barring an issue with the onClick. I orginally had the ArrayList highlight the selected row in red when the user clicked on the row. However, I also required the position of that click outside of the ArrayAdapter class.
The only way I could seem to do this is by adding an onClickListener, however this is were the issues began, although this solved my issue of getting the position the user click on, the row highlighting now seems to get overridden.
So basically my issue is allowing both selected rows to be highlighted and position to be passed to a variable outside of the ArrayAdapter class, please see my code below:
Array Adapter Class:
public class Shop extends ListActivity
{
private static class MyAdapter extends ArrayAdapter<String>
{
private ArrayList<String> data1, data2, data3, data4;
public MyAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> data1, ArrayList<String> data2, ArrayList<String> data3, ArrayList<String> data4)
{
super(context, resource, textViewResourceId, data1);
this.data1 = data1;
this.data2 = data2;
this.data3 = data3;
this.data4 = data4;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
TextView t1 = (TextView) v.findViewById(R.id.data1);
t1.setText(data1.get(position));
TextView t2 = (TextView) v.findViewById(R.id.data2);
t2.setText(data2.get(position));
TextView t3 = (TextView) v.findViewById(R.id.data3);
t3.setText(data3.get(position));
TextView t4 = (TextView) v.findViewById(R.id.data4);
t4.setText(data4.get(position));
spnCharacters.setVisibility(View.GONE);
Iterator<Integer> iterator = unavailableItem.iterator();
while (iterator.hasNext())
{
int NotAvailable = iterator.next();
if(position == NotAvailable)
{
v.setBackgroundColor(Color.rgb(170, 170, 170));
}
}
if(unavailableItem.size() == 1)
{
if(position == unavailableItem.get(0))
{
v.setBackgroundColor(Color.rgb(170, 170, 170));
}
}
if (position == 0)
{
v.setBackgroundResource(android.R.drawable.title_bar);
}
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
currentPositon = position;
if(data2.get(currentPositon).equals("0"))
{
btnSell.setEnabled(false);
}
else
{
btnSell.setEnabled(true);
}
}
});
return v;
}
}
private void NeedPosition()
{
int position = // need ArrayAdapter exact position here
}
}
ArrayAdapter 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/data1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/data2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/data3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/data4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
List View XML:
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearButtons"
android:layout_above="#+id/linerBuyBtns"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:listSelector="#drawable/list_selector">
</ListView>
ListActvity has its own way to manage cell click.
protected void onListItemClick (ListView l, View v, int position, long id)
This method will be called when you click on an row of the ListView. As you can see the second paramter is the position you need
try this...
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
onClick(...)
{
convertView.parent().setSelection(position); <---
}
}
I had a similar problem with custom listViews and Array adapters..
for me the android:listSelector did not work properly so i used android:Background instead
where i defined states selected, highlighted etc....At the end to achieve certain visual effects i had to use a combination of both, probably not the best solution but works perfectly.