opening buttons within the same main activity in android studio - android

I want to be able to open 3 buttons within the same java class in Android studio , a map , button leading to text and pictures and the last one a video ..
i used the following codes :
public void location(View view) {
Button great = (Button) findViewById(R.id.location);
great.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),location`enter code here`.class);
startActivity(intent);
}
});
}
the same for the other 2 buttons . as for the .xml file
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/history"
android:id="#+id/button"
android:onClick="location"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
and the string is
<string name="map">Map</string>
<string name="location">Location</string>
<string name="info">information about related subject
</string>
the thing is how can i open all 3 buttons in the same main layout ? do i use
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),location`enter code here`.class);
}
in all 3 buttons ? or do i have to assign each button to a different java class ? specially when all API setting (import google map ) shows up grey in the main java ?

Related

Android: onClick causing me to go back an Activity

I am trying to build a dynamic UI, but when I add the onClick method to the button whenever I push the button I go back to my previous activity. Any ideas on how to fix it?
my button's code: (the addMenu method is never run in the activities class)
<Button
android:text="New Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newButton"
android:layout_weight="1"
android:onClick="addMenu"/>
here is my addmenu code although no matter what goes in here(even if nothing at all) it still won't work
public void addMenu()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}
whenever I push the button I go back to my previous activity.
Sounds like your app is crashing and restarting... read the logcat, and you'd see something along the lines that your method signature is wrong.
android:onClick="addMenu" needs a method of public void addMenu(View v).
Or just use Java to set the button listener and remove android:onClick.
findViewById(R.id.newButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMenu();
}
}
try this
/**
* #param v android:id="#+id/newButton"
*/
public void addMenu(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}

Linking buttons to pages using android(Eclipse) - can't get it to work

I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
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="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.

Android - In a button On click event, button goes missing

I am trying to change the text color of button on click event. But while the button click event is fired, button goes missing. Code mentioned below.
Button design in Layout XML file
<Button
android:id="#+id/btnCategory1"
android:background="#000000"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
>
</Button>
Activity.java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
btnType1 = (Button)findViewById(R.id.btnCategory1);
btnType1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == (View)btnType1)
{
btnType1.setTextColor(R.color.darkorange);
}
}
Tried the below Option also. Still Button Goes missing. Log statement is fired successfully.
btnType1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v("AAAAAAAAAAA","BBBBBBBBBBB");
// TODO Auto-generated method stub
btnType1.setTextColor(R.color.orange);
}
});
If someone can find the reason, kindly share it.
You cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.
Make your button as below
Button bOne = (Button) findViewById(R.id.btnCategory1);
bOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bOne.setTextColor(getResources().getColor(R.color.YOURCOLOR));
}
});
Hmmmmm. I'm not seeing a good reason as to why that would happen.
I do think that there is maybe a better/cleaner way to do something so simple, and so I'll tell you it - go ahead and try it. This was should work.
Get rid of the btnType1.setOnClickListener(this); line from your java.
Then, go into your xml and add this to your button:
android:onClick="methodName"
Now, if you go into your java and create a method called methodName that takes a view as an argument:
public void methodName(View v) {
btnType1.setTextColor(R.color.darkorgange);
}
The color should be updated!
EDIT: Just looked again and the reason the previous code wasn't working was because you were trying to update btnType2, not btnType1. Still, try the method I just gave you. It is good practie, and a cleaner and easier way to do things for the future.
EDIT2:
All right, the mystery is solved. Here is your issue. When you do set color, you need to pass in the actual color, not just the id. Here is what you need to change that line to:
btnType1.setTextColor(getResources().getColor(R.color.darkorange));

Asdroid SDK Eclipse : Simple App gave Black Screen and Stopped?

First of all I'm totally new with Eclipse so my Code maybe full of errors *
Second I wrote a simple App about two buttons (Add,Sub) with One TextView , the idea is about click one of these buttons and then the TextView changes and gives a number (Add=+1 , Sub = -1) , I'm Sure Compiler gave No Errors
But I don't know why I have a lot of problems :
1-The Apk file is not generated in the bin folder .
2-I tried to Export it using Android tools , and then tried it on my mobile and it gave black Screen then stopped working.
3-I got 3 warnings in The Activity_Main.xml about buttons and textview "Hardcoded String Should use #String Resource "
I'm totally Confused , I don't know how to fix it i tried a lot and searched on the internet but i couldn't
Here is the Main_Activity.Java :
package com.example.counter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int Counter = 0;
Button A = (Button) findViewById(R.id.Add);
Button S = (Button) findViewById(R.id.Sub);
TextView C = (TextView) findViewById(R.id.Calculate);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter ++ ;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter -- ;
C.setText("Your Total Counter Is " + Counter);
}
});
}}
And here is the Activity_main.xml :
<TextView
android:id="#+id/Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Waiting To Calculate"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Add"
android:layout_alignBottom="#+id/Add"
android:layout_alignRight="#+id/Calculate"
android:text="Sub" />
<Button
android:id="#+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Calculate"
android:layout_below="#+id/Calculate"
android:layout_marginTop="66dp"
android:text="Add" />
The app suppose work even on API1 and Targeted API 17 (Jelly Bean) I tested it on Galaxy S3 Mini (have Android 4.2) and it crashed , tried simple app before with same settings and worked
*Any Help is Appreciated , thanks for your time and Sorry for make it Long *
The 2nd problem is on the View (e.g. Button, TextView) initialization.
Since they are declared and initialized at class scope, they are initialized before onCreate() (and thus setContentView()) is called. Because there is no layout inflated at the moment, the initialization will return null. Then, when setOnClickListener() is called, it will throw NullPointerException and crashes the app.
The solution is to move the initialization after setContentView() is called.
public class MainActivity extends Activity {
int Counter = 0;
Button A;
Button S;
TextView C;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A = (Button) findViewById(R.id.Add);
S = (Button) findViewById(R.id.Sub);
C = (TextView) findViewById(R.id.Calculate);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter++;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter--;
C.setText("Your Total Counter Is " + Counter);
}
});
}
}
For the 3rd problem, just as Juwee posted, you can define the String resources to be used in XML layout file (it can be used in the code too!).
Create an XML file in /values folder with <resources> root element (or just use strings.xml if it's already there). Then, you can declare a string resource with String value
Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add">Add</string>
</resources>
Then, on the XML layout, you can refer it as android:text="#string/add" (Or in code as setText(R.string.add))
Since this is just only a warning, you can actually ignore it. But if you decide your app to support localization, or if you use the same String quite often, then it is better to define it as app's resource.
Third problem : About this property android:text="Sub" ,you can define the text in the resource file "string.xml",like <string name='subString'>sub</string>,then make reference to it.android:text="#string/R.string.subString" . Have a try .

how do i switch between sets of images?

Hi I'd like to create 2 sets of images where one set is displayed if the user selects something from a radio button and the other is displayed if the user selects the other radio button,
In truth I'm an amateur at this and it may not even be radio buttons that I want but its one set of images for a girl and another for a boy, any suggestions welcome
this is what I did... its inside an edittext but it shouldnt make any difference :
<EditText
android:id="#+id/exerciseInputFilter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="search..."
android:textColor="#android:color/holo_blue_light" />
<ImageView
android:id="#+id/drillListSearchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/exerciseInputFilter"
android:layout_alignParentRight="true"
android:src="#drawable/search_white" />
in the xml for example... frame is also possible...
and in code...
SearchImage = (ImageView) findViewById(R.id.drillListSearchImage);
SearchImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!filterText.getText().equals(null)) {
filterText.setText("");
SearchImage.setImageResource(R.drawable.search_white);
}
return false;
}
});
so just create 1 image view and then on touch do something...
if you want to use a radio or checkbox its the same principle... on item click... set imageview to whatever you want...
rd2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rd1.setChecked(false);
SearchImage.setImageResource(R.drawable.search_white);
}
});
for example... does this answer your question?
Edit
after reading your comment (which wasnt understandable from your original question)... what you want is just to pass information between activities to do that use this:
Activity A:
create a boolean and a 2 check or radios and
CheckBox maleBox = (CheckBox) findViewById(R.id.Male);
maleBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
isMale = true;
isFemale = false;
femaleBox.setChecked(false);
}
}
});
you can do the same for female if you want several radios or checkboxs... then...you pass the information to activity two... like so:
intent = new Intent(whateverclass.this, whoeverclass.class);
intent.putExtra("Gender", isMale);
startActivity(intent);
now you can read it in activity two with:
if (getIntent().getBooleanExtra("Gender", false)) {
// set Images or buttons or what ever...(like i showed you above)
SetImage1.setImageResource(R.drawable.boy);
.
.
.
}
you can also use bundle to get your variables...
Intent intent = new Intent();
intent = getIntent();
Bundle bundle = intent.getExtras();
then just get the boolean...

Categories

Resources