New to Android, sorry if there's something I should be including here that I'm not.
Anyway, I'm trying to make a custom layout for a list. Whenever I try to run the app, it closes and sends a "AppName has crashed" error modal. When I use a built-in android layout, this doesn't happen. I'm not sure if the issue is my code or my emulator (Running on a Nexus 5 with 1GB RAM)
Here's layout/row_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textSize="50sp"
android:textStyle="bold"
android:padding="15dp"/>
</LinearLayout>
Here's 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="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"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/theListView"></ListView>
</LinearLayout>
And here's the onCreate method in MainActivity.java (I haven't edited anything else)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] favoriteTVShows = {"The Office", "The Wire", "Mr. Robot", "Parks and Rec",
"Fool Us", "Garbage Time", "Last Week Tonight", "Silicon Valley"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout,
favoriteTVShows);
ListView theListView = (ListView) findViewById(R.id.theListView);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View view, int position,
long id) {
String tvShowPicked = "You selected " +
String.valueOf(adapterView
.getItemAtPosition(position));
Toast.makeText(MainActivity.this, tvShowPicked,
Toast.LENGTH_SHORT).show();
}
});
}
You have to set the adapter to a view that can contain the text, meaning that instead of using a LinearLayout as a base, use a TextView:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textSize="50sp"
android:textStyle="bold"
android:padding="15dp"/>
Related
I have fetched data from the server from the category class.The getcategories method return the List of String containing spinner items. When I click on the spinner item. Nothing happens. Is there any mistake in my code. Please help.
This is my java code.
public void fetchPropertyType(){
category = new Category(); //Spinner Item model
//categories is a array list of String which contains the items of spinner
categories = category.getCategories(AddPropertyActivity.this);
//Property Type Spinner Adapter
propertyTypeSpinner = (Spinner) findViewById(R.id.property_type_spinner);
Log.e("Test", "Just a test message");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
propertyTypeSpinner.setAdapter(dataAdapter);
propertyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("Test", "Nothing selected on spinner activity");
}
});
}
This is my layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1">
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"
android:textAlignment="textEnd"/>
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
</RelativeLayout>
You just should do this.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/border">
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"
android:textAlignment="textEnd"/>
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
</RelativeLayout>
In the Spinner
Change
android:layout_height="match_parent"
To
android:layout_height="wrap_content"
Because you use android:layout_height="match_parent",so you can't see your list item.And nothing happens.
As you are using
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"/>
Which wraps the height of spinner so try to give custom height to it so that i t will be clickable
I did this
<Spinner
android:id="#+id/property_type_spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:spinnerMode="dropdown"/>
and keep your text view height wrap_content as
<TextView
android:id="#+id/spinner_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="Property Type"/>
I want to let the user the possibility to delete the items shown in dropdown list of the spinner if clicks on the "X" button on the right side of each item, see the sample below.
If the user click on the item the Spinner should act like the classic Android default Spinner.
How should I extend and re-define the Spinner to work in this way?
I think you are looking for this:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spnView3 = (Spinner) findViewById(R.id.spnView3);
ArrayList<String> aList = new ArrayList<String>();
aList.add("--Select--");
aList.add("Md. Shahadat Sarker");
aList.add("Developer");
aList.add("ErrrorPoint");
spnView3.setAdapter(new SpinnerAdapter(this, R.layout.spinner_row, aList, aList));
Toast.makeText(this, "Selected: " + spnView3.getSelectedItem(), 500).show();
}
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" >
<Spinner
android:id="#+id/spnView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
</RelativeLayout>
spinner_row.xml (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="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >
<TextView
android:id="#+id/spnItemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="#+id/spnItemDel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
Adapter:
public View getCustomView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_row, parent, false);
spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);
spnItemName.setText(iName.get(position)+"");
spnItemDel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
//iName[position] = null;
iName.remove(position);
notifyDataSetChanged();
}
});
return rowView;
}
Full example is here... download
Edited Part:
if( /*Your condition goes here*/ ){
spnItemName.setVisibility(View.GONE);
} else{
spnItemName.setVisibility(View.VISIBLE);
}
I encountered a silly problem.
I have an identical thing working already, but for the second try, the onItemClick is not doing anything. I re-re-rechecked so many times now, but nothing seems to catch my eye. Please take a look and let me know what is wrong
So here is my setup.
The activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promotionale);
ListView mylist = (ListView) findViewById(R.id.lv_promotionale);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(PromotionaleActivity.this,"I touched an item at position"+Integer.toString(position),Toast.LENGTH_SHORT).show();
}
});
The activity layout is:
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="370dp"
android:id="#+id/lv_promotionale" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Some text here"
android:id="#+id/textView"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:paddingTop="15dp"
android:paddingRight="0dp"
android:paddingEnd="0dp"
android:paddingBottom="0dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
EDIT
Here is the method where I load my listview with data using an external adapter
public void LoadListWithStuff(){
SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor mCursor = db.rawQuery("select _id,data,numar,idclient,numeclient,tipclient,trimisa from cereri_mst order by data,numar desc" , null);
idid.clear();
idclient.clear();
numeclient.clear();
tipclient.clear();
datacerere.clear();
numarcerere.clear();
trimisa.clear();
if (mCursor.moveToFirst()) {
do {
idid.add(Integer.toString(mCursor.getInt(0)));
datacerere.add(mCursor.getString(1));
numarcerere.add(mCursor.getString(2));
idclient.add(Integer.toString(mCursor.getInt(3)));
numeclient.add(mCursor.getString(4));
tipclient.add(mCursor.getString(5));
trimisa.add(mCursor.getString(6));
} while (mCursor.moveToNext());
}
DisplayCereriAdapter disadpt = new DisplayCereriAdapter(PromotionaleActivity.this,idid,idclient,
numeclient, tipclient,datacerere,numarcerere,trimisa);
ListView lv = (ListView) findViewById(R.id.lv_promotionale);
lv.setAdapter(disadpt);
mCursor.close();
db.close();
}
Also, I must say here that the listview is populated. I can see the contents of the listView. The only problem is that I cannot seem to catch the ItemClick and longClick events on the listview...
Why doesn't it work?????
I am going crazy here...
Thank you
The solution I found was to stop using the checkBox and replace it with ImageView... Now all works fine
Hello everyone and thank you for reading my question!
I'm currently developing an android app that has the following hierarchy:
One FragmentActivity is divided by three ListFragments.
One of these ListFragments uses a custom ArrayAdapter to display a List<> with a xml template on a ListView
This List<> is filled with dataset which are given id's. Datasets with id's lower than 10 are flagged "important", the other ones are flagged "not so important"
This is what I am looking for:
I have two layout templates, and I want to display the "important" datasets with one layout and the "not so important" with the other layout.
I have searched for quite a while now, but it looks like noone has asked a question like this before.
I tried to use a second ListView and apply a second custom ArrayAdapter on it, but unfortunately, there is a restriction: Since I'm using ListFragment, I'm forced to use findViewById like this.
ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);
When I tried to write the second custom ArrayAdapter, it showed an error here:
ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
Here is the entire ListFragment
public class LeaguePage extends ListFragment{
Global global_var;
ListView list, list_flat;
List <League> leagues = null;
ListAdapter adapter = null;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.league_page, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
try {
leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"));
} catch (IOException e) {
e.printStackTrace();
}
adapter = new LeagueAdapter (getActivity(), R.layout.list_row, leagues);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Global.selectedTeam = Integer.valueOf(leagues.get(arg2).getInternalID());
Global.mViewPager.setCurrentItem(1, true);
}
});
}
}
This is the xml file I pass on to my ArrayAdapter
<?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/liga_flat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="normal"
android:typeface="serif" />
</RelativeLayout>
This is the xml layout file for the ListFragment
<?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"
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=".Main_Activity$search_page" >
<RelativeLayout
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=".Main_Activity$search_page" >
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:divider="#null">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
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=".Main_Activity$search_page" >
<ListView
android:id="#+id/android:list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" >
</ListView>
</RelativeLayout>
</RelativeLayout>
You shouldn't give a ListView an ID like this :android:id="#+id/android:list2", the ID of your ListView should be
<ListView android:id="#id/android:list"
...... ></ListView>
or
<ListView android:id="#+id/sampleList"
...... ></ListView>
I have a list of items in android app, and i want to start another activity if the user clicks on any item in that list. I have the following piece of (relevant) code.
Strangely enough, its working good enough on HTC velocity 4g (android 2.3.7), whereas I have tried the same app on HTC one S (android 4.1.1) and nothing happens when i click any item in the list. Does anyone have any idea what could be the problem here???
lv = (ListView)findViewById(R.id.listView1);
this.adapter = new SimpleAdapter(DisplayWiFiListActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.abcd });
lv.setAdapter(this.adapter);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
String text = parentAdapter.getItemAtPosition(position).toString();
text = text.substring(5,text.length()-1);
Intent intent = new Intent(DisplayWiFiListActivity.this, MainActivity.class);
DisplayWiFiListActivity.this.startActivity(intent);
intent.putExtra(EXTRA_MESSAGE, text);
startActivity(intent);
Toast.makeText(DisplayWiFiListActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
row.xml is following
<?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" >
<TextView
android:id="#+id/abcd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:clickable="true"
android:layout_marginTop="20dp" />
</LinearLayout>
activity_display_wifi_list.xml is following
<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=".DisplayWiFiListActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:layout_marginTop="100dp" >
</ListView>
<Button
android:id="#+id/buttonScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/refresh_list" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonScan"
android:textStyle="normal|italic"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"/>
</RelativeLayout>
Please post your code of SimpleAdapter or if you have any Button Widget in listitem layout pls make it focus-able false;