In my application i'm using two tabs one tab app contacts and other one is all contacts ,in application opening a one tab to other one it tack 10sce ,in that time how to add the same type of message like loading screen etc ,
plz tell me how to do that in my android app?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
final TabHost tabHost = getTabHost();
TextView txtTab = new TextView(this);
txtTab.setText("Mobell Contacts");
txtTab.setPadding(8, 9, 8, 9);
txtTab.setTextColor(Color.WHITE);
txtTab.setTextSize(14);
//txtTab.setTypeface(localTypeface1);
txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
TabHost.TabSpec spec;
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab1").setIndicator(txtTab).setContent(new Intent(this, ContactList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
TextView txtTab1 = new TextView(this);
txtTab1.setText("All Contacts");
txtTab1.setPadding(8, 9, 8, 9);
txtTab1.setTextColor(Color.WHITE);
txtTab1.setTextSize(14);
//txtTab.setTypeface(localTypeface1);
txtTab1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
TabHost.TabSpec spec1;
// Initialize a TabSpec for each tab and add it to the TabHost
spec1 = tabHost.newTabSpec("Tab2").setIndicator(txtTab1).setContent(new Intent(this, TabAct2.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec1);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#101010"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#848284"));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#848284"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#181418"));
}
});
}
}
see this code plz tell how to add the progressDialog when tab change one to other...
You can do something like this for showing progress -
public class MainActivity extends Activity {
Button b;
ProgressBar pb1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
pb1 = (ProgressBar)findViewById(R.id.progressBar1);
}
public void click(View v){
new Thread("thread1"){
public void run(){
try{
for(int i=0; i<10; i++)
{
Thread.sleep(4000);
pb1.setProgress(i*10);
}
}
catch(InterruptedException e){
e.printStackTrace();
}
};
}.start();
}
}
In case you want to show a message, use Spinner.
Show progressbar on center of screen using visibility on/off
Since you are using TabActivity (and you haven't posted your work around yet), so, I am guessing you have used
TabActivity
Activity (for both tabs)
Now, when you switch from one tab to another you are actually calling Activity. So, just create an AsyncTask in the Activity which takes time to load. For instance,
class ContactLoadAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
#Override
protected void onPreExecute()
pd = ProgressDialog.show(YourActivity.this, "title", "loading", true, false);
}
#Override
protected Void doInBackground(Void... params) {
// call your contact loading functions here
return null;
}
#Override
protected void onPostExecute(Void result) {
pd.dismiss();
}
}
And, call this AsyncTask in onCreate() of your Activity.
new ContactLoadAsync().execute();
Related
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
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 am making an application where i have set two bottom tabs,
namely "Exit" and "Back".
Now i want to exit from the application on the click of the "Exit" tab(not button).
How can i do that, i know that in android we can never actually close the application, but i want to go to the Home-screen on clicking "exit".
i have studied following link also along with other links
Is quitting an application frowned upon?
EDIT
public class Man_age_ur_PhoneActivity extends TabActivity {
/** Called when the activity is first created. */
ListView listview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
setTabs();
}
private void setTabs()
{
addTab("Exit", R.drawable.tab_exit);
addTab("Back", R.drawable.tab_back);
//To add more tabs just use addTab() method here like previous line.
}
private void addTab(String labelId, int drawableId)
{
TabHost tabHost = getTabHost();
// Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
// spec.setContent(intent);
tabHost.addTab(spec);
}
}
on Click of your second tab , you just have to write "finish();". This will close previously opened activities and you can exit from your application.
you can use this code
public class Man_age_ur_PhoneActivity extends TabActivity {
/** Called when the activity is first created. */
ListView listview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
setTabs();
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = getTabHost().getCurrentTab();
Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.i("########## Inside onClick tab 0", "onClick tab");
}
else if (i ==1) {
Log.i("########## Inside onClick tab 1", "onClick tab");
}
}
});
}
private void setTabs()
{
addTab("Exit", R.drawable.tab_exit);
addTab("Back", R.drawable.tab_back);
//To add more tabs just use addTab() method here like previous line.
}
private void addTab(String labelId, int drawableId)
{
TabHost tabHost = getTabHost();
// Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
// spec.setContent(intent);
tabHost.addTab(spec);
}
}
Also import this
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
Taken from this link
Android TabWidget detect click on current tab
Generally Android apps shouldn't have an exit option, see for instance this post: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
If you really want to, you can also just call System.exit(), this is guaranteed to close down the application completely.
I have created a Tab Activity showing a number of tabs defined in an xml string array. Each tab has the same listview as content which I update with different data.
I can see my tabs and the content updated each time I click on a tab, but the first tab (when activity is created) is never displayed.
I can see in my logs that onTabChanged is called when onCreate is executing, but the result is not displayed. If I change selected tab and go back to the first, I can see it.
This is my code :
public class MyActivity extends TabActivity {
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LOGGER.debug("onCreate.start");
setContentView(R.layout.mylayout);
listview = (ListView) findViewById(R.id.listview);
adapter = new MyAdapter(getApplicationContext(), new ArrayList<Station>());
listview.setAdapter(adapter);
final Resources res = getResources();
final TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener(
(MyApplication) getApplicationContext()));
TabHost.TabSpec spec;
final String[] tabsString = res.getStringArray(R.array.tabs);
for (int i = 0; i < tabsString.length; i++) {
final String[] tab = tabsString[i].split("\\|");
spec = tabHost.newTabSpec(tab[0]).setIndicator(tab[1]);
spec.setContent(R.id.tabcontent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = TAB_WIDTH;
}
LOGGER.debug("onCreate.end");
}
public void onTabChanged(final String tabId) {
LOGGER.debug("onTabChanged.start");
refreshTask = new AsyncTask<Void, Void, List<Station>>() {
#Override
protected void onPreExecute() {
/* Show progress bar and hide list view. */
findViewById(R.id.loading).setVisibility(View.VISIBLE);
findViewById(R.id.listview).setVisibility(View.GONE);
};
#Override
protected List<Station> doInBackground(final Void... params) {
return getStations(0, 0, tabId);
}
#Override
protected void onPostExecute(final List<Station> result) {
if (result != null) {
adapter.setStations(result);
} else {
adapter.clear();
}
adapter.notifyDataSetChanged();
/* Hide progress bar and show list view. */
findViewById(R.id.loading).setVisibility(View.GONE);
findViewById(R.id.listview).setVisibility(View.VISIBLE);
}
};
refreshTask.execute();
LOGGER.debug("onTabChanged.end");
}
}
And this is the log I can see in LogCat console :
MyActivity onCreate.start
MyActivity onTabChanged.start
MyActivity onTabChanged.end
MyActivity onCreate.end
Do you have an idea ?
Thanks
Following Line of code is causing problem
adapter = new MyAdapter(getApplicationContext(), new ArrayList());
you are passing arralist with size 0 hence no data displayed.
But when tab changes at then in your listener you passing arralist which contains some data and hence you are able to see data when you come back.
Enjoy..
Try setting the first tab as active at the end of your onCreate after you create all the tabs:
tabHost.setCurrentTab(0);
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.