Search code examples
androidlistviewlayout-inflater

how to settittle to listview in infater layout?


I'm new to android..

I'm facing problem with tittle for listview..

How to implement tittle for list view? help me..

I try like this..

      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      viewList = Nexttopic.this.getLayoutInflater().inflate(R.layout.activity_nexttopic, null);
      dialogMarketList = new Dialog(Nexttopic.this);
      dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialogMarketList.setTitle("TOPICS");
      dialogMarketList.setContentView(viewList);
      dialogMarketList.show();     
      lvForDialog = (ListView) viewList.findViewById(R.id.List_view);
      lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
      ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist));
      lvForDialog.setAdapter(adapter);    

Its not working..


Solution

  • just remove this:

    dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    Edited answer:

    enter image description here

    use this way:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewList = getLayoutInflater().inflate(R.layout.activity_main_, null);
            dialogMarketList = new Dialog(MainActivity.this);
            dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialogMarketList.setTitle("TOPICS");
            dialogMarketList.setContentView(viewList);
            dialogMarketList.show();
            ListView lvForDialog = (ListView) viewList.findViewById(R.id.listView1);
    
            header = inflater.inflate(R.layout.header, null);
    
            lvForDialog.addHeaderView(header);
            lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
            ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
                    MainActivity.this, android.R.layout.simple_list_item_1, a));
            lvForDialog.setAdapter(adapter);
    

    header.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="my title"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    


    Edited 2nd time:

    avoid title go up when scrolling use below code insted of above.

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("my Title");
            builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
    
            builder.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                    R.layout.row, R.id.child_row, items),
                    new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
    
    
                        }
                    });
    
            builder.show();