I am new to android ,i am trying to create a view pager from a grid view but i have a problem while clicking on the item in the grid view.
here is my code
ImageAdapter.java
import com.netvariant.qareeboon.android.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // If it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
ImagePagerAdapter .java
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImagePagerAdapter extends PagerAdapter {
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
// References to our images in res > drawable
public Integer[] mThumbIds = {
R.drawable.bg_logo, R.drawable.three,
R.drawable.four, R.drawable.two,
R.drawable.one, R.drawable.btn_about,
};
}
ImageViewPager .java
import java.util.ArrayList;
import java.util.List;
import com.netvariant.qareeboon.android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
public class ImageViewPager extends Activity {
// Declare Variable
int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}
the activity
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.view.View;
public class InfoGraphicActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);
// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
// ViewPager viewPager =(ViewPager)findViewById(R.id.pager);
gridview.setAdapter(new ImageAdapter(this));
// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);
// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);
// Start ImageViewPager
startActivity(i);
}
});
}
// Not using options menu for this tutorial
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.info_graphic, menu);
return true;
}
}
you can see error log below:
08-21 11:03:38.289: E/AndroidRuntime(5196): FATAL EXCEPTION: main
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
08-21 11:03:38.289: E/AndroidRuntime(5196): at com.netvariant.android.qareeboon.InfoGraphicActivity$1.onItemClick(InfoGraphicActivity.java:40)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netvariant.qareeboon.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.netvariant.android.qareeboon.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MainMenu"
android:label="#string/title_activity_main_menu"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.EducateActivity"
android:label="#string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ConsultActivity"
android:label="#string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ContactUsActivity"
android:label="#string/title_activity_contact_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.InfoGraphicActivity"
android:label="#string/title_activity_info_graphic"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.qareeboon.android.SettingsActivity"
android:label="#string/title_activity_settings"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.SettingActivity"
android:label="#string/title_activity_setting"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MyConsultation"
android:label="#string/title_activity_my_consultation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AboutUs"
android:label="#string/title_activity_about_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ArticleActivity"
android:label="#string/title_activity_article"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.Comments"
android:label="#string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AddComment"
android:label="#string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".ImageViewPager" >
</activity>
</application>
</manifest>
Have a look at this in logcat:
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
You have not mentioned that activity in the AndroidManifest file of your application. Please do so.
<activity
android:name="com.netvariant.android.qareeboon.ImageViewPager">
</activity>
Declare your Activity in Manifest file please
You haven't defined the new Activity in your manifest. Which ever Activity you are launching, its name needs to be added between your Application tags like this:
<activity
android:name=".Activity_Class_Name" />
Related
I have been trying to go from an empty activity to a tab layout but the app keeps crashing once button is clicked. Without the empty activity the code worked fine.
Main activity (empty)
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void statsButton(View view){
Intent intent = new Intent(this, stats.class);
startActivity(intent);
}
}
Stats.java (where I want tabbed view to start)
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class stats extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tab1,tab2,tab3;
public PageAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tab1 = (TabItem) findViewById(R.id.Tab1);
tab2 = (TabItem) findViewById(R.id.Tab2);
tab3 = (TabItem) findViewById(R.id.Tab3);
viewPager = findViewById(R.id.viewpager);
pagerAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0 ) {
pagerAdapter.notifyDataSetChanged();
}
else if (tab.getPosition() == 1){
pagerAdapter.notifyDataSetChanged();
}
else if (tab.getPosition() == 2){
pagerAdapter.notifyDataSetChanged();
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
for some reason these errors are given in crash log once the stats button is clicked and I don't know what the problem is
crashlog
my manifest looks like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emptytab">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
just add this to your AndroidManifest.xml
<activity android:name=".stats"> </activity>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emptytab">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".stats" >
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Note :
So as not to be repeated this problem if you want to add new activity please follow these steps :-
right click > new > Activity > Empty Activity
I hope it will help you .
<activity android:name="stats" />
Add this line in your AndroidManifest.xml
I am loading single jpg image from drawable folder set file provider permission in manifest but when i share image on whatsapp i got error sharing failed please try again.When i share text it works fine but when i tried to share image it gives me error.Here is following files
Fullscreenadapter.java
package com.mobdev.birthdaycakesquotes;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ShareCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Fullscreenadapter extends PagerAdapter {
private Integer[] Image;
private int _resource;
private Activity _activity;
private LayoutInflater inflater;
private String images;
TextView t1,t2;
public Fullscreenadapter(Activity activity,
Integer[] image) {
this._activity = activity;
this.Image=image;
}
#Override
public int getCount() {
return this.Image.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
final ImageView imgDisplay,shareimage;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.fullscreenlayoutdesign,
container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.show);
shareimage=(ImageView)viewLayout.findViewById(R.id.share);
t1=(TextView)viewLayout.findViewById(R.id.currentposition);
t2=(TextView)viewLayout.findViewById(R.id.totalimage);
imgDisplay.setImageResource(Image[position]);
t1.setText(String.valueOf(position));
t2.setText(String.valueOf(Image.length));
images=createImageOnSDCard(R.drawable.sw);
shareimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri path = FileProvider.getUriForFile(_activity,
"com.mobdev.birthdaycakesquotes",new File(images));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is one image I'm
sharing.");
shareIntent.putExtra(Intent.EXTRA_STREAM, path);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
_activity.startActivity(Intent.createChooser(shareIntent,
"Share..."));
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
private String createImageOnSDCard(int resID) {
Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(), resID);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + resID + ".jpg";
File file = new File(path);
try {
OutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return file.getPath();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
AndroidManifestfile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobdev.birthdaycakesquotes">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomePage"
android:label="#string/title_activity_home_page"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".Details_activity"></activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mobdev.birthdaycakesquotes"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
</application>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
private void shareImage(Uri imagePath) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
//sharingIntent.setPackage("com.whatsapp"); for whatsapp only
startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));// for all generic options
}
Finaly in your manifest Add this without fail
<activity
android:name=".YourActivity"
android:icon="#drawable/share_this"
android:label="#string/shared_activity" >
<intent-filter>
<action android:name="android.intent.action.SEND" /> <!-- Send
action required to display activity in share list -->
<category android:name="android.intent.category.DEFAULT" /> <!--
Make activity default to launch -->
<!-- Mime type i.e. what can be shared with this activity only image and text -->
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
This will owrk
ArrayList<Uri> uriArrayList = new ArrayList<>();
uriArrayList.add(getUriFromFile(your file path))
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, myFilesUriList);
startActivity(intent);
public Uri getUriFromFile(File theSrcPath) {
Uri requirdUri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
requirdUri = FileProvider.getUriForFile(theCtx,
theCtx.getApplicationContext().getPackageName() + PROVIDER_FILE_EXTENSION,
theSrcPath);
} else {
requirdUri = Uri.fromFile(theSrcPath);
}
}
While several people have asked questions about casting fragments to activities (and how that doesn't work), I couldn't find a fix that worked for me. I'm going through this tutorial, and have a main activity that contains two fragments. I'm probably missing something fairly obvious:
There error I get: Cannot cast from Fragment to FragmentListView (line18)
MainActivity:
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends Activity implements FragmentListView.OnSiteSelectedListener{
Fragmentwebview web;
FragmentListView list;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
list = (FragmentListView) manager.findFragmentById(R.id.fragment1);
list.setRefrence(this);
}
#Override
public void onSiteSelected(int i) {
// TODO Auto-generated method stub
web = (Fragmentwebview) manager.findFragmentById(R.id.fragment2);
// Check for landscape mode
if (web!= null && web.isVisible())
{
web.setNewPage(i);
}
else
{
Intent intent = new Intent(this , FragmentSupport.class);
intent.putExtra("index", i);
startActivity(intent);
}
}
}
Manifest:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.numeraid.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.numeraid.FragmentSupport"
android:label="#string/title_activity_fragment_support" >
</activity>
<activity
android:name="com.example.numeraid.FragmentListView"
android:label="#string/title_activity_fragment_list_view" >
</activity>
<activity
android:name="com.example.numeraid.Fragmentwebview"
android:label="#string/title_activity_fragmentwebview" >
</activity>
</application>
</manifest>
The first fragment that's causing the error
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:name="com.example.NumerAid.list"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
EDIT: Adding FragmentListView class
package com.example.numeraid;
import android.R.string;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentListView extends Fragment implements AdapterView.OnItemClickListener
{
ListView list;
String [] websites = {"Google","Facebook","Twitter","Xda-developer"};
OnSiteSelectedListener SiteListener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlistview, container, false);
list = (ListView) v.findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<string>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1));
list.setOnItemClickListener(this);
return v;
}
#Override
public void onItemClick(AdapterView adapterView, View view, int position, long l)
{
SiteListener.onSiteSelected(position);
}
// Container Activity must implement this interface
public interface OnSiteSelectedListener {
public void onSiteSelected(int i);
}
public void setRefrence(OnSiteSelectedListener siteListener)
{
this.SiteListener = siteListener;
}
}
Thanks to #corsair for this answer, originally posted as a comment:
My guess is that your FragmentListView class extends android.support.v4.app.Fragment
instead of android.app.Fragment. If you want to use the support version, then you should
use getSupportFragmentManager() instead of getFragmentManager(). Otherwise, you need to
extend android.app.Fragment everywhere as done in the tutorial you are following.
Shifting between activities isn't working.
What the app does is it opens the android.xml file then after 5 secs open the main file
Here's the code:
android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.android"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANDROID" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android.java file:
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class android extends Activity{
MediaPlayer sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
sound1 = MediaPlayer.create(android.this , R.raw.android);
sound1.start();
Thread timer = new Thread()
{public void run()
{
try{
sleep(5000);
}
catch(InterruptedException ac){
ac.printStackTrace();
}
finally{
Intent openactivity = new Intent ("android.intent.action.MAIN");
startActivity(openactivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sound1.release();
finish();
}
}
Mainactivity.java:
package com.example.android;
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.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);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).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;
}
#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;
}
}
}
android.xml file:(the first file that launches up)
<?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:orientation="vertical"
android:background = "#drawable/android"
>
</LinearLayout>
Try this way
Intent openactivity = new Intent(android.this,MainActivity.class);
startActivity(openactivity);
And for more information go to starting-new-activity
First, always Capitalise 1st letter of class names (code convention),
and then write your intents like below:
Intent intent = new Intent(android.this, MainActivity.class);
startActivity(intent );
You can check Starting Another Activity in Android Developers website.
i am creating an application which i want share images to any social platform.
i used a grid view successfully, which shows all the images and can be viewed in specific full image activity to show the user selected image.
i want to add a social share function to the applications which get the image location and share to the social platform.
Here is my MainActivity.java
package com.jai.desimeme;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
#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;
}
}
with my activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
i used the FullImageActivity.java for the preview of image selected //here i want to add the sharing link.
package com.jai.desimeme;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MenuItem;
import android.widget.ImageView;
#SuppressLint("SdCardPath")
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (item.getItemId()) {
case R.id.item:
Uri uri = Uri.fromFile(dest);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
for above code i used this full_image.xml activity.
<?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:orientation="vertical" >
<ImageView
android:id="#+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView android:id="#+id/full_image_view"
android:layout_width="100dp" android:layout_height="100dp"
android:src="#drawable/image_border"
style="#style/myImageView" />
</LinearLayout>
in here i created ImageAdapter.java to store the image files.
package com.jai.desimeme;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.rage_0001,
...
R.drawable.rage_178,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
and here is my AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jai.desimeme"
android:versionCode="1"
android:versionName="1.1.3" android:installLocation="auto">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">
<activity
android:name="com.jai.desimeme.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<!-- FullImageActivity -->
<activity android:name=".FullImageActivity"></activity>
<activity android:name=".About" android:theme="#android:style/Theme.Dialog"> </activity>
</application>
guide me how to add sharing link to this application, i tried so many ways but end up in errors.
You can use this on any click event of your UI element.
BitmapDrawable bm = (BitmapDrawable) yourImageView.getDrawable();
Bitmap mysharebmp = bm.getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mysharebmp, "MyImage", null);
Uri uri = Uri.parse(path);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent,
"Share image using"));