This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture
I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.
Please provide some directions.
create_team_new.xml(layout where gridview resides)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/parentcreateteam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TextView
android:id="#+id/citytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:layout_alignTop="#+id/teamcity"
android:layout_alignParentLeft="true" />
<AutoCompleteTextView
android:id="#+id/teamname"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/teamcity"
android:layout_alignRight="#+id/gridviewplayer"
android:text="#string/teamname"
android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/createteam"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Create Team"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/red_button"
style="#+style/button_text" />
<TextView
android:id="#+id/teamnametextview"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignBaseline="#+id/teamname"
android:layout_alignBottom="#+id/teamname"
android:layout_alignParentLeft="true"
android:text="#string/teamname" />
<AutoCompleteTextView
android:id="#+id/teamcity"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cricket"
android:layout_alignRight="#+id/gridviewplayer"
android:layout_below="#+id/teamname"
android:layout_marginTop="22dp"
android:ems="10"
android:text="#string/city"
android:textSize="18sp" >
</AutoCompleteTextView>
<TextView
android:id="#+id/radiotextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/teamcity"
android:layout_marginTop="15dp"
android:text="Pick a Sport" />
<GridView
android:id="#+id/gridviewplayer"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_above="#+id/createteam"
android:layout_below="#+id/pick_sport"
android:layout_centerHorizontal="true"
android:background="#drawable/gv_bkg"
android:padding="5dp"
android:numColumns="4" >
</GridView>
<RadioGroup
android:id="#+id/pick_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/createteam"
android:layout_below="#+id/radiotextview"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/cricket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<RadioButton
android:id="#+id/soccer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer" />
</RadioGroup>
</RelativeLayout>
gv_createplayer.xml(view to be inflated inside gridview)
<?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"
>
<Button android:id="#+id/btn_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text= "+"
android:background="#drawable/red_button">
</Button>
</LinearLayout>
PlayerAdapter.java(gridview Adapter)
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.GridView;
import android.widget.ImageView;
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
// Keep all Images in array
public Integer[] mThumbIds = {};
/* R.drawable.gridview_createteam, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15
};
*/
// Constructor
public PlayerAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressWarnings("static-access")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = new View(mContext);
view.inflate(mContext, R.layout.gv_createplayer,null);
return view;
}
}
1) How to create a list view inside a main view, only using a half of the screen.
This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.
2) How to populate list view in grid manner (a row of two columns will repeat itself)
Well, with the GridView (the adapter API is the same) (:
Related
I want to make a app like that custom layout ScreenShot: .This is one row and I add like that row 100 more.So the people can clik number or its information(i).
But it is shown like that : two buttons overlapping .But I want two button next to each other it is like that in custom_layout.xml(you can see screen shot).
This is my custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="87dp" />
<Button
android:id="#+id/button2"
android:layout_width="51dp"
android:layout_height="49dp"
android:text="Button"
tools:layout_editor_absoluteX="317dp"
tools:layout_editor_absoluteY="87dp" />
</android.support.constraint.ConstraintLayout>
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="162dp"
android:layout_height="424dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
</ScrollView>
And this is my MainActivity.java:
package com.hey.task.attendancesystem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button[] button=new Button[15];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
CustomAdapter customAdapter = new CustomAdapter();
//listeyi customlayouttaki şekilde oluşturdum hepsini
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return button.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
Button button1 = (Button) view.findViewById(R.id.button);
Button button2 = (Button) view.findViewById(R.id.button2);
for (int j = 0;j<15;j++)
button1.setText(i + "");
for (int j = 0;j<15;j++)
button2.setText("i");
return view;
}
}
}
I don't know if i get your question but you can simply use LinearLayout with weight. eg:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 2" />
</LinearLayout>
You can use linear layout with a android:weightSum="" attribute in place of constraint layout in your custom_layout.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:weightSum="1.0"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.2"/>
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.8"/>
</LinearLayout>
weightSum attribute is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.
I have a list view that for each item needs to display an image, a book title, the author and the price. Under normal circumstances my output is fine and looks like the following
My client has a large portion of customers that need to set the font size to huge. When this is done and the authors name is very long my output looks like below
This is not right and I would like it to look like this instead
In other words the height of list view and elements inside grow to fit larger amounts of text.
Here is the code which replicates my problem.
The book class
public class Book {
public String name;
public String author;
public String price;
public Book(String name, String author, String price){
this.name = name;
this.author = author;
this.price = price;
}
}
The main activity code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
ArrayList<Book> bookList = new ArrayList<Book>();
bookList.add(new Book("Book 1", "By Christopher Christopheson", "£0.50"));
bookList.add(new Book("Book 2", "Author 2", "£0.75"));
BookAdapter adapter = new BookAdapter(this, R.layout.row, bookList);
listView.setAdapter(adapter);
}
}
The book list view adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import android.widget.TextView;
public class BookAdapter extends ArrayAdapter<Book> {
private ArrayList<Book> items;
private LayoutInflater lInflater;
public BookAdapter(Context context, int textViewResourceId, ArrayList<Book> items){
super(context, textViewResourceId, items);
lInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
v = lInflater.inflate(R.layout.row, null);
}
Book b = items.get(position);
if(b!= null){
TextView bookName = (TextView)v.findViewById(R.id.book_name);
TextView bookPrice = (TextView)v.findViewById(R.id.book_price);
TextView bookAuthor = (TextView)v.findViewById(R.id.book_author);
if(bookName != null){
bookName.setText(b.name);
}
if(bookPrice != null){
bookPrice.setText(b.price);
}
if(bookAuthor != null){
bookAuthor.setText(b.author);
}
}
return v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="barnetttabs.com.basiclistview.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
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="wrap_content"
android:minHeight="150dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
>
<TextView
android:id="#+id/price_background"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#0000ff"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:minHeight="150dp"
android:layout_toLeftOf="#id/price_background"
android:layout_alignParentLeft="true"
android:gravity="top"
android:background="#d3d3d3"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:paddingLeft="110dp"
android:paddingRight="4dp"
/>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/lvplaceholderimage"
/>
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/book_image"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/book_price"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
I've tried fixing the layout_height of the relative layout in row.xml. This keeps the height consistent but when the author is too long some of the text is hidden. When I've searched for solutions on this most things say the solution is to set height as wrap_content which I have done but it doesn't the problem.
Is there a way to fix this through either changing the xml layout or the java code? Maybe the relative layout I've used here in row.xml is not the right approach?
Chetan's suggestion was the way to go. The basic idea is to change from a relative layout to a horizontal linear layout. The middle portion is given weight 0 and the 2 end bits have their lengths set as before. The only drawback is the method needs to have a nested layout to get the blue bit to work but hopefully not too much of a performance hit.
In activity_main.xml change the listview code to
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="5.0sp"
android:layout_width="match_parent">
</ListView>
Change row.xml to
<?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:minHeight="150dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:background="#d3d3d3"
>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lvplaceholderimage"
android:layout_gravity="top"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_gravity="top"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
/>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:background="#0000ff">
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignParentTop="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_below="#id/book_price"
android:layout_alignParentBottom="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
</LinearLayout>
Then it looks like this
I have two listviews, but they don't scroll. How do I correct this?
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/backgrund" >
<!-- Header Starts -->
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:background="#layout/header" >
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<TextView
android:id="#+id/textAD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/header"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:layout_marginRight="26dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_gravity="center_horizontal"
android:focusable="false"
android:paddingBottom="5px"
android:paddingTop="10px"
android:src="#android:drawable/divider_horizontal_bright" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000"
android:focusable="false" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:orientation="vertical" >
<TextView
android:id="#+id/textm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Malzemeler"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:id="#+id/editaramalzeme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/btnmalzlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:text="Ara" />
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<ListView
android:id="#+id/listsecili"
android:layout_width="wrap_content"
android:layout_height="210dp"
android:layout_weight="1"
android:background="#FFFFFF" >
</ListView>
<EditText
android:id="#+id/txtNot"
android:layout_width="match_parent"
android:layout_height="88dp"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="6"
android:singleLine="false" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/btnkaydet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="malzeme ekle" />
<Button
android:id="#+id/btntoplugonder"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textAD"
android:layout_below="#+id/btnkaydet"
android:text="toplu gonder" />
</RelativeLayout>
</ScrollView>**
Never put ListView in ScrollView. ListView itself is scrollable.
By default ListView is scrollable. Do not put ScrollView to the ListView
I know this question is 4-5 years old, but still, this might be useful:
Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. That's because the operating system doesn't view it as actually exceeding the screen.
I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. And it was a real mystery. As soon as I added a few more, it started to scroll.
So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". It did, for me.
Practically its not good to do. But if you want to do like this, just make listview's height fixed to wrap_content.
android:layout_height="wrap_content"
Listview so have inbuild scrolling capabilities. So you can not use listview inside scrollview. Encapsulate it in any other layout like LinearLayout or RelativeLayout.
This is my working code. you may try with this.
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:background="#ffffff">
<TextView android:id="#+id/tvEmpId"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.3"/>
<TextView android:id="#+id/tvNameEmp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.5"/>
<TextView
android:layout_height="wrap_content"
android:id="#+id/tvStatusEmp"
android:textSize="12sp"
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="0.2"/>
</LinearLayout>
details.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listEmployeeDetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/page_bg"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lLayoutGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/page_bg"
android:orientation="vertical" >
................... others components here............................
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alwaysDrawnWithCache="true"
android:dividerHeight="1dp"
android:horizontalSpacing="3dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:verticalSpacing="3dp"
android:layout_marginBottom="30dp">
</ListView>
</LinearLayout>
</RelativeLayout>
Adapter class :
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<EmployeeBean> employeeList;
publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
this.context = context;
this.employeeList = employeeList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
EmployeeBeanHolder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
holder = new EmployeeBeanHolder();
holder.employeeBean = employeeList.get(position);
holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);
row.setTag(holder);
holder.tvEmpId.setText(holder.employeeBean.getEmpId());
holder.tvName.setText(holder.employeeBean.getName());
holder.tvStatus.setText(holder.employeeBean.getStatus());
if (position % 2 == 0) {
row.setBackgroundColor(Color.rgb(213, 229, 241));
} else {
row.setBackgroundColor(Color.rgb(255, 255, 255));
}
return row;
}
public static class EmployeeBeanHolder {
EmployeeBean employeeBean;
TextView tvEmpId;
TextView tvName;
TextView tvStatus;
}
#Override
public int getCount() {
return employeeList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
employee bean class:
public class EmployeeBean {
private String empId;
private String name;
private String status;
public EmployeeBean(){
}
public EmployeeBean(String empId, String name, String status) {
this.empId= empId;
this.name = name;
this.status = status;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId= empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status =status;
}
}
in Activity class:
onCreate method:
public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();
//create emplyee data
for(int i=0;i<=10;i++) {
EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
EMPLOYEE_LIST .add(emplyee );
}
ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
Putting ListView inside a ScrollView is never inspired.
But if you want your posted XML-like behavior, there're 3 options to me:
Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).
Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)
If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect.
To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
I found a tricky solution... which works only in a RelativeLayout.
We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview
android:clickable="false" />
<View
android:layout_width="match_parent"
android:background="#drawable/gradient_white"
android:layout_height="match_parent"
android:clickable="true"
android:layout_centerHorizontal="true"
android:layout_alignTop="#+id/listview" />
I had this problem and I found the solution. In my case the listView was bigger than the screen, so the list was not big enough to need scroll, but I coudn´t see it because half list was out of screen.
I solved it adding enough marginBotton to make the listView shorter and it suits to the screen.
<ListView
android:id="#+id/lvDatos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="300dp"/>
I have design like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="horizontal" >
<Button
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<Button
android:id="#+id/nextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
<!-- the two columns part -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:id="#+id/submenue"
>
<!-- this will be the menue list -->
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contents"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.20"
android:id="#+id/mainLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second Name" />
</LinearLayout>
</LinearLayout>
I added the following code to the next button such that it display and fill the list
this.next = (Button)this.findViewById(R.id.nextbutton);
this.next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] MainMenue = new String[] { "aaaa", "bbb", "ccc"};
menueview = (ListView)findViewById(R.id.MyHiddenListView);
menueview.setVisibility(ListView.VISIBLE);
menueview.setAdapter(new submenueadapter(getApplicationContext(), MainMenue));
}
});
and the adaptor class is
package com.appnetics;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class submenueadapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public submenueadapter(Context context,String[] objects)
{
super(context, R.layout.main, objects);
this.context = context;
this.values = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.contents);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
return rowView;
}
}
the problem is that the list be populated with the array plus the other controls in the page (the 2 buttons )
the layout before clicking next
the layout after clicking
any idea to fix that please
Best regards
Best regards
The main problem is that you are including everything in one main.xml layout.
And due to this problem, you are getting back/next button for every listitem (Actually whole layout is being repeating as Listitem).
Soluition:
Instead, Try to define separate layout for the ListItem and inflate that layout inside the getView() method. so there must be 2 XML layouts file, one is having main design with ListView only, and another one with only views that you want for every list item.
for example, (1) main.xml (2) row_item.xml
Now, you just need to make correction in code of inflating layout:
View rowView = inflater.inflate(R.layout.row_item, parent, false); // row_item.xml is inflated here
ViewGroup class do offers various methods to add view anywhere and using LayoutParams classes you can pass the various parent container class specific attributes to applied over the child view to be added in the container.
That you can google and is easily doable.
I can also think of putting a ListView in the XML with the Visibility GONE.
And on specific action trigger make it visible. That will prevent me doing various programing stuff required to add view at a perticular position.
And i also don't feel any harm of this approach.
e.g
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</Linearlayout>
In the .java file
btnBack = (Button)findViewById(R.id.backbutton);
myListView = (ListView)findViewById(R.id.MyHiddenListView);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myListView.setVisibility(ListView.VISIBLE);
myListView.setAdaptor(new MyListViewAdaptor());
}
});
Hope it helps :)
I have made custom listview in android activity which has progressbar. On top of that listview there is another progress bar which shows progress as day passes in the month. Now i want to set a bar according to the progress of the month progress bar in custom listview which has four progress bars. Please see the attached image.
The progress of the month progress bar and the bar which we put in listview progress bar should be at same position means aligned and this bar which we put should move when the top progress increases as day passes.
In Layout file I have used LinearLayout where I want to put the bar.
<?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="wrap_content"
android:layout_marginTop="5dip">
<RelativeLayout android:id="#+id/Top"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content">
<TextView android:id="#+id/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_alignParentLeft="true" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true">
<TextView android:id="#+id/TextOne" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_marginRight="5dip" />
<TextView android:id="#+id/TextTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/outof"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextOne" android:layout_marginRight="5dip" />
<TextView android:id="#+id/RightText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextTwo" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/Bottom"
android:layout_below="#+id/Top" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true">
<ProgressBar android:id="#+id/CustomPro"
android:layout_width="250dip" android:layout_height="wrap_content"
android:progress="0" android:max="100" android:secondaryProgress="0"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/accountdataprogress"
android:layout_centerHorizontal="true" />
<TextView android:id="#+id/MiddleText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0%"
android:textColor="#000000" android:textStyle="bold"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:id="#+id/Middle"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content" android:layout_below="#+id/Top">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/PuttingBar"
android:layout_alignParentLeft="true">
</LinearLayout>
</RelativeLayout>
<View android:layout_width="fill_parent" android:layout_height="10dip"
android:background="#00000000" android:layout_below="#+id/Bottom" />
</RelativeLayout>
In Java I have done something like this.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressListAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<ProgressList> mPList;
private TextView mLeft, mMiddle, mRight, mTextOne;
private ProgressBar mCustomProgressBar;
private LinearLayout mLinearLayout;
public ProgressListAdapter(Context nContext, ArrayList<ProgressList> nPList)
{
this.mContext = nContext;
this.mPList = nPList;
}
#Override
public int getCount()
{
return mPList.size();
}
#Override
public Object getItem(int nPosition)
{
return mPList.get(nPosition);
}
#Override
public long getItemId(int nPosition)
{
return nPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ProgressList mEntry = mPList.get(position);
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.customprogress, null);
}
mLeft = (TextView) convertView.findViewById(R.id.LeftText);
mLeft.setText(mEntry.getValueText());
mMiddle = (TextView) convertView.findViewById(R.id.MiddleText);
mCustomProgressBar = (ProgressBar) convertView.findViewById(R.id.CustomPro);
int mBarPosition = Usage.mElapsed;
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
View mView = new View(mContext);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(2, 25);
layoutParams.setMargins(mBarPosition, 0, 0, 0);
mView.setMinimumHeight(25);
mView.setMinimumWidth(2);
mView.setBackgroundColor(Color.rgb(12, 56, 99));
mLinearLayout.addView(mView, layoutParams);
mView.invalidate();
if(mEntry.getValueNumber() > mBarPosition)
{
mCustomProgressBar.setProgress(mBarPosition);
mCustomProgressBar.setSecondaryProgress(mEntry.getValueNumber());
}
else if(mEntry.getValueNumber() <= mBarPosition)
{
mCustomProgressBar.setProgress((mEntry.getValueNumber()));
mCustomProgressBar.setSecondaryProgress(0);
}
mMiddle.setText(Integer.toString(mEntry.getValueNumber()) + "%");
mTextOne = (TextView) convertView.findViewById(R.id.TextOne);
mTextOne.setText(mEntry.getValue());
mRight = (TextView) convertView.findViewById(R.id.RightText);
mRight.setText(mEntry.getValueOne());
return convertView;
}
}
But When I run It shows extra bar in progress bar like this
Anybody has done kind of work please give me your hand. Waiting for fruitful reply.
Thanks.
You should add call to removeAllViews in your getView funcion:
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
mLinearLayout.removeAllViews();
View mView = new View(mContext);
That's because LinearLayout accumulates views, and there might be multiple calls to your addView function. So you need to remove already added view before adding another PuttingBar with new margin.