I am trying to hide the ActionBar in my Activity. I have an Activity with a Fragment, which looks like this:
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro_activity);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
ActionBar actionBar = getSupportActionBar();<---- Error here
actionBar.hide();
}
}
#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_intro, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.intro_page_1, container, false);
return rootView;
}
}
}
The error I get is Cannot resolve method getActionBarActivity method. I have tried extending ActionBarActivity, but when I do, I get onCreate errors, and several others.
How can I hide the action bar?
It does not look like you are using the support library? If that is the case you should call getActionBar() not getSupportActionBar()
From your imports, it looks like you're not using support library. So you should use getActionBar() instead of getSupportActionBar().
use this code
ActionBar bar = getActionBar();
bar.hide();
This will solve your problem
Related
I'm trying to learn about unit testing in Android and have been writing a test for a fragment. I've found that when using the platform fragments getView returns the fragment's view fine. However, when I use a support fragment getView returns null during the test.
I've created a sample project to demonstrate this at https://github.com/appassembla/fragmenttestingsample. Running HelloWorldExampleActivityTest passes fine but HelloWorldExampleActivityTest2 fails because getView is null in the support fragment. I'm using Android Studio to run the tests.
Not sure if it's something I'm doing wrong or whether there is a problem with support fragments. Does anyone have any ideas?
Thanks for your help.
My test class is as follows:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
public class HelloWorldExampleActivity2Test extends ActivityUnitTestCase<HelloWorldExampleActivity2> {
public HelloWorldExampleActivity2Test() {
super(HelloWorldExampleActivity2.class);
}
private HelloWorldExampleActivity2 activity;
#Override
protected void setUp() throws Exception {
super.setUp();
startActivity(new Intent(getInstrumentation().getTargetContext(),
HelloWorldExampleActivity2.class), null, null);
activity = getActivity();
}
/**
* Adds the fragment to a the activity, thereby fully initializing its view.
*/
private void startFragment(Fragment fragment) {
FragmentManager manager = activity.getSupportFragmentManager();
manager.beginTransaction().add(fragment, null).commit();
manager.executePendingTransactions();
}
public void testSimpleFragment() {
SimpleFragment2 fragment = new SimpleFragment2();
startFragment(fragment);
assertNotNull(fragment.getView());
}
}
My activity is:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class HelloWorldExampleActivity2 extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_example);
}
#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_hello_world_example, 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);
}
}
My fragment is:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link android.app.Fragment} subclass.
*/
public class SimpleFragment2 extends Fragment {
public SimpleFragment2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_simple, container, false);
}
}
Im starting Android developing and learning java. My problem is the "the method add(int fragment) in the type fragmenttransaction is not applicable" error in
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
<==here==> .add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
}
when i used import android.support.v4.app.Fragment; elypse show no error but on my phone app crash
Thanks For Help!
You can use getSupportFragmentManager() if you use android.support.v4.app.Fragment only. Try using getFragmentManager() or could you copy the error log?
I dont know what happen with my sdk and eclipse. I install google services,it is install successfully but now i add activity to my old project but now .java file is extends FragementActivity. i dont know why this is happen. Please give reply please.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class WebViewActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.web_view, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I looked already a lot for a solution for my problem and found similar items - but unfortunately I could not get round with that all to solve my problem. Here is what messes me up:
My small project is with a button and an onClickListener on it.
package com.example.wbbtn;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity {
Button wb1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
wb1 = (Button) findViewById(R.id.b1);
wb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I put a breakpoint at wb1 = (Button) findViewById(R.id.b1);
When I start the debugging with my tablet already set to the debugging mode and connected to the PC and hit the F6 key I get the message that a source is not found.
I've made a screen shot to make things a bit more clear but I was told to have at least 10 reputations to post a pic. Okay - sorry !
Hello - sorry for this late response. Okay here are the links to the screen shots - hope it works:
a) source not found
b) Break point
I am new to android development.I am trying to create an activity method on clicking a button.
Below is my MainActivity.java file code.
I am getting errors
1."cannot resolve method'setVisibility (int)'" and
2."cannot resolve symbol 'TextView'"
please guide.
MainActivity.java
package com.AndroidLove;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onLoveButtonClicked(View view) {
TextView textView =(TextView) findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Import this package as well
import android.widget.TextView;