I am new to Android Development. I just started learning Android. I have a very simple android code. On the click of a button I want to display some log text. My code is as follows for MainActivity.java file
package com.amit.amitapps;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void clickFunction(View view){
Log.i("Info", "button Tapped");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And through the properties panel of Button. I have assigned function to button through onClick property. But when I run this code and click on button, my applications stops and gives me an error as Unfortunately, Amit Apps has stopped
My XML File:
<?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.ayush.myfirstandroidapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginTop="53dp"
android:id="#+id/button"
android:background="#android:color/holo_blue_bright"
android:onClick="clickFunction (MainActivity)" />
</RelativeLayout>
Anyone can help me??
the onclick on your xml file has to be set like this:
android:onClick="clickFunction"
Related
I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?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">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity
I have successfully created a sample card on android wear by referring following tutorial https://developer.android.com/training/wearables/ui/cards.html
But my sample card doesn't minimize on down swipe.
This is the output I'm getting
I want some thing like this
and when I swipe down card will minimize like this
Just like media player app working on wearable!
Can anyone help me with this? I have just started learning development for wearable.
Here is my code
package com.example.cardsample;
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.CardScrollView;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect);
CardScrollView cardScrollView = (CardScrollView)
findViewById(R.id.card_scroll_view);
cardScrollView.setCardGravity(Gravity.BOTTOM);
}
}
rect.xml
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/ic_full_sad"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.wearable.view.CardScrollView
android:id="#+id/card_scroll_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_box="bottom">
<android.support.wearable.view.CardFrame
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Heading"
android:textColor="#color/black"
android:textSize="20sp"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="description"
android:textColor="#color/black"
android:textSize="14sp"/>
</LinearLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
I think what you are looking for is a notification: https://developer.android.com/training/wearables/notifications/creating.html
A notification shows the title when its minimized and when you swipe up it shows the full text or your custom layout (https://developer.android.com/training/wearables/apps/layouts.html).
I'm very new to Android developing.
When I create a new project and try to simply add a button using the graphical layout and try refering to it it won't find it.
Sometimes doing a "Clean..." works sometimes it doesn't. Also sometimes restarting eclipse works and sometimes it doesn't.
Is eclipse really that unstable or am I doing something wrong?
Also I should mention that I did go over all the posts of other people (I know this question is being asked alot) but I still haven't found an asnwer
Here is the .java file:
package com.example.testapp;
import com.example.testapp.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
}
}
and here is the 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
I am new to Android/java programming. I really have no clue what I am doing, and would like a little help. What I am trying to do is build a form very similar to the one below. I have already put in all of the layout, and strings information, but I am having trouble with the functions. The error that I am getting is View cannot be resolved to a type line 14. I am using eclipse to help me program this app. Here is my main.xml, and MadisonStudios.java file's contents as well.
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-forms/
main.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome" />
<EditText
android:id="#+id/EditTextName"
android:layout_height="wrap_content"
android:hint="#string/name"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<Spinner
android:id="#+id/SpinnerStatus"
android:layout_height="wrap_content"
android:prompt="#string/status"
android:layout_width="fill_parent"
android:entries="#array/statuslist">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/changebody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/ButtonSendChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendChange"
android:text="#string/changebutton">
</Button>
</LinearLayout>
MadisonStudios.java
package com.madisonstudios.supportapp;
import android.app.Activity;
import android.os.Bundle;
public class MadisonStudios extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendChange(View button) {
// Do click handling here
}
}
import android.view.View;
Add this line in import statements..
You should import it. If you are using Eclipse, press Ctrl+Shift+o.
I'm trying to change my checkbox dynamically.
for some reason the app throw my an exception.
what am I doing wrong?
this is my java code:
package com.school.commtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
public class OtherPlacesMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox c1 = (CheckBox)this.findViewById(R.id.schools);
c1.setChecked(true);
setContentView(R.layout.otherplacesmenu);
}
}
and this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/table">
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow1">
<CheckBox android:text="School" android:id="#+id/schools" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow2">
<CheckBox android:text="Supermaker" android:layout_height="wrap_content" android:id="#+id/supermarkets" android:layout_width="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow3">
<CheckBox android:text="Gardens" android:layout_height="wrap_content" android:id="#+id/gardens" android:layout_width="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
I'm sure its a simple question, but I cannot find the answer anywhere.
p.s.
I can't set the value of the checkbox to be true at the xml because I would later want to change the checkbox from the user setting.
10x!
Usually you should post the exception message when asking a question like this, but it looks like your problem is that you're trying to findViewById before you setContentView. Your view doesn't exist to be found when you look for it.