I've seen many times this problem, and I think it's not that hard to fix; still I've tried several hours, can't get how it works.
Simple problem : I have 2 EditText in a listView, and they both loose instantly loose focus as soon as I touch their field.
Must be some Hierarchy stuff. Actually I can fix it with adjustpan property, but my listView doesn't scroll anymore while softKeyboard is on.
Here is my code :
package com.android.activity;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class NotesCursorAdapter extends CursorAdapter{
private Context context;
// private Cursor cursor;
private int addNoteTopPosition;
private int addNoteBottomPosition;
private LayoutInflater inflater;
private boolean deleteMode = false;
private static final int TYPE_ITEM = 0;
private static final int TYPE_ADD_NOTE_BOTTOM = 1;
private static final int TYPE_ADD_NOTE_TOP = 2;
private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1;
public NotesCursorAdapter (Context context, Cursor cursor, int flag, boolean enableDelete){
super(context, cursor);
this.context = context;
addNoteTopPosition = 0;
addNoteBottomPosition = cursor.getCount()+1;
inflater = LayoutInflater.from(this.context);
deleteMode = enableDelete;
}
#Override
public int getCount() {
return super.getCount() + 2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ADD_NOTE_TOP:
convertView = inflater.inflate(R.layout.add_note_top, null);
holder.view = (EditText)convertView.findViewById(R.id.add_note_top_id);
break;
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.row_note, null);
holder.delete = (Button)convertView.findViewById(R.id.delete);
if (deleteMode){
holder.delete.setVisibility(View.VISIBLE);
}else{
holder.delete.setVisibility(View.GONE);
}
holder.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.view = (TextView)convertView.findViewById(R.id.note);
getCursor().moveToPosition(position - 1);
((TextView) holder.view).setText(getCursor().getString(getCursor().getColumnIndex("content_note")));
break;
case TYPE_ADD_NOTE_BOTTOM:
convertView = inflater.inflate(R.layout.add_note_bottom, null);
holder.view = (EditText)convertView.findViewById(R.id.add_note_bottom_id);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
#Override
public int getItemViewType(int position) {
int type;
if (position == addNoteTopPosition){
type = TYPE_ADD_NOTE_TOP;
} else if (position == addNoteBottomPosition){
type = TYPE_ADD_NOTE_BOTTOM;
}else {
type = TYPE_ITEM;
}
return type;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public View delete;
public View view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return null;
}
}
Here are XML files :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_diary_big"
android:focusable="false"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_gravity="right"
android:gravity="fill_vertical"
android:orientation="horizontal"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/calendar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/calendar_up" />
</LinearLayout>
<TextView
android:id="#+id/currentDay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="0.5"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/done"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/pen_blue_big" />
</LinearLayout>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false"
android:orientation="vertical" >
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:cacheColorHint="#00000000"
android:descendantFocusability="beforeDescendants" >
</ListView>
</LinearLayout>
</LinearLayout>
add_note_top.xml :
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_note_top_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:hint="#string/add_note"
android:textColor="#000000" >
</EditText>
add_note_bottom.xml :
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_note_bottom_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/add_note"
android:background="#00000000"
android:textColor="#000000" >
</EditText>
row_note.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutContainer"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_gravity="right"
android:gravity="fill_vertical"
android:orientation="horizontal"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<Button
android:id="#+id/delete"
android:layout_width="21dp"
android:layout_height="22dp"
android:background="#drawable/sens_interdit" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:focusable="false"
android:textColor="#000000" >
</TextView>
</LinearLayout>
I think I put the important, if you guys need some more like activity code or something else, just ask.
Thanks for reading.
Try to set
android:windowSoftInputMode="adjustPan"
for your activity in the AndroidManifest.xml
Related
When user clicks on icon adjacent to the item in spinner, some action has to be performed. i have not added functionality for button click handler. Before that i am facing below issue. For button with icon, i have used ImageButton in Spinner.
But when item in spinner is selected, all the items are shown and list down is not closing.
I am using Android api 28 to test.
MainActivity.java
package com.example.ravispinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<FileItem> mList;
private FileAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
Spinner spinnerCountries = findViewById(R.id.spinner_countries);
mAdapter = new FileAdapter(this, mList);
spinnerCountries.setAdapter(mAdapter);
spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FileItem clickedItem = (FileItem) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getName();
Toast.makeText(MainActivity.this, clickedCountryName + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void initList() {
mList = new ArrayList<>();
mList.add(new FileItem("Edit", R.drawable.ic_action_edit));
mList.add(new FileItem("New", R.drawable.ic_action_new));
mList.add(new FileItem("Remove", R.drawable.ic_action_remove));
}
}
FileAdapter
package com.example.ravispinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
public class FileAdapter extends ArrayAdapter<FileItem> {
public FileAdapter(Context context, ArrayList<FileItem> countryList) {
super(context, 0, countryList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.file_spinner_row, parent, false
);
}
ImageButton imageViewFlag = convertView.findViewById(R.id.image_view_flag);
TextView textViewName = convertView.findViewById(R.id.text_view_name);
FileItem currentItem = getItem(position);
if (currentItem != null) {
//imageViewFlag.setImageResource(currentItem.getFlagImage());
textViewName.setText(currentItem.getName());
}
return convertView;
}
}
FileItem
package com.example.ravispinner;
public class FileItem {
private String mName;
private int mFlagImage;
public FileItem(String name, int flagImage) {
mName = name;
mFlagImage = flagImage;
}
public String getName() {
return mName;
}
public int getFlagImage() {
return mFlagImage;
}
}
file_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_view_flag"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toEndOf="#+id/image_view_flag"
android:gravity="center"
android:text="India"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="#+id/icon"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_remove"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ravispinner.MainActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow android:layout_margin="2dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="2dp"
android:padding="3dp"
android:text="Test Scenario\t: "
android:textSize="20dp" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp" />
</TableRow>
</TableLayout>
</RelativeLayout>
This is one more solution to achieve UI try once may it helps you.
change your file_spinner_row.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:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center|start"
android:text="India"
android:textColor="#android:color/black"
android:textSize="16sp" />
<ImageView
android:id="#+id/image_view_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scaleType="center"
android:src="#drawable/ic_baseline_remove_circle_24" />
</LinearLayout>
and activity_main.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:layout_gravity="center|top"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight="0.5"
android:text="Test Scenario:"
android:textSize="20sp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:spinnerMode="dropdown" />
</LinearLayout>
Chat_thread.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="vertical"
android:gravity="left"
android:paddingLeft="10dp"
android:paddingRight="50dp">
<!-- For self message -->
<LinearLayout
android:background="#drawable/balloon_incoming_normal"
android:elevation="2dp"
android:layout_margin="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:padding="8dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_marginLeft="10dp"
android:id="#+id/textViewMessage"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello how are you?" />
<TextView
android:layout_marginLeft="10dp"
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Belal Khan, 2016-05-29 16:02:00?"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
**Chat_other_thread.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:orientation="vertical"
android:gravity="right"
android:paddingRight="10dp"
android:paddingLeft="50dp">
<LinearLayout
android:background="#drawable/balloon_outgoing_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="2dp"
android:orientation="vertical"
android:padding="8dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_marginRight="10dp"
android:id="#+id/textViewMessage"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello how are you?" />
<TextView
android:layout_marginRight="10dp"
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
**Demo_chat.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity" >
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#color/colorPrimary"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/back_white"
android:layout_marginRight="10dp"
/>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/black_mix"
android:layout_marginRight="10dp"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.5"
>
<TextView
android:id="#+id/chat_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kamal Jain"
android:textSize="20dp"
android:textColor="#fff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last seen 2 days ago"
android:textColor="#b6dbe5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="7dp"
android:layout_height="7dp"
android:background="#drawable/white_no_border"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="7dp"
android:layout_height="7dp"
android:background="#drawable/white_no_border"
android:orientation="vertical"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
>
</LinearLayout>
<LinearLayout
android:layout_width="7dp"
android:layout_height="7dp"
android:background="#drawable/white_no_border"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--main title end here-->
<ListView
android:id="#+id/chat_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.00"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:divider="#null"
android:dividerHeight="5dp"/>
<include
android:id="#+id/footer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/footer" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/footer_for_emoticons"
android:layout_width="match_parent"
android:layout_height="#dimen/keyboard_height"
android:background="#android:color/transparent"
android:visibility="gone" />
</LinearLayout>
GroupChatListAdapter.class
package com.trackliy.trackliy.messanger;
/**
* Created by cbr on 3/24/2017.
*/
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.trackliy.trackliy.R;
import com.trackliy.trackliy.Session;
import com.trackliy.trackliy.model.GroupMessageModel;
import com.trackliy.trackliy.model.Message;
public class GroupChatListAdapter extends BaseAdapter {
private ArrayList<Spanned> comments;
private Context mContext;
private LayoutInflater inflater;
private Session session;
//Tag for tracking self message
private final int Comment_Log_In_User = 0;
private final int Comment_Other_User = 1;
//ArrayList of messages object containing all the messages in the thread
private ArrayList<GroupMessageModel> groupMessagesList;
// public ChatListAdapter(Context context, ArrayList<Spanned> comments,String users) {
//Constructor
public GroupChatListAdapter(Context context, ArrayList<GroupMessageModel> groupMessages) {
this.mContext = context;
this.groupMessagesList = groupMessages;
session = new Session(context);
this.comments = comments;
this.inflater = LayoutInflater.from(context);
}
public void addGroupMessage(GroupMessageModel m) {
groupMessagesList.add(m);
notifyDataSetChanged();
}
public void addGroupMessageList(ArrayList<GroupMessageModel> groupMessagesList) {
this.groupMessagesList.addAll(groupMessagesList);
Log.d("Addmessage",groupMessagesList.toString());
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
//getting message object of current position
GroupMessageModel message = groupMessagesList.get(position);
// Here you have to write logic for which position have which type of row and return the same
if (message.getUserid().equals( session.get_id())) {
return Comment_Log_In_User;
} else if (!message.getUserid().equals(session.get_id()) ) {
//Returning other user
return Comment_Other_User;
}
//else returning position
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
ViewHolder holder = null;
// View v = convertView;
if (getItemViewType(position) == Comment_Log_In_User) {
if (view == null) {
view = inflater.inflate(R.layout.chat_thread, parent, false);//set layout for displaying items
holder = new ViewHolder();
holder.textViewMessage = (TextView) view.findViewById(R.id.textViewMessage);
holder.textViewTime = (TextView) view.findViewById(R.id.textViewTime);
// holder.reciever_UserName=(TextView)view.findViewById(R.id.chat_user_name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
GroupMessageModel message = groupMessagesList.get(position);
SpannableString spannedTxt = new SpannableString(message.getMessage());
holder.textViewMessage.setText(spannedTxt);
//holder.textViewTime.setText(message.getTime_stamp());
String formatedDate = convertTimeStampIntoFormat(String.valueOf(message.getTime_stamp()));
holder.textViewTime.setText(formatedDate);
} else if (getItemViewType(position) == Comment_Other_User) {
if (view == null) {
view = inflater.inflate(R.layout.chat_thread_other,parent, false);//set layout for displaying items
holder = new ViewHolder();
holder.textViewMessage = (TextView) view.findViewById(R.id.textViewMessage);
holder.textViewTime = (TextView) view.findViewById(R.id.textViewTime);
// holder.reciever_UserName=(TextView)view.findViewById(R.id.chat_user_name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
GroupMessageModel message = groupMessagesList.get(position);
holder.textViewMessage.setText(message.getMessage());
holder.textViewTime.setText(message.getName());
String formatedDate = convertTimeStampIntoFormat(String.valueOf(message.getTime_stamp()));
holder.textViewTime.setText(formatedDate);
}
// final Spanned item = comments.get(position);
/*TextView fans_image = (TextView) v.findViewById(R.id.item_text);
fans_image.setText(item);*/
return view;
}
#Override
public int getCount() {
return groupMessagesList.size();
}
#Override
public Object getItem(int position) {
return groupMessagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private String convertTimeStampIntoFormat(String time_stamp) {
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String finalDate = "";
if (time_stamp.contains(".")) {
time_stamp = time_stamp.substring(0, time_stamp.indexOf('.'));
}
try {
finalDate = sdf1.format(sdf2.parse(time_stamp));
} catch (Exception e) {
e.printStackTrace();
}
return finalDate;
}
public class ViewHolder {
public TextView textViewMessage;
public TextView textViewTime;
public TextView reciever_UserName;
}
}
Whenever I send or fetch a new message, the layout of the message doesn't shift with the message rather than message get shift in other direction.
i.e left to right but it should move on the same direction.
Please tell me what I'm missing.
I have a text below a listview and I need that if listview contains more items, to push the text down more. Because many items in my listview, causes the view to look bad. Look at the example to understand
Picture
My code is
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="MaƱana"
android:id="#+id/manianaTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaManiana"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tarde"
android:id="#+id/tardeTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaTarde"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Noche"
android:id="#+id/nocheTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaNoche"
android:layout_gravity="center_horizontal" />
</LinearLayout>
`
It is better to use a ListView with section. In that case you can categorize each section in your ListView instead of using many ListViews and TextViews.
I found this example from this website and I did some modification in the code
Create your List Item View
<?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">
<ImageView
android:id="#+id/photo"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="32dp"
android:src="#drawable/female"
android:contentDescription="#string/app_name"/>
<TextView
android:id="#+id/photo_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/photo"
android:layout_alignTop="#+id/photo"/>
</RelativeLayout>
Create a layout for the header section
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_vertical"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF000000" />
</LinearLayout>
Create an adapter for your List View
import java.util.ArrayList;
import java.util.TreeSet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
class CustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<EntityObject> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final EntityObject item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.snippet_item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.photo_details);
holder.photo = (ImageView)convertView.findViewById(R.id.photo)
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.snippet_item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position).getText());
holder.setImageResource(mData.get(position).getImage());
return convertView;
}
public static class ViewHolder {
public TextView textView;
public ImageView photo;
}
}
Good day to all.
I am currently working on a customized ListView which is not inflating (is not becoming visible) inside the activity.
Below are the xml and ListView adapter classes.
main.xml
<LinearLayout
android:id="#+id/llListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_below="#+id/rlFirstRow">
<ListView
android:id="#+id/lvErrorsReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"/>
</LinearLayout>
listview_row.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">
<LinearLayout
android:id="#+id/llError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txtErrorInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llError">
<TextView
android:id="#+id/txtStatus"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Status: "/>
<TextView
android:id="#+id/txtStatusInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llMaterials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llStatus">
<TextView
android:id="#+id/txtMaterials"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Materials: "/>
<TextView
android:id="#+id/txtMaterialsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llMaterials">
<Button
android:id="#+id/btnShowMaterials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Materials"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
BaseAdapter class
public class RepairReportListViewAdapter extends BaseAdapter
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;
public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
this.mContext = context;
this.errors = err;
this.statuses = stat;
this.materials = mat;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return 0;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.repairreport_listview, null);
holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.error.setText(this.errors.get(position).toString());
holder.status.setText(this.statuses.get(position).toString());
if(this.materials.get(position) == true)
{
holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
holder.showMaterials.setVisibility(Button.VISIBLE);
holder.showMaterials.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//LATER
}
});
}
else
{
holder.material.setText("No materials where used to fix this error.");
}
return convertView;
}
static class ViewHolder
{
TextView error;
TextView status;
TextView material;
Button showMaterials;
}
}
I've already tried changing the layouts as suggested here but the ListView is still not revealed.
Any ideas why it is not showing? Sorry for the code dump but I really cannot figure out what the heck is going on.
P.S. I did set the adapter to the ListView in the main activity.
adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
lvErrors.setAdapter(adapter);
your ListView does not inflate nothing because getCount is returning 0
change
#Override
public int getCount()
{
return 0;
}
with
#Override
public int getCount()
{
return (stat == null) ? 0 : stat.size();
}
Also, instead of keeping three differents ArrayList, you can create a class that holds all the info you need and an ArrayList of this class
I want to make listview same like instagram...(Sectionised)
I searched in google and find some examples, but it is not working properly as I need.
This is the link i searched : Link 1 and Link 2. But in the first link I coudn't find any solution for instagram-like listview.
As for fhe second link, I made a demo and I run it and it works perfectly. The only problem is that when I put two textviews in the header then it is not working .
If you can help me regarding this then it would be great for me..
This demo from Second Link..
package com.example.Section_Listview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class InstaHeaderActivity extends Activity implements AbsListView.OnScrollListener{
ListView list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new Adapter(this));
list.setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)
{
//the listview has only few children (of course according to the height of each child) who are visible
for(int i=0; i < list.getChildCount(); i++)
{
View child = list.getChildAt(i);
ViewHolder holder = (ViewHolder) child.getTag();
//if the view is the first item at the top we will do some processing
if(i == 0)
{
boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
int offset = holder.previousTop - child.getTop();
if(!(isAtBottom && offset > 0))
{
holder.previousTop = child.getTop();
holder.header.offsetTopAndBottom(offset);
holder.header.invalidate();
}
} //if the view is not the first item it "may" need some correction because of view re-use
else if (holder.header.getTop() != 0)
{
int offset = -1 * holder.header.getTop();
holder.header.offsetTopAndBottom(offset);
holder.previousTop = 0;
holder.header.invalidate();
}
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {}
private static class Adapter extends ArrayAdapter<String> {
public Adapter(Context context) {
super(context, R.layout.row, R.id.header);
for(int i=0; i < 50; i++){
add(Integer.toString(i));
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
ViewHolder holder=new ViewHolder();
holder.header = (TextView) convertView.findViewById(R.id.header);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.header.setText(getItem(position));
return convertView;
}
}
public static class ViewHolder
{
TextView header;
int previousTop = 0;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
child_header.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="vertical" >
<TextView
android:id="#+id/childHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="childTest" />
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#FFFFFF">
<ListView
android:id="#+id/childList"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header"
android:layout_marginTop="16dp" >
</ListView>
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:padding="12dp"
android:text="Deneme Row"
android:textColor="#ffffff"
android:background="#000000"/>
</RelativeLayout>
First of all, you can't have two public classes in one file, so move
public class ViewHolder {
TextView header;
int previousTop = 0;
}
to a seperate file. And while you're at it, remove the static modifier, it's not allowed.
To display a second TextView in your header row, you have to declare it in row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<ListView
android:id="#+id/childList"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header"
android:layout_marginTop="16dp" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#000000"
android:padding="12dp"
android:textColor="#ffffff" />
<TextView
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#000000"
android:padding="12dp"
android:text="second textview"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
This modified row.xml will give you a layout like this:
To set the content of the second textview, change your ViewHolder class to:
public class ViewHolder {
TextView header;
TextView header2;
int previousTop = 0;
}
and change your getView-method in your activity to:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
ViewHolder holder = new ViewHolder();
holder.header = (TextView) convertView.findViewById(R.id.header);
holder.header2 = (TextView) convertView.findViewById(R.id.header2);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.header.setText(getItem(position));
holder.header2.setText("whatever you want");
return convertView;
}