Here is the my code for the Dialog With ListView and here i also maintain the state when the user click on list it background will be fill with green as you can see in my image below
But problem that i have is below
1>I want to increase my row height .How can do?
2>and i also want to put image beside in all row is this possible?
Here is my code for all this stuff.
package com.android.listselector;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ListSelector extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
private Context mContext = ListSelector.this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.selected_example);
String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula",
"vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat",
"placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus" };
// populate the model - a simple a list
list = new ArrayList<String>();
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this, 0, list);
selectedAdapter.setNotifyOnChange(true);
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.selected_example);
dialog.setTitle("Custom Dialog");
ListView listview = (ListView) dialog.findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
dialog.show();
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
// move up event handler
// move down event handler
// Move selected item "up" in the ViewList.
private void moveUp() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos > 0) {
String str = list.remove(selectedPos);
list.add(selectedPos - 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos - 1);
}
}
// Move selected item "down" in the ViewList.
private void moveDown() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos < list.size() - 1) {
String str = list.remove(selectedPos);
list.add(selectedPos + 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos + 1);
}
}
public class SelectedAdapter extends ArrayAdapter<String> {
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition() {
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
}
// get text view
TextView label = (TextView) v.findViewById(R.id.txtExample);
// change the row color based on selected state
if (selectedPos == position) {
label.setBackgroundColor(Color.GREEN);
} else {
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
* // to use something other than .toString() MyClass myobj =
* (MyClass)this.getItem(position);
* label.setText(myobj.myReturnsString());
*/
return (v);
}
}
}
and here are the layout used in my code
<?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/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/btnMoveUp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/btnMoveDown"
/>
</LinearLayout>
here is other one
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
Use the following code to get your expectation. image view is placed to the below of text view. If you want to see image view right side of taxt view then instead of using android:orientation="vertical" in linearlayout use android:orientation="horizontal"
selected_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
<ImageView
android:id="#+id/accountIcon"
android:layout_width="wrap_content"![enter image description here][1]
android:layout_height="wrap_content"
android:background="#drawable/btn_public_message_focus"
android:layout_marginRight="6dip" />
</LinearLayout>
Donot forget to vote if my response is useful for you.
Thanks
Deepak
You should customize the listview to increase the row height.
possible to put image only using the customized listview in dialog.
check this
If you want to make the ListView with different settings, you have to create the Custom ListView and also CustomAdapter for that listview.
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
You have to make seprate layout for this to add image in the cell
Use this layout for your List row.....
<?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="50dip"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon"
/>
</LinearLayout>
Yes i get solution by here all my answer
first i have to modify selected_row xml file by this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="100dip">
<TextView android:layout_width="fill_parent"
android:layout_height="40px" android:id="#+id/txtExample"
android:textSize="18sp" android:textColor="#000000"
android:gravity="center_vertical"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="40px" android:id="#+id/imgbeside"
android:scaleType="center" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/selector"
android:layout_marginRight="10dip" />
and also in my java file i put the following thing below this line
TextView label = (TextView) v.findViewById(R.id.txtExample);
ImageView mImageview = (ImageView) v.findViewById(R.id.imgbeside);
this will work for me.Ok Thanks For all reponse.
Related
I have a popup menu inside every item on a listview. When you click the imageview to the left (settings img with 3 dots) a popup menu should showup. however, I'm getting error
Could not find a method showPopupMenu(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'settings_img'
does anyone know what this error is. it is obvious that can't find the method, but is it because it can't find the class/view? if so how can I fix it? thanks
main_activity
package com.example.george.hostscanmenus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List hostList;
ArrayAdapter<String> hostAdapter;
PopupMenu popup;
String subnet = "192.168.10.";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostList = new ArrayList<String>();
//populating host addresses
for(int i = 0; i < 255; i++) {
hostList.add(subnet+i +"-aa:bb:00:cc:33:ee");
}
//inflating adapter
listView = (ListView) findViewById(R.id.scan_list);
hostAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.host_item, R.id.ip_address, hostList);
listView.setAdapter(hostAdapter);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(menuItem);
}
}
});
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.host_menu, popup.getMenu());
popup.show();
}
}
array adapter
package com.example.george.hostscanmenus;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by george on 8/11/17.
*/
public class HostAdapter extends ArrayAdapter<String> {
public HostAdapter(Context context, int num, ArrayList<String> allHost) {
super(context, 0, allHost);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
ImageView nodeHostImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.ip_address);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.mac_address);
ImageView nodeArrowImgView = (ImageView) convertView.findViewById(R.id.port_scan_arrow);
// Populate the data into the template view using the data object
nodeHostImgView.setImageResource(R.drawable.ic_computer_white_24dp);
nodeIpTxtView.setText(host.substring(0,host.indexOf("-")));
nodeMacTxtView.setText(host.substring(host.indexOf("-")));
nodeArrowImgView.setImageResource(R.drawable.ic_keyboard_arrow_right_white_24dp);
// Return the completed view to render on screen
return convertView;
}
}
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scan_list_linear"
tools:context=".MainActivity">
<!-- labels for ip / mac addres list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_label">
<TextView
android:id="#+id/host_port_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Host"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/ip_open_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20dp"
tools:text="IP / MAC"
android:paddingLeft="10dp"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/scan_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Scan"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
</LinearLayout>
<!--Listview to display scan output-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scan_list"/>
</LinearLayout>
list item xml
<?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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/settings_img"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:padding="3dp"
android:src="#drawable/ic_action_more_vert"
android:onClick="showPopupMenu"/>
<ImageView
android:id="#+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_computer_white_24dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="#+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="#+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
<ImageView
android:id="#+id/port_scan_arrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"/>
</LinearLayout>
</RelativeLayout>
menu xml
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/change_icon"
android:title="change icon"
/>
<item android:id="#+id/notifications"
android:title="set notification"
/>
</menu>
You should define your settings_img on your adapter getView and add a click listener for each element.
example:
ImageView settingsImgView = (ImageView) convertView.findViewById(R.id.settings_img);
settingsImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.showPopupMenu() //or popupmenu logic in here if you want
}
});
Usually, when it comes to the adapter, I pass the activity itself instead of the context. Then I pass the activity's context to super() and keep the activity in the field variable. That way I'm able to use all public methods of the parent activity.
I searched a lot to find a direct and clear solution for I think a popular problem but unfortunately I couldn't find it.
We want to have a list of cardviews so each card bind to a specific data and each card has a list inside that shows meta or detail data about its parent.
So we have a nested list inside a cardview.
With a simple search, we know that we should use expanded list view which parents items are cardviews and we must have another layout for its child.
So when you click on cards a list of items appears below of your cards on the root.
But we want to show child list inside of cards?
And there is no access to the something like child list id or any other things to refer to a transition animation and change of layout.
So the clear question is how to add a listview inside a cardview?
i follow my work by using tablelayout and table row. and prefer not to use external libraries for stability and version problems.
this is what i mean.
Image that describe my question
If you don't want to use an external library, you can make your CardView expand like this:
Layout file for an expandable CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<TextView
android:id="#+id/textView_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!--My dropdown Button -->
<RelativeLayout
android:id="#+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:gravity="center"
>
<View
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/triangle"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
</RelativeLayout>
</RelativeLayout>
<!--The layout below is my ExpandableLayout -->
<LinearLayout
android:id="#+id/expandableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView_Owner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Owner"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView_OwnerUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My ExpandableRecyclerAdapter Class
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
private List<Repo> repos;
private SparseBooleanArray expandState = new SparseBooleanArray();
private Context context;
public ExpandableRecyclerAdapter(List<Repo> repos) {
this.repos = repos;
//set initial expanded state to false
for (int i = 0; i < repos.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ExpandableRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
this.context = viewGroup.getContext();
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ExpandableRecyclerAdapter.ViewHolder viewHolder, final int i) {
viewHolder.setIsRecyclable(false);
viewHolder.tvName.setText(repos.get(i).getName());
viewHolder.tvOwnerLogin.setText("Owner: " +repos.get(i).getOwner().getLogin());
viewHolder.tvOwnerUrl.setText(repos.get(i).getOwner().getUrl());
Picasso.with(context)
.load(repos.get(i).getOwner().getImageUrl())
.resize(500, 500)
.centerCrop()
.into(viewHolder.ivOwner);
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i);
}
});
}
#Override
public int getItemCount() {
return repos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvName,tvOwnerLogin, tvOwnerUrl;
private ImageView ivOwner;
public RelativeLayout buttonLayout;
public LinearLayout expandableLayout;
public ViewHolder(View view) {
super(view);
tvName = (TextView)view.findViewById(R.id.textView_name);
tvId = (TextView)view.findViewById(R.id.textView_id);
tvUrl = (TextView)view.findViewById(R.id.textView_url);
tvOwnerLogin = (TextView)view.findViewById(R.id.textView_Owner);
tvOwnerUrl = (TextView)view.findViewById(R.id.textView_OwnerUrl);
ivOwner = (ImageView) view.findViewById(R.id.imageView_Owner);
buttonLayout = (RelativeLayout) view.findViewById(R.id.button);
expandableLayout = (LinearLayout) view.findViewById(R.id.expandableLayout);
}
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE){
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
}else{
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
//Code to rotate button
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
}
In your Activity/Fragment, set up RecyclerView like this
private RecyclerView recyclerView;
private List<Repo> data;
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//fetch data and on ExpandableRecyclerAdapter
recyclerView.setAdapter(new ExpandableRecyclerAdapter(data));
So, its quite simple to create an expandable CardView without external Library. The code is almost exactly the same as using a normal CardView with RecyclerView, the main change is setting the View.GONE/View.VISIBLE on button click.
You can use ExpandLayout,and then add it in cardview.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>
I am creating a student details application in android.I need to display student's photo at the top and display name,roll number,address etc as a table.I added a imageview at the top and listview just below the image view.Can i add the image inside the list view ( Now image is not scrollable ).
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/def"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="#id/list"
android:layout_above="#+id/ad_view"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Activity.java
list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> productsList;
productsList = new ArrayList<HashMap<String, String>>();
/*
adding data from database to productsList
*/
ListAdapter adapter =
new SimpleAdapter(this, productsList, R.layout.full, new String[]{"left", "right"},
new int[]{R.id.left, R.id.right});
list.setAdapter(adapter);
full.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:layout_weight="1"
android:gravity="center"
android:background="#drawable/background_border"
android:padding="15dp"
>
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="cnsdsdsf:"
android:layout_weight="1"
android:gravity="center"
/>
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
you can use
ImageView imageView = new ImageView(this);
listView.addHeaderView(imageView);
to add a imageView on the head of listview
You can treat the top row or any other row as special for the image. in getView() inflate with separate layout for that image according to position.
You can also modify your item list layout file (full.xml) and create a custom listview adapter. This method will give you more freedom in terms of design. You can play around with this full.xml
full.xml
<?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="wrap_content"
android:paddingLeft="64dp"
android:paddingRight="32dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
tools:background="#ffaa00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/studentPicture"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/student_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/studentPicture"
android:layout_toEndOf="#+id/studentPicture"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/left"
android:layout_toEndOf="#+id/left"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
</RelativeLayout>
Then you create a adapter that will use this list item layout file:
StudentsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class StudentsAdapter extends BaseAdapter{
private Context mContext;
private String[] mNames;
private String[] mOtherInfo;
public StudentsAdapter (Context context, String[] mNames, String[] mOtherInfo) {
mContext = context;
mNames = students;
mOtherInfo = mOtherInfo;
}
#Override
public int getCount() {
return mNames.length;
}
#Override
public Object getItem(int position) {
return mNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.full, null);
holder = new ViewHolder();
holder.studentImageView = (ImageView) convertView.findViewById(R.id.studentPicture);
holder.left= (TextView) convertView.findViewById(R.id.left);
holder.right= (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//here you set the picture, name, etc.
holder.studentImageView.setImageResource(/* your method to find specific image view */);
holder.left.setText(mNames[position]);
holder.right.setText(mOtherInfo[position]);
return convertView;
}
private static class ViewHolder {
ImageView studentImageView;
TextView left;
TextView right;
}
}
Your activity.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="No data to display"
android:textColor="#ffffffff"/>
</RelativeLayout>
And now implement this adapter in your Activity.java
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
public class Activity extends Activity {
//you will need to set this data
private String[] mNames;
private String[] mOtherInfo;
ListView mListView;
TextView mEmptyTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mListView = (ListView) findViewById(android.R.id.list);
mEmptyTextView = (TextView) findViewById(android.R.id.empty);
DayAdapter adapter = new DayAdapter(this, mNames, mOtherInfo);
mListView.setAdapter(adapter);
mListView.setEmptyView(mEmptyTextView);
}
}
I can't see the row that I've added to a list view and am wondering if it has something to do with my layout or ListViewAdapter. In debug I can see that my array is filled with the data passed to my ArrayList playerList, and I see the data in Toast but nothing shows up on screen.
MainActivity 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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/txtV_battingOrder_Title"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddButton_title" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.89" >
</ListView>
</LinearLayout>
New Row Layout:
<?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:layout_weight="1"
android:background="#android:color/transparent"
android:padding="5dp" >
<TextView
android:id="#+id/txtV_player_input_position_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="21dp"
android:width="180dp" />
<Button
android:id="#+id/btn_Edit_Title"
style="?android:attr/buttonStyleSmall"
android:layout_width="#dimen/btn_edit_player_width"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtV_player_input_position_title"
android:layout_alignBottom="#+id/txtV_player_input_position_title"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:padding="3dp"
android:text="#string/bnt_Edit_title" />
</RelativeLayout>
Custom ListViewAdapter
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<String> playerList;
public CustomListViewAdapter(Activity context, List<String> playerList) {
super();
this.playerList = playerList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return playerList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder {
public TextView text;
public Button edit;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String player = playerList.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.new_player_view, null);
ViewHolder holder = new ViewHolder();
// assign components from new_player_view
// TextView playerInfo = (TextView)vi.findViewById(R.id.txtV_player_input_position_title);
holder.text = (TextView)vi.findViewById(R.id.txtV_player_input_position_title);
holder.edit = (Button)vi.findViewById(R.id.btn_Edit_Title);
// Button editButton = (Button) vi.findViewById(R.id.btn_Edit_Title);
holder.edit.setOnClickListener(MainActivity.editPlayerButtonListener);
holder.text.setText(player);
//playerInfo.setText("this is a test");
vi.setTag(holder);
return vi;
}
First of all, you are using too much the weight property in your xml.. you should use it wisely, I don't know why the RelativeLayout has the weight 1 but anyway, I think the issue you have might be one of the following:
make sure getCount() returns > 0
make sure your are using this method to do the inflate: inflater.inflate(R.layout.new_player_view, parent, false);
change the row layout to the follwing:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp" >
<TextView
android:id="#+id/txtV_player_input_position_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"/>
<Button
android:id="#+id/btn_Edit_Title"
style="?android:attr/buttonStyleSmall"
android:layout_width="#dimen/btn_edit_player_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:padding="3dp"
android:text="#string/bnt_Edit_title" />
And the main layout ListView to:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I hope something will help you, otherwise, please post the full code of the adapter.
EDIT:
Please try to look at this example of ViewHolder pattern implementation, that is working and it should help you implement yours better.
You have set layout_weight as 0.89 without setting the weightSum for the parent layout. Set android:weightSum = "1" for your parent LinearLayout
I am new to Android, and the first project I am working on is putting players onto a team. I have a listview that grabs all of the players from a database. Each name is supposed to have a checkbox next to them and then there is a button at the bottom that is supposed to update the database. So far this is what I am getting.
http://dyp.im/W79JdmfIk
Here is my activity
package com.example.sqllitetest;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Tester extends ListActivity {
MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new PlayersDBAdapter(this);
mDbHelper.open();
Cursor myCur = mDbHelper.fetchAllPlayers();
startManagingCursor(myCur);
setContentView(R.layout.playertoteam);
// registerForContextMenu(getListView());
// ((Tester) context).setContentView(R.layout.playertoteam);
//getListView().addFooterView(button);
mListAdapter = new MyAdapter(Tester.this, myCur);
setListAdapter((ListAdapter) mListAdapter);
Button button = (Button) findViewById(R.id.alertBox);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.playertoteam, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.playertoteam, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.code);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);
tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
}
}
}
And here is my xml file
<?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="vertical"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/alertBox"
/>
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
I would like to know how to remove the buttons from the rows and the check box that is underneath the main confirm button. Also I would like to know if this is the best way to do something like this, because, once again, I am new and I don't know how to search for the correct methods.
ResourceCursorAdapter creates views ,for every row of the ListView, that are inflated in newView() method. You are getting button in every row because of this reason.
Soln: Create a separate xml defining the row layout for the ListView and inflate this new layout in the newView() method. And have only the button and ListView in main layout i.e. playertoteam.xml
playertoteam.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:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/confirm" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/alertBox" />
</RelativeLayout>
row_layout.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">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can find example for ListView here
you can use addFooterView method of listview.
View footerView =
((LayoutInflater)
ActivityContext.
getSystemService
(Context.LAYOUT_
INFLATER_SERVICE)).inflate
(R.layout.footer_
layout, null, false);
ListView.addFooterView
(footerView);