I am developing android application which having multiple tabs.
Now in launcher activity tabs display perfectly and can navigate through the tabs.
but if i call activity(which i wanted to show as Tab) on Button Clicked then tabs seems disappear.
please refer the given code and let me know if i am doing something wrong.
This is Main TabActivity
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabHost tabHost=getTabHost();
TabSpec deshTab=tabHost.newTabSpec("Deshboard");
deshTab.setIndicator("Deshboard");
Intent DeshboardIntent=new Intent(this,DeshboardActivity.class);
deshTab.setContent(DeshboardIntent);
tabHost.addTab(deshTab);
TabSpec clientTab=tabHost.newTabSpec("client");
clientTab.setIndicator("client");
Intent intent=new Intent(this,ClientActivity.class);
clientTab.setContent(intent);
tabHost.addTab(clientTab);
}
}
Now i wanted to start client activity like
void onButtonClick(View view)
{
int id = view.getId();
switch(id)
{
case R.id.client_btn:
Intent clientIntent = new Intent(DeshboardActivity.this,ClientActivity.class);
startActivity(clientIntent);
break;
}
}
but when i click this button it starts new activity but NOT in tab.
what should i do to display this activity in tab on button click also.?
Below code is ClientActivity which i wanted to display as TAB.
public class ClientActivity extends Activity
{
private DatabaseHandler dbHandler;
private ListView clientListView ;
private BaseAdapter listAdapter;
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.clientactivity);
dbHandler = new DatabaseHandler(this);
final List<Client> clientList = dbHandler.getAllclient();
clientListView = (ListView)findViewById(R.id.client_list);
listAdapter = new ClientListAdapter(this, clientList);
clientListView.setAdapter(listAdapter);
clientListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Client client = clientList.get(position);
Toast.makeText(getApplicationContext(), client.getFirstName(), Toast.LENGTH_LONG).show();
Intent clientInfoIntent = new Intent(getApplicationContext(), ClientInfoActivity.class);
clientInfoIntent.putExtra("client",client);
startActivity(clientInfoIntent);
//finish();
}
});
}
}
That's because your ClientActivity is launched on top of your TabActivity. That way your tabbar is not shown anymore.
I suggest you use fragments for your ClientActivity.
A tutorial how to use fragments:
http://developer.android.com/guide/components/fragments.html
Related
I have two activities, in the main one I have a text field and two buttons, the user can enter text in the text field.
When clicking the add button,text will be added to some ArrayList.
When clicking show in list button a new activity with its own fragment should open showing a listview that contains the names that the user has entered.
I used ArrayAdapter and this is working fine. When I am in the list activity and when I click the back button of the device everything goes fine and the data in the ArrayList is not lost.
If I click show in list again I will find the old data I entered at the first time. But, if I click the back icon provided by Android at the top left of the device screen, the ArrayListbecomes empty and when I click show in list the listview shows as empty.
Here is the main activity code,
public class MainActivity extends ActionBarActivity {
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> itemList= new ArrayList<String>();
final EditText text = (EditText) findViewById(R.id.text_field);
text.setHint("Enter name here");
Button addButton = (Button) findViewById(R.id.add_button);
Button showButton = (Button) findViewById(R.id.show_button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name= text.getText().toString();
itemList.add(name);
if (itemList.size()==1){
Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "name has been added to the list", Toast.LENGTH_SHORT).show();
}
});
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), ListActivity.class);
intent.putExtra("list", itemList);
startActivity(intent);
}
});
}//end oncreate
This is the ListActivity and its fragment code,
public class ListActivity extends ActionBarActivity {
static ArrayList <String> itemList;
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemList= getIntent().getStringArrayListExtra("list");
setContentView(R.layout.list_activity);
if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new list()).commit();
}
}
public static class list extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
ListView listView = (ListView) rootView.findViewById(R.id.list);
ArrayAdapter <String> adapter= new ArrayAdapter<String>(getActivity(),R.layout.fragment_main, R.id.row, itemList);
listView.setAdapter(adapter);
return rootView;
}
}
}
Can anyone please tell me why this is happening? How can I solve this issue?
Any help is appreciated.
Many thanks.
create a class Constantss.java and declare
public static ArrayList <String> itemList=null;
then inside your class
use
Constantss. itemList.add(name);
instead of
itemList.add(name);
// replace itemList with Constantss. itemList in all of your classes
I believe this is a navigation issue. So you should close your ListActivity like this:
public class ListActivity extends ActionBarActivity {
//your code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID ) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Anyway your data can be lost if android destroys your MainActivity. So if you navigate from ListActivity back to MainActivity, MainActivity will be recreated and you data get lost. So you should find the way how to save your
data permanent.
there are two common ways:
Save data to data base (SQLite)
Save data to Preferences How to store list of values in SharedPreferences
I have an android tabhost with a listview inside, when I click on a listviewitem I want to show a new list at the place of the old list inside the tablayout.
How can I implement this? (I need to use listviews not fragments)
Thanks!
I found a solution: I put an activitygroup inside the tabhost, like so:
public class DrillDownWrapper extends ActivityGroup {
private ArrayList<View> history;
public static DrillDownWrapper group;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
history = new ArrayList<View>();
group = this;
//put the first listactivity
Intent i = new Intent(DrillDownWrapper.this, ListActivity.class);
View view = getLocalActivityManager().startActivity("ListActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back() {
if(history.size() > 1) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
} else {
finish();
}
}
#Override
public void onBackPressed() {
DrillDownWrapper.group.back();
return;
}
}
inside the listactivity you can get the context (ie to show a dialog) by calling getParent() method
Calling Specific Method from ListActivity which invokes method in one of the Parent(TabActivity) w/o Losing TabHost Layout
I have Two Activity Inside TabHost
1.PlayingActivity
2.AlbumActivity
By Clicking Button inside AlbumActivty will jump to LIstActivity
->By clicking Item in ListActivity, I want to jump back to one of the method inside-PlayingActivity w/o Losing Tab Layout.
I can accomplish task by calling Activity n specific Method using these
ListActivity clicking on Item invokes specific Method in PlayingActivity
Class SongList extends ListActivty
{
public void onCreate(Bundle savedInstanceState)
{
//implemented list adapter-ls
listadapter. . . .
ls.setOnItemClickListener(New View.onItemClickListener)
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
int songIndex=position;
Intent i=new Intent(getApplicationContext(),AlbumActivity.class);
i.putExtra("methodName", "myMethod");
i.putExtra("index", songIndex);
startActivity(i);
}
}
}
}
MainActivity which host PlayingActivity & AblumActivity Under TabHost
public class MainActivity extends TabActivity {
private static final String NOW_PLAYING = "Playing";
private static final String ALBUM = "Album";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Playing Tab
TabSpec PlayingSpec = tabHost.newTabSpec(NOW_PLAYING);
// Tab Icon
PlayingSpec.setIndicator(NOW_PLAYING, getResources().getDrawable(R.drawable.icon_now_playing));
Intent PlayingIntent = new Intent(this, PlayingActivity.class);
// Tab Content
PlayingSpec.setContent(PlayingIntent);
// Album Tab
TabSpec AlbumSpec = tabHost.newTabSpec(ALBUM);
AlbumSpec.setIndicator(ALBUM, getResources().getDrawable(R.drawable.icon_music));
Intent AlbumIntent = new Intent(this, AlbumActivity.class);
AlbumSpec.setContent(AlbumIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(PlayingSpec); // Adding Playing tab
tabHost.addTab(AlbumSpec); // Adding Album tab
}
}
In PlayingActivity called specific method(playSong())
class PlayingActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String index=intent.getStringExtra("index");
if(intent.getStringExtra("methodName").equals("myMethod")){
playSong(Integer.parseInt(index));
}
}
private void playSong(int i)
{
}
}
Now the Catch iz I can somehow invokes specific Method in PlayingActivity but Playing Activity which is under TabHost loses it's TabLayout
Is there AnyWay we can save Playing Activity to lose from it's TabLayout??
Just Found out that. Little bit of logic have saved my time.
Adding
finish();
inside the activity would done all my work.
I have a TabActivity with 4 tabs. When clicking on a button within one of the tabs and starting a new Activity (a new Activity not within the TabHost), the new Activity does not register OnClick(). The new Activity can't even show a Toast wich makes me think the TabHost is somehow blocking the ui?
When putting the Activity as one of the Tabs the OnClick works just fine.
Any ideas what the reason for this is?
I've included 3 classes:
1) The TabActivity
2) The Activity in a tab that starts:
3) The new Activity that cannot register OnClick()
1) TabActivity:
public class OverView extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
/** TabHost will have Tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
/** TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec Search = tabHost.newTabSpec("tid1");
TabSpec AllArtists = tabHost.newTabSpec("tid1");
TabSpec Favorites = tabHost.newTabSpec("tid1");
TabSpec About = tabHost.newTabSpec("tid1");
/** TabSpec setIndicator() is used to set name for the tab. */
Search. setIndicator("Search");
AllArtists. setIndicator("AllArtists");
Favorites. setIndicator("Favorites");
About. setIndicator("About");
/** TabSpec setContent() is used to set content for a particular tab. */
Search.setContent (new Intent(this, Search.class));
AllArtists.setContent (new Intent(this, AllArtists.class));
Favorites.setContent (new Intent(this, Favorites.class));
About.setContent (new Intent(this, About.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(Search);
tabHost.addTab(AllArtists);
tabHost.addTab(Favorites);
tabHost.addTab(About);
}
}
2) AllArtists (The Activity within the tab. Clicking a list item starts a new Activity):
public class AllArtists extends Activity {
// Debug
private final String TAG = this.getClass().getSimpleName();
// XML
EditText searchBox;
ListView listView;
// Adapter
ListAdapter listAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_allartists);
listAdapter = new ListAdapter(this, null);
// XML
listView = (ListView)findViewById(R.id.allartists_listview);
searchBox = (EditText)findViewById(R.id.allartists_searchbox);
listView.setAdapter(listAdapter);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String memberID = (String)listAdapter.getID(position).toString();
if (!memberID.equals("HEADER")){
Log.d(TAG, "Jumping to Artists.class");
Intent intentArtist = new Intent (AllArtists.this, Artist.class);
intentArtist.putExtra("ID", memberID);
startActivity(intentArtist);
}
}
});
}
3) Artist (The new Activity started. This class does not register OnClick):
public class Artist extends Activity implements OnClickListener{
// Debug
private final String TAG = this.getClass().getSimpleName();
// XML
Button favorite_btn;
LinearLayout tel;
LinearLayout mob;
LinearLayout email;
LinearLayout www1;
LinearLayout www2;
LinearLayout add;
TextView name_tv;
TextView tel_tv;
TextView mob_tv;
TextView email_tv;
TextView www_tv1;
TextView www_tv2;
// Strings
String memberID;
String sirName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artist);
Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT);
// XML
favorite_btn = (Button)findViewById(R.id.artist_ib_favorite);
tel = (LinearLayout)findViewById(R.id.artist_tel_container);
mob = (LinearLayout)findViewById(R.id.artist_mob_container);
email = (LinearLayout)findViewById(R.id.artist_email_container);
www1 = (LinearLayout)findViewById(R.id.artist_www_container1);
www2 = (LinearLayout)findViewById(R.id.artist_www_container2);
add = (LinearLayout)findViewById(R.id.artist_add_container);
name_tv = (TextView)findViewById(R.id.artist_tv_name);
tel_tv = (TextView)findViewById(R.id.artist_tel_dynamic);
mob_tv = (TextView)findViewById(R.id.artist_mob_dynamic);
email_tv = (TextView)findViewById(R.id.artist_email_dynamic);
www_tv1 = (TextView)findViewById(R.id.artist_www_dynamic1);
www_tv2 = (TextView)findViewById(R.id.artist_www_dynamic2);
// OnClickListeners
favorite_btn.setOnClickListener(this);
tel.setOnClickListener(this);
mob.setOnClickListener(this);
email.setOnClickListener(this);
www1.setOnClickListener(this);
www2.setOnClickListener(this);
add.setOnClickListener(this);
// Code here to get memberID for fillContent()
}
private void fillContent(String memberID) throws JSONException {
// Code here to fill the TextViews etc with content from the DataBase.
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.artist_ib_favorite:
Toast.makeText(this, "onClick", Toast.LENGTH_SHORT);
Log.d(TAG, "Neo is attempting to insert member into favorites");
MyDB db = new MyDB(this);
db.insertFavorite(memberID, sirName);
break;
case R.id.artist_tel_container:
break;
case R.id.artist_mob_container:
Log.d(TAG, "OMG CLICKED THE MOBILE!");
break;
case R.id.artist_email_container:
break;
case R.id.artist_www_container1:
break;
case R.id.artist_add_container:
break;
}
}
Thanks ;)
For Toast you need to call show.
Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT).show();
For the click Operation on anything apart from Button you need to define
android:clickable="true"
in the layout.
Solved this. In the layout there was an (empty) GridView at the bottom of the layout set to android:layout_height="fill_parent" wich stole the touchevent.
The weird part about this is that when putting the exact same activity with the exact same XML inside a tab, the onClick() worked fine.
i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.