I want to display all five Icons evenly in my toolbar. But currently there is only one icon displayed on the left hand side of the toolbar, the rest of the space inside the toolbar is empty. I tried to manage the items with a Method called setupEvenlyDistributedToolbar.
I got this Code in MainActivity:
package com.example.timomichel.myapplication;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.Point;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setupEvenlyDistributedToolbar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.items, menu);
return true;
}
public void setupEvenlyDistributedToolbar() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.items);
toolbar.setContentInsetsAbsolute(0, 0);
int childCount = toolbar.getChildCount();
int screenWidth = (width/5);
Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < childCount; i++) {
View childView = toolbar.getChildAt(i);
childView.setLayoutParams(toolbarParams);
}
}
}
My XML file for MainActivity:
<?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="com.example.timomichel.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<include layout="#layout/toolbar" />
</RelativeLayout>
Here is my XML for the toolbar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
</android.support.v7.widget.Toolbar>
And finally the XML for the items in the menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/profile"
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="profile" />
<item
android:id="#+id/friends"
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="friends"/>
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="maps"/>
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="events" />
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="Settings" />
</menu>
Hope you can also help me with this question, thanks a lot!
Related
My navigation drawer doesn't show any items. I changed the background to white, theme has changed but still no items showing up. I am also using the frame layout inside drawer and linear layouts. No errors or messages. What am I doing wrong?
EDIT: I oversimplified my work, but still nothing. No items..
EDIT2: After deleting more I made some progress, now I see that, in preview, my drawer_menu shows up like options menu on top right corner even though in the postLoginactivity.xml file preview shows what I aimed to do. Still no items on emulator.
EDIT3: I have Cutt my code in halve, still can't see what did I do wrong.
activity_post_login.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"
>
<!-- burdan sonra komple sıkıntı gibi?-->
<LinearLayout
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Light" >
<TextView
android:id="#+id/textView"
android:layout_width="326dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:accessibilityLiveRegion="assertive"
android:paddingStart="100dp"
android:paddingEnd="100dp"
android:text="#string/deneme"
android:textSize="30sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.Toolbar>
<!-- android:layout_height=?attr/actionBarSize-->
<FrameLayout
android:id="#+id/mainFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
>
<ExpandableListView
android:id="#+id/list_sliderMenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="176dp"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"
/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
nav_header.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="176dp"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher_round"/>
</LinearLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<!--tools:showIn="navigation_view"-->
<group android:checkableBehavior="single"
android:id="#+id/menu_list">
<item android:title="#string/satisYonetimi"
android:id="#+id/satis_yonetimi"/>
<item
android:id="#+id/insanKaynaklari"
android:title="#string/insanKaynaklari" />
<item
android:id="#+id/muhasebeYonetimi"
android:title="#string/muhasebeYonetimi" />
<item
android:id="#+id/aktiviteYonetimi"
android:title="#string/aktiviteYonetimi" />
<item
android:id="#+id/destekYonetimi"
android:title="#string/destekYonetimi" />
</group>
</menu>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
v21\styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="#style/AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
Plus if anyone thinks problem might be somewhere else here are category.java(expandable list), subcategory.java (future sub menus will be implemented ) and and my activity for the drawer to be displayed(under implementation)
category.java
public class Category {
private String cat_name;
private int cat_code;
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public String getCatName(){
return cat_name;
}
public void setCat_code(int cat_code) {
this.cat_code = cat_code;
}
public int getCat_code() {
return cat_code;
}
}
subcategory.java
public class SubCategory {
private String subcat_name;
private String subcat_code;
public void setSubCatName(String subcat_name) {
this.subcat_name = subcat_name;
}
public String getSubCatName(){
return subcat_name;
}
public void setSubCatCode(String subcat_code) {
this.subcat_code = subcat_code;
}
public String getSubCatCode() {
return subcat_code;
}
}
postLogingActicity.java
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
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.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import java.util.ArrayList;
public class PostLoginActivity extends AppCompatActivity {
ActionBar bar;
ViewPager viewPager;
FragmentPagerAdapter fpAdapter;
Fragment mMainScreen;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ExpandableListView mCategoryList;
private ArrayList<Category> category_name = new ArrayList<Category>();
private ArrayList <ArrayList<Category>> subcategory_name = new ArrayList
<ArrayList<Category>>();
private ArrayList<Integer> subCatCount = new ArrayList<Integer>();
int previousGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_login);
//Toolbar toolbar =findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//mDrawerLayout = findViewById(R.id.drawer_layout);
//ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
//mDrawerLayout, toolbar,
// R.string.navigation_drawer_open,
//R.string.navigation_drawer_close);
//mDrawerLayout.addDrawerListener(toggle);
//toggle.syncState();
//this.getCatData();
//Utils.deleteStringFromSp(this, "email");
}
public void onBackPressed(){
if(mDrawerLayout.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
}
}
your main activity must be as follows
<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:layoutDirection="rtl"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
And activity_main_drawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_menu7"
android:icon="#drawable/ic_school_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu11"
android:icon="#drawable/ic_thumb_up_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu12"
android:icon="#drawable/ic_insert_drive_file_black_24dp"
android:title="XXXX" />
<item
android:id="#+id/nav_menu6"
android:icon="#drawable/ic_developer_mode_black_24dp"
android:title="XXXX" />
</group>
</menu>
In Kotlin Navigation Menu
// Navigate Menu
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_menu7 -> {
val intent = Intent(this, Computer::class.java)
startActivity(intent)
}
R.id.nav_menu6 -> {
val menu6 = Menu6()
val fragmentManager = supportFragmentManager
fragmentManager.beginTransaction().replace(R.id.fragment, menu6).addToBackStack(null).commit()
}
R.id.nav_menu12 -> {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://XXXXX"))
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
I hope this solve your problem
Menu works with Activity,Fragment,URL
You've missed to reference the menu of the navigation view
As you named it "drawer_menu_xml", then the modification would be:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"/>
I'm trying to design a dialog, which looks similarly to the system one:
Are these buttons planted on the application title bar? Or are they simply placed in the UI?
You can use a ToolBar to do this. This is an example with icon-based buttons, but you can easily modify it into Text Buttons.
ToolBar Tutorial
1 - Add library compability
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
2 - Create a file name color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#FF5722</color>
<color name="ColorPrimaryDark">#E64A19</color>
</resources>
3 - Modify your style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
</resources>
4 - Create a file name toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp"
>
</android.support.v7.widget.Toolbar>
5 - Include the ToolBar into your main_activity.xml
<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=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<TextView
android:layout_below="#+id/tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/TextDimTop"
android:text="#string/hello_world" />
</RelativeLayout>
6 - Put it inside your MainActivity class
package com.example.hp1.materialtoolbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity { /* When using Appcombat support library
you need to extend Main Activity to
ActionBarActivity.
*/
private Toolbar toolbar; // Declaring the Toolbar Object
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
}
#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);
}
}
7 - And finally, add your "Button Items" to the menu_main.xml
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=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom"
></item>
<item
android:id="#+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="#drawable/ic_user"
app:showAsAction="ifRoom"></item>
</menu>
I have made a tabhost. Now the problem is I can not change the tabIndicator's background and the text color. I have seen some tutorial and some solution for my problem,,, but I could not understand them how to use that. I have attached my app's picture. It will make you clear about my problem.
here is my activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.shockwaveplayer.MainActivity"
tools:ignore="MergeRootFrame"
android:background="#drawable/drk9" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="9dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
style="#style/MyTheme"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="242dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#android:id/tabhost"
android:divider="#b5b5b5"
android:listSelector="#drawable/list_selector" >
</ListView>
</RelativeLayout>
and my mainActivity.java
package com.example.shockwaveplayer;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
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.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.os.Build;
import android.provider.MediaStore;
#SuppressLint("NewApi") public class MainActivity extends ActionBarActivity{
Context context;
View decorView;
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = this;
ArrayList<String> alv = new ArrayList<String>();
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION };
#SuppressWarnings("deprecation")
Cursor cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
selection, null, null);
#SuppressWarnings("rawtypes")
// final ArrayAdapter adapter = new ArrayAdapter<String>(this,
// R.layout.list_row,R.id.title,alv);
ListView lv = (ListView) findViewById(R.id.listView1);
tab = (TabHost) findViewById(android.R.id.tabhost);
tab.setup();
TabSpec pec = tab.newTabSpec("assignments_tab");
TabSpec pec2 = tab.newTabSpec("assignments_tab");
TabSpec pec3 = tab.newTabSpec("assignments_tab");
TabSpec pec4 = tab.newTabSpec("assignments_tab");
pec.setContent(R.id.tab1);
pec2.setContent(R.id.tab2);
pec3.setContent(R.id.tab3);
pec4.setContent(R.id.tab4);
pec.setIndicator("album");
pec2.setIndicator("artist");
pec3.setIndicator("songs");
pec4.setIndicator("playing");
tab.addTab(pec);
tab.addTab(pec2);
tab.addTab(pec3);
tab.addTab(pec4);
Property p = new Property(context,cursor);
lv.setAdapter(p);
}
#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;
}
}
}
How can I change the color? Any help or suggestion?
Try to use like following
TabWidget tabWidget = getTabWidget();
TabHost tab = getTabHost();
tab.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
tab.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_selected); // unselected
}
tab.getTabWidget().getChildAt(tab.getCurrentTab())
.setBackgroundResource(R.drawable.tab_unselected); // selected
}
});
Intent intent = new Intent(FromClass.this,
ToClass.class);
TabSpec tabpiechartActivity = tab.newTabSpec("Name")
.setIndicator(prepareIndicator("Name"))
.setContent(intent);
and your prepareIndicator will be
private View prepareIndicator(String string) {
View view = LayoutInflater.from(this).inflate(R.layout.customtab, null);
// ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
TextView tv = (TextView) view.findViewById(R.id.tabText);
// iv.setImageResource(resId);
tv.setText(string);
return view;
}
And your res/layout/customtab xml is:
<?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:background="#drawable/tab_selected"
android:orientation="vertical" >
<TextView
android:id="#+id/tabText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
And res/drawable/tab_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="225"
android:endColor="#11029E"
android:startColor="#DD000000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="0dp"
android:topRightRadius="7dp" />
</shape>
And res/drawable/tab_selected.tab_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#FFFFFFFF" />
<gradient
android:angle="225"
android:endColor="#0F7801"
android:startColor="#DD000000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="0dp"
android:topRightRadius="7dp" />
</shape>
I hope this will help you
You should achieve this by styling your tabhost.
These two answers can guide you through:
Android remove space between tabs in tabwidget
How to change the font size of tabhost in android
But what you need is essentially apply a theme to your tab widget, and add your color of choice / color state drawable (if you want the color to change on focus / press / etc) to the widget's text appearance style:
<style name="CustomTheme" parent="#android:style/Theme">
<item name="android:tabWidgetStyle">#style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="#android:style/Widget.TabWidget">
<item name="android:textAppearance">#style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText"
parent="#android:style/TextAppearance.Widget.TabWidget">
<item name="android:color">#color/red</item>
</style>
Here is a code snippet which i use to customize my Tab Color and bg :-
View view= createTabView(mContext,TAB_1_TAG);
mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator(view), UpcomingTaskFragment.class, null);
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.dashboard_fragment_tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.dashboard_tab_heading);
tv.setText(text);
return view;
}
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/dashboard_tab_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/tab_text_bg"
android:gravity="center"
android:paddingBottom="#dimen/seven"
android:paddingTop="#dimen/seven"
android:shadowColor="#android:color/white"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.5"
android:textColor="#color/dashboard_tab_selector"
android:textSize="#dimen/dashboard_tab_heading_text_size"
android:textStyle="bold" />
For the parent , i use the following selector :-
tab_bg_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#android:color/white"
/>
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_unselected"
/>
</selector>
For the textview text color , i use :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#0094e0"/>
<item android:state_focused="true" android:color="#0094e0"/>
<item android:state_pressed="true" android:color="#0094e0"/>
<item android:color="#808080"/>
</selector>
I try to design action bar. When i create new android project, Appcompat imported directly. I found this question link,
I think this is due to library. it is showing on menu bar but I want to use action bar which has writing actionBarDemo(near actionBarDemo writing). How can I solve this problem ?
How can I change it ? What is problem ?
this is my main.class
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.MenuInflater;
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);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionmenu, menu);
return true;
}
}
this Xml of menu bar:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.bbb.MainActivity"
tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/derya"
android:orderInCategory="100"
android:icon="#drawable/indoor"
android:title="#string/action_search"
android:showAsAction="always"/>
<!-- Location Found -->
<item android:id="#+id/duygu"
android:orderInCategory="100"
android:icon="#drawable/refresh"
android:title="#string/action_location_found"
android:showAsAction="always"/>
</menu>
I'm using the setBackgroundDrawable method to change the background of the button on my Activity. The onClickListener also has an Intent to open up a new Activity. However, when I return to the past Activity by hitting the physical back button, the button with the onClickListener assigned to it still has the onClick background set. If I return to the previous Activity by using the back button in the Action Bar, it works correctly. I tried to use a selector XML, but Android Studio gives me render errors, and it doesn't load when I compile.
Here is the MainActivity.java:
package com.jordandebarth.supercalculator;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton pythag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5")));
final ImageButton pythag = (ImageButton) findViewById(R.id.pythag_button);
pythag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pythag.setBackgroundResource(R.drawable.pythag_button_selector);
Intent pythagIntent = new Intent(MainActivity.this, PythagoreanActivity.class);
startActivity(pythagIntent);
}
});
}
#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;
}
}
activity_main.xml:
<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"
android:background="#e5e5e5"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:layout_width="fill_parent"
android:background="#drawable/pythag_button"
android:layout_height="wrap_content"
android:id="#+id/pythag_button"
android:focusable="true"/>
</RelativeLayout>
user selector in xml file :
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"></item>
<item android:drawable="#drawable/btn_hover" android:state_focused="true"></item>
<item android:drawable="#drawable/btn" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/btn_hover" android:state_enabled="false"></item>
</selector>
layout.xml
<Button
android:id="#+id/btnOk"
android:background="#drawable/button_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5dp"
android:text="#string/yes" />