how to add full scrollview in layout? - android

In the below code how to scroll not just the ListView, but all of the Views with it? If I am adding full page scrollview then listview not showing full page.
But top menu should be constant. Inside search button I want to scroll full list.
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#027fdb"
android:id="#+id/linearlayout1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="Contact -"
android:id="#+id/textView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_mylead_listcount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_alignRight="#+id/textView"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/lin_symbols"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_marginRight="20dp">
<LinearLayout
android:id="#+id/lin_addmechines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right">
<ImageView
android:id="#+id/img_addmachines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_machine"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lin_addmechines"
android:layout_gravity="center_vertical"
android:gravity="right"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/img_addfilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/filter"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/search"
android:hint=" Search"
android:paddingLeft="5dp"
android:imeOptions="actionSearch"
android:inputType="text"
android:id="#+id/textSearch"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin_contactPersons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:id="#+id/img_contactprs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/contactprs"
/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<ListView
android:id="#+id/users"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

Use this function to full view list on scroll view.
ListView listViewObject;
// after binding list view.
getListViewSize(listViewObject);// pass your listview object here.
public void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
Hope! it is helpful to you...

You should copy the layouts, you want to scroll with the listview in a different layout XML and put it to the top of the listview as a different first item. You can do it with a custom ListAdapter:
//WARNING: I did not try this code, handle it like pseudocode
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<Item> mData = new ArrayList<>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Item item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size() + 1;
}
#Override
public Item getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(position==0){
//initialize your custom view
}else{
//initialize a normal list item
}
return convertView;
}
}
Check this ListAdapter example, if you still have questions!

Related

custom listview shows large space between them when adding a new item by clicking on button in android studio

I have got a custom listview. I am trying to add items to the custom listview by clicking on the button. When I click the button, I want the items to show one under the other but it shows a big space between items. Data read from the database are listed in the listview at the same time.it works like chat applications. Thanks for your help
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Mainactivity">
<ListView
android:id="#+id/listid"
android:layout_width="match_parent"
android:divider="#drawable/dessen"
android:layout_above="#+id/lineerid"
android:dividerHeight="3dp"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:id="#+id/lineerid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="0dp"
android:layout_marginBottom="3dp"
android:background="#91f1f1f1"
android:orientation="horizontal"
android:paddingBottom="2dp">
<EditText
android:id="#+id/mesajid"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/gonderid"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/sendMessageButton"
android:layout_weight="0.72"
android:ems="10"
android:maxHeight="80dp" />
<Button
android:id="#+id/gonderid"
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:background="#drawable/buttonsekil"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Gönder" />
</LinearLayout>
</RelativeLayout>
custom _layout
<?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:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nickid"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/tarihid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/formesajid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:textColorLink="#9C27B0"
android:textSize="16sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</LinearLayout>
custom adapter
public class customadapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<Model> modelList;
public customadapter(Activity activity,List<Model> modelList) {
layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.modelList=modelList;
}
#Override
public int getCount() {
return modelList.size();
}
#Override
public Object getItem(int position) {
return modelList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
view=layoutInflater.inflate(R.layout.custom_layout,null);
TextView nick =view.findViewById(R.id.nickid);
TextView tarih=view.findViewById(R.id.tarihid);
TextView mesaj=view.findViewById(R.id.formesajid);
Model model=modelList.get(position);
nick.setText(model.getNickname());
tarih.setText(model.getTarih());
mesaj.setText(model.getMesaj());
return view;
}
}

Set ListView Height programmatically in Android

I have a ListView in my activity's Layout that exists in a ScrollView like that:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:padding="4dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="#string/add_to_my_resources"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/article_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<ListView
android:id="#+id/authors_listView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:divider="#color/light_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="#null"
android:scrollbars="none"
android:textAlignment="center">
</ExpandableListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
And I have a List Adapter names MyListAdapter that populate my listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.my_list, null);
holder = new ViewHolder();
holder.txtViewName = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewAff = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewName.setGravity(Gravity.LEFT);
holder.txtViewName.setText(name.get(position));
if (aff != null) {
holder.txtViewAff.setGravity(Gravity.LEFT);
holder.txtViewAff.setText(aff.get(position));
}
return convertView;
}
And my_list.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/ic_draw"/>
</RelativeLayout>
I want to set listview height dynamically to avoid scrolling list view inside the parent layout. So I use this method:
public static void setListViewHeightBasedOnChildren(ListView listView) {
AuthorsListAdapter listAdapter = (AuthorsListAdapter) listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LinearLayoutCompat.LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
This method works correctly in normal but when txtViewAff value is more than 2 or 3 lines, the last item of listview goes behind of the ExpandableListView. I add constant value like 40 to my totalHeight in method setListViewHeightBasedOnChildren:
totalHeight += view.getMeasuredHeight() + 40;
but it adds some blank space for normal items that were true without adding 40.
What can I do to resolve this problem?

Gravity center text not working for first child element of Gridview

I'v created adapter with textview which set text to child elements in gridview. Below is my Adapter Code-
class CustomAdapter1 extends BaseAdapter
{
private Context context;
private List<String> list;
LayoutInflater inflater;
public CustomAdapter1(Context context,List<String> list)
{
this.context=context;
this.list=list;
inflater= (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return list.size();
}
#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) {
TextView textView;
if(convertView==null)
{
convertView=inflater.inflate(R.layout.singlie_textview, null);
}
else
{
textView=(TextView)convertView;
}
textView=(TextView)convertView.findViewById(R.id.text1);
textView.setText(list.get(position));
return textView;
}
}
I set gravity of textview to center but my problem is that first child element of gridview not centered. I've tried using internal list layout of android device it works perfectly. Then what will be the problem? thanks in advance.
Below is my Xml layout-
<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="com.urva.educationapp.BarakhadiActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:padding="3dp"
android:layout_weight="4">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/Maintext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:textSize="60dp" />
</RelativeLayout>
<GridView
android:id="#+id/gridView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:numColumns="3"
android:layout_gravity="right"
android:layout_weight="2"
>
</GridView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_gravity="right"
android:layout_margin="2dp"/>
<com.urva.educationapp.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#fff" />
</LinearLayout>
</LinearLayout>
Here is my textview xml file -
<?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" >
<TextView
android:id="#+id/text1"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="1dp"/>
</RelativeLayout>

Set Text on a GridView Image in Android

I have a grid view that have 8 images
Now on a particular image view i want to set a text value that will be changed dynamically
Main XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/blue_bar"
android:orientation="horizontal" >
</LinearLayout>
<GridView
android:id="#+id/gridView1"
android:layout_below="#+id/mainLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
**Inner XML Layout **
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Code Main Activity
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, DASHBOARD_LINKS));
Adapter Class
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] dashBoardValues;
public ImageAdapter(Context context, String[] dashBoardValues) {
this.context = context;
this.dashBoardValues = dashBoardValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from dashboard_inner.xml
gridView = inflater.inflate(R.layout.dashboard_inner, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.ic);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return dashBoardValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
So how could i do this in a grid view .Please help me .
As others have already suggested you can use a Relative Layout or FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="20dp"
android:text="TextView" />
</RelativeLayout>
Snap
Check this answer kcoppock which will give you an idea of how to use framelayout
Placing/Overlapping(z-index) a view above another view in android
Also it is better to use a ViewHolder Pattern
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Here is actual working code
public class MainActivity extends Activity {
private static final String TAG = "AAA";
private static Handler mHandler;
private LayoutInflater layoutInflater;
private GridView mGridView;
private int click = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layoutInflater = LayoutInflater.from(getApplicationContext());
click = 0;
setContentView(R.layout.my_grid_view);
mGridView = (GridView)findViewById(R.id.my_grid_view);
mGridView.setAdapter(new MyAdapter());
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterViews, View pView, int position, long id) {
click++;
TextView textView = (TextView)pView.findViewById(R.id.my_text_view);
if(textView!=null){
textView.setText("Changed : Position = " + position + " : Click No = " + click);
}else{
Log.d(TAG, "Fish : textView = " + textView);
}
}
});
}
private class MyAdapter extends BaseAdapter{
#Override
public int getCount() {
return 200;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){ //This is memory efficient, but after certain no of time u will get similar view repetition,
//to over come that overwrite getViewTypeCount() & provide proper way of persistent view & data management mechanism.
convertView = layoutInflater.inflate(R.layout.child_one, null);
}
return convertView;
}
}}
And XMLs
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
And
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/my_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="#drawable/flower_1" />
<TextView
android:id="#+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#android:color/darker_gray"
android:text="Default Text" />
</FrameLayout>
instead of image view pass framelayout reference. it will work.
framelayout is view group which sub class of view only.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView android:scaleType="fitXY"
android:src="#drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image_view"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_gravity="top|right"
android:background="#android:color/darker_gray"
android:layout_height="wrap_content"
android:id="#+id/badge_view"
android:text="10"/>
</FrameLayout>
snap shot
Get this image and add to your project drawble. new http://www.flickr.com/photos/113556524#N05/11793569304/
This is the screen shot nw http://www.flickr.com/photos/113556524#N05/11793569304/
This your xml code to use :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/aaa"
android:text="" />
</LinearLayout>
</LinearLayout>
In your activity java file use this :
package com.example.dashboard;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2,b4,b3,b5,b6,b7,b8;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.neww);
b1 =(Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b5 = (Button) findViewById(R.id.button7);
b6 = (Button) findViewById(R.id.button8);
/* Wat ever you want validation , anything do here */
// if you need to set text to button
b1.setText("Hi pooja");
// blah blah blah - wat ever you want to any button text
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Got it ;)
Use FrameLayout as child of GridView. Within FrameLayout first put ImageView & on top of it put a TextView as mentioned by "sush".
Then you can change Visibility & content of TextView on demand, through program.
It is not working for you because probably for all TextViews of you have put same "android:id" put different id for different TextView. It is bound to work. I've tested.
http://i.stack.imgur.com/HUY4o.png Check the sample image...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/cover"
android:layout_width="148dp"
android:layout_height="148dp"
android:src="#drawable/empty_photo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cover"
android:background="#70000000"
android:padding="6dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
---> Use this layout and inflate it in your adapter.You will get the text at bottom of image.

android list view is not being shown on 4.1.2 version

I have a list view inside a ViewSwitcher that works fine in android version 2.2 but it does not show in my device which has android 4.1.2 version.
I also debugg it and dont see any errors, and also checked to see if the list I pass to adapter is not null, and saw that it was not empty.
I have another list view which is located in a TabHost that is being shown correctly.
Regards
Edited:
Here is my code:
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewswitcher"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:layout_gravity="center|top"
>
<TextView
android:id="#+id/dateLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/lblChooseaDate" />
<DatePicker
android:id="#+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center" />
<Button
android:id="#+id/btnChooseDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/startdayexpensebtn" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
<TextView
android:id="#+id/descLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/lbldaydescription" />
<EditText
android:id="#+id/dayDesctiptionText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:singleLine="false"
android:minLines="1"
android:maxLines="3"
android:inputType="textMultiLine" />
<Button
android:id="#+id/addnewItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:enabled="false"
android:text="#string/btnAddnewItem" />
<LinearLayout
android:id="#+id/listcontaioner"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scrollbars="vertical"
>
<ListView
android:id="#+id/android:list"
android:divider="#bababa"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/btnlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:paddingRight="10dp"
android:paddingLeft="10dp" >
<Button
android:id="#+id/cancelbtn"
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btncancel" />
<Button
android:id="#+id/savebtn"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btnAdd" />
</LinearLayout>
</LinearLayout>
</ViewSwitcher>
also for each row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp" >
<ImageView
android:id="#+id/expenselisitem_imgaetype"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="35dp"
android:src="#drawable/fail" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/expenselisitem_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/expenselisitem_category"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/expenselisitem_desc"
android:paddingBottom="6dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/expenselisitem_amount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/expenselisitem_category"
android:layout_alignBottom="#+id/expenselisitem_category"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:paddingBottom="6dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
and here is the adapter:
public class CustomAddExpenseItemListAdapter extends BaseAdapter {
private ArrayList<ExpenseItem> _list=new ArrayList<ExpenseItem>();
private final Activity _context;
private static LayoutInflater inflater=null;
public CustomAddExpenseItemListAdapter(Activity activity,ArrayList<ExpenseItem> data){
_list = data;
_context=activity;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return _list.size();
}
public Object getItem(int position) {
return position;
}
static class ViewHolder {
protected TextView _cexpenseDesctiption;
protected TextView _cAmount;
protected ImageView _cType;
protected TextView _cCategory;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=null;
if(convertView==null)
{
inflater = _context.getLayoutInflater();
vi = inflater.inflate(R.layout.expense_item_list_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder._cexpenseDesctiption= (TextView) vi.findViewById(R.id.expenselisitem_desc);
viewHolder._cAmount = (TextView) vi.findViewById(R.id.expenselisitem_amount);
viewHolder._cType=(ImageView)vi.findViewById(R.id.expenselisitem_imgaetype);
viewHolder._cCategory=(TextView)vi.findViewById(R.id.expenselisitem_category);
vi.setTag(viewHolder);
}
else
{
vi = convertView;
}
ViewHolder holder = (ViewHolder) vi.getTag();
ExpenseItem item;
item = _list.get(position);
holder._cexpenseDesctiption.setText(item.get_description());
holder._cAmount.setText(String.valueOf(item.get_amount()));
if(item.get_type()==0)
{
holder._cCategory.setText(item.get_category().get_title());
holder._cType.setImageResource(R.drawable.downarrow);
}
else
{
holder._cCategory.setText("-");
holder._cType.setImageResource(R.drawable.uparrow);
}
return vi;
}
}
Usually this is solved by wrapping the ListView inside a LineaLayout or RelativeLayout but depending on the problem you've countered the solution might be something entirely different.
Here is a working example of a ViewSwitcher that switches between a ListView and TextView.
From the following XML text you can see that ListView has been wrapped in a RelativeLayout together with a Button that enables the switch between the views(layouts).
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/viewswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Show Next" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/btn_next"></ListView>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:text="HEllO WORLD">
</TextView>
<Button
android:id="#+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Previous" />
</LinearLayout>
</ViewSwitcher>
</RelativeLayout>
And here is the Java code that delivers the magic:
public class MainActivity extends Activity {
ViewSwitcher mViewSwitcher;
ListView listview;
String[] values = new String[] { "Hello1", "Hello2", "Hello3",
"Hello4", "Hello5", "Hello6", "Hello7", "Hello8",
"Hello9", "Hello10", "Hello11", "Hello12", "Hello13", "Hello14",
"Hello15"};
Button shownext;
Button showprevious;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewSwitcher = (ViewSwitcher)findViewById(R.id.viewswitcher);
listview = (ListView) findViewById(R.id.listview);
shownext = (Button)findViewById(R.id.btn_next);
showprevious = (Button)findViewById(R.id.btn_previous);
shownext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mViewSwitcher.showNext();
}
});
showprevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mViewSwitcher.showPrevious();
}
});
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This has been tested successfully on Android 4.1.2.
Hope that helps!

Categories

Resources