I want to share an image in my application.
This is my layout file
activity_xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.example.dc.google.MainActivity">
<Button
android:id="#+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share on Google+"
/>
</RelativeLayout>
This is my java class file
MainActivity.java
package com.example.dc.google;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View v) {
// Launch the Google+ share dialog with attribution to your app.
Intent shareIntent = shareButton.setText("Welcome to Google+")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent,0);
}
});
}
}
Here the .setContentUrl shows an error saying the method can not be resolved.
I can't find the solution.
How can I resolve this error?
Need to read the documentation again...
You shouldn't setText on your button... You setText on a new PlusShare.Builder
Worry about sharing an image once you get code that will compile. (Hint: that documentation also shows sharing images).
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Launch the Google+ share dialog with attribution to your app.
Intent shareIntent = new PlusShare.Builder(MainActivity.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
});
Related
It Shows no error on Android Studio but app even not starting on phone.Is there any problem with the code.I feel like there may be any mistakes in syntax but i am not able to find one.Please help me.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bDriver,bCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bDriver=(Button)findViewById(R.id.driver);
Button bCustomer=(Button)findViewById(R.id.customer);
bDriver.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new
Intent(MainActivity.this,DriverLoginActivity.class);
startActivity(intent);
finish();
}
});
bCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent ryt=new
Intent(MainActivity.this,CustomerLoginActivity.class);
startActivity(ryt);
finish();
}
});
}
}
And activity_main.xml is this and please explain if there is any problem related to SDK version also upload the detail.
<?xml version="1.0" encoding="utf-8"?>
<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="com.trillcore.uber.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/driver"
android:layout_below="#+id/customer"
android:text="I am a Driver"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Customer"
android:layout_above="#+id/driver"
android:id="#+id/customer"/>
</RelativeLayout>
Below code works fine, but it opens a new layout and shows the toast message. but i wanted it to show toast & popup menus on the current layout. I'm working on popup menu project, i want to show popup menu on same layout. Thanks in Advance.
MainMactivity.java
package com.example.twoact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openpopactivity = new Intent(MainActivity.this,
MainActivity2.class);
startActivity(openpopactivity);
}
});
}
}
MainActivity2.java
package com.example.twoact;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity2 extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "ya am in",
Toast.LENGTH_SHORT).show();
}
}, 1000);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.twoact.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="142dp"
android:text="Button" />
</RelativeLayout>
You can use Popup menu and Popup window. in this you can use your custom layout so you can get your view as window in same activity.
Popup window will be good option because it is since API LEVEL 1
please refer this docs and example 1,example 2
And Popup Menu is since API LEVEL 11. so you have to manage for lower version
docs for Popup Menu and example
popup window
this my xml file main
<?xml version="1.0" encoding="utf-8"?>
<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:background="#raw/christmas"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/by_kostas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="83dp"
android:text="#string/by_kostas"
android:textSize="20sp"
android:textColor="#F2F3F4"/>
<ImageView
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nameOfMethod"
android:src="#raw/settings" />
</RelativeLayout>
and my java Main
package com.kostas.mytorch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//start our layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);{
final ImageView diskView = (ImageView) findViewById(R.id.settings);
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//my codes
}
});
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent("com.kostas.standroid.settings"));
}
});
}
}
my problem is when i click in the settings icon, my program crashes instead of what i wanted to create a new layout (settings xml is just black page),can someone be kind enough to help me out
You can attach click listener to any view by two ways:
In xml, write onClick attribute of the view and pass the method
name you have implemented in the activity.
Example: android:onClick="someMethod" and in the activity code, declare a method
public void someMethod(View view)
{
// handle click here
}
In activity, use setOnClickListener() to the view.
For now, try this:
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent(Main.this, settings.class));
}
});
Also remove this line from xml: android:onClick="nameOfMethod"
Some time there is contextual problem. Try this.
Intent intent = new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(intent);
Also don't forget to define class in Manifest file
try to remove
android:onClick="nameOfMethod"
from the xml layout. I believe this is the method that it's called when you click the settings button and it doesn't exist so it crashes.
users have an activity with an edittext and a button.
in the edittext they have to insert their login ID. and press go to display a other activity with webview loading the right site.
an example. this is the link i want users to visit . www.example.com/time/id=1/type=student
if sombody enters a 2 in the edittext it wil open the times for student id 2 and so on.
this is what i have.
LOGIN
package com.beter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class LoginActivity extends Activity {
Button go_btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
go_btn = (Button) findViewById(R.id.go_btn);
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
and the layout
<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" >
<EditText
android:id="#+id/idfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/id"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/idfield"
android:text="#string/login" />
The Webview Activity
package com.beter;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://roosters.gepro-osi.nl/roosters/rooster.php?wijzigingen=1&leerling=116277&type=Leerlingrooster&afdeling=12-13_OVERIG&school=905");
}
}
and again the layout
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How do i make the user input. display the good webview. ?
First of all get a reference to your edittext in your Login Activity as
go_btn = (Button) findViewById(R.id.go_btn);
ed_txt = (EditText)findViewById(R.id.youredittextid);
The change your onClick to
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText());
LoginActivity.this.startActivity(myIntent);
}
Then retrive this id in your WebActivity onCreate as
String id = getIntent().getExtras().getString("id");
Then your url will be
String myURL = "www.example.com/time/id="+id+"/type=student";
Okay so I'm working on a project and I'm trying to use buttons with text inside of them in an activity, but eclipse can't find the id or chooser.xml
Here's myActivity class
package com.xxxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.*;
public class MyActivity extends Activity {`
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//chooser is underlined (chooser cannot be resolved or is not a field)
setContentView(R.layout.chooser);
//all ids are underlined(id cannot be resolved or is not a field)
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity2.class);
startActivity(myIntent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activty3.class);
startActivity(myIntent);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity4.class);
startActivity(myIntent);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity5.class);
startActivity(myIntent);
}
});
}
}
Here's the chooser.xml file
<?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">
<Button
android:id="#+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_1" />
<Button
android:text="#string/button_2"
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_3"
/>
<Button
android:id="#+id/button_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_4"
/>
</LinearLayout>
And here's the string resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test App</string>
<string name="test_image">test.png</string>
<string name="button_1">button_1</string>
<string name="button_2">button_2</string>
<string name="button_3">button_3</string>
<string name="button_4">button_4</string>
</resources>
Your gen files most likely aren't generated yet. Try building it, it should make the lines go away.
1) You need to import your R class. import <android package>.R Ref: R.id cannot be resolved AND R cannot be resolved - Android error
2) Aren't your R.id.button1 supposed to be R.id.button_1 and so forth for other buttons?
Is chooser.xml in the correct folder (the default layout folder provided with the project)? Have you perhaps tried to go to Project and choose Clean? Everything looks okay in your code.
I know this is a very old post, though I still stumbled upon it in my search for a similar problem. When I built the android project, it automatically added:
import android.R;
When I used the advice above,
import <android package>.R;
and I still used R.id....., it still propagated an error. I removed
import android.R;
and no errors.