Two xml layout for one activity java class

0 Kate Jabbour · March 7, 2015
Hello,
Is it possible to have two xml layout files for one java class?
Actually I am handling two SQLite tables in one class and want to print their contents on two different xml files.

Will be thankful if a sample code can be provided of how to use two xml files.

Thanks!

Post a Reply

Replies

Oldest  Newest  Rating
0 Kate Jabbour · March 9, 2015
thank you so much
0 Dol Lod · March 9, 2015
np
0 Dol Lod · March 8, 2015
Technically yes, if you are planning to nest layouts for one activity. To clarify nesting, I mean use a layout within a layout. 

I have used a listview to do SQL so I do have code that works. What I use is secondhand, look up Derek Banas for a much better explanation of how things work. Basically, listviews have adapters that can format data for things like strings. 

Keep in mind, that using ListActivity instead of Activity can make things much easier to use. 

Sorry, but please ignore the SherlockPart and use whatever you are using to extend your activity. A few of the things that say Sherlock might be a bit different, but use their native Android counterparts. Everything else is fine. 

This is sample code showing how to use a listview and adapter

package com.myteams.myteamsapp;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.myteams.myteamsapp.R;
import com.myteams.myteamsapp.DatabaseHandler;

public class MySearchView extends SherlockListActivity
{
public boolean onCreateOptionsMenu(Menu menu) 
{
        getSupportMenuInflater().inflate(R.menu.my_search_view_menu, menu);
        return (super.onCreateOptionsMenu(menu));
    }

public boolean onOptionsItemSelected(MenuItem item)
{
   // Handle item selection
   switch (item.getItemId())
   {
    case R.id.action_settings:
    changeDisplaySettings();
    break;
    case R.id.homeIcon:
    callMainActivity();
    break;
    case R.id.addNewTeamIcon:
    addNewTeam();
    break;
   }
   return super.onOptionsItemSelected(item);
}
ImageView abs__search_button;
TextView abs__search_badge;
EditText searchQuery;
ImageView abs__search_close_btn;
TextView teamId;
ListView listView; //this refers to the listview in the xml layout file
private String searchterm;
private ArrayList<Team> searchResults;
private OnClickListener listener=new OnClickListener
() 
{

@Override
public void onClick(View view) 
{
switch(view.getId())
{
case R.id.abs__search_button:
search();
break;
case R.id.abs__search_close_btn:
   clearSearchTerm();
break;
default:
break;
}


   
};
DatabaseHandler databaseHandler = new DatabaseHandler(this);

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.my_search_view);
abs__search_button=(ImageView)findViewById(R.id.abs__search_button);
abs__search_badge=(TextView)findViewById(R.id.abs__search_badge);
searchQuery=(EditText)findViewById(R.id.searchQuery);
abs__search_close_btn=(ImageView)findViewById(R.id.abs__search_close_btn);
listView=getListView(); //this initializes the listview
initializeListeners(); 
}

public void callMainActivity()
{
Intent theIntent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(theIntent);
}

public void initializeListeners()
{
abs__search_button.setOnClickListener(listener);
abs__search_close_btn.setOnClickListener(listener);
}

public void search()
{
setListAdapter(null); //this resets the adapter since it will be needing a new list
searchterm=searchQuery.getText().toString();
if(searchResults!=null) //this makes sure that all entries are removed
searchResults.removeAll(searchResults);
if(isSearchable())
{
searchResults = databaseHandler.getAllSearchResults(searchterm);

if (searchResults.size() >= 0)
{

listView.setOnItemClickListener(new OnItemClickListener() //this is what executes when an entry on the listview is clicked
{

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) //this is the actual part that overwrites this method
{

       teamId = (TextView) view.findViewById(R.id.teamId);


String teamIdValue = teamId.getText()
.toString();

// Signals an intention to do something
// getApplication() returns the application that owns
// this activity

Intent theIndent = new Intent(getApplication(),ViewTeam.class);
theIndent.putExtra("teamId", teamIdValue);
startActivity(theIndent);
}
});

ListAdapter adapter = new TeamAdapter(this,searchResults,R.layout.team_entry); //this adds the list to the adapter
setListAdapter(adapter); //this sets the adapter of the listview
}
}
}

public boolean isSearchable()
{
for(int position=0;position<searchterm.length();position++)
{
Character testedCharacter=searchterm.charAt(position);
testedCharacter=Character.toLowerCase(testedCharacter);
switch(testedCharacter)
{
case ' ':
break;
default:
return true;
}
}
return false;
}

public void changeDisplaySettings()
{
Intent theIntent = new Intent(getApplicationContext(),TeamDisplaySettings.class);
startActivity(theIntent);
}

public void addNewTeam() 
{
Intent theIntent = new Intent(getApplicationContext(),addNewTeam.class);
startActivity(theIntent);
}

public void clearSearchTerm()
{
searchterm="";
searchQuery.setText(searchterm);
}
}





//This is the code used to implement TeamAdapter which creates an adapter for the listview to take in and customize

TeamAdapter.java

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RatingBar;
import android.widget.TextView;

public class TeamAdapter extends ArrayAdapter<Team> //done to make it work for all listviews using inheritance
{
private final Context m_context;
    private final ArrayList<Team> m_teamArrayList;
    private int m_layoutId;

    public TeamAdapter(Context context, ArrayList<Team> teamArrayList, int layoutId) {

        super(context,layoutId, teamArrayList);
        m_layoutId=layoutId; //gets the layout id
        m_context= context; //gets the context 
        m_teamArrayList=teamArrayList; //gets the list of entries
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      Team team=m_teamArrayList.get(position); //think about this part as essentially populating each team entry in the list       where    this code is essentially implemented by the superclass using inflation
        LayoutInflater inflater = (LayoutInflater) m_context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //this creates a layout inflator
        View view = inflater.inflate(m_layoutId, parent, false); //this inflates the view
        if(m_layoutId==R.layout.team_checkable_entry) //this determines the specific view I'm using the adapter for
        {
          TextView teamId= (TextView) view.findViewById(R.id.teamId); //this populates the view that was created for this entry
          TextView teamName= (TextView) view.findViewById(R.id.teamName);
        }
        return view; //this returns the view created for each entry
    }
}
  • 1

Java / Android Development

107,094 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator