Navigation Drawer, ListView Items don't show - android

I'm trying do a news sports app with a NavigationDrawer. But when I open the app I want to show a WebView and after from left to come the ListView. I did something but now the ListView doesn't show the items. I have 4 classes and 2 xml.
package com.baronescu.sportsnews2;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new String[]{"Football","Tennis","Basketball","Hockey","Handball"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.dfurl, detail).commit();
}
});
Thread timer = new Thread(){
public void run (){
try {
sleep(2000);
} catch (InterruptedException e ){
e.printStackTrace();
} finally {
Intent odu = new Intent ("com.baronescu.sportsnews2.DEFAULTURL");
startActivity(odu);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
package com.baronescu.sportsnews2;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
TextView text;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
String menu = getArguments().getString("Menu");
text= (TextView) view.findViewById(R.id.detail);
text.setText(menu);
return view;
}
}
package com.baronescu.sportsnews2;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.baronescu.sportsnews2.OurViewClient;
public class DefaultUrl extends Activity {
WebView firstnews;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstnews = (WebView) findViewById(R.id.dfurl);
firstnews.setWebViewClient(new OurViewClient());
firstnews.getSettings().setJavaScriptEnabled(true);
firstnews.getSettings().setLoadWithOverviewMode(true);
firstnews.getSettings().setUseWideViewPort(true);
try {
firstnews.loadUrl("http://www.digisport.ro");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.baronescu.sportsnews2;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class OurViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView v, String url) {
// TODO Auto-generated method stub
v.loadUrl(url);
return true;
}
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sportsnews" >
<!-- android:id="#+id/content_frame" -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="match_parent" > -->
<WebView
android:id="#+id/dfurl"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:textSize="40px" />
</LinearLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baronescu.sportsnews2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DefaultUrl"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.baronescu.sportsnews2.DEFAULTURL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

Start reading that does not hurt!
implementing drawer navigation
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies
z-ordering and the drawer must be on top of the content.
The main content view is set to match the parent view's width and height, because it represents the entire UI when the navigation drawer is hidden.
The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL).
The drawer view specifies ts width in dp units and the height matches the parent view. The drawer width should be no more than 320dp so the user can always see a portion of the main content.
As a good practice i recommend:
create layout for drawer as above
create main activity consuming layout
create second layout (main content layout) with WebView
make fragment for coresponding layout
add fragment via transaction in main activity

Related

Getting error while running an activity with custom navigation drawer

Im getting error when i try to run the below activity.
The activity has a custom navigation drawer.
I'm new to android ....
The error is occuring at places where actionbar is involved.
Im getting error when i try to run the below activity.
The activity has a custom navigation drawer.
I'm new to android ....
The error is occuring at places where actionbar is involved.
Im getting error when i try to run the below activity.
The activity has a custom navigation drawer.
I'm new to android ....
The error is occuring at places where actionbar is involved.
Activity
import android.app.ActionBar;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
DrawerLayout drawer_layout;
ListView drawer_list;
ActionBarDrawerToggle drawer_toggle;
ActionBar abar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
drawer_layout= (DrawerLayout) findViewById(R.id.drawer_layout);
drawer_list= (ListView) findViewById(R.id.drawer_list);
drawer_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
drawer_list.setOnItemClickListener(this);
abar=getActionBar();
/*drawer_toggle = new ActionBarDrawerToggle(this, drawer_layout,
R.drawable.tick, R.string.app_name, R.string.app_name) {
*//** Called when a drawer has settled in a completely closed state. *//*
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle("Yuride");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
*//** Called when a drawer has settled in a completely open state. *//*
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle("Yuride");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};*/
abar.setHomeButtonEnabled(true);
abar.setDisplayHomeAsUpEnabled(true);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
class Myadapter extends BaseAdapter
{
Context context;
String[] drawer_items;
public Myadapter(Context context)
{
this.context=context;
drawer_items=context.getResources().getStringArray(R.array.drawer_items);
}
#Override
public int getCount() {
return drawer_items.length;
}
#Override
public Object getItem(int position) {
return drawer_items[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=null;
if(convertView==null)
{
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.drawer_row,parent,false);
}
else
{
row=convertView;
}
ImageView drawer_icon= (ImageView) row.findViewById(R.id.drawer_icon);
TextView drawer_text= (TextView) row.findViewById(R.id.drawer_text);
drawer_text.setText(drawer_items[position]);
drawer_icon.setImageResource(R.drawable.tick);
return null;
}
}
}
xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="#+id/drawer_list">
</ListView>
</android.support.v4.widget.DrawerLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.yusoft.yuride.yuride">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Entrance" />
<activity android:name=".HomeActivity"></activity>
</application>
</manifest>
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.yusoft.yuride.yuride/in.yusoft.yuride.yuride.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
at in.yusoft.yuride.yuride.HomeActivity.onCreate(HomeActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6584)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:6066) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660) 
The problem is in setting up ActionBar.
Instead of
abar=getActionBar();
abar.setHomeButtonEnabled(true);
abar.setDisplayHomeAsUpEnabled(true);
Write in XML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/MyMaterialTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/MyMaterialTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="#+id/drawer_list">
</ListView>
</android.support.v4.widget.DrawerLayout>
And in Java Class right after setContentView(R.layout.activity_home);:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

How to switch between fragments during onclick?

I have a project I'm trying to do. I ran into a little issue and I'm not sure how to get around it. Below is an image of the application so far.
What I would like it to do is when the user onclicks one of the list items, the part which says "Hello! It's Fragment2" changes to a new xml which I declared in the app. So if I click the ATO listItem, then the fragment on the right side should change to something like "Hello! It's ATO Fragment"
Here is my code so far:
AndroidListFragmentActivity:
package com.exercise.AndroidListFragment;
import android.app.Activity;
import android.os.Bundle;
public class AndroidListFragmentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Fragment2:
package com.exercise.AndroidListFragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2, container, false);
}
}
MyListFragment1:
package com.exercise.AndroidListFragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListFragment1 extends ListFragment {
String[] options ={
"ATO",
"BETA",
"DELT",
"PHI DELT",
"SAE",
"SIG NU",
"FIJI",
"SIG CHI",
"PHI PSI"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, options);
setListAdapter(myListAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.listfragment1, container, false);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
Fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/fragment2text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! It's Fragment2" />
</LinearLayout>
listfragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:name="com.exercise.AndroidListFragment.MyListFragment1"
android:id="#+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="com.exercise.AndroidListFragment.Fragment2"
android:id="#+id/fragment2"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
Not sure what's the minimal fix to get your code working, but have you looked at using a Navigation Drawer to switch between the fragments? It looks to me like the example in the official docs matches pretty much exactly what you want to achieve.
A key is to have some kind of container for the currently displayed fragment (instead of using <fragment> like in your XML). For example:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then, switching fragments goes something like this:
Fragment fragment = new Fragment2();
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
Further reading: Add a Fragment to an Activity at Runtime in Android developer docs.
private void openFragment (int number) {
Fragment fragment = new Fragment();
switch (number) {
case FRAGMENT_1:
fragment = new *Fragment1*();
break;
case FRAGMENT_2:
fragment = new *Fragment2*();
break;
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment)
.commit();
}

Android — Add new tab when button click like Google Chrome new button?

In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?
First, is it possible in Android?
If possible, how to do this?
When I click on the + button, a new tab should be generated. How to do this?
Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.
Your Manifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dynamictab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BlankActivity"/>
</application>
</manifest>
here is your activity_main.xml file for Tab activity
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_weight="1">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip" />
</HorizontalScrollView>
<Button android:id="#+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Add"/>
</LinearLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.
Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_event"
android:clickable="true"
android:focusable="true"
android:text="Default Tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is your MainActivity.java file
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab();
((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabIndex++;
addTab();
}
});
}
private void addTab(){
LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);
Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
protected void setupTab(View tabBtn, Intent setClass,String tag) {
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
tabHost.addTab(setContent);
}
}
And here is the BlankActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BlankActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(BlankActivity.this);
tv.setText("Blank activity");
setContentView(tv);
}
}
For button click create a new tabspec.
TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
//What ever thing you want to display inside the tab
TextView text = new TextView(CONTEXT);
text.setText("New tab");
return(text);
}
});
spec.setIndicator("New");
th.addTab(spec);
Use the following youtube tutorial as reference. Its simple.
http://www.youtube.com/watch?v=NcKSFlYEqYY
Create an Activity as below:
public class WebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView wv=new WebView(this);
setContentView(wv);
}
}
Now use #Partik's method to add a new tab, on each + button click:
private void addTab(){
Button tabBtn = new Button(MainActivity.this);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, WebViewActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}

ProgressDialog in Tabbar android

I am trying to make a tab bar application in android, after searching in google i found some tutorial and i succeeded. I want 3 tabs, when i click on 1st tab some data will be taken from server using asyntask and show the progressdialog in frontend, but the progressdialog is capturing the whole screen(with tabbar) and because of that i can't be able to switch the 2nd or 3rd tab.
Expected Output: Whenever i click on any tab a data will be taken from server in background, as i know data is loaded in background so for that time i want to switch 2nd tab or any other tab.
package com.tabexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ArrowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrowspage);
Button back = (Button) findViewById(R.id.BackButton2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog p = new ProgressDialog(getParent());
p.setMessage("Loading");
p.setCancelable(false);
p.show();
p.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
});
}
}
package com.tabexample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class EditActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListContent));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
startActivity(new Intent(EditActivity.this, OptionsActivity.class));
}
});
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3"};
}
package com.tabexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
}
}
package com.tabexample;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabExmapleActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, ArrowsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));
tabHost.setCurrentTab(0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Arrows Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton2"
android:text="Back" android:layout_below="#+id/TextView02">
</Button>
<Button android:id="#+id/Button01" android:layout_below="#id/BackButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Indeterminate Dialog"></Button>
<Button android:id="#+id/Button02" android:layout_below="#id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Options Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton1"
android:text="Back" android:layout_below="#+id/TextView01">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/NextButton1"
android:text="Next" android:layout_toRightOf="#+id/BackButton1"
android:layout_below="#+id/TextView01"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tabexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TabExmapleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ArrowsActivity"></activity>
<activity android:name="EditActivity"></activity>
<activity android:name="OptionsActivity"></activity>
</application>
</manifest>
Just Put Progressbar for the child Activitys you are adding in TabHost .
First it will show progress bar of first activity in onCreate which you set as current activity and if you click on next tab it will show its progress dialog
if i get you correctly, you need to access the below activity components when the progress dialog is active.
for this you need to make non-modal/modeless progress dialog. non-modal dialogs will allow you to accept events by behind ui components
please refer the post below
timed modeless dialog

Android: Landscape layouts not being used

In my application I have one Main activity that sets up a tabbed ViewPager with three different fragments, each one with it's own layout and class. I've created landscape versions of each layout file and placed them all in res/layout-land. But when I run the app and switch orientations, the landscape layout isn't being used?
MainActivity:
package me.zaydbille.utilitywatch;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
// Tab and ViewPager variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Coin Flip", "Counter", "Choice"};
int numTabs = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, numTabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
#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_main, 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) {
Intent myIntent = new Intent(this, HelpActivity.class);
startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
// Counter Fragment helper methods
public void addCount() {
Preferences.setIntPref(this, Preferences.getIntPref(this) + 1);
}
public void clearCount() { Preferences.setIntPref(this, 0); }
public int getCount() { return Preferences.getIntPref(this); }
// Choice Fragment helper methods
public void saveList(ArrayList<String> list){ Preferences.saveList(this, list); }
public ArrayList<String> getList() { return Preferences.getList(this); }
}
Fragment 1:
package me.zaydbille.utilitywatch;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class CoinFlip extends Fragment {
Button flipButton;
Random randomizer;
ImageView mSpinningCoin;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.coin_flip,container,false);
flipButton = (Button) v.findViewById(R.id.flipButton);
mSpinningCoin = (ImageView) v.findViewById(R.id.coin_spinning);
flipButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
randomizer = new Random();
int number = randomizer.nextInt(2);
if (number == 0) { // Heads
((AnimationDrawable) mSpinningCoin.getBackground()).stop();
mSpinningCoin.setBackgroundResource(R.drawable.coin_spin_heads);
((AnimationDrawable) mSpinningCoin.getBackground()).start();
} else if (number == 1) { // Tails
((AnimationDrawable) mSpinningCoin.getBackground()).stop();
mSpinningCoin.setBackgroundResource(R.drawable.coin_spin_tails);
((AnimationDrawable) mSpinningCoin.getBackground()).start();
}
}
});
return v;
}
}
Fragment 1's layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ColorPrimaryLight">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="200dp">
<ImageView
android:id="#+id/coin_spinning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coin_spin_heads"
android:contentDescription="#string/coinDescription" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/flipButtonText"
android:id="#+id/flipButton"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.zaydbille.utilitywatch" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenLayout|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HelpActivity"
android:label="#string/title_activity_help"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="me.zaydbille.utilitywatch.MainActivity" />
</activity>
</application>
</manifest>
When you use android:configChanges="orientation|screenLayout|screenSize" you are saying that you do not want the system to do any of its default behavior when these configuration changes happen. This includes changing any layouts. Remove that line or actually handle the change yourself.
Just remove this piece of code from your AppManifest.xml:
android:configChanges="orientation|screenLayout|screenSize"
This disables your activity to change its orientation. Removing that will fix it.

Categories

Resources