WarSame
WarSame

Reputation: 1000

Android Listview Hides Titlebar

I'm trying to make an app - it has 3 activities. The first 2 are listviews in linear layouts, and each of them prevents the title bar from showing up. By title bar I mean the section that is normally at the top of an activity which displays the activity's name as well as an option setting. My third activity is not a listview, and displays the title bar normally, which leads me to think it may be a problem with my listviews.

The xml for my first page is:

<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".SelectClass">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

My java class doesn't do much to the display, although it does set an array adapter and an onClick listener. If those are necessary to understand what's going on then let me know and I'll post them. I appreciate any help or clues. Thank you!

EDIT: first page's java:

package com.example.graeme.dnd5echaracterroller;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.view.View;
import android.widget.TextView;


public class SelectClass extends ListActivity {

private static String classString;

public static void setClassString(String classString) {
    SelectClass.classString = classString;
}

public static String getClassString() {

    return classString;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_class);

    //Initialize the available class choices
    final String[] classes = {"Barbarian","Bard","Cleric","Druid",
    "Fighter","Monk","Paladin","Ranger","Rogue","Sorcerer",
    "Warlock", "Wizard"};
    ArrayAdapter<String> classAdapter = new ArrayAdapter<>(getListView().getContext(),
            R.layout.classlayout, R.id.classname, classes);

    getListView().setAdapter(classAdapter);

    //Set on click listener to get selected class item
    AdapterView.OnItemClickListener itemClickedHandler = new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View v, int position, long id){
            //Start a new intent headed to selectRoll, fill it with the class string selected
            Intent sendClassIntent = new Intent(SelectClass.this, SelectRoll.class);

            //Each list item has an image, and text
            //First grab the list item, then grab the text from it
            LinearLayout ll = (LinearLayout)v;
            TextView tv = (TextView)(ll).findViewById(R.id.classname);
            setClassString((String)(tv.getText()));

            startActivity(sendClassIntent);
        }
    };
    getListView().setOnItemClickListener(itemClickedHandler);
}

@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_select_class, 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);
}
}

Upvotes: 0

Views: 812

Answers (2)

Linh
Linh

Reputation: 60973

You should modify you first activity as this structure

public class SelectClass extends AppCompatActivity {
    ...
    private ListView mListView;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_class);

        // Initialize the lisview
        mListView = (ListView) findViewById(R.id.lisview_id); // lisview id


        //Initialize the available class choices
        final String[] classes = {"Barbarian","Bard","Cleric","Druid",
        "Fighter","Monk","Paladin","Ranger","Rogue","Sorcerer",
        "Warlock", "Wizard"};
        ArrayAdapter<String> classAdapter = new ArrayAdapter<>  (this,
                R.layout.classlayout, R.id.classname, classes); // update

        mListView.setAdapter(classAdapter);

       ...

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       ....
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        .....
    }
    }

And in the ListView of first activity

<ListView
android:id="@+id/lisview_id"  // use this id for initialize listview
android:layout_width="match_parent"
android:layout_height="match_parent">

Hope this help

Upvotes: 1

Alvaro
Alvaro

Reputation: 1448

I think that the problem is not the ListView, normally it is hided because of the style you have set in your manifest. This is what you have in the main activity inside manifest.xml:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

the android:theme="@style/AppTheme" on the listView activity must be set as the Apptheme that shows the "Title bar".

Hope this helps

Upvotes: 0

Related Questions