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
}
});
}}
Related
This question may have been asked before but I seem to not make much progress.
Basically I want to add an onclicklistener to my HorizontalScrollView that expands the image when pressed.
Here's the current code in the XML file:
<HorizontalScrollView
android:id="#+id/firstscrollview"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="left"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<LinearLayout
android:id="#+id/firstlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/Cercie_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="#drawable/image"/>
I've been trying out different things but I can't seem to get it to work, if anyone knows or can find a good example it would be very much appreciated!
Try this .
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ImageView Cercie_button = findViewById(R.id.Cercie_button);
Cercie_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something here
}
});
}
if i am not getting it wrong then what so difficult in it you have to just do this in your on create method
ImageView btn = findViewById(R.id.Cercie_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//write what you want to do
}
});
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 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
i'm getting this error when i try to run my app in the emulator (any android version): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test/com.android.test.HomeScreen}: java.lang.NullPointerException
my question is: is this because in 2.2 there is no "golf button" in the layout, and in 3.0 there is?
this is my activity:
public class HomeScreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
Button basketball = (Button)findViewById(R.id.basketball);
basketball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent basketball = new Intent(HomeScreen.this, basketball.class);
startActivity(basketball);
}
});
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent golf = new Intent(HomeScreen.this, golf.class);
startActivity(golf);
}
});
}
}
i got 2 layouts
layout 1: layout-normal-mdpi <= running in 2.2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/basketbal"
android:layout_marginTop="46dp"
android:layout_marginRight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</Button>
</RelativeLayout>
layout 2: xlarge-port <= running in 3.0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:background="#drawable/basketbal"
android:layout_height="225dp"
android:layout_width="225dp"
android:layout_marginRight="70dp"
android:layout_marginTop="90dp"
android:layout_alignParentRight="true">
</Button>
<Button
android:id="#+id/golf"
android:background="#drawable/golf"
android:layout_height="550dp"
android:layout_width="211dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="390dp"
android:layout_alignParentLeft="true">
</Button>
</RelativeLayout>
if you are getting error in the below line,
Button golf = (Button)findViewById(R.id.golf);
which is not actually present in the first layout you have provided here, it is no wonder. This definitely doesn't exist and which obviously returns null to you and there you go, getting null pointer exception.
To avoid this either you have to catch the exception else you will have change the layout accordingly such that it contains both the buttons in both the layouts.
You're lacking golf button in your first layout, as you stated.
So, when you run
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
golf is set to null (first line) and then you're trying to assign it something...
Try this:
Button golf = (Button)findViewById(R.id.golf);
if (golf != null) {
golf.setOnClickListener(new View.OnClickListener() {
EDITED:
Put these lines in your 2.2 layout:
<Button
android:id="#+id/golf"
android:visibility="invisible"
android:layout_height="1dp"
android:layout_width="1dp"
android:layout_alignParentLeft="true">
</Button>
With attribute visibility="invisible" your app should work and your layout shouldn't be wasted
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.