ActionButton/ActionBar does not showup in TabHost method - android

I'm trying to have a different action bar for each new tab (activity) in my project
I have created my first Activity and put it as a tab in my Main Activity
my first activity (tab) has the action button ( Start ) in the action bar of its activity
somehow nothing show up in the action bar for this tab
and if so I would to put more activities (tabs) no action bar/buttons will be shown
there is no error in my logcat
this is my code :
AndroidTabLayoutActivity.java
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for activity1
TabSpec activity1 = tabHost.newTabSpec("Photos");
activity1.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, OneActivity.class);
activity1.setContent(photosIntent);
tabHost.addTab(activity1); // Adding photos tab
}
}
OneActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class OneActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity1_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_start:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="b3du.im.tabLayout.AndroidTabLayoutActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
photos_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for Activity1 -->
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="im the first one "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
activity1_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Exit -->
<item android:id="#+id/action_start"
android:title="Start"
android:showAsAction="withText|always" />
</menu>

you need to add the Action buttons in the main activity (where the tabs are hosted) NOT in the tab activiy.
And later manage them in the tab activity by using
getParent().getActionBar()
and also..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getParent().getMenuInflater().inflate(R.menu.home_tabs, menu);
//getMenuInflater().inflate(R.menu.home_tabs, menu);
return true;
}

Related

Popup Dialog with Android ActionBar when ActionBar Menu Item is Clicked

I'm building an app where I need an information activity with another Android Actionbar to popup when the info ActionBar Item is clicked on.
Here is the screenshot of the activity_main_menu.xml:
activity_main_menu.xml code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/menu_item_info"
android:icon="#drawable/info_outline"
android:title="#string/information"
android:titleCondensed="#string/info"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_item_share"
android:title="#string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
When the "menu_item_info" is clicked, here is what I want to happen:
Desired Result
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="horizontal"
tools:context=".MainActivity">
<!-- <clip xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clipOrientation="vertical"
android:drawable="#drawable/bitcoin"
android:gravity="top" /> -->
<TextView
android:id="#+id/tvTotal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="Text"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:fontFamily="sans-serif"
app:lineHeight="60dp" />
<ImageView
android:id="#+id/ivBitcoin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="You own text!"
android:padding="10dp"
app:srcCompat="#drawable/bitcoin" />
</LinearLayout>
MainActivity.java code:
package com.shikhar_mainalee.iownallbitcoin;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView tvTotal;
private ImageView ivBitcoin;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTotal = findViewById(R.id.tvTotal);
ivBitcoin = findViewById(R.id.ivBitcoin);
Main.ivBitcoin = ivBitcoin;
tvTotal.setText("You Own\n21,000,000 / 21,000,000\nBTC!");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
shareActionProvider.setShareIntent(createShareIntent());
new ShareActionProvider(this).setShareIntent(null);
// Return true to display menu
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
// What code should go here?
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
What is the best way to go about this? Any help is appreciated! Thank you!
If you want to go to another activity here is what you should do:
Create another activity
Create an Intent to go to that activity
Intent myIntent = new Intent(this, SecondActivity.class);
you can call that Intent from anywhere in your main activity. In your case:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
startActivity(myIntent); // switch to second activity
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
to change the second activity action bar menu you should create a new Menu and like your main activity Inflate it in onCreateOptionsMenu
getMenuInflater().inflate(R.menu.activity_second_menu, menu);
try this:
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
this is a example of a template from android studio.

Title missing with ViewPager

I want something like the swiping pages in the Google Play Store.
activity_main.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"
android:orientation="vertical"
tools:context="com.nielyouri.pluff.MainActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_height="0px"
android:layout_weight="1"
android:layout_width="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
MainActivity.java
package com.nielyouri.pluff;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
//private static final String TAG = MainActivity.class.getSimpleName();
private DayAdapter mDayAdapter;
private ViewPager mViewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDayAdapter = new DayAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
But when i run the app simulated it shows up without the "Pluff" title...
My fragment and adapter
http://pastebin.com/2utCxSZx
http://pastebin.com/eexzKpb6
Tried multiple things found on the internet, changing xml etc doesn't do the trick.
In xml you have to put below Toolbar code for the action bar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In java file:
a. extend class with AppCompatActivity
b. you have to put the below code for set the title:
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
mActionBarToolbar.setTitle("Pluff");
setSupportActionBar(mActionBarToolbar);
In manifest file you have to set theme for the activity, see below:
<activity
android:name=".MainActivityPage"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
>
</activity>

Android Action Bar, adding action buttons

I'm trying to add button to the Action Bar with no luck. There are no errors. Button is in the #drawable. But it doesn't show up.
What am I doing wrong?
(It asks me to add more details, but I have nothing more to add)
test activity :
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
public class test extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
setupCREATEbut();
}
private void setupCREATEbut() {
Button CREATEbut = (Button) findViewById (R.id.appenter2);
CREATEbut.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent,0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topmenu, menu);
return super.onCreateOptionsMenu(menu);
}
}
test 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="rs.test">
<LinearLayout
android:id="#+id/centerblock"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relcenterblock"
android:layout_width="fill_parent"
android:layout_height="415dp" >
<Button
android:id="#+id/appenter2"
android:layout_width="160dip"
android:layout_height="160dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/bigroundbutton"
android:gravity="center"
android:text="#string/request"
android:textColor="#ffffff"
android:textSize="30sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
topmenu xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"/>
</menu>
Try to use showAsAction=always or showAsAction=ifRoom.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"
yourapp:showAsAction="always"/>
</menu>

I cannot figure out whats wrong with the onClickListener function of button here in fragments

**I want to click on a button in my fragment 2 , and replace fragment 1 with fragment 3 as shown in the code.
But the findViewById(r.id.mybutton) is returing 'null' .
I tried debugging the code but its not able to use the created 'buttontoggle'
Hope you can help me out with the above problem
Thanks in advance :)
package com.vivekmishra1991.testfrag;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_container)!=null){
if(savedInstanceState!=null){
return;
}
//fragment 1
f1 F1= new f1();
F1.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, F1).commit();
//fragment2(the fragment with the button)
f2 F2=new f2();
F2.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, F2).addToBackStack(null).commit();
}
//code for buttton onclick function
Button toggleButton = (Button)findViewById(R.id.mybutton);
toggleButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ // fragment 3 that has to be replaced with fragment 1
f3 F3=new f3();
F3.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,F3).addToBackStack(null).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;
}
}
EDIT
Here is my activityMain.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="0dp"
android:layout_weight="6"
android:layout_width="match_parent" />
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
</LinearLayout>
f2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Next!"
android:id="#+id/mybutton"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
You can check your .xml file to make sure that "mybutton" does exist.
If so, try Project > Clean. Occasionally, eclipse doesn't add the button to the R.java file, and cleaning the project tends to fix that. If its not that either, you can always try to recreate the button in your xml file.

Navigate between fragments (ViewFlipper)

I have an activity (android.support.v4.app.FragmentActivity) with a ViewFlipper and within this I have several fragments
src/com.package.WelcomeActivity.java
package com.package;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class WelcomeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
}
res/layout/activity_welcome.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/flip2"
class="com.package.fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<fragment
android:id="#+id/flip2"
class="com.package.fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- ... -->
<fragment
android:id="#+id/flipN"
class="com.package.fragmentN"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</ViewFlipper>
</LinearLayout>
Now each fragment are composed by two mayor parts: content and actions (back, continue) when the user tab on continue action execute a function inside the fragment but i dont know how to call the ViewFlipper.showNext() and ViewFlipper.showPrevious() inside the fragments
Let your Activity do it.
Make public methods in your WelcomeActivity that call ViewFlipper.showNext() and ViewFlipper.showPrevious, something like this:
public void showNextFragment() {
mViewFlipper.showNext();
}
public void showPreviousFragment() {
mViewFlipper.showPrevious();
}
In your fragments, you can then call the Activity's methods, like this:
WelcomeActivity parent = (WelcomeActivity) getActivity();
parent.showNextFragment();
// or parent.showPreviousFragment();
I just typed the code here and didn't try it, there might be typo, so don't just copy-paste. But I hope it's illustrating my point well.

Categories

Resources