I am trying to set the ActionBar to a tab activity in android. i tried a lot.
But can't solve this topic. i'm using the Tab Activity, i want to display actionbar above the tabhost. How can I add actionbar in this activity?
I'm using the android studio and sdk is 25
here is my code:
package com.example.lenovo.monitoringapp;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
public class KwhActivity extends TabActivity {
TabHost TabHostWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kwh);
TabHostWindow = (TabHost) findViewById(android.R.id.tabhost);
TabSpec TabMenu1 = TabHostWindow.newTabSpec("Graph");
TabSpec TabMenu2 = TabHostWindow.newTabSpec("Data List");
TabMenu1.setIndicator("Graph");
Intent intent = new Intent(KwhActivity.this, GraphActivity.class);
// intent.putExtra("uname", username);
// Set tab 1 activity to tab 1 menu.
TabMenu1.setContent(intent);
// Setting up tab 2 name.
TabMenu2.setIndicator("Data List");
Intent i1 = new Intent(KwhActivity.this, DataListActivity.class);
// i1.putExtra("uname", username);
// Set tab 3 activity to tab 1 menu.
TabMenu2.setContent(i1);
TabHostWindow.addTab(TabMenu1);
TabHostWindow.addTab(TabMenu2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kwhactivity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_other1:
AlertDialog.Builder builderSingle = new AlertDialog.Builder(KwhActivity.this);
builderSingle.setIcon(R.drawable.calender);
builderSingle.setTitle("Select");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(KwhActivity.this, android.R.layout.select_dialog_item);
arrayAdapter.add("Today");
arrayAdapter.add("Yesterday");
arrayAdapter.add("Week");
arrayAdapter.add("30 Days");
arrayAdapter.add("Year");
builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
if(strName == "Today")
{
Toast.makeText(KwhActivity.this, "Today!",
Toast.LENGTH_LONG).show();
}
}
});
builderSingle.show();
break;
default:
break;
}
return false;
}
}
Here is activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:background="#drawable/rounded_edittext"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_height="30dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Use Toolbar above TabHost by using below code;
<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" />
Related
I have created a custom toolbar for the fragment that I have created and it is supposed to look like this:
The toolbar shown on the left side is from my fragment_purchase_item_list and the right side is for my fragment_purchase_list.
I have tried checking some other posts from here which I followed but it will not show up the menu that I have created.
When I run the code, it will look like this:
The search icon (left), scan and new icon (right) will not show up. Here are my codes:
PurchaseItemListFragment.java
package com.example.devcash.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
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.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.devcash.R;
/**
* A simple {#link Fragment} subclass.
*/
public class PurchaseItemListFragment extends Fragment implements SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
Toolbar itemListToolbar;
Spinner itemListSpinner;
public PurchaseItemListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate the menu
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_purchase_item_list, container, false);
itemListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaseitemlist);
itemListSpinner = (Spinner) view.findViewById(R.id.spinner_allcategories);
///
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.custom_spinner_item,
getResources().getStringArray(R.array.dropdownitempurchase));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemListSpinner.setAdapter(myAdapter);
itemListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),
itemListSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
//handles the search menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.searchmenu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("Search..");
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
}
PurchaseListFragment.java
package com.example.devcash.Fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.devcash.R;
/**
* A simple {#link Fragment} subclass.
*/
public class PurchaseListFragment extends Fragment {
Toolbar purchaseListToolbar;
Spinner purchaseListSpinner;
public PurchaseListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_purchase_list, container, false);
setHasOptionsMenu(true);
purchaseListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaselist);
purchaseListSpinner = (Spinner) view.findViewById(R.id.spinner_customertype);
///
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.customer_type));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
purchaseListSpinner.setAdapter(myAdapter);
purchaseListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),
purchaseListSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//set adapter
//set click listener
//show no data found text when listview is empty
// button click listener
Button btnpay = view.findViewById(R.id.btn_paypurchasetransaction);
btnpay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment chargeFragment = new ChargeFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.customersales_content, chargeFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.purchaselist_menu, menu);
}
}
fragment_purchase_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.PurchaseItemListFragment"
android:orientation="vertical"
android:gravity="center_horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_purchaseitemlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
style="#style/PrimaryHeaderBar"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner_allcategories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_cry_face"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_items"
android:gravity="center"
android:layout_marginTop="#dimen/text_padding"/>
<Button
android:layout_width="150dp"
android:layout_height="30dp"
android:text="#string/go_inventory"
android:layout_gravity="center"
android:layout_marginTop="#dimen/text_padding"
android:background="#color/colorPrimary"
android:textColor="#color/whiteBG"
android:textSize="#dimen/small_title"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
fragment_purchase_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.PurchaseListFragment"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_purchaselist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Light"
style="#style/HeaderBar"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner_customertype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#color/light_gray"
android:padding="#dimen/text_padding"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:src="#drawable/ic_delete"
android:tint="#color/dark_text"
android:layout_gravity="left"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item_count_title"
android:gravity="center"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/opencurly"
android:paddingLeft="#dimen/text_inputs"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/count"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/closecurly"
android:textSize="#dimen/page_title"
android:textColor="#color/dark_text"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical"
android:gravity="bottom"
android:padding="#dimen/text_padding">
<Button
android:id="#+id/btn_paypurchasetransaction"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorPrimary"
android:text="#string/charge"
android:textColor="#color/whiteBG"/>
</LinearLayout>
</LinearLayout>
purchaselist_menu.xml
<?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/action_scan"
android:title="#string/scan_label"
android:icon="#drawable/ic_qr_code_scan"
app:showAsAction="always"
android:actionLayout="#layout/custom_purchaselist_layout"/>
<item
android:id="#+id/action_new"
android:title="#string/new_label"
android:icon="#drawable/ic_add"
app:showAsAction="always"
android:actionLayout="#layout/custom_purchaselist_add_layout"/>
</menu>
searchmenu.xml
<?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/action_search"
android:title="#string/search_title"
android:orderInCategory="2"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
</menu>
i have a pager tab strip in my project and when run on emulator or device it starts center like this
what i would like to achieve is to have the tab start left like this
I have tried layout graviity left in my xml but this is not working for me and i can't put my finger on how to resolve this, here is my xml with the pager tab strip.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/gradient_bg"
android:textColor="#fff"
android:textSize="20sp"
android:id="#+id/tab_strip"
android:focusableInTouchMode="false">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
java code
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends FragmentActivity {
//adview
ViewPager pager;
PagerTabStrip tab_strp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
ma_pager_adapter mapager=new ma_pager_adapter(getSupportFragmentManager());
pager=(ViewPager)findViewById(R.id.pager);
pager.setAdapter(mapager);
tab_strp=(PagerTabStrip)findViewById(R.id.tab_strip);
tab_strp.setTextColor(Color.WHITE);
// tab_strp.setTextSize(14,14);
// tab_strp.setTabIndicatorColor(Color.WHITE);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.firsteps);
mp.start();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
ImageView welcomeText = (ImageView)findViewById(R.id.btsMainTextImage);
//welcomeText.animate().rotation(1440f).setDuration(2000);
}
}
#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.share) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.Locale;
//final MediaPlayer mp = MediaPlayer.create(this, R.raw.short_whoosh2);
public ma_pager_adapter(FragmentManager fm) {
super(fm);
}
//final MediaPlayer mp = MediaPlayer.create(this, R.raw.short_whoosh2);
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
tab1 t1 = new tab1();
return t1;
case 1:
tab2 t2 = new tab2();
return t2;
case 2:
tab4 t4 = new tab4();
return t4;
case 3:
tab3 t3 = new tab3();
return t3;
case 4:
tab5 t5 = new tab5();
return t5;
}
return null;
}
#Override
public int getCount() {
return 5;
}//set the number of tabs
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
case 2:
return "Tab3";
case 3:
return "Tab4";
case 4:
return "Tab5";
}
return null;
}
}
That is not possible with PagerTabStrip and ToolBar. Since ActionBarTabs have been deprecated, use a TabLayout.
Here is the Code that how you can use TabLayout easily.
Here is the output that you will get.
main_activty.xml or any of your XML file where your want to use these Tabs and Viewpager
<android.support.design.widget.AppBarLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" android:id="#+id/appBarLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#f9f8f8"
app:tabSelectedTextColor="#fff"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="left"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</android.support.v4.view.ViewPager>
That XML code should work for you. For the complete code, you can visit here - https://tutorialvilla.com/how/how-to-use-tablayout-and-viewpager-in-android-kotlin
I'm trying to get a sliding drawer set up. I want the text to appear in the main body of the activity, but instead it's appearing inside of the toolbar, and it only says This is fragment 2, regardless of whether I pulled up the fragment that's supposed to say This is fragment 1 or This is fragment 2.
How can I make it pull up the proper fragment, instead of always fragment 2, and how can I make the toolbar opaque, with text appearing under it instead of in it.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
tools:context="me.paxana.alerta.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="8dp"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_content"
android:layout_alignParentTop="true">
</RelativeLayout>
<ListView
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="#+id/lv_sliding_menu"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package me.paxana.alerta;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import com.parse.ParseUser;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.paxana.alerta.adapter.SlidingMenuAdapter;
import me.paxana.alerta.fragment.Fragment1;
import me.paxana.alerta.fragment.Fragment2;
import me.paxana.alerta.fragment.Fragment3;
import me.paxana.alerta.model.ItemSlideMenu;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private List<ItemSlideMenu> listSliding;
private SlidingMenuAdapter adapter;
private ListView listViewSliding;
private DrawerLayout drawerLayout;
private android.support.v7.app.ActionBarDrawerToggle actionBarDrawerToggle;
private Toolbar mToolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null) {
navigateToLogin();
}
else {
Log.i(TAG, currentUser.getUsername());
}
listViewSliding = (ListView)findViewById(R.id.lv_sliding_menu);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
listSliding = new ArrayList<>();
//add item for sliding list
listSliding. add(new ItemSlideMenu(R.drawable.ic_action_settings, "Settings"));
listSliding.add(new ItemSlideMenu(R.drawable.ic_action_about, "About"));
listSliding.add(new ItemSlideMenu(R.drawable.ic_logout_black_48dp, "Log Out"));
adapter = new SlidingMenuAdapter(this, listSliding);
listViewSliding.setAdapter(adapter);
//display icon to open/close slider
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//item selected
listViewSliding.setItemChecked(0, true);
//close menu
drawerLayout.closeDrawer(listViewSliding);
//handle on item click
listViewSliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 2) {
ParseUser.logOut();
navigateToLogin();
} else {
//replace fragment
replaceFragment(position);
//item selected
listViewSliding.setItemChecked(position, true);
//close menu
drawerLayout.closeDrawer(listViewSliding);
}
}
});
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
private void navigateToLogin() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int itemId = item.getItemId();
if (itemId == R.id.action_logout) {
ParseUser.logOut();
navigateToLogin();
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
//create method replace fragment
private void replaceFragment(int pos) {
android.support.v4.app.Fragment fragment = null;
switch (pos) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = new Fragment1();
break;
}
if(null != fragment) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
I solved one part of this issue, though I'm not certain I used best practices. I added padding to the top of main_content in a size of "?attr/actionBarSize"
But it's still only displaying This is Fragment 2, regardless of which fragment I'm trying to select.
If you want the content below the toolbar, you can wrap toolbar and content in one layout as:
<LinearLayout
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="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_content"
android:background="#8000ff00"
>
</RelativeLayout>
</LinearLayout>
Besides, if you are using Android Studio, creating Navigation Drawer Activity through the wizard is the best way to ensure that everything works properly.
I am developing an android application in that,I am using dashboard layout that contains icons(showed in image-1).Dashboard layout is common for all the three tabs,Now my dashboard layout is looking below,
Bydefaultly ,when i am login my app the invitation tab will opened as similar to above image.In that image dashboard layout have 3 icons(i.e "dropdown,event,settings")
But i want to change icon on dashboard based on tab click functionality.
for example,
In image-2 ,"Invitation tab" need to show "settings icon" only on dashboard layout except that icon no other icon need to display when i am in "invitation tab".
Similarly in image-3, when i am in "event tab" i need to show "event & settings" icons in dashboardlayout.
In image-4, when i am in "groupchat tab" i need to show "dropdown & settings" icons in dashboard layout.
My menu_dashboard code is below
<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="ringee.app.com.ringeeapp.UserDashBoard">
<item
android:id="#+id/dropdown"
android:icon="#drawable/dropdown_icon"
android:title="Dropdown"
appmunu:showAsAction="always">
<menu>
<item
android:id="#+id/all"
android:title="All" />
<item
android:id="#+id/event"
android:title="Event" />
<item
android:id="#+id/invitation"
android:title="Invitation" />
</menu>
</item>
My dashboard coding is below,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
My "userdashboard activity" programming code is below,
package com.ringee.app;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.ringee.app.R;
import com.ringee.app.dataobjects.MessageMO;
import com.ringee.app.utility.AppActivityStatus;
import com.ringee.app.utility.Constants;
import java.util.HashSet;
import java.util.Set;
//new header file is added
import android.widget.TabHost.OnTabChangeListener;
public class UserDashBoardActivity extends ActionBarActivity {
/** Called when the activity is first created. */
private static final String TAB_1_TAG = "Invitation";
private static final String TAB_2_TAG = "Event";
private static final String TAB_3_TAG = "GroupChat";
private FragmentTabHost tabHost;
private Context context;
private SharedPreferences sharedpreferences;
private Gson gson = new Gson();
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onPause();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user_dash_board, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
Context.MODE_PRIVATE);
// Get TabHost Refference
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("Invitation"), InvitationFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("Event"), OccasionFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("GroupChat"), GroupChatFragment.class, null);
// Set drawable images to tab
/*tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);*/
//invitation tab highlighted by default
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.Orange));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.scandal));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.scandal));
/* if(getIntent().getExtras() != null && getIntent().getExtras().containsKey("messageMO")) {
MessageMO messageMO = (MessageMO) getIntent().getExtras().get("messageMO");
getIntent().getExtras().remove("messageMO");
Log.i(Constants.TAG, "messageMo object" + messageMO.getMobileNumber());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("messageMo",gson.toJson(messageMO));
editor.commit();
tabHost.setCurrentTab(2);
}*/
/*tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
tabHost.getCurrentTabView().setBackgroundColor(getResources().getColor(R.color.scandal));
}
});*/
//onTabChangedListener added for move one tab to others
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
setTabColor(tabHost);
}
});
}
//setTabColor method added for highlighting the tabs
public void setTabColor(FragmentTabHost tabHost) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(R.color.scandal));//unselected
if(tabHost.getCurrentTab()==0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //1st tab selected
else if(tabHost.getCurrentTab()==1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //2nd tab selected
else if(tabHost.getCurrentTab()==2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //3rd tab selected
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.account_settings) {
Intent userSettingIntent = new Intent(getApplicationContext(),ActivityUserSettings.class);
userSettingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(userSettingIntent);
return true;
}
if (id == R.id.profile) {
Intent profileIntent = new Intent(getApplicationContext(),ImageUploadActivity.class);
profileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(profileIntent);
return true;
}
if(id == R.id.create_occasion){
Intent occasionAct = new Intent(getApplicationContext(), OccasionActivity.class);
// Clears History of Activity
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
return super.onOptionsItemSelected(item);
}
}
How to fix this problem please help me.
Use The ToolBar With Icons in the Activity and when the tab changed Customise your layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
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="wrap_content"
android:background="#color/primary"
android:drawingCacheQuality="low"
android:minHeight="#dimen/abc_action_bar_default_height_material"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#android:color/transparent"
android:gravity="right">
<ImageView
android:id="#+id/notification"
android:layout_width="#dimen/abc_action_button_min_width_material"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_centerVertical="true"
android:gravity="center"
android:lines="1"
android:padding="5dp"
android:src="#drawable/ic_action_notification"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<ImageView
android:id="#+id/searchView"
android:layout_width="#dimen/abc_action_button_min_width_material"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_centerVertical="true"
android:gravity="center"
android:lines="1"
android:padding="5dp"
android:src="#drawable/ic_title_search_default"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
and on your Activity something like this
myTabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (TAB_1_TAG.equals(tabId)) {
notification.setVisibility(View.GONE);
searchView.setVisibility(View.VISIBLE);
}
if (TAB_2_TAG.equals(tabId)) {
notification.setVisibility(View.VISIBLE);
searchView.setVisibility(View.GONE);
}
}
});
HERE İS MY MAİN 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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tag="tabPane" />
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/address_bar"
android:layout_width="270px"
android:layout_height="50px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="http://" />
<Button
android:id="#+id/go_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/address_bar"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/address_bar"
android:text="GO" />
<Button
android:id="#+id/new_Tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/go_Button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/go_Button"
android:text="NewTab" />
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
HERE İS MY MAİN ACTİVİTY FİLE:
package com.example.androidtablayoutactivity;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
TabHost tabHost;
TabSpec photospec;
TabSpec songspec;
Intent songsIntent;
Button go;
Button newTab;
TextView text;
Intent photosIntent ;
private int counter=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_tab_layout);
go=(Button)findViewById(R.id.go_Button);
newTab=(Button)findViewById(R.id.new_Tab);
text=(TextView)findViewById(R.id.address_bar);
tabHost = getTabHost();
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// Tab for Photos
go();
}
});
}
public void go(){
photospec = tabHost.newTabSpec("");
// setting Title and Icon for the Tab
URL url = null;
try {
url = new URL( text.getText().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(counter==0){
photospec.setIndicator(url.getHost().replace("www."," ").replace(".com"," "));
photosIntent = new Intent(Intent.ACTION_VIEW);
photosIntent.setClass(this, PhotosActivity.class);
photosIntent.putExtra("URL1", text.getText().toString());
photospec.setContent(photosIntent);
tabHost.addTab(photospec);
}
else{
tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(0));
photospec.setIndicator(url.getHost().replace("www."," ").replace(".com"," "));
photosIntent = new Intent(Intent.ACTION_VIEW);
photosIntent.putExtra("URL1", text.getText().toString());
photosIntent.setClass(this, PhotosActivity.class);
photospec.setContent(photosIntent);
tabHost.addTab(photospec);
}
counter++;
}
}
HERE İS MY OTHER ACTİVİTY WHİCH MAKİNG BROWSER PROCESS
package com.example.androidtablayoutactivity;
import com.example.androidtablayoutactivity.SongsActivity.web;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
public class PhotosActivity extends Activity{
WebView web;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_layout);
web=(WebView)findViewById(R.id.web);
web.setWebViewClient(new web());
web.getSettings().setJavaScriptEnabled(true);
Bundle extras = getIntent().getExtras();
if(extras==null){
web.loadUrl("");
}
else
{
web.loadUrl(extras.getString("URL1"));
}
}
public class web extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webview,String url){
webview.loadUrl(url);
return true;
}
}
}
HERE İS İTS LAYOUT FİLE
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/web"
android:layout_width="385dp"
android:layout_height="fill_parent" />
</LinearLayout>
İ am doing browser in android with go and new tab button when i click first time go button mybrowser is going to url which i want however when i click one moretimes my address does notchange
I am removing the rest part as its not what your looking for
Edit click here