I am using Xamarin and I have a ListView activity with some Custom Views. I also have a map activity. The map activity starts as the start up project. After the map activity has started up, I wish to display a ListView (that is currently in an activity).
My question is this: Do I need to start a new activity, with an intent, to display this ListView? Can I display a ListView from my map activity?
Thanks in advance
You can remove all views from content view of current Activity and create your ListView in code then add it to your content view.Or you can use switchView to show it.Also you can create a layout that has your map and list,at first set your list invisible and then set your map invisible and change list visible.
1-
ViewGroup vg = (ViewGroup)(mapView.getParent());
vg.removeView(mapView);
//then create your listview and add it:
...
vg.addView(myListView);
2-If you want change visibility,see Android : difference between invisible and gone?
Finally you can search in about switching views in android,It will be useful.
You can make an AlertDialog which can be launched from current Activity by creating a Custom Layout for AlertDialog.
MainActivity.java
package com.example.alertlist;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
AlertDialog al;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder b = new AlertDialog.Builder(this);
LayoutInflater i = getLayoutInflater();
b.setView(i.inflate(R.layout.alert_list, null));
al = b.create();
al.show();
lv = (ListView) al.findViewById(R.id.listView1);
String[] myStringArray = {"this", "is", "it"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray);
lv.setAdapter(adapter);
}
#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;
}
}
alert_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Related
I am testing making a Tabbed Layout with Different Content each Tab.
In this Tab i want make layout with Image and Text inside Listview.
This is code i wrote so far but not working.
Layout_One.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"
tools:context="com.example.kreuzell.projecttest.MainActivity$PlaceholderFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/menuText"/>
</RelativeLayout>
Layout_One.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ImageView;
import java.util.List;
public class Layout_One extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_one, container, false);
String[] menuText = {
"Text 1",
"Text 2",
"Text 3"
};
Integer[] menuImage = {
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3
}
ListView listView = (ListView) rootView.findViewById(R.id.menuText);
ListView listView = (ListView) rootView.findViewById(R.id.menuImage);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity();
android.R.layout.simple_list_item_1,
menuText,
menuImage
)
listView.setAdapter(listViewAdapter);
return rootView;
}
}
I am new of this, Anybody can help me? Thank you
Use adapter for the list view and edit the layout of the adapter
You need to make a custom layout(layout1) which has your image and text positioned as you want. Then make a custom list adapter(mAdapter), use layout1 inside your mAdapter and fill it with data.Then set mAdapter to the list.
Please have a look at these tutorials, they will help you to understand how all this custom lists work:
Vogella List Tutorial
Journal Dev List Tutorial
I am new to android,and i want to create a fragment which has listview in it and i want to add that fragment to my main activity.I am trying but i cant add it to my activity and also cant define my own layout to it.So please help me.
Thanks in advance.
This is my MainActivity.java
package com.example.sample;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] codeLearnChapters = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};
ArrayAdapter<String> codeLearnArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codeLearnChapters);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView codeLearnLessons = (ListView) findViewById(R.id.idListView1);
setContentView(R.layout.activity_main);
codeLearnLessons.setAdapter(codeLearnArrayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And its xml 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" tools:context=".MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="300dp"
android:name="com.example.sample.myFragment"
android:id="#+id/fragment"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
My fragment java is myFragment.java
package com.example.sample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created on 11/4/2015.
*/
public class myFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.act_fragment,
container, false);
return view;
}
}
Its xml i.e act_fragment.xml is
<?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">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/idListView1">
</ListView>
</RelativeLayout>
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
In my words, a fragment is a sub-activity.
You can read more about what a fragment is http://developer.android.com/guide/components/fragments.html
There is also a sample which you could use (and will be helpful as you will learn something new). The sample is http://developer.android.com/training/basics/fragments/index.html
You can also look it up on youtube (once you have read about it and understood what fragment is). There is also a video tutorial to start you with. Just search "Android Fragment Lifecycle Part 1: Android Application Development Tutorial [HD 1080p]" - sorry, Stackoverflow is not allowing me to add more than two links.
They have five tutorials and I would recommend it as it will help you start better by yourself.
You can read more about what a fragment is
link
Code :
public class Tab2Fragment extends Fragment {
EditText fromdate;
EditText todate;
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab2_view, container, false);
EditText fromdate=(EditText)V.findViewById(R.id.fromdate);
EditText todate=(EditText)V.findViewById(R.id.todate);
fromdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Listview Data
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
lv = (ListView) V.findViewById(R.id.list_view);
// Adding items to listview
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
return V;
}
}
hope this link helpful you
http://developer.android.com/guide/components/fragments.html
Place this in your activity class
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
and place below code in activitylayout code
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />
I got a fragment that should only display a list but every time I try to display the list the previous activity is displayed in the background.
So my question is, how do I display the list on a empty page? What can I put instead of getActivity() in the ArrayAdapter.
My XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/fundraiser"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
And my code looks like this
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FundraiserFragment extends ListFragment{
String[] values = new String[] { "Item 1", "Item 2", "Item 3"};
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(),android.R.layout.simple_list_item_1,values);
setListAdapter(adapter);
}
Don't forget that layouts are transparent by default, unless you assign them a background color.
Assign a background color to the LinearLayout of your fragment in the XML file and the parent activity should disappear.
android:background="#android:color/white"
I have tried so many things to get my android ListView to scroll. However I have been unsuccessful in my attempt. Here is my xml code below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:listSelector="#color/theme_colour"
android:scrollbarStyle="outsideInset">
</ListView>
</RelativeLayout>
I have also added my java code below. I am not sure how to fix this problem and your help would be greatly appreciated.
package com.outdeh;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
/**
* Created by horacedaley on 2/15/14.
*/
public class EventsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
populateListView();
}
public void populateListView(){
ParseQueryAdapter<ParseObject> adapter =
new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
// Here we can configure a ParseQuery to our heart's desire.
ParseQuery query = new ParseQuery("Events");
query.orderByAscending("eDate");
return query;
}
});
adapter.setTextKey("eTitle");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setFastScrollEnabled(true);
listView.setAdapter(adapter);
}
}
Try changing android:layout_height="wrap_content"s to android:layout_height="match_parent". I guess it doesn't scroll because when you set the height to "wrap_content" the view will go beyond the visible area in activity, and since all items are considered displayed (in spite of not being in the visible area), there won't be scrolling available.
I am basically trying to get which items have been selected within a multiple choice enabled ListActivity. I am sure that there are many beginning Android programmers that would love this to be clarified. This app runs fine until it gets to the FOR loop and tries to assign the values of the multi-choice items to ArrayList 'items'. I'm simply trying to use Toast to see if the multi-choice items are being assigned to the Arraylist 'items'. The app crashes when the button is clicked. testing is done in android 2.2 emulator
Here is my Main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Items" />
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:choiceMode="multipleChoice"
android:textFilterEnabled="true" android:fastScrollEnabled="true" >
</ListView>
</LinearLayout>
and here is the code
package com.tests.TestCode;
import android.app.ListActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] list = {"wrenches","hammers","drills","screwdrivers","saws","chisels","fasteners"};
// Initializing An ArrayAdapter Object for the ListActivity
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListView thelistview = (ListView) findViewById(android.R.id.list);
ArrayList<String> items = null;
SparseBooleanArray checkedItems = thelistview.getCheckedItemPositions();
if (checkedItems != null) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
String item = adapter.getItem(checkedItems.keyAt(i)).toString();
items.add(item);
}
}
Toast.makeText(getBaseContext(), items.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "checkedItems == null", Toast.LENGTH_LONG).show();
}
}
});
}
}
Much obliged to anyone that can provide a good answer as to the best way to go about accomplishing this ListActivity scenario, or anyone that can explain why the app crashes at the point of assignment to the ArrayList. Thanks!
Your items list is null and you try to use it before initializing it.
You need to use items = new ArrayList<String>(); somewhere before trying to use it, or you will keep getting NullPointerExceptions.