My app uses a button which when clicked goes to a new activity which displays a list view. All of this seems to be working fine. But the list view is displayed on top of the same activity with the button. It is not displayed separately. So i see the main activity in the background and the overlapped listview! Can this problem be solved? My code is as follows:
Button CLick event:
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Assignments.this, ViewEntry.class);
startActivity(i);
}
});
And the listview code:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.database_view);
//ListView lv = (ListView)findViewById(R.id.listView1);
dbControl = new DatabaseControl(ViewEntry.this);
dbControl.open();
ArrayList<String> data = dbControl.getData();
if (data.equals(null)) {
Toast.makeText(ViewEntry.this, "Database Empty!", Toast.LENGTH_LONG)
.show();
} else {
final Dialog notice = new Dialog(this);
final TextView msgbox = new TextView(this);
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
setListAdapter(new ArrayAdapter<String>(ViewEntry.this,
android.R.layout.simple_list_item_1, data));
final ListView listView = getListView();
//lv.setAdapter(arrayAdapter);
//lv.setTextFilterEnabled(true);
}
}
My xml code is as follows. But i don't think it makes a difference since i haven't used the setContentView. Using setContentView(R.layout.database_view); returns a debug error.
<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:layout_gravity="center_horizontal"
android:background="#000000"
android:fadingEdge="vertical"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ListView>
<TextView
android:id="#+id/textViewDatabase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
Thanks in advance!
Change background color of second activity layout to non-transparent. When you start second Activity the first is paused and goes into background but is still there like when dialog is shown.
Edit:
Activity is not transparent by default, check if you have properly defined appTheme in styles.xml and make sure you don't use android:theme="#android:style/Theme.Translucent" or "#00XXXXXX" color anywhere in your styles or manifest file.
If there is everything OK with styles then I don't know why your ListActivity is still transparent but you can change this by setting following layout as contentView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ad_catalog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888" <!-- example non-transparent color -->
android:orientation="vertical" >
<ListView
android:id="#android:id/list" <!-- has to be #android:id/list for ListActivity -->
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
or by setting ListView background color:
getListView().setBackgroundColor(Color.rgb(xxx, xxx, xxx));
Instead of using transparent background color try using non transparent colors
try below code:
<RelativeLayout 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:layout_gravity="center_horizontal"
android:background="#b3b3b3"
android:fadingEdge="vertical"
android:orientation="vertical" >
Use an Intent to go to the other activity and create a list view itself in the new activity.
Use the Button to trigger the intent.
Related
obviously not a profound question, but I think theres probably an error in my approach.
So I've added the file listview.xml to my project in the layout folder.
it contains this:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
I've also tried:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
And i my main activity function generally looks like this:
public class ToDoManagerActivity extends ListActivity {
private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "TodoManagerActivityData.txt";
private static final String TAG = "Lab-UserInterface";
// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;
ToDoListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
// TODO - Inflate footerView for footer_view.xml file
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
// NOTE: You can remove this block once you've implemented the assignment
if (null == footerView) {
return;
}
// TODO - Add footerView to ListView
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
This keeps breaking with the error:
...java.lang.RunTimeException: content must have a ListView whose id attribute is 'android.R.id.list'
I noticed that if I comment out the setContentView(R.layout.footer_view) bit the error disappears, which is quite confusing as well, because it seems like the error should trigger before there.
This is confusing the hell out of me because as far as I can tell this element exists. Its seems like maybe I'm missing a step to load the ListView? I'm banging my head against the wall here and this seems like something really basic, and being a n00b sucks...So any help is much appreciated!
Cheers!
EDIT:
footer_view.xml contents:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
EDIT2:
Current onCreate after suggested edits:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
setListAdapter(mAdapter);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footerView);
}
For an Activity that is extending ListActivity, the ListView needs to exist within the xml file that you are using in the activity, in your case that's the footer_view file.
Taken from the Google Dev Site - "ListActivity 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 a ListView object with the id "#android:id/list""
To incorporate your footer below your list, try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
</LinearLayout>
Here, try this in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
That is exactly what I have in my app.
Then in your onCreate(), just this:
setContentView(R.layout.activity_main);
Try it and let me know if it works. That code should work. From there, we can continue on to isolate the problem.
EDIT:
Man, now I see your problem. Not a big deal here. Let's look at your code (edited to be shorter). The onCreate() method has this:
super.onCreate(savedInstanceState);
mAdapter = new ToDoListAdapter(getApplicationContext());
getListView().setFooterDividersEnabled(true);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
Do you see R.layout.activity_main anywhere there? There's your issue! Your R.layout.footer_view only contains a textview, it doesn't contain the ListView, and your Activity keeps looking for the ListView that you promised it that you'd have by extending a ListActivity.
Try this - change your onCreate() method to this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new ToDoListAdapter(getApplicationContext());
setListAdapter(mAdapter);
}
Then your activity_main.xml layout file should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
This should work. Let me know.
EDIT2:
Now add this to the end of your onCreate() method:
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
Voila! Should work now.
I have a listview with buttons in each row in fragment, the number of buttons retrieve based on sqlite database. I want to make when button click it move to an another new activity and parse button names. But when i click the buttons nothing happens, i have already try to use this code in xml file from ListView setOnItemClickListener not working by adding button, but it still not working.
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
Does anyone have solution? Thanks before and below is my code.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DatabaseHandler db = new DatabaseHandler(getActivity());
listView = (ListView) getView().findViewById(R.id.list_addcard);
List<CardType> listCardType = db.getAllCardType();
for(CardType ct : listCardType){
String log = "ID: " + ct.getTypeId() + " Category Name: " + ct.getTypeCategory();
Log.d("Result: ", log);
categoryArray.add(ct);
}
adapter = new ListAdapterAddCard(getActivity(), R.layout.list_row_addcard, categoryArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, final int position, long id){
categoryName = categoryArray.get(position).getTypeCategory();
Intent intent = new Intent(getActivity(), DetailCategory1.class);
intent.putExtra("categoryName", categoryName);
startActivity(intent);
}
});
}
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<ListView
android:id="#+id/list_addcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:layout_marginTop="30dp"/>
</RelativeLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<Button
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
Firstly remove the following from your button xml..
android:focusable="false"
android:focusableInTouchMode="false"
then in your custom adapter where you inflates your custom xml cell in the list, get the button reference and set the onclick listenet over there for that button.
For me it did not work having an startActivity function within the application as within the function you don't have access to the context, you need to add the variable of the host activity. Basically I think you need to do the following:
Activity host = (Activity) listView.getContext();
Intent intent = new Intent(host, DetailCategory1.class);
....
host.startActivity(intent);
This is what I need to do in getView method of my adapter for an standard listview.
category_name Button is stealing the focus from the ListView item, you could either add onClick listener to the each item button or simply use TextView instead styled as Button, this is a very bad practice though and confusing to your users
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
style="#android:style/Widget.Button"/>
</RelativeLayout>
First you need to set clickable to your custom layout button in your case
<Button
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/category"
android:clickable="true"/>
Next, you are setting the OnItemClickListener to the "ListView" (<< parent) itself not the
(Button>>) children listView.setOnItemClickListener(new OnItemClickListener(){
BUT,
you can get a reference to the category_name button once you have inflated your ListView by doing something like this;
Button myButton = (Button) list_addcard.findViewById(R.id.category_name);
If you need more help you can always visit the Android Documentation for
ListView ListView Documentation
Cheers.
So finally i found the answer, i change the setOnClickListener to my adapter class and it's work fine for me. Thank you all for your suggestion.
holder.txtCategoryName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, DetailCategory.class);
int pos = (Integer) v.getTag();
categoryName = categoryItems.get(pos).getTypeCategory();
intent.putExtra("categoryName", categoryName);
context.startActivity(intent);
}
});
I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method
I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.
Many of you know, that there's is a class RealViewSwticher created by Marc Reichelt http://marcreichelt.blogspot.com/2010_09_01_archive.html
If someone used or uses this thing, please can you help me.
Here's my .xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<de.marcreichelt.android.RealViewSwitcher
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/real_view_switcher">
<GridView android:id="#+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent"></GridView>
<GridView android:id="#+id/gridView2" android:layout_width="match_parent" android:layout_height="match_parent"></GridView>
</de.marcreichelt.android.RealViewSwitcher>
</LinearLayout>
And here's my main class.
public class MyMain extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
RealViewSwitcher realViewSwitcher = (RealViewSwitcher)findViewById(R.id.real_view_switcher);
GridView gridview = (GridView) findViewById(R.id.gridView1);
GridView gridview2 = (GridView) findViewById(R.id.gridView2);
gridview.setAdapter(new ImageAdapter(this));
gridview2.setAdapter(new ImageAdapter(this));
realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
}
private final RealViewSwitcher.OnScreenSwitchListener onScreenSwitchListener = new RealViewSwitcher.OnScreenSwitchListener() {
#Override
public void onScreenSwitched(int screen) {
// this method is executed if a screen has been activated, i.e. the screen is completely visible
// and the animation has stopped (might be useful for removing / adding new views)
Log.d("RealViewSwitcher", "switched to screen: " + screen);
}
};
}
I really don't understand what i am doing wrong, in the Example there's comment how to use it in xml:
// note that you can also define your own views directly in a resource XML, too by using:
// <de.marcreichelt.android.RealViewSwitcher
// android:layout_width="fill_parent"
// android:layout_height="fill_parent"
// android:id="#+id/real_view_switcher">
// <!-- your views here -->
// </de.marcreichelt.android.RealViewSwitcher>
The problem is: It is not working. I can see only my main GridView and i can't scroll to the second. If soemeone knews how where i made a mistake or if someone knews another way to do a real scrolling between views, please answer this.
why don't you use ScrollView? Try to use something like this:
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="110px">
<LinearLayout
android:id="#+id/grds"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<GridView android:id="#+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent"></GridView>
<GridView android:id="#+id/gridView2" android:layout_width="match_parent" android:layout_height="match_parent"></GridView>
</LinearLayout>
</ScrollView>
if it does't work, looak at this post
Switching between two GridViews in the same Activity