Fill a listview without calling setContentView - android

I want to fill a list with some information. My problem is that I want to fill the last, at the end of an activity.
I try to explain:
Start -> activity1 (layout1) -> activity2 (layout2) -> end (layoutEnd)
Now I want to add some information to a list and I want to do this the onCreate of the end (activity). I can't setContentView to the layout of the activity, because I set that to layoutEnd.
Code end:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.training_beendet);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(new yourAdapter(this, new String[] { "Anfänger 1"}));
}
Layout with listView:
<?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="horizontal" >
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
Layout with textViews for the listView:
<?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"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"
android:id="#+id/header"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#id/header" />
</RelativeLayout>

You can preserve a boolean flag either in SharedPreference or in database inside MainActivity, my mean of flag is to maintain a value whether it's a fresh start or not.
Inside onCreate() method of MainActivity, just check that boolean flag variable, if it's fresh start then launch Start activity otherwise Activity with ListView.
If it's fresh start and you are in the end, then write an Intent to launch activity which is having listview with information.

Related

PreferenceFragment overlaps the Main Activity's Layout?

I've been on stackoverflow for a week now trying to find the answer to this 'overlap' problem.
Some people don't even use setContentView()??? Others insist on it...
I'm missing something.
Snapshot of before and after clicking on Settings menu
SettingsActivity:
public class SettingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
getFragmentManager().beginTransaction().replace(R.id.pref_frag_id, new appPreferences()).commit();
}
public static class appPreferences extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
Layout, preferences.xml, for the PreferenceFragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}"
>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/pref_frag_id"
android:name ="com.testng.firstapp.SettingsActivity$appPreferences"
/>
</FrameLayout>
The 'main' layout, Main.xml, in case I'm missing something:
<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="${relativePackage}.${activityClass}" >
<ListView
android:id="#+id/lvToDos"
android:layout_width="206dp"
android:layout_height="380dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:smoothScrollbar="false"
android:layout_above="#+id/itemEntryView"
android:layout_alignParentTop="true">
</ListView>
<LinearLayout
android:id="#+id/itemEntryView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" >
<Button
android:id="#+id/btnNewToDo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="editToDoItem"
android:text="#string/new_item"
android:textSize="#dimen/smallTextSize" />
</LinearLayout>
</RelativeLayout>
I think you're better off with 2 fragments, one for your initial view and one for your settings view. In the main activity XML, there should just be one FrameLayout. This is where you will put your main fragment and your settings fragment. By having only 1 FrameLayout in your main view and swapping between those 2 fragments, you're guaranteeing that only 1 will be visible at a time. However, I would recommend that you use a separate activity for hosting your settings fragment because I'm not sure that those 2 views have anything to do with each other.
Oops.
I had the wrong theme for my activity's manifest.xml file:
<style name="Theme.Holo.Light.Panel">
Theme.Holo.Light.Panel makes the window floating, with a transparent background.

Multiple layout within one activity android

In my activity IndividualActivity i have set the contextview of individual.xml now i also want to add another sub_individual.xml in the activity without removing, replacing or hiding the individual.xml so that they remain together.
public class IndividualActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual);
...
}
}
individual.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=".IndividualActivity"
android:background="#drawable/default_wall" >
<ListView
android:id="#+id/listx"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:dividerHeight="1dp"/>
...
sub_individual.xml
<?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="40dip"
android:layout_marginLeft="0dp"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:text="custom title bar" >
<ImageView
android:id="#+id/bk_btn"
android:layout_width="35dip"
android:layout_height="35dip"
android:background="#DDD" />
...
This functionality is supported by using Fragments. See http://developer.android.com/guide/components/fragments.html
You can use this from xml
Use "include" tag in your first xml and add second xml in this tag.

Listview dynamically allocate width

I want to allow my ListView to be wrapped to its content on its width and making it aligning to the parent centre.
This is what a normal ListView is giving me
This is almost what i want to achieve, only that i want the ListView to wrap by the longest test in the ListView. Currently this is achieved by declaring the dp which is something I would like to avoid if possible
Some codes for first image:
<ListView
android:id="#+id/lst_test"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" >
</ListView>
Some codes for second image:
<ListView
android:id="#+id/lst_test"
android:layout_width="200dp"
android:layout_height="wrap_content" >
</ListView>
Do I have to use custom adapter for this or I can stick with the current one? If I would need to use custom adapter, how do i go about doing this?
You don't need to create custom adapter. Here is an example which can help you.
public class MainActivity extends Activity {
HashMap <String, CheckBox> options = new HashMap<String, CheckBox>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_view = (ListView) findViewById(R.id.listView1);
ArrayList<String> str = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
str.add("Example " + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView1, str);
list_view.setAdapter(adapter);
}
}
Above I have created an activity in which i am getting listview and setting data using ArrayAdapter.
I have created two layouts :
First layout: Contains listview.
Second layout: Contains textview which need to display.
Main logic to align data at center of Listview is you need to set gravity of textview as center.
You can manage second layout according to your needs.
Here is my first XML layout:
<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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Here is my second layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" />
</RelativeLayout>
If you are still facing any problem then you can ask. If you need more flexibility then you can create custom adapter by inheriting ArrayAdapter class.
Here is an example of custom adapter: How do I link a checkbox for every contact in populated listview?
Hope it will help you.

Why does empty view in layout file shows up on screen even though listview has items?

I have a Listview to show some item and i have also used an empty view in case my adapter has no item for listview to show.Problem is when I go this this activity first it shows the empty view on the screen for a second and then loads the item and show them in listview.
My Activity onCreate looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_exams);
list = (ListView) findViewById(R.id.favoriteExamsList);
View empty = findViewById(R.id.favoriteExamsListEmptyView);
list.setEmptyView(empty);
new readingFavFileTask().execute(UtilityFuctions.FAV_EXAMS_FILE_NAME);
}
My layout file is:
<?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/favoriteExamsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="#+id/favoriteExamsListEmptyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/favoriteExamsEmptyViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="You do not have any favorite Exams.Please add Exams using below button."
android:textSize="20dp" />
<Button
android:id="#+id/favoriteExamsAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickAddExams"
android:text="Add Exams"
>
</Button>
</LinearLayout>
</LinearLayout>
How I Do make sure that empty view never shows up even for a second if we have data in our adapter.Should I attach the empty view to list later after checking the adapter or is thr any other way to do this?
I found the answer. I just need to apply Empty View list.setEmptyView(empty) in my AsyncTask onPostExecute() method after setting my adapter to list and it works perfectly.

How do I show a mapview and a listview on the same screen?

Usually I would just seperate out the activities but I want to show a screen that has a view text views, a list, and then a map view. I'm not sure how to go about doing that because I know the activity can only extend either listview or mapview.
I have one xml file like this we'll call it the main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/relativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#ffffff">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/tvName"
android:textColor="#color/dark" android:text="Name"></TextView>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/etName"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/btnAdd"
android:text="Add location"></Button>
<ListView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#android:id/list"></ListView>
</LinearLayout>
and another xml file that is just a big map
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/mapsView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="true"
android:apiKey="myapikey" />
The main activity inflates the first xml sheet with the list, but then how do I add the map to this? Do I nest a class that extends the map activity class and some how add the view?
This is what I was thinking
public class AddLocationActivity extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
public class ShowMap extends MapActivity{
public ShowMap(){
//maybe get the map.xml and add the view somehow?
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
}
Like mkso commented, you do not need to extend from ListActivity to use a ListView, see this for an example (not the best, but what I could find quickly). Just create the layout how you want it, extend from MapActivity and use the ListView by calling findViewById().

Categories

Resources