So I just started using the Android Development kit and learned how to create buttons and get them to work. To test what I know I made a simple app that displays "correct" if you click the button on the right and "idiot" if you click the one on the left. Everything is working other than the fact that it's displaying the opposite message for the button clicked.
Here is my xml code:
<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=".MainActivity" >
<TextView
android:id="#+id/textv1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:text="Click the Right Button" />
<Button
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textv1"
android:layout_marginTop="29dp"
android:layout_toLeftOf="#+id/textv1" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left"
android:layout_alignBottom="#+id/left"
android:layout_toRightOf="#+id/textv1" />
Here is my Java code:
package com.clicktherightbutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button rightb;
Button leftb;
TextView Display;
String yes = "Correct!!!";
String no = "Idiot!!!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rightb=(Button)findViewById(R.id.right);
leftb=(Button)findViewById(R.id.left);
Display=(TextView)findViewById(R.id.textv1);
rightb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(rightb.isPressed())
Display.setText(yes);
rightb.setEnabled(false);
leftb.setEnabled(false);
}
});
leftb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(leftb.isPressed())
Display.setText(no);
rightb.setEnabled(false);
leftb.setEnabled(false);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I understand if I switch "left" and "right" the buttons will do as I want but I want to know why it's doing the opposite of what I coded it to do? Is something wrong with my code?
Thank you
Change your layout to:
<TextView
android:id="#+id/textv1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:text="Click the Right Button" />
<Button
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textv1"
android:layout_marginTop="29dp"
android:layout_toLeftOf="#id/textv1" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/left"
android:layout_alignBottom="#id/left"
android:layout_toRightOf="#id/textv1" />
You can only declare #+id once - it creates a variable. Later use #id to reference the same variable.
Related
my fist very simple android app is crashed, if I press my button. It's just a test. If the button click is working, I want to put an sql query in myClick function.
package com.example.connectdroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
void myClick(){
//here is my problem:
LinearLayout lView = (LinearLayout)findViewById(R.id.mylinear);
TextView selection = (TextView) findViewById(R.id.editText1);
String mytext = "apple";
selection.setText(mytext);
}
}
The 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"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Button"
android:onClick="myClick" />
<LinearLayout
android:id="#+id/mylinear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginLeft="17dp"
android:layout_marginTop="57dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mylinear"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="22dp"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</RelativeLayout>
void myClick(){
should be
public void myClick(View view){
Your app is crashing because you declared android:onClick="myClick" but the signature in the java file of myClick is wrong
thats true dear blackbelt,
The method called onClick should be following features
public method and
the method should contain View parameter
error is that, R is underlined red! and com.example.guessthenumber.R is also underlined red!
when hovered it says to create a class R in package com.example.guessthenumber.R
xml file:
<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="#drawable/Guess"
android:gravity="start"
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Done!" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Enter your guessed number below"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#191970"
android:textStyle="bold" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_centerVertical="true"
android:text="0"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/editText1"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-condensed"
android:text="#string/guess"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/game" />
</RelativeLayout>
below one is the java file:
package com.example.guessthenumber;
import android.os.Bundle;
import com.example.guessthenumber.R
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1 = (TextView) findViewById(R.id.textView1);
final TextView guessText = (TextView) findViewById(R.id.textView2);
final EditText userGuess = (EditText) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.button1);
final String[] myNames={"Shashank","Sarthak","Gowda","$hankar"};
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randText="";
// TODO Auto-generated method stub
int rando = (int) (Math.random()* 6);
int userNumber = Integer.parseInt(userGuess.getText().toString());
if(userNumber == rando){
guessText.setText("You got it right!");
}else{
guessText.setText("Guess again! :(");
}
randText=Integer.toString(rando);
t1.setText(randText);
userGuess.setText("0");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Replace:
import android.R;
by
import com.example.guessthenumber.R
OR
remove import.android.R and use ctrl+shift+O, organise imports if you are using eclipse.
Clean, build your project.
Also, pls read more about R:
Android: What is R? Why is it so Cryptic?
Check your imports - at the top of your activity, below your package name. And make sure it has something like this line:
import your_application_package_name.R;
Agreed with gunar's comment above,
In your xml, the "id" has not been defined properly, for your TextView. Please post the xml code, may be the activity's too.
Correct way for "id" in TextView, xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Check that you are importing the R class from your package and not the Android's one. Check also that your resources do not contains error, otherwise the R class will not be generated.
I wanted to do some thing like this as shown in image below,Is there any widget to do this or we need to add a view & make it transparent.
Thanks .
you must be create a custom view for it and make it transparent..
Main Activity:
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopup();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initPopup() {
final Dialog view = new Dialog(this);
view.requestWindowFeature(Window.FEATURE_NO_TITLE);
view.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
view.setContentView(R.layout.transparent_layout);
view.setCanceledOnTouchOutside(true);
view.show();
RelativeLayout rl_chooseone_menu_main = (RelativeLayout) view
.findViewById(R.id.rl_chooseone_menu_main);
rl_chooseone_menu_main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.dismiss();
}
});
/*Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
view.cancel();
view.dismiss();
}
}, 3000);*/
}
}
activity_main.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"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This is the Transparent View name is transparent_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/rl_chooseone_menu_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="30dp"
android:text="Tap to view all" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:text="Tap to place your order" />
</RelativeLayout>
</RelativeLayout>
Enjoy Brother,i have used dimmy images,replace it with your own images..
You could make a tutorial Activity with a semi-transparent background, containing ImageViews and/or TextViews as shown in your example.
Tutorial activity's layout
<LinearLayout ...
android:background="#color/semitransparent"
... >
...
colors.xml
...
<color name="semitransparent">#80000000</color>
...
Then just start it on top of your usual Activity if you notice this is the first launch of your app by the user.
I am new to android and making a simple application of a temperature converter. i had placed 2 text views and a button on the screen. i just wanted to saw that when i press the button an event comes or not when i run the application no event came after pressing the button so i put it in debug mode then it gives a message
Source Not Found:Source attachment does not contain the source for the file view.class
Here is my code:
Activity_main.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/red"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="136dp"
android:layout_marginLeft="94dp"
android:onClick="onclick"
android:text="#string/Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="38dp"
android:text="#string/Large_Text"
android:textStyle="italic"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="42dp"
android:text="#string/Large_Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/button1"
android:ems="10"
android:inputType="numberSigned" />
</RelativeLayout>
Strings.Xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button">Convert</string>
<string name="Large_Text">Deg C</string>
<string name="Large_Text1">Deg F</string>
<color name="red">#D3D3D3</color>
</resources>
Main_activity.java
package com.example.converter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have created a classs1.java
package com.example.converter;
import android.app.Activity;
import android.widget.TextView;
public class Classs1 extends Activity{
public void onclick(){
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
}
U did not call event listener to ur view
public class Classs1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.ur_button_id);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
});
}
Edit:
Error : Source Not Found:Source attachment does not contain the source for the file view.class
Did u add classs1.java in your manifest file Plz check that too.
i am trying to write a program that changes the background color of the screen to the color that i decided.
i wrote something like that but when it runs it crashes
what is sthe problem,please help me.here is the xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#FFFFFF"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="White" />
</LinearLayout>
</LinearLayout>
and here is the .java code
package com.example.flashlight;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class FlashLight extends Activity {
Button red,green,blue,white;
LinearLayout view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
red=(Button) findViewById(R.id.button1);
green=(Button) findViewById(R.id.button2);
blue=(Button) findViewById(R.id.button3);
white=(Button) findViewById(R.id.button4);
red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.RED);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_flash_light, menu);
return true;
}
}
you need to assign an id to your LinearLayout
<LinearLayout
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#FFFFFF"
then initialize your view
view = (LinearLayout) findViewById(R.id.view)
red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.RED);
}
});
you are not assigning the view variable to anything, that is probably causing a NullPointerException in your code
try adding in your XML file this line under the LinearLayout:
android:id="#+id/view"
and adding this line to your onCreate:
view = (LinearLayout)findViewBiId(R.id.view);
Try this code
This will work for me...
In youyr xml file set id for your Linear Layout as a view.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:id="#+id/view"
android:background="#FFFFFF"
>
In Your java file your linear layout view is null because of id.
so put below line in your java file
view = (LinearLayout) findViewById(R.id.view);
This will work for me....
Happy coding..
After onClick use this below code:
view.setBackgroundColor(Color.DKGRAY);
Example:
public void onClick(View v) {
view.setBackgroundColor(Color.DKGRAY); //Add any color you want
}