Please have a look at the following code
game_activity.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=".Game" >
//Please note the GUI Has been removed
<include layout="#layout/common_status_bar"/>
</RelativeLayout>
Game.java
package game.games;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class Game extends Activity {
private ImageView goToLanguageSelection;
private ImageView goToInternet;
private Button giveUp;
private LayoutInflater layoutInflater;
private View commonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
//Intializing instance variables
goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
giveUp = (Button)commonView.findViewById(R.id.giveUpButton);
flag = getIntent().getBooleanExtra("LANGUAGE", true);
//Add listeners
goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
//Will get activated when the ThunderBolt image is clicked
private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
}
common_status_bar.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="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backToLanguageSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:paddingTop="5dp"
android:src="#drawable/thunderbolt" />
<ImageView
android:id="#+id/internetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/globe_small_2" />
<ImageView
android:id="#+id/justForFun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/giveUpButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Button" />
</LinearLayout>
Here, the common_status_bar.xml is a common layout. I have added this layout to game_activity.xml by <include layout="#layout/common_status_bar"/>.
It displays everything fine, no issue. But the case is I can't make any of it's elements to work. I have attached a OnClickListener to the first element of it, the goToLanguageSelection but it is not responding to the Click. I tested this by adding the click event to all other buttons, images as well (one at a time) and I got the same result. It is not responding to user click, or whatever.
Why this button and images in separate layout do not respond to the user events?
The commonView should not be created again.
If you want keep a reference of the common_status_bar.xml, you can give name it by an id, like the code below:
<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="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<include
android:id="#+id/common_status_bar"
layout="#layout/common_status_bar" />
</RelativeLayout>
Then, change this code:
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
to:
commonView = findViewById(R.id.common_status_bar);
Related
I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="#+id/etItems"
android:textSize="24sp"
android:textColor="#android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="#+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
edit text shrinked
Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue
Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.
I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. 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"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.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"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}
I'm trying to make the app show 5 pictures that are include in the drawable file of project, I need when the user tap the "Next" button for first time show the first picture when he tap second show the second picture and so on. but when he tap the "Back" button the previous picture show up, for example the user click 4 times "next" button then when he tap "Back" button it shows the third picture and so on.
JAVA CODE:
package com.example.tables;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory{
ImageSwitcher imgS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgS=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
imgS.setFactory(this);
Animation inShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
imgS.setInAnimation(inShow);
Animation outShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
imgS.setOutAnimation(outShow);
}
#Override
public View makeView() {
ImageView tableImage=new ImageView(getApplicationContext());
tableImage.setScaleType(ImageView.ScaleType.FIT_CENTER);///mal2 alcenter
tableImage.setLayoutParams(new ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT , ActionBar.LayoutParams.WRAP_CONTENT));
return tableImage;
}
public void buttonNext(View view){
imgS.setImageResource(R.drawable.i);
}
public void buttonBack(View view){
imgS.setImageResource(R.drawable.ii);
}
}
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="com.example.tables.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reserve table" />
<TextView
android:id="#+id/selectTableViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#f00"
android:layout_marginLeft="30dp"
android:text="Please check the button up to Reserve the table">
</TextView>
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="buttonNext"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="buttonBack"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Make an array of the images then add that to your image switcher. You would also need to add some type of current index constant variable so you can index the switching
I have searched high and low to find a simple way to turn buttons into a menu (any menu style) I currently have buttons in my app and I would like to turn them into a menu (i.e. the 3 dots in the right corner) for a more simple UI
I would like the following to appear in a menu:
bCheckOnline
bSettings
bLogout1
If this is alot more complicated than i thought please contact me and I will pay someone to modify my code. (however, any help is appreciated!)
I have included my res/layout/activity_main.xml below:
<?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"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#color/black"
android:paddingBottom="#dimen/sixteen"
android:paddingTop="#dimen/sixteen"
android:weightSum="10" >
<TextView
android:id="#+id/tvSyncDate_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone" />
<TextView
android:id="#+id/tvVersion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9.06"
android:gravity="center_vertical"
android:paddingLeft="13dp"
android:singleLine="true"
android:textColor="#color/white"
android:text="TAP+REPORT"
android:textSize="#dimen/twentysize"
android:textStyle="bold" />
<Button
android:id="#+id/bSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.55"
android:background="#drawable/btn2"
android:text="Sync"
android:textColor="#color/white"
android:textSize="#dimen/twentysize" />
</LinearLayout>
<ScrollView
android:id="#+id/svMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:background="#color/white"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/thirteen"
android:paddingRight="#dimen/thirteen"
android:paddingTop="5dp"
android:text="Tap tag now with top-back of phone and hold to report. Upon completion tap sync."
android:textColor="#color/darkgrey"
android:textSize="#dimen/seventeen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="4.76"
android:gravity="top|center_vertical|center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/myanimation"
android:layout_width="match_parent"
android:layout_height="338dp"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="#anim/anim_android" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="#dimen/twentysize"
android:paddingLeft="#dimen/twentysize"
android:paddingRight="#dimen/twentysize" >
<Button
android:id="#+id/bCheckOnline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8.55"
android:background="#drawable/btn3"
android:text="View Online"
android:textColor="#color/darkgrey"
android:textSize="#dimen/seventeen" />
<Button
android:id="#+id/bLogout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:background="#null"
android:gravity="right|center_horizontal"
android:paddingRight="5dp"
android:text="Logout"
android:textColor="#color/holobluelight"
android:textSize="#dimen/seventeen" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
I have included the java file:
package com.test.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//import android.provider.Contacts.Intents;
import android.widget.TextView;
import java.util.Random;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.net.Uri;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import java.io.IOException;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import android.util.Log;
//import org.ndeftools.Message;
//import org.ndeftools.Record;
//import org.ndeftools.wellknown.SmartPosterRecord;
//import org.ndeftools.wellknown.TextRecord;
//import org.ndeftools.wellknown.UriRecord;
import android.app.PendingIntent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class ScanInputActivity extends Activity implements OnClickListener {
SharedPreferences app_preferences;
TextView tvSyncDate_temp;
Vehicle vehicle;
private NfcAdapter mNfcAdapter;
public static final String MIME_TEXT_PLAIN = "text/plain";
public static final String TAG = "NfcDemo";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
final AnimationDrawable myAnimationDrawable
= (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
Button bSettings= (Button) findViewById(R.id.bSettings);
bSettings.setOnClickListener(this);
Button bCheckOnline= (Button) findViewById(R.id.bCheckOnline);
bCheckOnline.setOnClickListener(this);
Button bLogout1 = (Button) findViewById(R.id.bLogout1);
bLogout1.setOnClickListener(this);
You need to extends ActionBarActivity insted of Activity.
Then make a function for onCreateOptionsMenu
#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;
}
Then make onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
// Do something when Settings button is clicked.
return true;
}
return super.onOptionsItemSelected(item);
}
and in you Resourses folder there must be a menu folder with main.xml something like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
You can play with the above code to experiment with it and modify the same to get your desired results. I hope the information was helpful. Read more about it here.
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"
}