Getting error while running an activity with custom navigation drawer - android

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);
}

Related

NullPointerException - I don't understand what's wrong

I am a complete newbie in android development. At the moment I am trying to replicate an app I saw on where it is possible to convert multiple images (jpeg, png, jpg) into a single pdf file. I have tried to do the most I can with debugging. But I don't understand why there are still this NullPointer Exception.
Oh and by the way please pardon me if I have some mistakes in placing proper tags or such in my question this is my first question thank you.
Here's my code for the ConvertToPDFConverter.java
package com.example.lenovo_thinkpad.mobilelectureboard;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
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.MenuItem;
import android.view.View;
public class ConvertToPDFConverter extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
android.support.v7.app.ActionBar bar;
CustomActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert_to_pdfconverter);
bar = this.getSupportActionBar();
assert bar != null;
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
bar.setDisplayShowTitleEnabled(false);
drawer = findViewById(R.id.drawer_layout);
drawer.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
try {
toggle = new CustomActionBarDrawerToggle(this, drawer);
} catch (RuntimeException e) {
e.printStackTrace();
}
drawer.addDrawerListener(toggle);
// Set navigation drawer
// Set com.example.lenovo_thinkpad.mobilelectureboard.Home fragment
Fragment fragment = new Home();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.nav_camera:
fragment = new Home();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
break;
case R.id.nav_gallery:
fragment = new ViewFiles();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {
CustomActionBarDrawerToggle(Activity mActivity,
DrawerLayout mDrawerLayout) {
super(mActivity, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
}
#Override
public void onDrawerClosed(View view) {
bar.setTitle(getString(R.string.navigation_drawer_close));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
bar.setTitle(getString(R.string.navigation_drawer_open));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}
}
and for the layout activity_convert_to_pdfconverter.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ConvertToPDFConverter">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/mb_white"
android:id="#+id/content"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Here's the stacktrace
02-13 15:12:17.976 1548-1548/com.example.lenovo_thinkpad.mobilelectureboard E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lenovo_thinkpad.mobilelectureboard, PID: 1548
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo_thinkpad.mobilelectureboard/com.example.lenovo_thinkpad.mobilelectureboard.ConvertToPDFConverter}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.lenovo_thinkpad.mobilelectureboard.ConvertToPDFConverter.onCreate(ConvertToPDFConverter.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5001) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method) 
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme_picker">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WhiteBoardActivity" />
<activity android:name=".ConvertToPDFConverter" />
</application>
Since you are extending the AppCompatActivity, try to get the actionbar via 'getSupportActionbar()'.

Android: Default Tabbed Activity wont start when button is clicked

I know this has been asked a lot, already have done search on this particular topic. and all of them returns the same answer but doesn't solve my problem, I have a button created on separate xml, used as a fragment. TabbedActivity just wont fire up, App is crashing when button is clicked, even throws NPE on first code used. just don't know what to do.
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set HomeFragment as user starts the app.
if (savedInstanceState == null){
InitStartFrag();
}
//-----BUG!-----//
1st:Code Used
android.support.v7.widget.AppCompatButton myButton = (android.support.v7.widget.AppCompatButton)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
}
});
2nd Code:
public void doThis(View view){
Intent intent = new Intent(this, TabbedActivity.class);
MainActivity.this.startActivity(intent);
}
3rd Code:
public void doThis(View view){
setContentView(R.layout.activity_tabbed);
}
//-----Bug Ends-----//
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void InitStartFrag() {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
.....
.....
}
The Log Cat:
Process: com.my_app.app_one, PID: 3799
java.lang.IllegalStateException: Could not find a method doThis(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'myButton'
at android.view.View$1.onClick(View.java:3843)
at android.view.View.performClick(View.java:4471)
at android.view.View$PerformClick.run(View.java:18778)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5324)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: doThis [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:864)
at android.view.View$1.onClick(View.java:3836)
at android.view.View.performClick(View.java:4471) 
at android.view.View$PerformClick.run(View.java:18778) 
at android.os.Handler.handleCallback(Handler.java:808) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5324) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
The Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_app.app_one">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TabbedActivity"
android:label="#string/title_activity_tabbed"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.my_app.app_one.MainActivity" />
</activity>
</application>
</manifest>
The XML, used as Fragment : buttons_layout.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" >
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Upper Buttons-->
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:text="Fire"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn1"
android:textColor="#color/colorBtnText1"
android:drawableStart="#drawable/fire_icon"
android:drawablePadding="-62dip"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:singleLine="true"
android:gravity="center"
android:onClick="doThis"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_below="#id/myButton"
android:text="Flood"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn3"
android:textColor="#color/colorBtnText3"/>
<!-- Divider -->
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_toEndOf="#id/myButton1"
android:text="Thunder"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn2"
android:textColor="#color/colorBtnText2"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#id/myButton2"
android:layout_gravity="center_horizontal"
android:layout_toEndOf="#id/myButton1"
android:text="Civil Defense Operation Center"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn4"
android:textColor="#color/colorBtnText4"/>
<!--Lower Buttons-->
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_below="#id/myButton3"
android:text="Landslide"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn5"
android:textColor="#color/colorBtnText5"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_below="#id/myButton5"
android:text="Women and Children Protection Center"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn7"
android:textColor="#color/colorBtnText7"/>
<!-- Divider -->
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_below="#id/myButton4"
android:layout_toEndOf="#id/myButton3"
android:text="Storm"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn6"
android:textColor="#color/colorBtnText6"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/myButton8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#id/myButton6"
android:layout_gravity="center_horizontal"
android:layout_toEndOf="#id/myButton5"
android:text="Earthquake"
android:textSize="15sp"
app:layout_heightPercent="25%"
app:layout_widthPercent="50%"
app:backgroundTint="#color/colorBtn8"
android:textColor="#color/colorBtnText8"/>
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
New Code Used:
private void InitBtnListener(){
LayoutInflater layoutInflater = this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.buttons_layout,null);
android.support.v7.widget.AppCompatButton myButton = (android.support.v7.widget.AppCompatButton)view.findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
}
});
}
TabbedFragment.java Code
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class TabbedFragment extends Fragment implements OnClickListener {
View myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.survival_tips_layout,container,false);
android.support.v7.widget.AppCompatButton myButton = (android.support.v7.widget.AppCompatButton)myView.findViewById(R.id.myButton);
myButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.myButton:
//--Missing Code:--//
After Btn Click Fire up that TabbedActivity.
//--Ends--//
// Can I do intents Here?
Intent intent = new Intent(MainActivity.this,TabbedActivity.class);
startActivity(intent);
break;
}
}
}
This line needs to be above any findViewById
setContentView(R.layout.activity_main);
If the android:onClick is defined from the XML, you need a public method that accepts a View parameter with that name.
For example, android:onClick="doThis" means you need this method
public void doThis(View view) {
// TODO: Handle click
}
i have a button created on separate xml, used as a fragment
Then you need to find that button from the Fragment class. You can't find it from the Activity.
For example
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Inflate the layout
View rootView = inflater.inflate(R.layoutbuttons_layout, parentViewGroup, false);
Button button = (Button) rootView.findViewById(R.id.button);
// TODO: button.setOnClickListener();
return rootView;
}
// OR... if you have android:onclick="doThis"
public void doThis(View view) {
// TODO: Handle click
}
}

Bluetooth app crashes on button press

I'm new to Android & was trying out the Bluetooth APIs of Google. Whenever I run my app on my phone, it crashes for some reason. I'm following the Google Developer's tutorial for the APIs & have made changes to my MainActivity.java & content.xml accordingly, but no use. Please have a look.
Here's my MainActivity.java
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.bluetooth.*;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn;
private BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
private int REQUEST_ENABLE_BT = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
#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) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void bluetoothDiscovery()
{
if(BA == null)
{
System.out.println("System Doesn't Support Bluetooth");
}
if(!BA.isEnabled())
{
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTIntent,REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this,"Turned On!!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "ALREADY ON", Toast.LENGTH_LONG).show();
}
}
}
Here's my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vertex2016.mvjce.edu.bluetoothapptry">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="vertex2016.mvjce.edu.bluetoothapptry.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<!-- <android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email"
android:onClick="bluetoothDiscovery"/> -->
</android.support.design.widget.CoordinatorLayout>
Here's my content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="vertex2016.mvjce.edu.bluetoothapptry.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTButton"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="bluetoothDiscovery"/>
</RelativeLayout>
And here's my logcat
03-13 02:06:34.719 18759-18759/vertex2016.mvjce.edu.bluetoothapptry E/AndroidRuntime: FATAL EXCEPTION: main
Process: vertex2016.mvjce.edu.bluetoothapptry, PID: 18759
java.lang.IllegalStateException: Could not find method bluetoothDiscovery(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4789)
at android.view.View$PerformClick.run(View.java:19881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Thank you for your time
Your method bluetoothDiscovery must accept a parameter of type View, see View.onClick for reference.
public void bluetoothDiscovery(View v) {}
When this method is called, v will be the View object that was clicked.
i.e.
switch ( v.getId() ) {
case R.id.button:
// do something
break;
case R.id.other_button
// do something else
break;
}
Where r.id.button comes from your definition in XML,
android:id="#+id/button"
It is using this, you could have a single onClick method that is called by multiple buttons, each one with their own action.
You need a View parameter in your onClick method
public void bluetoothDiscovery(View v)
Personally, I try not to add onClick in the XML because they are hard to find as you add more classes and layouts. You can just set the OnClickListener from the Java code for clearer visibility.

Title not showing in Toolbar when part of DrawerLayout

I'm new to Android programming.
I'm trying to replicate an IOS app of mine while learning the ropes.
I'm not using Android Studio wizards to create any activities - doing everything manually so that I may understand what goes on.
I've setup an AppCompat activity using a DrawerLayout and a toolbar.
Initially, before I noticed the existence of DrawerLayout I had setup the same overall design using a RelativeLayout.
While my Toolbar was inside the Relative Layout, I could set its title without a problem.
When I replaced RelativeLayout and put the Toolbar inside the DrawerLayout the title stopped displaying.
I can see in the debugger that the title is getting set correctly, it just does not show up.
I have currently tied up an ActionBarDrawerToggle with DrawerLayout.
Previously, I was not using an ActionBarDrawerToggle.
In both cases, Toolbar title is not displaying.
If I restore RelativeLayout, the title is showing up again.
I have searched everywhere in SO but can't find a solution.
Layout for the activity is this:
<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/app_drawer">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/fragment_items_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginTop="52dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="256dp"
android:layout_height="match_parent"
android:background="#color/lightGray"
android:id="#+id/fragment_nav_list_container"
android:layout_gravity="start"
/>
Layout for the toolbar is this:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="52dp"
android:layout_width="match_parent"
android:minHeight="52dp"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="App Title"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:background="#color/red"
/>
Code for the activity is this:
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import DatabaseMaster;
public class MainWrapperActivity extends AppCompatActivity {
class MainWrapperDrawerToggle extends ActionBarDrawerToggle {
public MainWrapperDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, openDrawerContentDescRes, closeDrawerContentDescRes);
}
public MainWrapperDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
}
// Drawer Listener
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView,slideOffset);
Log.d("debug","Main App Drawer: SLIDE");
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Log.d("debug","Main App Drawer: OPENED");
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Log.d("debug","Main App Drawer: CLOSED");
}
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
Log.d("debug", "Main App Drawer: STATE CHANGED: " + newState);
}
}
private MainWrapperDrawerToggle mActionBarDrawerToggle;
private DrawerLayout mDrawerLayout;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_wrapper);
// Add / Configure Toolbar
mToolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
// Setup App Drawer
mDrawerLayout=(DrawerLayout)findViewById(R.id.app_drawer);
mActionBarDrawerToggle=new MainWrapperDrawerToggle(this,mDrawerLayout,mToolbar,R.string.app_name,R.string.app_name);
mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
//getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setDisplayShowTitleEnabled(false);
//mActionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_nav_menu);
// Add Fragment(s)
FragmentManager fm=getSupportFragmentManager();
// Check for existing fragment by the ID of its Container
Fragment fragment=fm.findFragmentById(R.id.fragment_items_list_container);
if (fragment == null) {
fragment=new SectionsListFragment();
// Add fragment to it's Container
fm.beginTransaction().add(R.id.fragment_items_list_container,fragment).commit();
}
}
#Override
protected void onPostCreate (Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
mToolbar.setNavigationIcon(R.drawable.ic_nav_menu);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
protected void onStart () {
super.onStart();
}
#Override
protected void onResume () {
super.onResume();
TextView toolbarTitleView=(TextView)mToolbar.findViewById(R.id.toolbar_title);
toolbarTitleView.setText(R.string.app_name);
}
#Override
protected void onDestroy () {
DatabaseMaster.getInstance().getRealm().close();
DatabaseMaster.getInstance().setRealm(null);
super.onDestroy();
}
#Override
public boolean onSupportNavigateUp () {
Log.i("debug", "Toggling hamburger menu");
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return false;
}
}
DrawerLayout only contains two childs i.e. only two layouts. You can replace your FrameLayout with another RelativeLayout and place your FrameLayout and include tag in that new RelativeLayout.
<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/app_drawer">
<include layout="#layout/new_container"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="256dp"
android:layout_height="match_parent"
android:background="#color/lightGray"
android:id="#+id/fragment_nav_list_container"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
And in Relative Layout Use this
<RelativeLayout
android:id="#+id/new_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/fragment_items_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
</RelativeLayout>
Rest is fine.Hope This Help.

Navigation Drawer, ListView Items don't show

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

Categories

Resources