1

I'm beginner in android development.. I don't know where is the problem..

The activity does not show custom actionbar... i tried it by watching a tutorial.. pls give some solution.. thanks in advance..

actoin_bar.xml

`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:background="#009688" >

    <TextView
        android:id="@+id/title_text_small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ic_action_name"
        android:layout_margin="8dp"
        android:layout_toLeftOf="@+id/refresh"
        android:layout_toStartOf="@+id/refresh"
        android:contentDescription="@string/tabb" />
    <ImageView
        android:id="@+id/backward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_margin="8dp"
        android:src="@drawable/ic_action_backward"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/forward"
        android:layout_toStartOf="@+id/forward"
         />

    <ImageView
        android:id="@+id/refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ic_action_reload"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp" />

</RelativeLayout>

`

subtitle.java

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Subtitle extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subtitle);



        ActionBar mActionBar;
        mActionBar = getSupportActionBar();
        if (mActionBar != null) {

            getSupportActionBar().setCustomView(R.layout.action_bar);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        }


        TextView mTitleTextView = null;
        if (mActionBar != null) {

            mTitleTextView = (TextView) mActionBar.getCustomView().findViewById(R.id.title_text_small);
        }
        if (mTitleTextView != null) {
            mTitleTextView.setText(R.string.title);
        }

        ImageView reloadButton = null;
        if (mActionBar != null) {
            reloadButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.refresh);
        }
        if (reloadButton != null) {
            reloadButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    mWebView.reload();
                    Toast.makeText(getApplicationContext(), "Refresh",
                            Toast.LENGTH_LONG).show();
                }
            });
        }


        ImageView backButton = null;
        if (mActionBar != null) {
            backButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.backward);
        }
        if (backButton != null) {
            backButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    }
                }
            });
        }


        ImageView forButton = null;
        if (mActionBar != null) {
            forButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.forward);
        }
        if (forButton != null) {
            forButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (mWebView.canGoForward()) {
                        mWebView.goForward();
                    }
                }
            });
        }



    }

        @Override
        public void onBackPressed () {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                super.onBackPressed();
            }
        }


    }

1 Answers1

0

First create an inflater and custom view like below:

LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.your_custom_actionbar_xml, null);

TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text_small);
mTitleTextView.setText("Bla bla bla);

ImageView img = (ImageView) mCustomView.findViewById(R.id.forward);
//.... more view

Then set custom view for action bar:

mActionBar.setCustomView(mCustomView);
xxx
  • 3,315
  • 5
  • 21
  • 40
  • @sajolhossain what is the theme you are using? – xxx Aug 03 '16 at 06:08
  • 1
    Theme.AppCompat.DayNight.NoActionBar – sajol hossain Aug 03 '16 at 15:05
  • just change it to other theme and it will work well. Keep learning guy! :) – xxx Aug 03 '16 at 15:22
  • 1
    but how to change action bar size and which theme will be best for custom bar?? – sajol hossain Aug 03 '16 at 15:29
  • Sure you can change the size: http://stackoverflow.com/questions/17439683/how-to-change-action-bar-size But now i recommend you to use Toolbar instead of ActionBar, it's better than actionBar,http://stackoverflow.com/questions/29813474/should-we-replace-action-bar-by-toolbar – xxx Aug 03 '16 at 15:33