actionBar setCustomView not workin - android

I want to change dynamically the aspect of my actionbar. Applying the custom view layout i've created before, but still not working. This is my code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.activity_main);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
#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) {
int id = item.getItemId();
ActionBar mActionBar = getSupportActionBar();
Toast.makeText(this,item.toString(),Toast.LENGTH_LONG).show();
if(item.toString() == "search"){
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.search);
}
return super.onOptionsItemSelected(item);
}
Custom layout xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>

consider trying item.toString().equals("search")

Related

Android toolbar : Why three dots does not appear ?

Why I create toolbar I do not have three dots who will appear. How can I do ?
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
And XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="woowin.woowin.Main">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#393939"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
And can you explain me, how add options in this three dots after that ?
thank you
you forgot to add your menu in your activity use below code to add menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
You need to add at least one menu item either via XML or in java code using add into your menu to see the menu options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"title");
return true;
}

start a new layout when click on items in navigation drawer

I have a MainActivity.java and NavigationDrawer. I want to when I click on items in navdrawer, start new layout. I know I can start new Activity, but I don't want a new activity. new layout is suitable for me. is this possible???
this is my ActivityMain.java:
public class MainActivity extends AppCompatActivity {
public Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(),"SOMETHING",Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
}
and this is my second layout that I want start when I click on items:`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#132740"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:textColor="#FFFFFF"
android:id="#+id/textView"
android:textSize="22sp"
android:layout_marginTop="20dp"
android:text="blah blah"/>
</RelativeLayout>
is this possible?
If is just the layout you wanna to change, there is a very simple soluction:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
But considering using Fragments:
https://developer.android.com/guide/components/fragments.html
With fragments, you will be able to divide your code in multiples class, for each layout. It will be more readable and light:
Here is an example
Use fragment in order to user multiple layout with different operation. If u just want to change the layout then u can set new layout on item click with setContentView(new layout); .

Inflate the actionbar from menu in tabhost activity

In my app i inflate the action bar in Tabhost activity. Here actionbar dosn't displayed even i inflate using onCreateOptionsMenu(Menu menu)In follwing i have posted my code
TabhostActivity.xml
<?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="match_parent" android:layout_height="match_parent"
android:background="#color/appblue">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
This is my menuItem.xml (Actionbar items)
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.after2.svirtzone.after2_gradle.UserDashBoardActivity">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/favourite_icon"
appmunu:showAsAction="always"
android:title="Favourite" />
<item
android:id ="#+id/action_navigation"
android:icon="#drawable/ic_menu"
appmunu:showAsAction = "always"
android:title="navigation"
android:layout_gravity="left"/>
</menu>
This is my TabhostActivity
public class TabHostActivity extends TabActivity {
private Context context;
private static final String TAB_1_TAG = "AboutCollege";
private static final String TAB_2_TAG = "Focus of Course";
private static final String TAB_3_TAG = "Admision";
private static final String TAB_4_TAG = "Contact Details";
private static final String TAB_5_TAG = "Back";
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
//here this the code for displaying the actionbar
#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_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host);
context = getApplicationContext();
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec tag5= tabHost.newTabSpec(TAB_5_TAG);
TabHost.TabSpec tag4= tabHost.newTabSpec(TAB_4_TAG);
TabHost.TabSpec tag3= tabHost.newTabSpec(TAB_3_TAG);
TabHost.TabSpec tag2= tabHost.newTabSpec(TAB_2_TAG);
TabHost.TabSpec tag1= tabHost.newTabSpec(TAB_1_TAG);
tag1.setIndicator("", getResources().getDrawable(R.drawable.college_hover)).setContent(new Intent(this,AdmissionActivity.class));
tag2.setIndicator("", getResources().getDrawable(R.drawable.course)).setContent(new Intent(this, AboutCollegeActivity.class));
tag3.setIndicator("", getResources().getDrawable(R.drawable.admission)).setContent(new Intent(this, AboutCollegeActivity.class));
tag4.setIndicator("", getResources().getDrawable(R.drawable.contact)).setContent(new Intent(this, AboutCollegeActivity.class));
tag5.setIndicator("", getResources().getDrawable(R.drawable.back)).setContent(new Intent(this, AboutCollegeActivity.class));
tabHost.addTab(tag1);
tabHost.addTab(tag2);
tabHost.addTab(tag3);
tabHost.addTab(tag4);
}
}
In here Actionbar dosn't displayed but all other activites other than TabhostActivity it worked perfectly
Define a private variable of appcompatActivity first like these
private AppCompatActivity myact;
then add below lines inside your oncreate method
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
myact.setSupportActionBar(toolbar);
for api's below level 11
myact.getActionBar(toolbar);
I think this should solve the problem.See the root cause of this problem is your class is extending TabActivity not AppcompatActivity or Activity.So we have to specifically mention whether to show the Actionbar or not. I would recommend to use the Android Studio's standard Tabsliderview to design your application it is much easier to use and is well structured.

Action bar options are not showing in android studio

enter image description hereI am new to android, right now I am referring the sunshine app by udacity.com tutorials. The code is the same, but still it is not showing the option settings and doesn't refresh on the action bar
How could I resolve my problem?
MainActivity. java code for main menu
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new ForecastFragment())
.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.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);
}
}
and ForecastFragment.java for refresh menu
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
the main menu xml file code
<menu 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"
tools:context="com.example.android.sunshine.app.MainActivity" >
<item android:id="#+id/action_settings"
android:orderInCategory="100" />
app:showAsAction="never"
android:title="#string/action_settings"
android:orderInCategory="100" /></menu>
the refresh menu is in forecastfragment.xml file code for refrsh menu is
<item android:id="#+id/action_refresh"
app:showAsAction="never"
android:title="#string/action_refresh"
android:orderInCategory="100" />
the manifest file is
<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>
</application>
the style.xml file
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:icon">#drawable/ic_launcher</item>
</style>
<!--style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style-->
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
In your fragment's onCreateView method write
setHasOptionsMenu(true);
and add this super.onCreateOptionsMenu(menu, inflater); to your onCreateOptionsMenu
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.forecastfragment, menu);
}
you can add this below code in your activity_main.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
and this in your MainActivity.java
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
Your complete MainActivity should look like this
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
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.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 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);
//setHasOptionsMenu(true);
return rootView;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
}
}
and if you want to show your refresh menu then you need to put this code in your fragment setHasOptionsMenu(true);
and you activity_main.xml can be like this
<?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="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.sunshine.app.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
it should work...
In your menu.xml you have to set app:showAsAction="always" and it will show.
Material design does'nt support ActionBar. So you cannot see ActionBar as well as App icon. If you want ActionBar like functionality then you can use toolbar like the following code does...
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Extend you baseClass from ActionBarActivity class.
This should take care of this issue.
public class MainActivity extends ActionBarActivity{
}

how to get the action bar button? findviewbyid seems doesn't work

Suppose I have two buttons, one is an action button which is in the action bar(#+id/button1). And another is common button in the layout(#+id/button2).
How can I set the button1 disabled when i click the button2?
findViewById(button1) seems not work. it will return null.
this is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/button1"
android:title="submit"
android:showAsAction="always" />
</menu>
and this is my mainacticity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.button1 ) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
button1 = (Button) findViewById(R.id.button1);/*which return null*/
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button1.setEnabled(false);/*what i failed to do*/
}
});
}
1.Create a variable MenuItem in your Activity class
MenuItem menuItem;
2. Find your variable in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.item_circuit);
return super.onCreateOptionsMenu(menu);
}
Disable the item in your buttonClick method
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItem.setEnabled(false);
}
});
Try this
public void button1click(View v)
{
Button button2=(Button)getActionBar().getCustomView().findViewById(R.id.button2);
button2.setClickable(false);
button2.setEnabled(false);
}
You have to inflate the layout/views before using findViewById.
Like that:
private Toolbar toolbar;
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cartLayout = (RelativeLayout) toolbar.findViewById(R.id.rl_cart);
Your toolbar layout would be like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<RelativeLayout
android:id="#+id/rl_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="#drawable/ic_cart" />
</RelativeLayout>
</RelativeLayout>
You can find more answer here:
How can I implement custom Action Bar with custom buttons in Android?
How to set two custom actionbar button in left and right in android?

Categories

Resources