I am able to read pdf file, but I have one problem
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/sdcard/Test.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PDFTest.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
Am using the above code, when I click Button it takes me to another view where PDF content is visible. I dont want to use two activities. But I want to see the pdf content on same view. How to do that. How to stop navigating to next view.
My XML file is:
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="Button"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
`
Can anyone help me on this context.
You need to use a Library not an Intent.
Try the open source library muPDF
Related
I created an app working with saving a screenshot to the USB storage. But I cannot open that image from USB storage. Please help me to solve this issue. This image I don't want to open and keep it in the ImageView but I want to load with the image viewer or another app like we open from Gallery.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/main"
tools:context=".MainActivity">
<Button
android:id="#+id/btn"
android:text="Take Screen"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#color/colorPrimary"
android:scaleType="fitCenter" />
<Button
android:id="#+id/btnLoad"
android:text="Take Screen"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java
btn_load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// My image name "restaurant.jpg"
// image keep in USB storage\restaurant.jpg
}
});
Glide is an open source library that can able to show an image from any source. You can use this in the following steps.
Add this is build.gradle file:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
And you MainActivity:
//Initialize ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);
btn_load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Loading image from any file location into imageView
String photoPath = Environment.getExternalStorageDirectory()+"/restaurent.jpg"; // need to add storage read permission
Glide.with(MainActivity.this)
.load(new File(photoPath))
.into(imageView);
}
});
Hope this will help.
// Now this is how to open PDF file from storage or external App
// Open from Storage File
public void openFileFromStorage(){
File file = new File("/sdcard/testpdf/test.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
// Open External App. How to find Package name install "System Info" in Play Store.
public void openAppExternal(){
Intent intent = getPackageManager().getLaunchIntentForPackage("com.adobe.reader");
startActivity(intent);
}
// Open External Website
public static Intent openApp(Context context){
try {
context.getPackageManager().getPackageInfo("https://www.geeksforgeeks.org/file-class-in-java/",0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));
}catch (Exception e){
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));
}
}
I'm making a button in xml(res / layout / activity_home.xml), like this:
<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=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.
I would like to write a text in my activity like
Buy my app
I need to make this text clickable. So once one click on it, it opens the Market app and my app shows up.
What is the appropriate view? How can I add the web link?
Would you please help (provide a small code)?
Use this code to achieve your goal.
public void onCreate(Bundle request)
{
super.onCreate(request);
setContentView(R.layout.market);
Button buy = (Button)findViewById(R.id.btnBuyMyApp);
buy.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnBuyMyApp:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=your package name"));
startActivity(intent);
break;
}
}
In xml you can use this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="5dip" >
<Button
android:id="#+id/btnBuyMyApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:padding="6dip"
android:text="Buy"
android:textColor="#F8B334"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
I hope it helps
Write following line of code in onClick() method
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=YourPackageName"));
startActivity(intent);
This will open default AndroidMarket application in android mobile and shows your application
You can simply use a TextView in your layout xml file with the text "But my app".
Find that TextView in the android code using the findViewById(). Then set a onClickListener() on the TextView. In the onClickListener() use the following code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.rovio.angrybirds&hl=en")););
I have just used the angry birds app example, instead of that you can give the link of your own app.
Please use below code.
TextView myWebSite = (TextView)findViewById(R.id.myWebSite)
myWebSite.setText("Link is:- " + "market://details?id=your package name");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);
And please see below link for more information.
Android – Creating links using linkify
Check this:
<TextView
android:id="#+id/click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Market"/>
in Activity:
TextView text=(TextView) findViewById(R.id.click);
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com"));
startActivity(intent);
}
});
Dont forget to include internet permission in manifest file.
is it possible to open an picture, which is stored in the res/drawable folder with the Android build in Image Viewer? I don't get it work. I tried the follwoing way
Button ButtonPhase2 = (Button) findViewById(R.id.button1);
ButtonPhase2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
myIntent.setData(Uri.parse("android.resource://de.test.app/" + R.drawable.testbild));
myIntent.setType("image/png");
startActivityForResult(myIntent, 1);
}
});
But I get the Error:
Unable to start Activity ComponentInfo{com.android.gallery/com.android.camera.ViewImage}: java.lang.nullPointerException
I think it is the wrong path?
ya you can do i by this way:
first you need to declare your layout in xml
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:orientation="horizontal">
<ImageButton android:id="#+id/anim_btn_play"
android:layout_width="155dp" android:layout_height="80dp"
android:layout_marginLeft="260dp" android:layout_marginTop="150dp"
>
</ImageButton>
</LinearLayout>
Java file:
ImageButton tns_imgv_back;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_and_show);
{
tns_imgv_back= (ImageButton)findViewById(R.id.anim_btn_play)
tns_imgv_back.setImageResource(R.drawable.icon);
anim_btn_play11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// you can do any thing over here when you click it will fire
}
});
}}
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.welcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
I want to load contents from another XML file named welcome.xml, but i do get an error welcome cannot be resolved or is not a field
This Page1.java class is next screen of my Android Application.
My Welcome.xml
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
It should be working.
If you don't set the handler, do you see the button in the screen?
Is the file actually named «*W*elcome.xml»? Try to remove the capital letter (rename it to welcome.xml). Then do a clean, rebuild and check if it works now...
Could you paste your complete xml file and log? my first guess is you have a case issue , your layout file is named "Welcome" and you have setContentView to "welcome" . Also dont have same names for layouts and controls , it will get confusing.
firend you are making silly mistake:see this
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(**R.id.Button01**);//use id of button here not layout name
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Is this all what your welcome.xml has?
Your button isn't under a layout. thus, the layout file itself will be throwing out exceptions.
secondly, android:text is not correct. the entry you have made there, should be under android:id
and it shouldn't be:
final Button button = (Button) findViewById(R.id.welcome);
but:
final Button button = (Button) findViewById(R.id.Button01);
The Welcome.xml contains Button with id welcome which is not an layout to setContentView
Views can be List, relative, absolute table etc .. in which you can add a button.
And also check for the Case in filename and specified R.layout.*
Sample xml file with linearlayout and a button. Save it as welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/linearlayoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="#+id/ButtonWelcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button"
>
</Button>
</LinearLayout>
in Your Code
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.ButtonWelcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
**//You have called Page1.class again which is the name of this class //again**
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Create another activity similarly and the call that class in the intent marked bold.
Your Welcome.xml is not complete, should be something like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="3dip"
android:orientation="vertical">
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
</LinearLayout>
Also, if your are still having problemas, try to clean your project so the R.java get updated with new id values like welcome id (R.id.welcome),because if R.java does not contain welcome id, you will get errors like that.