How Can I add a textview on top of a fragment. I'm using the following adapter to populate the listfragment and the xml file is each row in the list fragment. I need to add a textview ontop of listview which must be scrollable along with the list?
public class Adapter extends BaseAdapter {
Context context;
public TextView txtName;
public TextView txtTitle;
private LayoutInflater mInflater;
private Storage storage;
FontManager fontManager;
Typeface typeface;
public Adapter(Context _context,Storage _storage) {
context = _context;
mInflater = LayoutInflater.from(context);
this.storage = _storage;
fontManager = new FontManager(_context);
typeface = fontManager.getTypeFace();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _storage.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = mInflater.inflate(R.layout.row3, null);
txtName = (TextView) convertView.findViewById(R.id.tvName);
txtName.setTypeface(typeface);
txtName.setText(storage.getName(position));
txtTitle = (TextView) convertView.findViewById(R.id.tvTitle);
txtTitle.setTypeface(typeface);
txtTitle.setText(storage.getTitle(position));
return convertView;
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="20dp"
android:scrollbars="none"
android:id="#+id/ll1">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black" />
</LinearLayout>
ListFragment:
public class SampleListFragment extends ListFragment {
int number;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
number = getArguments().getInt(
"num", 0);
new SampleAsyncTask(this, getActivity(), number).execute();
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
}
}
}
Following code is used in AsyncTask class to populate the list fragment
Adapter adapter = new Adapter(context, storage);
listFragment.setListAdapter(adapter);
You can do as below or add a textview as a header to your listivew
Quoting from the docs
ListActivity(ListFragment is similar) has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain aListView` object with the id "#android:id/list" (or list if it's in code)
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.listfragment.MyFragment"
android:id="#+id/frag"
android:layout_above="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
MyFragment.java
public class MyFragment extends ListFragment {
String names[] ={"A","B","C"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.list_frag, container, false);
TextView tv = (TextView) myFragmentView.findViewById(R.id.textView1);
tv.setText("My Header");
setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
return myFragmentView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// on click display the item in toast
Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
}
}
list_frag.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
snap shot
Edit: if you want the textview to scroll
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="TextView" />
</RelativeLayout>
Before calling setListAdapter
View view = inflater.inflate(R.layout.text, null);
TextView textinlfated = (TextView) view.findViewById(R.id.textView1);
ListView lv = getListView();
textinlfated.setText("TextView scrolls");
lv.addHeaderView(view);
change adapter's getView method and put a case on position and in case of 0, return your textview in other cases listview items.
Related
I have a problem with spinner it do not let me select one item. I tried a lot of things and that still not working.
The picture shows that the spinner is in blank when the activity load
When I clicked the arrow it shows the items
but when I choose one, nothing happends.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.Inspeccion.DatosGeneralesActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
>
<TextView
android:id="#+id/tvSubestacionTitulo"
android:layout_below="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSubestacion"
android:textSize="18sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvSubestacionDato"
android:layout_below="#+id/tvSubestacionTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Prueba"
/>
<Spinner
android:id="#+id/spinnerSubEstacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSubestacionDato"
>
</Spinner>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the Layout of the activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvNumeroOpcion"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="1"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDescriptionOption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="Guatemala"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
That is the custom layout for the spinner
Public class ComboAdapter extends BaseAdapter{
private List<Combo> combos;
private Activity activity;
private LayoutInflater inflater;
public ComboAdapter(List<Combo> combos, Activity activity) {
this.combos = combos;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return combos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, null);
TextView tvId = (TextView) view.findViewById(R.id.tvNumeroOpcion);
TextView tvDescripcion = (TextView) view.findViewById(R.id.tvDescriptionOption);
tvId.setText(combos.get(position).getId());
tvDescripcion.setText(combos.get(position).getDescripcion());
}
return view;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getView(position, convertView,parent);
}
}
That is my Adapter
And below is my activity.
public class DatosGeneralesActivity extends AppCompatActivity {
private TextView tvSubestacionDato;
private List<Combo> listaCombo;
private Spinner spinnerSubestacion;
private ArrayAdapter<Combo> adapterSubestacion;
String seleccion;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_generales);
//Inicializando textos
tvSubestacionDato = (TextView) findViewById(R.id.tvSubestacionDato);
//Inicializanco listas
listaCombo = new ArrayList<>();
//Inivializando spinners
spinnerSubestacion = (Spinner) findViewById(R.id.spinnerSubEstacion);
AppService service = API.getCombos().create(AppService.class);
Call<List<Combo>> subestacionCall = service.getSubestacion();
subestacionCall.enqueue(new Callback<List<Combo>>() {
#Override
public void onResponse(Call<List<Combo>> call, Response<List<Combo>> response) {
listaCombo.clear();
listaCombo.addAll(response.body());
}
#Override
public void onFailure(Call<List<Combo>> call, Throwable t) {
}
});
//final ComboAdapter adapter = new ComboAdapter(listaCombo, DatosGeneralesActivity.this);
final ArrayAdapter<Combo> adapter = new ArrayAdapter<Combo>(this, R.layout.support_simple_spinner_dropdown_item, listaCombo);
spinnerSubestacion.setAdapter(adapter);
spinnerSubestacion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
Toast.makeText(DatosGeneralesActivity.this, ""+position, Toast.LENGTH_SHORT).show();
tvSubestacionDato.setText(listaCombo.get(position).getDescripcion());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this changes:
Call adapter like:
ComboAdapter adapter = new ComboAdapter(DatosGeneralesActivity.this,
R.layout.combo_list_item, R.id.tvDescriptionOption, listaCombo);
now in adapter class:
public ComboAdapter(Activity context,int resouceId, int textviewId, List<Combo> list){
super(context,resouceId,textviewId, list);
this.combos = list;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Also inside your getView() method inflate layout like:
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, parent , false);
I am Trying to insert GridView in fragment. I have developed custom adapter to populate the gridview. Now everything is working fine but the gridview is not displaying in the fragment. I have check the getCount() method it is returning total length. So that means data is there but it is not displaying. I am pretty new with gridview so have no idea why it is happening. I tried the following answer but they didn't help me either.
Android: getView in adapter never called
Custom Adapter getView() method is not called
Gridview not displaying in fragment
Here is my code:
Tile Fragment Class:
public class TileFragment extends Fragment
{
public TileFragment(){
}
GridView gv;
Context context;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.mipmap.dicover_disable,R.mipmap.discover_active, R.mipmap.home_disable,
R.mipmap.home_active,R.mipmap.lists_disable,R.mipmap.lists_active,
R.mipmap.profile_disable,R.mipmap.profile_active,R.mipmap.ic_search };
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
#SuppressLint("InflateParams")
// View v = LayoutInflater.from(getActivity()).inflate(R.layout.tile_fragment, null);
View v = inflater.inflate(R.layout.tile_fragment, container, false);
gv = (GridView) v.findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList,prgmImages));
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Custom Adapter:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(FragmentActivity tileFragment, String[] prgmNameList, int[] prgmImages) {
result=prgmNameList;
imageId=prgmImages;
context = tileFragment;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageId.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.programlist, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
tile_fragment:
<?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" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3"
android:visibility="visible">
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>
prgramlist
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
This is how I am adding the fragment:
HomeFragment:
public class HomeFragment extends Fragment
{
public HomeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
FragmentTabHost mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
Bundle b = new Bundle();
b.putString("key", "Tile");
mTabHost.addTab(mTabHost.newTabSpec("tile").setIndicator("Tile"), TileFragment.class, b);
//
b = new Bundle();
b.putString("key", "Map");
mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), ProfileFragment.class, b);
return mTabHost;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//
}
Any Help will be appreciated ...
Try this:
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
where R.layout.my_parent_fragment is your layout fragment where you have the tabhost.
Once you have verified that the GridView is displayed correctly in an Activity, we could deduce that this is not a problem with the Gridview neither the Adapter, the problem come from your FragmentTabHost definition or setup. You are returning your tabHost from your onCreateView(), for that you must set your layout or container in your tabHost, if you don't do this, how the fragment know where find your layout?
For more info, api here
Can't figure out the root of the problem but try this:
View rowView = convertView;
if(rowView != null) {
rowView = inflater.inflate(R.layout.programlist, null);
}
Try using this layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</LinearLayout>
I am creating a gridview with different images and texts from a class containing those images and texts in a fragment. The GridView is inside a TabHost. The TabHost is working fine. I can see other buttons in the same view. But nothing is inside the GridView. Here is the code:
public class MenuPage extends Fragment{
GridView CategoryGridview;
View MainMenuView, HomePageView, GalleryView;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
this.MainMenuView = inflater.inflate(R.layout.mainmenupage, container, false);
this.GalleryView = inflater.inflate(R.layout.gallery, container, false);
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
:
:
:
return MainMenuView;
}
Adapter:
public class GalleryAdapter extends BaseAdapter{
ArrayList<Gallery> list;
Context context;
GalleryAdapter(Context context){
this.context = context;
list = new ArrayList<Gallery>();
int[] TempGalleryId = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q, R.drawable.r, R.drawable.s};
String[] TempGalleryName = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"};
for (int i = 0; i < 19; i++){
Gallery TempGallery = new Gallery(TempGalleryId[i], TempGalleryName[i]);
list.add(TempGallery);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_gallery, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else{
holder = (ViewHolder) row.getTag();
}
Gallery temp = list.get(position);
holder.imageViewGallery.setImageResource(temp.GalleryId);
return row;
}
}
class ViewHolder{
ImageView imageViewGallery;
ViewHolder(View v)
{
imageViewGallery = (ImageView) v.findViewById(R.id.imageViewGallery);
}
}
class Gallery{
int GalleryId;
String GalleryName;
Gallery(int GalleryId, String GalleryName){
this.GalleryId = GalleryId;
this.GalleryName = GalleryName;
}
}
In mainmenupage.xml, one of the tab include gallery.xml file for animation with a gridview imageViewGallery and some buttons in it.
<LinearLayout
android:orientation="vertical"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/DinnerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<include
android:id="#+id/gallery"
layout="#layout/gallery" />
</LinearLayout>
SingleGallery.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/a" />
</RelativeLayout>
This is Gallery.xml
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Back" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<GridView
android:id="#+id/gridViewGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnBack"
android:numColumns="auto_fit"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:stretchMode="spacingWidthUniform">
</GridView>
</RelativeLayout>
I tried change
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
to
CategoryGridview.setAdapter(new GalleryAdapter(context));
and
this.CategoryGridview = GridView)MainMenuView.findViewById(R.id.gridViewGallery);
to
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
still doesn't work.
I also tried open a new project with extend Activity(){}. It work fine with
CategoryGridview.setAdapter(new GalleryAdapter(this));
So I guess the problem is somewhere else. Can anyone help?
I try to implement a longClick in the my list view but I get longClick only in the edit text field: how I can get longClick only over the textView?
Thanks, Daniele
Other details:
the main layout is "activity_main.xml" and in the list view "listView_listData" I put the "row.xml" layout with "RigaAdvAdapter" class;
the layout "row.xml" has two field: textView and editText;
I used a ArrayList to store the data that I want to view in the rows (separated by commas - Ex. "viewTextValue,editTextValue");
I need to get longClick in the each row to offer more options;
I dont't need the longClick in the editText otherwise the copy-paste not work;
In the future I'll need more data, and so I'll use an array of ArrayList;
######################### LAYOUT: activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:orientation="vertical" >
<ListView
android:id="#+id/listView_listData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:listSelector="#drawable/list_selector"
android:fadingEdge="none" >
</ListView>
</LinearLayout>
######################### LAYOUT: row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:shrinkColumns="*" android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/RelativeLayout_rowData"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="3dip"
android:layout_width="0dp"
android:textColor="#666666"
android:focusable="false" />
<EditText
android:id="#+id/editTextField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:inputType="text">
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
MainActivity
public class MainActivity extends Activity
{
ArrayList<String> arrayDataSource= new ArrayList<String>();
...
I fill the array "arrayDataSource" with a string as "text,another text"
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView_listData =(ListView)findViewById(R.id.listView_listData);
listView_listData.setAdapter(new RigaAdvAdapter());
listView_listData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long arg3)
{
mostraToast("hh long click"+String.valueOf(pos));
return true;
}
});
}
Adapter class
public class RigaAdvAdapter extends BaseAdapter
{
#Override
public int getCount() {return arrayDataSource.size();}
#Override
public Object getItem(int position) {return position;}
#Override
public long getItemId(int position) {return position;}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
}
Instead of setting longclick to ListView you have to set it for TextView.In RigaAdvAdapter class you have to add this
textViewField.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
Try to set you edittext property set clickable and focusable to false. As you have added Editttext so it will gets the first focus and touch event always.
And to add the Longclick event on TextView you will have to implement the longclick listener inside your adapter class.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
textViewField.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
You should use onItemLongClickListener inside getView method of the adapter. This question can help Android : How to set onClick event for Button in List item of ListView
I am trying to create a master/detail flow application on Android 4.2.
I have created a master detail flow project, but I want to implement an ExpandableListView instead of the ListView that is provided.
The master/detail uses fragments, and here’s where I am stuck… I have successfully created an expandable list view in a separate project. How can I implement it inside a master/detail flow?
I assume you used the IDE wizard to create the Master/Detail example project. If this is so, then you probably saw that the wizard created an ItemListFragment class which by default extends ListFragment.
If you need to replace the simple list with an expandable one, then you'll have to:
Extend from Fragment, instead of ListFragment
Create an xml layout file where you declare an ExpandableListView
Override onCreateView() and inflate the layout that contains the ExpandableListView
Get a reference to the ExpandableListView, and then use it as you did before.
Something like this:
// extend from Fragment
public class ItemListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the layout that contains the ExpandableListView
View view = inflater.inflate(R.layout.fragment_items_list, container, false);
// get a reference to ExpandableListView
ExpandableListView list = (ExpandableListView)view.findViewById(R.id.my_list);
// set the adapter
// set listeners
return view;
}
}
I have no idea how to create ExpandableListView, but you can achieve with custom listview like I implemented in my Project
1. Create Custom list row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
<RelativeLayout
android:id="#+id/rel_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/imageView1"
android:paddingBottom="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#685f56" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/text1"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/highlight_icon" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rel_main"
android:visibility="gone" >
</RelativeLayout>
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:background="#685f56" />
</RelativeLayout>
**2. Create Custom Adapter in "ItemListFragment.java" **
public class CustomAdepeterNewJob extends BaseAdapter{
//String[] tablecontent;
int Click=0;
Map<String, DummyItem> tablecontent = new HashMap<String, DummyItem>();
Context context;
public CustomAdepeterNewJob(Context context, Map<String, DummyItem> titles)
{
this.context = context;
this.tablecontent = titles;
}
public class ViewHolder {
public TextView txt;
public ImageView img;
public RelativeLayout relativeLayout1,rel_main;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return tablecontent.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.c_simple_list_item, parent, false);
holder = new ViewHolder();
holder.txt = (TextView) view.findViewById(R.id.text1);
holder.img = (ImageView) view.findViewById(R.id.imageView1);
holder.rel_main=(RelativeLayout) view.findViewById(R.id.rel_main);
holder.relativeLayout1=(RelativeLayout) view.findViewById(R.id.relativeLayout1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
String s=DummyContent.ITEMS.get(position).content;
System.out.println("==dummy "+s);
holder.txt.setText(s);
if (s.equalsIgnoreCase("All"))
{
holder.img.setImageResource(R.drawable.all_icon);
}else if (s.equalsIgnoreCase("Note")) {
holder.img.setImageResource(R.drawable.notes_icon);
}else if (s.equalsIgnoreCase("Highlight")) {
holder.img.setImageResource(R.drawable.highlight_icon);
}else if (s.equalsIgnoreCase("Snapshots")) {
holder.img.setImageResource(R.drawable.snapshot_icon);
}else if (s.equalsIgnoreCase("Draw")) {
holder.img.setImageResource(R.drawable.draw_icon);
}else if (s.equalsIgnoreCase("Record Sounde")) {
holder.img.setImageResource(R.drawable.sound_recorder_icon);
}
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
if (holder.relativeLayout1.getVisibility()==View.GONE)
{
holder.rel_main.setBackgroundResource(R.drawable.list_item_bg_pressed);
holder.relativeLayout1.setVisibility(View.VISIBLE);
}else {
holder.relativeLayout1.setVisibility(View.GONE);
holder.rel_main.setBackgroundColor(Color.TRANSPARENT);
}
}
});
return view;
}
}
3. And ad Custom Adapter in "onCreate" in ItemListFregment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
/*setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
R.layout.c_simple_list_item, R.id.text1, DummyContent.ITEMS));*/
setListAdapter(new CustomAdepeterNewJob(getActivity(), DummyContent.ITEM_MAP));
}