App crashing due to FindViewById(), issue with componentInfo - java

So whenever the these two lines are commented out its fine, but with them it crashes.
EditText convertValue = (EditText) findViewById(R.id.et_value_convert);
TextView convertResult = (TextView) findViewById(R.id.txt_result);
here's my full class code, xml is below.
package com.doubleelite.maxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ConverterActivity extends Activity {
EditText convertValue = (EditText) findViewById(R.id.et_value_convert);
TextView convertResult = (TextView) findViewById(R.id.txt_result);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_converter, menu);
return true;
}
// Custom method to handle button clicks.
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_to_cel:
convertResult.setText(String.valueOf(convertFahrenheitToCelsius(Integer.valueOf(convertValue.getText().toString()))));
}
}
private float convertFahrenheitToCelsius(float f) {
return (f-32) * (5/9);
}
private float convertCelsiusToFahrenheit(float c) {
return c * (9/5) + 32;
}
}
here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ConverterActivity" >
<EditText
android:id="#+id/et_value_convert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/text_value_convert"
android:inputType="number"
/>
<Button
android:id="#+id/btn_to_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_celsius"
android:onClick="onClick"
/>
<Button
android:id="#+id/btn_to_fahr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_fahr"
android:onClick="onClick"
/>
<TextView
android:id="#+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>

Currently you are trying to find view's before setting layout for Activity.change your code as :
public class ConverterActivity extends Activity {
EditText convertValue ;
TextView convertResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
convertValue = (EditText) findViewById(R.id.et_value_convert);
convertResult = (TextView) findViewById(R.id.txt_result);
}
///YOUR CODE HERE

Related

android add TextView on click of (+) button with setText as 1and in next 2 and so on

I have one textView and two EditText when i am clicking on the plus button the textView and the two EditText should be added ,and i am able to do that .My problem is the when i am adding the TextView the text should be set as 1 then when the TextView is added again then the text should be set as 2 and so on .i am not able set the text by using t1.setText(i).Here is my code-
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
Button btnDisplay;
ImageButton btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (ImageButton) findViewById(R.id.btnAdd);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
MyLayoutOperation.add(this, btnAdd);
MyLayoutOperation.display(this, btnDisplay);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
MyLayoutOperation.java
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyLayoutOperation {
public static void display(final Activity activity, Button btn)
{
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
java.util.ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
TextView t2=(TextView)innerLayout.findViewById(R.id.t1);
t2.setText(i);
EditText e1=(EditText)innerLayout.findViewById(R.id.e1);
EditText edit = (EditText) innerLayout.findViewById(R.id.editDes);
msg.add(t2.getText().toString());
msg.add(e1.getText().toString());
msg.add(edit.getText().toString());
}
Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
t.show();
}
});
}
public static void add(final Activity activity, ImageButton btn)
{
final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LinearLayout newView = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.rowdetail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutForm.removeView(newView);
}
});
linearLayoutForm.addView(newView);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutTeste"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textView1"
android:layout_width="95dp"
android:layout_height="fill_parent"
android:layout_alignBottom="#+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btnAdd"
android:gravity="center_vertical|center_horizontal"
android:text="#string/titleTecnologies"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/btnAdd"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<ScrollView android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="275dp">
<LinearLayout android:id="#+id/linearLayoutForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDisplay" />
</LinearLayout>
rowdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowdetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_marginLeft="10dp"
android:id="#+id/t1"/> <requestFocus />
<EditText android:id="#+id/e1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<EditText android:id="#+id/editDes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<ImageButton android:id="#+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/btnRemove"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
Got the answer just do
t2.setText(i+".");

Error on creating button listener first android app

As this is my first android app; i couldn't figure out what goes wrong.
Error
Error on line "caller.textView.setText(“You clicked the button!”);"
Multiple markers at this line
Syntax error, insert "AssignmentOperator Expression" to complete
Assignment
Syntax error, insert ";" to complete BlockStatements
Line breakpoint:MyOnClickListener [line: 12] - onClick(View)
setText cannot be resolved or is not a field
screenshot of error
MainActivity.java
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new MyOnClickListener(this));
textView = (TextView) findViewById(R.id.textView1);
}
#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;
}
}
MyOnClickListener.java
package com.example.myfirstandroidapp;
import android.view.View;
import android.view.View.OnClickListener;
public class MyOnClickListener implements OnClickListener {
MainActivity caller;
public MyOnClickListener(MainActivity activity) {
this.caller = activity;
}
public void onClick(View view) {
caller.textView.setText(“You clicked the button!”);
}
}
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: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:layout_marginTop="70dp"
android:text="Button" />
</RelativeLayout>

Can't get textview to display user output on android

I'm trying to make a rudimentary app to take the user's name (input) and display their name reversed (output). However, I can't seem to get this code to display the output I want. I just came to Android. How do I display a string in the user output?
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=".Main" >
<Button
android:id="#+id/reverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="166dp"
android:text="#string/reverse" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="#string/desc"
android:textAppearance="?
android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/desc"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignTop="#+id/reverse"
android:layout_marginTop="67dp"
android:text="#string/reversed"
android:textAppearance="?
android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/displayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/reverse"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:text="#string/medium_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Java:
package com.example.basicapp;
import android.os.Bundle;
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 Main extends Activity {
Button reverse;
#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;
}
public class Reverser extends android.app.Activity {
TextView displayed = (TextView)findViewById(R.id.displayName);
EditText nametext = (EditText)findViewById(R.id.name);
String namestring = nametext.getText().toString();
public void onClickListener() {
reverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String reverse = new StringBuffer(namestring).reverse().toString();
String dr = reverse;
displayed.setText(dr);
}
});
}
}
}
Thank you.
public class Main extends Activity {
Button reverse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView displayed = (TextView)findViewById(R.id.displayName);
EditText nametext = (EditText)findViewById(R.id.name);
String namestring = nametext.getText().toString();
reverse=(Button)findViewById(R.id.reverse);//assuming you have button with id reverse
reverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String reverse = new StringBuffer(namestring).reverse().toString();
String dr = reverse;
displayed.setText(dr);
}
});
}
#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;
}
}

listview does not update with notifydatasetchanged() call

My Android listview does not update with notifydatasetchanged() call.
The Main Code Activity:
package com.example.jokesbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.jokesbook.MESSAGE";
CustomAdapter Adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JokeDB.jokesList = new ArrayList<Joke>();
JokeDB.jokesList.add(new Joke("DDD"));
lv = (ListView)findViewById(android.R.id.list);
Adapter= new CustomAdapter(MainActivity.this, R.layout.joke_list_item, JokeDB.jokesList);
lv.setAdapter(Adapter);
}//onCreate
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
Adapter.notifyDataSetChanged();
}
class CustomAdapter extends ArrayAdapter<Joke>{
Context context;
int layoutResourceId;
ArrayList<Joke> data = null;
private LayoutInflater mInflater;
public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<Joke> data) {
super(customAdapter, layoutResourceId, data);
Log.d("jokesbook", "CustomAdapter ");
this.layoutResourceId = layoutResourceId;
this.context = customAdapter;
this.data = data;
this.mInflater = LayoutInflater.from(customAdapter);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//item_list
convertView = mInflater.inflate(R.layout.joke_list_item, null);
holder = new ViewHolder();
//fill the views
holder.joke = (TextView) convertView.findViewById(R.id.ListTextView1);
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();//
}
holder.joke.setText(data.get(position).jokeStr);
return convertView;
}
class ViewHolder {
TextView joke;
}
}
/** Called when the user clicks the Add a new joke button */
public void goNewJoke(View view) {
Intent intent = new Intent(this, New_joke.class);
startActivity(intent);
}
}
Its xml:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:onClick="goNewJoke"
android:text="#string/Add_a_new_joke"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When adding content to JokeDB.jokesList in another activity that its code is:
package com.example.jokesbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class New_joke extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_joke, menu);
return true;
}
public void addJoke(View view){
EditText editTextJoke= (EditText)findViewById(R.id.edit_text_jokeToAdd);
JokeDB.jokesList.add(new Joke(editTextJoke.getText().toString()));
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
and its XML is:
<LinearLayout 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="#color/green"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
tools:context=".New_joke" >
<EditText
android:id="#+id/edit_text_jokeToAdd"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginBottom="40dp"
android:background="#color/white"
android:hint="#string/edit_message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/AuthorText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Author"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Author"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_letters_allowed"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/DateText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Date"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Date"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_digits_allowed"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="addJoke"
android:text="#string/Add" />
</LinearLayout>
I cannot see the updated list in the main activity eveb though in onResume I used notifydatasetchanged() function.
Try This....
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
notifyDataSetChanged();
}

Android checkBox onClick app doesn't work, stops unexpectedly?

So,
I am building a basic app which has 2 activivities. Now, the first one works well, the second one does not. It does not show any errors while I edit the codes, but when I run the app and when I am in the second acitivity it gives me an annoying error. Saying "Stops unexpectedly...." You know. I tried debugging but failed. In the second activity called Troll.java I should be able to check a checkbox and when I do that a text is supposed to change to something else. It says stops unexpe.....
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
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" >
<include
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/resuable_pizza_layout" />
<TextView
android:id="#+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:text="#string/display"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<ImageView
android:id="#+id/lol"
android:onClick="onlol"
android:contentDescription="#string/lol2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/display"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:src="#drawable/lol" />
troll.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:background="#FF69B4"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<CheckBox
android:id="#+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onChecked1"
android:text="#string/ch1"
android:textColor="#000000" />
<CheckBox
android:id="#+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onChecked2"
android:text="#string/ch2"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="0.26" >
<TextView
android:id="#+id/textView1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/trollFace"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
mainActitvity.java :
package com.example.pizza2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
CheckBox pepp, cheese;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pepp = (CheckBox) findViewById(R.id.check1);
cheese = (CheckBox) findViewById(R.id.check2);
tv = (TextView) findViewById(R.id.display);
}
public void onShow(View view){
StringBuilder str = new StringBuilder();
if (pepp.isChecked() && cheese.isChecked()){
str.append("Pepperoni, extra cheese pizza!");
}
else if (pepp.isChecked()){
str.append("Pepperoni pizza!");
}
else if (cheese.isChecked()){
str.append("Extra cheese pizza!");
}
tv.setText(str);
}
public void onlol(View view){
Intent intent = new Intent(this, Troll.class);
startActivity(intent);
}
#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;
}
}
Troll.java:
package com.example.pizza2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class Troll extends Activity{
CheckBox box11, box22;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.troll);
box11 = (CheckBox) findViewById(R.id.box1);
box22 = (CheckBox) findViewById(R.id.box2);
tv = (TextView) findViewById(R.id.display);
}
public void onChecked1(View view){
StringBuilder str = new StringBuilder();
if(box11.isChecked()){
str.append("Lol bro!");
}
tv.setText(str);
}//end of "onChecked1"
public void onChecked2(View view){
StringBuilder str = new StringBuilder();
if(box22.isChecked()){
str.append("XD XD Kaki");
}
tv.setText(str);
} //end of "onChecked2"
}

Categories

Resources