Hello i am making a simple push button in android and this is my mainactivity file
package dk.troll.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.troll.dk"));
startActivity(browserIntent);
}
});
}
}
and this is my xml file
<?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="Lorem Ipsum and so much other stuff"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go To Troll" />
</LinearLayout>
On this line button = (Button) findViewById(R.id.button1); i get the error
cannot find symbol:variable id location:class R
Since the contents of class
r
are auto-generated,how can i fix the error?
fist restart eclipse
then doing this :
1.import R class to your activity, Import dk.troll.android.R;
2.be sure the all recurse file have not any error then try to clean project from Project then Clean.
3.build your project
Related
im trying to make a button play a sound for a language learning app.. so when the user clicks the button it will say something like "How are you"
anyway, im getting errors "cannot resolve method" on (Tab1Fragment.this, R.raw.howareyou); , setContentView and findViewById
here is my code..
Tab1Fragment.java
package com.storey.user.storeytom.fragments;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import com.demo.slidingmenu_tabhostviewpager.R;
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1fragment);
final MediaPlayer buttonSound = MediaPlayer.create(Tab1Fragment.this, R.raw.howareyou);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent("com.storey.user.storeytom"));}
});
and here is my .xml file if you need that also...
tab1fragment.xml
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Conversation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
sorry for the "nooby" question... thanks in advance
final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound));
Button play_button = (Button)this.findViewById(R.id.button);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
OR Check this :android - how to make a button click play a sound file every time it been pressed?
I want to add a button to one of my activities (that displays a news article), so when the user clicks the button the article is opened in their browser. So far I have added the button in my xml and it appears. I'm just having some trouble with the click listener. Below is my code, i'm getting an error for 'setOnClickListener' which is 'Non-static method 'setOnClickListener(android.view.View.onClickListener)' cannot be referenced from a static content.
I'm not sure what this means! maybe i'm not calling the method in the right place or there is just an error with the method itself?
Please could someone take a look, thanks!
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import org.json.JSONException;
import org.json.JSONObject;
public class NewsItemActivity extends AppCompatActivity {
//Declare java object for the UI elements
private TextView itemTitle;
private TextView itemDate;
private TextView itemContent;
private NetworkImageView itemImage;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_item);
itemTitle = (TextView) findViewById(R.id.itemTitle);
itemDate = (TextView) findViewById(R.id.itemDate);
itemContent = (TextView) findViewById(R.id.itemContent);
itemImage = (NetworkImageView) findViewById(R.id.itemImage);
EDANewsApp app = EDANewsApp.getInstance();
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String url = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id="+itemId;
JsonObjectRequest request = new JsonObjectRequest(url, listener, errorListener);
app.requestQueue.add(request);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
});
};
activity_news_item.xml
<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=".NewsItemActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/itemTitle"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#3183b9"/>
<TextView
android:id="#+id/itemDate"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:textSize="16sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/itemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/itemContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:textSize="16sp"
/>
<Button
android:id="#+id/BrowserButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View in browser"
android:layout_marginLeft="15dp"
style="#style/BrowserButton"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Change
Button.setOnClickListener(new View.OnClickListener() { ... });
to
Button button = (Button) findViewById(R.id.BrowserButton);
button.setOnClickListener(new View.OnClickListener() { ... });
You need to call the setOnClickListener method on an instance of Button, not on the class itself.
The problem is this :
Button.setOnClickListener(new View.OnClickListener() {
You have to declare it out of onCreate() as you did with your Textviews as :
Button btn;
Why?
Because if you put it on onCreate() you wont be able to use this object out of onCreate() so it's recomendable to put it as a public object.
Then in your onCreate() do this :
btn = (Button)findViewById(R.id.BrowserButton);
Then you do the OnclickListener() as follows :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
});
I'm still getting an error for onClickListener, saying it 'cannot resolve symbol'.
Make sure that you've imported this :
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
You can do it doing this :
Add implements OnClickListener { as follows :
public class NewsItemActivity extends AppCompatActivity implements View.OnClickListener {
Then on your Button you do this :
btn = (Button)findViewById(R.id.BrowserButton);
btn.setOnClickListener(this);
Then add a switch case as follows :
public void onClick(View v) {
switch(v.getId()) {
case R.id.BrowserButton:
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
break;
}
}
I created a button to take me to another page and in that other page, there is another button that takes to another page, so page1, page2 and page3 that it's not working with me, check http://oi62.tinypic.com/2m7g4et.jpg when I click on "Fish guide" it takes me to the other one which has another button "Fish", when I click on "Fish" it doesn't take me to the 3rd page!
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fishguide"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish guide"
android:textSize="20sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fishguide.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3"
android:textStyle="italic" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fish"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish"
android:textSize="22sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fish.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1d72c3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/bala" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Balabanka"
android:textSize="18sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fishguide);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
}
}
I did the third page, it runs but it was .. wrong
MainActivity2.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity2 extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
fishlink = (Button) findViewById(R.id.fish);
fishlink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageFish.class);
startActivity(intent);
}
});
}
}
PageFish.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageFish extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fish);
}
}
Help :)
Edit / it goes "Fish guide" to "Fish" when I click on "Fish" it takes me again to "Fish" and so on, it doesn't go to the third page!
PageThree.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
You have used
setContentView(R.layout.fishguide);
in PageThree.java
Change it to the xml you want to show with that third image in image. say, third.xml
setContentView(R.layout.third);
And you are done.
NOTE: You are getting the same Layout again and again because in PageThree.java you are starting the activity with intent PageThree.java. Instead just change that to PageFish.java.
So when you are at second layout (PageThree.java), just start activity for third layout (PageFish.java) on button click as follows -
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(PageThree.this, PageFish.class);
startActivity(intent);
}
});
}
}
I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.xml:
<?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" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.
Simple code which is copied from web doesnt seem to be working. There is no action when i click button but text of it is correctly changed. What's wrong?
package android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button closeButton = (Button)this.findViewById(R.id.button1);
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG);
}
});
closeButton.setText("dupa");
}
}
main.xml
<?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:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="wrap_content" android:text="#string/button"></Button>
</LinearLayout>
Thanks for any help,
Chris
You need to call .show() on your Toast.
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG).show();
}