Creating Listview programatically without xml : not showing any cell - android

I'm trying to create listview programatically in android.
My goal is to create a dynamic view where if i click one button it will change the content inside the layout from listview into another view or vice versa.
Everything seems good i successfully put the listview inside a linear layout from the java file and i did that by creating a custom layout for the cell.
The problem is it shows the listview but it does not add the cell.
I know that it place the listview because the background become green when I run it but it does not show up the cell of the listview.
Am i missing something?
<?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"
android:backgroundTint="#color/colorPrimary">
<TextView
android:id="#+id/listContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mainView,sideView;
ListView notelist;
String[] Names = {"great","good","average","great","good","average"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView = (LinearLayout)findViewById(R.id.mainView);
notelist = new ListView(this);
notelist.setBackgroundColor(Color.GREEN);
CustomAdapter customAdapter = new CustomAdapter();
notelist.setAdapter(customAdapter);
notelist.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mainView.addView(notelist);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
return Names.length;
}
#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) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return null;
}
}
}
main 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/sideView"
android:layout_weight=".20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="#+id/oneBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/noct"
/>
<ImageButton
android:id="#+id/twoBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/note"
/>
<ImageButton
android:id="#+id/threeBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/journal"
/>
<ImageButton
android:id="#+id/fourBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/mic"/>
<ImageButton
android:id="#+id/fiveBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/add"
/>
<ImageButton
android:id="#+id/sixBtn"
android:layout_width="#dimen/ButtonSize"
android:layout_height="#dimen/ButtonSize"
android:background="#android:color/transparent"
android:src="#drawable/pencil"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/mainView"
android:layout_weight=".80"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>

Do not return null in getView
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return view;
}

public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) convertView.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return convertView;
}

Related

Android ListView requires two fingers to scroll

I have a rather simple layout that I am intending on using for basic chat in a fragment, but scrolling through the ListView requires me to place one finger on the screen on an item in the ListView and then scroll up or down with another finger. I was under the impression that a ListView should only require a single finger/touch to be able to scroll. Is there something I am overlooking in my layout, adapter or even potentially the code I am using to populate the adapter with?
Chat Layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<ListView
android:id="#+id/msgListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form"
android:divider="#null"
android:dividerHeight="0dp"
android:scrollbarSize="3dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:paddingBottom="10dp" />
<LinearLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/primary"
android:orientation="horizontal"
android:paddingBottom="2dp" >
<EditText
android:id="#+id/messageEditText"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/button1"
android:layout_toLeftOf="#+id/sendMessageButton"
android:layout_weight="0.72"
android:ems="10"
android:maxHeight="80dp" />
<ImageButton
android:id="#+id/sendMessageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/chat_send_button" />
</LinearLayout>
</RelativeLayout>
ListView Adapter
public class ChatMessageAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
ArrayList<ChatMessage> chatMessageList;
public ChatMessageAdapter(Activity activity, ArrayList<ChatMessage> list) {
chatMessageList = list;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return chatMessageList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessage message = chatMessageList.get(position);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.chat_bubble, null);
TextView msg = (TextView) vi.findViewById(R.id.message_text);
msg.setText(message.body);
TextView dateTime = (TextView) vi.findViewById(R.id.msgDateTime);
dateTime.setText(message.dateTime);
LinearLayout layout = (LinearLayout) vi
.findViewById(R.id.bubble_layout);
LinearLayout parent_layout = (LinearLayout) vi
.findViewById(R.id.bubble_layout_parent);
// if message is mine then align to right
if (message.isMine) {
layout.setBackgroundResource(R.drawable.bubble2);
parent_layout.setGravity(Gravity.RIGHT);
}
// If not mine then align to left
else {
layout.setBackgroundResource(R.drawable.bubble1);
parent_layout.setGravity(Gravity.LEFT);
}
msg.setTextColor(Color.BLACK);
return vi;
}
public void add(ChatMessage object) {
chatMessageList.add(object);
}
}
I had the same problem!
If you're using the navigation drawer, in the app_bar_main.xml change the CoordinatorLayout for the NestedScrollView
and then in your fragment in the scrollView set android:nestedScrollingEnabled="true"

ListView is not displayed in fragment

I implemented my own navigation drawer that comes from the left screen in my android application. The problem is that the ListView it is not visible and I can't figure out why.
This is the method onCreateView of the fragment where the list is (first I initialize the header of the layout and then the list with my custom adapter):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View firstAccessView = inflater.inflate(R.layout.fragment_navigation_drawer, null);
this.profileImageDrawer = (ImageView) firstAccessView.findViewById(R.id.imageViewProfileDrawer);
RoundImage roundedImage = new RoundImage(BitmapFactory.decodeResource(getResources(), R.mipmap.iconuseranonymous));
//this.profileImageDrawer.setImageDrawable(roundedImage);
Picasso.with((Activity) getActivity()).load("https://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?height=105&width=105")
.placeholder(roundedImage)
.transform(new CircleTransform()).fit().centerCrop().into(this.profileImageDrawer);
this.nameSurnameDrawer = (TextView) firstAccessView.findViewById(R.id.textViewDrawner);
this.nameSurnameDrawer.setText(Profile.getCurrentProfile().getFirstName() + " " + Profile.getCurrentProfile().getLastName());
List<String> example = new ArrayList<String>();
example.add("Profile");
example.add("Ask");
example.add("Logout");
this.adapter = new DrawerAdapter(getActivity(), example);
list = (ListView) firstAccessView.findViewById(R.id.listDrawer);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
list.setItemChecked(mCurrentSelectedPosition,true);
return firstAccessView;
}
This is the code of my custom adapter
private Activity context;
private List<String> list;
private View view;
private TextView textView;
public DrawerAdapter(Activity context, List<String> list) {
super(context, R.layout.drawerlist_layout);
this.context=context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.drawerlist_layout, null);
textView = (TextView) view.findViewById(R.id.sectionDrawer);
textView.setText(this.list.get(position));
textView.setTextColor(Color.BLACK);
return view;
}
and finally this is the layout of my fragment: at the top I have an header with an image and a TextView and then I have my custom ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="0.40"
android:background="#ffb200">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageViewProfileDrawer"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="20px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="#+id/textViewDrawner"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:textIsSelectable="true" />
</FrameLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="262dp" android:choiceMode="singleChoice"
android:divider="#cecece" android:dividerHeight="1dp"
android:background="#FFFFFF"
tools:context="com.bellantoni.chetta.lieme.NavigationDrawerFragment"
android:id="#+id/listDrawer"
android:drawSelectorOnTop="false"
android:smoothScrollbar="true"
android:longClickable="false"
android:nestedScrollingEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="true"
android:paddingLeft="10dp"
android:stackFromBottom="false"
android:scrollingCache="false"
android:paddingRight="10dp"
android:textFilterEnabled="false"
android:theme="#style/Base.Widget.AppCompat.ActionMode"
android:textAlignment="center"
android:transitionGroup="true"
android:layout_weight="0.60" />
I found my error in this line of code:
super(context, R.layout.drawerlist_layout);
where I missed the third parameter which in this case is list so it should be
super(context, R.layout.drawerlist_layout,list);
Set both FameLayout and ListView layout_width to 0dp instead of match_parent if you want to use layout_weight.

ListView with BaseAdapter doesn't show up If I use horizontal LinearLayout

I tried to implement very simple BaseAdapter.
However the items are not displayed in the list and list rows seem to be very thin so that anything can't be shown.
My code is as bellow.
Activity:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new BaseAdapter() {
String[] list = new String[] {"Spot", "Stripe"};
#Override
public int getCount() {
return list.length;
}
#Override
public Object getItem(int position) {
return list[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item, null);
}
TextView text1 = (TextView) convertView.findViewById(R.id.text_left);
TextView text2 = (TextView) convertView.findViewById(R.id.text_right);
text1.setText("Name");
text2.setText(getItem(position).toString());
return convertView;
}
});
}
}
Activity layout(activity_my.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ListView item layout(item.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">
<TextView
android:id="#+id/text_left"
android:textAppearance="?android:attr/textAppearance"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<View android:layout_width="20dp"
android:layout_height="1dp"/>
<TextView
android:id="#+id/text_right"
android:textAppearance="?android:attr/textAppearance"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
Edit:
If I change LinearLayout's orientation to "vertical" in item.xml the item appears.
Can somebody explain why if I set the orientation to "horizontal" the row seems to be very thin?
I found my mistake. I set layout_height="1dp" in item.xml for View placing a space between two TextView. that make the row's height very thin. so I can't see anything in the ListView. If I change it as "match_parent" as same as other two TextView's layout_height, it solves this problem.

How to put a ListView in a Fragment

I had a good ListView, but now I added Fragments, now my Code doesn't work in the onCreateMenu...
How to make my ListView back?
So here's my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView;
Bundle b = getArguments();
int Nr = b.getInt(ARG_SECTION_NUMBER);
if (Nr == 0) {
myFragmentView = inflater.inflate(R.layout.layoutexample, container, false);
} else {
myFragmentView = inflater.inflate(R.layout.another_layout_example, container, false);
}
return myFragmentView;
}
I don't understand, where to put my code (for example if I want a ListView)!
Can I use something similar to the inflater?
Please help, as a beginner I only understand half of the information, Google Developers gives me...
This is my ListView:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
That's the list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
Here's the example.xml (they both look the same):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
After having inflated your view, you can add :
ListView listView = myFragmentView.findViewById(android.R.id.list);
listView.setAdapter(....);
Edit : I didn't know the id you set to your ListView. Replace android.R.id.list by R.id.listView1

Fragment overlaying an Activity layout

I'm having problems working with fragments. The problem is: I have a layout in my activity with a ListView. When i select an item in this ListView, instantiates a fragment replacing the activity layout. My result is both fragment and activity contents are showing on screen, overlaying each other. What can I fix that (the fragment just replacing the activity content)? Here is the codes:
activityLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity"
android:orientation="vertical">
<FrameLayout android:id="#+id/fragment_to_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical"
android:layout_gravity="center" >
<TextView android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_select_table"
android:textSize="30dp"/>
<ListView android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="#+id/listView"
android:visibility="visible"
android:paddingTop="40dp" />
</FrameLayout>
</LinearLayout>
Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
View view = findViewById(R.id.activity);
//Getting the ListView
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setTextFilterEnabled(true);
//Setting the adapter
listView.setAdapter(new Adapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Creates a fragment
addFragment();
}
});
}
public void addFragment(){
MyFragment newFragment;
FragmentTransaction newFragmentTransaction = getSupportFragmentManager().beginTransaction();
newFragment = (MyFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment);
if (newFragment == null){
newFragment = new MyFragment(this, getSupportFragmentManager());
newFragmentTransaction.replace(R.id.fragment_to_replace,
new SelectDrinkFragment(this, getSupportFragmentManager()));
newFragmentTransaction.addToBackStack(null);
newFragmentTransaction.commit();
}
}
fragmentLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_two"
android:textSize="20dp"/>
<LinearLayout android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView" />
<TextView android:id="#+id/textView_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>
<TextView
android:id="#+id/textView_three"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_three"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/listView_two"
android:visibility="visible"
android:paddingTop="40dp" />
</LinearLayout>
Fragment.java
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle saveInstanceState){
View view = inflater.inflate(R.layout.fragment, container, false);
final ListView listView = (ListView) view.findViewById(R.id.listView_two);
CharSequence [] list = getResources().getStringArray(R.array.array);
listView.setAdapter(new ListAdapter(getContext(), list));
return view;
}
ListAdapter.java
public class ListAdapter extends BaseAdapter{
private Context context;
private CharSequence[] drinkArraySize;
public ListAdapter(Context context, CharSequence[] array){
super();
this.context = context;
this.array= array;
}
#Override
public int getCount() {
return array.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public Context getContext(){
return context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(getContext());
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(array[position]);
linearLayout.addView(radioButton);
return linearLayout;
}
}
FragmentTransaction.replace() looks like it only replaces another fragment (and not just any View), so I'm pretty sure this is expected behavior (or at least, I know I've had this happen to me before). I suppose you have a few options:
I don't see in your code anything that would make the fragment's background opaque. You could try that first and see if it helps. If it does, that might be the simplest.
You could just set the other items' visibilities to View.GONE at the same time you perform your transaction, but that has some issues as far as backstack goes.
You could replace the ListView and TextView with a ListFragment instead. This is probably the "best" way to do it, but also is probably the most work.

Categories

Resources