Skip to main content

Set Actionbar In Your Android Application

1] Create Code In MainActivity.java File:
   
package com.example.actionbardemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity {
    private ShareActionProvider actionprovider;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @SuppressLint("NewApi")
    @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);
        MenuItem mnuitem=menu.findItem(R.id.ShareItem);
        actionprovider=(ShareActionProvider)mnuitem.getActionProvider();
        setShareIntent(createShareIntent()); 
        return true;
    }
    public void setShareIntent(Intent shareintent)
    {
        if(actionprovider!=null)
        {
            actionprovider.setShareIntent(shareintent);
        }
    }
    private Intent createShareIntent() { 
        Intent shareIntent = new Intent(Intent.ACTION_SEND); 
        shareIntent.setType("text/plain"); 
        shareIntent.putExtra(Intent.EXTRA_TEXT, 
                  "http://www.codekyahoga.blogspot.com"); 
        return shareIntent; 
   } 
}

2] Write Code In main.xml File:

     <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Output:                                                    
                                             

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"