Android application: calc function does not work after changing to DrawerLayout - java

I changed the layout in my app from RelativLayout to RelativLayout as a child of DrawerLayout. Everything works fine, except the everything inside onClick() in MainActivity.java. After pressing the button button1 the result of the calculation is NaN which was working before. It has to do something with the change of the layout. I don't have a clue what causes this issue.
Maybe someone can help me out?
GitHub: https://github.com/ephlox/GradesCalculator2
MainActivity.java
package com.lob.gradescalculator;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayoutgesamt;
ActionBarDrawerToggle drawerToggle;
int sum = 0;
int ects = 0;
float mark = 0;
NumberFormat numberFormat = new DecimalFormat("0.00");
Button button1;
TextView textView1;
EditText editText1;
EditText editText2;
EditText editText3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayoutgesamt, R.string.auf, R.string.zu);
drawerLayoutgesamt.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle.syncState();
button1 = (Button)findViewById(R.id.button1);
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editTextGDI);
editText2 = (EditText)findViewById(R.id.editTextGDW);
editText3 = (EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedPreferences();
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v){
ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI
System.out.println("edittext1");
ects += 5;
try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW
System.out.println("edittext2");
ects += 5;
try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){
System.out.println("edittext3");
ects += 7;
try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){}
}
}
}
mark = (float)sum / (float)ects;
textView1.setText(String.valueOf(numberFormat.format(mark)));
savePreferences("editText1", editText1.getText().toString());
savePreferences("editText2", editText2.getText().toString());
savePreferences("editText3", editText3.getText().toString());
sum = 0;
ects = 0;
mark = 0;
}
}
);
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
editText1.setText(sharedPreferences.getString("editText1", ""));
editText2.setText(sharedPreferences.getString("editText2", ""));
editText3.setText(sharedPreferences.getString("editText3", ""));
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(new Configuration());
}
}
activity_main.xml
<?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"
tools:context="com.lob.gradescalculator.MainActivity">
<android.support.v4.widget.DrawerLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/drawerlayoutgesamt"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<!-- Activity Layout für Hauptbereich -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activitylayout"
>
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#c6d9ff"
android:fitsSystemWindows="true"
>
</android.support.v7.widget.Toolbar>
<!-- TextViews -->
<TextView
android:id="#+id/WIF_Title"
android:text="Wirtschaftsinformatik B.Sc."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_below="#+id/toolbar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textViewGDW"
android:text="Grundlagen der Wirtschaftsinformatik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="#+id/textViewGDI"
android:layout_marginTop="30dp"
android:layout_x="45dp"
android:layout_y="344dp"
android:layout_alignStart="#+id/textViewGDI" />
<TextView
android:id="#+id/TextViewProgrammieren1"
android:text="Programmieren 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="#+id/textViewGDW"
android:layout_marginTop="32dp"
android:layout_x="113dp"
android:layout_y="238dp"
android:layout_alignStart="#+id/textViewGDW" />
<!-- EditTexts -->
<!-- Buttons -->
<EditText
android:id="#+id/editTextGDI"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/WIF_Title"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:digits="12345"
android:maxLength="1"
/>
<TextView
android:id="#+id/textViewGDI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grundlagen der Informatik"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="center"
android:layout_x="125dp"
android:layout_y="184dp"
android:layout_below="#+id/WIF_Title"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:layout_marginTop="15dp"/>
<EditText
android:id="#+id/editTextGDW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:ems="10"
android:layout_x="69dp"
android:layout_y="91dp"
android:layout_alignBaseline="#+id/textViewGDW"
android:layout_alignBottom="#+id/textViewGDW"
android:layout_alignStart="#+id/editTextGDI"
android:layout_alignEnd="#+id/editTextGDI" />
<EditText
android:id="#+id/editTextProgrammieren1"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="#drawable/selector"
android:background="#drawable/roundedborders"
android:ems="10"
android:layout_x="78dp"
android:layout_y="264dp"
android:layout_width="50dp"
android:layout_alignBaseline="#+id/TextViewProgrammieren1"
android:layout_alignBottom="#+id/TextViewProgrammieren1"
android:layout_alignStart="#+id/editTextGDW"
android:layout_alignEnd="#+id/editTextGDW" />
<TextView
android:id="#+id/textView1"
android:text="..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="71dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button1"
android:text="Berechne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_x="128dp"
android:layout_y="442dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/textViewGDW" />
</RelativeLayout>
<!-- Drawerlayout für links -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/drayerlayoutsingle"
android:layout_gravity="start"
android:background="#fff">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Solved it. I had to change ViewGroup group = (ViewGroup)findViewById(R.id.activity_main); to ViewGroup group = (ViewGroup)findViewById(R.id.activitylayout);.

Related

How to avoid the softkeyboard overlapping the EditText in fragment?

I try both methods addling line android:windowSoftInputMode="adjustPan" in manifest or adding getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);on Fragment onCreateView but still facing the same issue. That when i type in the my custom EditText the screen does not auto resize.
here is my fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/white"
android:fillViewport="true"
tools:context=".Fragments.HelpersFrag.UseAvailablePackage.AddListBasicFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<include layout="#layout/firstpage_indicator_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#string/pencil_icon"
android:src="#drawable/pencil_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="#style/TextViewTitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basic" />
<include
android:id="#+id/edt_jobTitle"
layout="#layout/custom_edittext" />
<include
android:id="#+id/cbAceptCreditCard"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbCoupons"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbSos"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbNonSmoker"
layout="#layout/checkbox_layout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Custom EditText:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:are="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="15dp"
android:background="#drawable/boundaries">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottombar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:padding="5dp">
<com.chinalwb.are.AREditText
android:id="#+id/arEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="#string/job_description" />
</ScrollView>
<LinearLayout
android:id="#+id/bottombar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:weightSum="1000"
tools:ignore="RtlSymmetry">
<com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault
android:id="#+id/areToolbar"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1000"
android:gravity="center_vertical"/>
<ImageButton
android:id="#+id/btnUnlink"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_unlink"
android:scaleType="fitCenter"
android:src="#drawable/unlink_icon" />
<ImageButton
android:id="#+id/btnUndo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_undo"
android:scaleType="fitCenter"
android:src="#drawable/undo_icon" />
<ImageButton
android:id="#+id/btnRedo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_redo"
android:scaleType="fitCenter"
android:src="#drawable/redo_icon" />
</LinearLayout>
</RelativeLayout>
checkbox
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:fontFamily="#font/raleway_bold"
android:text="#string/financial_planning"
android:textColor="#color/texthint_color"
android:textSize="13sp"
android:padding="10dp"
android:theme="#style/CheckBox" />
AddListFragment.java
package com.blendev.niffty.Fragments.HelpersFrag.UseAvailablePackage;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.blendev.niffty.Constants.Constants;
import com.blendev.niffty.Fragments.BaseFragment;
import com.blendev.niffty.R;
import com.chinalwb.are.AREditText;
import com.chinalwb.are.styles.toolbar.IARE_Toolbar;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Bold;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Italic;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Link;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListBullet;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListNumber;
import com.chinalwb.are.styles.toolitems.IARE_ToolItem;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import ren.qinc.edit.PerformEdit;
public class AddListBasicFragment extends BaseFragment {
Button btnNext;
CheckBox cbCreditCard, cbCoupons, cbSos, cbNonSmoker;
View view;
private IARE_Toolbar mToolbar;
private AREditText mEditText;
PerformEdit mPerformEdit;
EditText edtTitle;
ImageButton btnUndo, btnRedo, btnUnlink;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
String selectedText;
private String mParam1;
private String mParam2;
List arrayList;
public AddListBasicFragment() {
}
public static AddListBasicFragment newInstance(String param1, String param2) {
AddListBasicFragment fragment = new AddListBasicFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_list_basic, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
initView();
initToolbar();
return view;
}
void initView() {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("");
arrayList = new ArrayList<String>();
ImageButton btnClose = (ImageButton) view.findViewById(R.id.btnClose);
edtTitle = view.findViewById(R.id.edt_jobTitle);
btnNext = view.findViewById(R.id.btnNext_id);
cbCreditCard = view.findViewById(R.id.cbAceptCreditCard);
cbCoupons = view.findViewById(R.id.cbCoupons);
cbSos = view.findViewById(R.id.cbSos);
cbNonSmoker = view.findViewById(R.id.cbNonSmoker);
btnUndo = view.findViewById(R.id.btnUndo);
btnRedo = view.findViewById(R.id.btnRedo);
btnUnlink = view.findViewById(R.id.btnUnlink);
cbCreditCard.setText(R.string.cbAcceptCreditCard);
cbCoupons.setText(R.string.cbCoupons);
cbSos.setText(R.string.cbSosHelper);
cbNonSmoker.setText(R.string.cbNonSmoker);
edtTitle.setHint(R.string.job_title);
btnClose.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnUndo.setOnClickListener(this);
btnRedo.setOnClickListener(this);
btnUnlink.setOnClickListener(this);
}
private void initToolbar() {
mToolbar = view.findViewById(R.id.areToolbar);
IARE_ToolItem bold = new ARE_ToolItem_Bold();
IARE_ToolItem italic = new ARE_ToolItem_Italic();
IARE_ToolItem listNumber = new ARE_ToolItem_ListNumber();
IARE_ToolItem listBullet = new ARE_ToolItem_ListBullet();
IARE_ToolItem link = new ARE_ToolItem_Link();
mToolbar.addToolbarItem(bold);
mToolbar.addToolbarItem(italic);
mToolbar.addToolbarItem(listNumber);
mToolbar.addToolbarItem(listBullet);
mToolbar.addToolbarItem(link);
mEditText = view.findViewById(R.id.arEditText);
mEditText.setTextSize(13);
mEditText.setToolbar(mToolbar);
mPerformEdit = new PerformEdit(mEditText);
bold.getStyle().getImageView().setImageResource(R.drawable.bold_icon);
bold.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
italic.getStyle().getImageView().setImageResource(R.drawable.italic_icon);
italic.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try {
listNumber.getStyle().getImageView().setImageResource(R.drawable.numbering_icon);
listBullet.getStyle().getImageView().setImageResource(R.drawable.bullet_icon);
link.getStyle().getImageView().setImageResource(R.drawable.link_icon);
} catch (Exception e) {
Log.e("ERROR: ", e.toString());
}
}
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext_id:
String jobTitle = edtTitle.getText().toString();
String jobDescription = mEditText.getText().toString();
validateInputs(jobTitle, jobDescription);
break;
case R.id.btnClose:
getActivity().onBackPressed();
break;
case R.id.btnUndo:
mPerformEdit.undo();
break;
case R.id.btnRedo:
mPerformEdit.redo();
break;
case R.id.btnUnlink:
break;
}
}
void validateInputs(String jobTitle, String jobDescription) {
if (jobTitle.isEmpty() || jobDescription.isEmpty()) {
Log.d("TAG", "String field empty");
Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
"Please fill the requirements....", Snackbar.LENGTH_LONG);
snackBar.show();
} else {
Bundle bundle = new Bundle();
bundle.putString(Constants.JOB_TITLE, jobTitle);
bundle.putString(Constants.JOB_DESCRIPTION, jobDescription);
arrayList.clear();
if (cbCreditCard.isChecked()) {
arrayList.add(cbCreditCard.getText());
}
if (cbCoupons.isChecked()) {
arrayList.add(cbCoupons.getText());
}
if (cbSos.isChecked()) {
arrayList.add(cbSos.getText());
}
if (cbNonSmoker.isChecked()) {
arrayList.add(cbNonSmoker.getText());
}
Log.d("TAG", "List size: " + arrayList.size());
Fragment imgFileFragment = new ImagesFilesFragment();
imgFileFragment.setArguments(bundle);
this.changeFragment(imgFileFragment, Constants.ADD_LIST_BASIC_FRAGMENT);
}
}
}
Manifest
<activity
android:name=".Activities.MainScreenActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"/>

error in navigating from one activity to another activity

/* i have tried many times but i am not able to navigate to another activity while clicking on the item in the navigation drawer and when i click on that Item the app crashes(error:open app again).*/
drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/admin"
android:title="Admin Profile"
android:icon="#drawable/ic_dashboard"/>
<item
android:id="#+id/createaccount"
android:title="User Registration"
android:icon="#drawable/ic_event_black_24dp"/>
<item
android:id="#+id/manageuser"
android:title="Manage User"
android:icon="#drawable/ic_settings_black_24dp"/>
<item
android:id="#+id/us"
android:title="About us"
android:icon="#drawable/ic_call_to_action_black_24dp"/>
<item
android:id="#+id/logout"
android:title="Logout"
android:icon="#drawable/ic_cancel_black_24dp"/>
</menu>
/activity_navigation_bar.xml/
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="com.ereports.navigationBar">
<android.support.v7.widget.LinearLayoutCompat
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.LinearLayoutCompat>
<android.support.design.widget.NavigationView
app:headerLayout="#layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
app:itemTextColor="#color/darkgray"
app:itemIconTint="#color/darkgray"
app:menu="#menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
/navigationBar.java/
package com.ereports;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import static com.ereports.R.id.us;
public class navigationBar extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_bar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
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();
//noinspection SimplifiableIfStatement
if (id == R.id.us) {
Intent intent = new Intent(this,Registration.class);
this.startActivity(intent);
return true;
}
if (id == R.id.action_search) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_settings) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
/*another activity to navigate for::
'Registration.java*/
package com.ereports;
import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Registration extends Fragment implements OnClickListener {
private static View view;
private static EditText fullName, emailId, mobileNumber, location,
password, confirmPassword;
private static TextView login;
private static Button signUpButton1;
private static CheckBox terms_conditions;
public Registration() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_registration, container, false);
initViews();
setListeners();
return view;
}
// Initialize all views
private void initViews() {
fullName = (EditText) view.findViewById(R.id.fullName);
emailId = (EditText) view.findViewById(R.id.userEmailId);
mobileNumber = (EditText) view.findViewById(R.id.mobileNumber);
location = (EditText) view.findViewById(R.id.location);
password = (EditText) view.findViewById(R.id.password);
confirmPassword = (EditText) view.findViewById(R.id.confirmPassword);
signUpButton1 = (Button) view.findViewById(R.id.signUpBtn);
login = (TextView) view.findViewById(R.id.already_user);
terms_conditions = (CheckBox) view.findViewById(R.id.terms_conditions);
// Setting text selector over textviews
#SuppressLint("ResourceType") XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(),
xrp);
login.setTextColor(csl);
terms_conditions.setTextColor(csl);
} catch (Exception e) {
}
}
// Set Listeners
private void setListeners() {
signUpButton1.setOnClickListener(this);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.signUpBtn1:
// Call checkValidation method
checkValidation();
break;
}
}
// Check Validation Method
private void checkValidation() {
// Get all edittext texts
String getFullName = fullName.getText().toString();
String getEmailId = emailId.getText().toString();
String getMobileNumber = mobileNumber.getText().toString();
String getLocation = location.getText().toString();
String getPassword = password.getText().toString();
String getConfirmPassword = confirmPassword.getText().toString();
// Pattern match for email id
Pattern p = Pattern.compile(Utils.regEx);
Matcher m = p.matcher(getEmailId);
// Check if all strings are null or not
if (getFullName.equals("") || getFullName.length() == 0
|| getEmailId.equals("") || getEmailId.length() == 0
|| getMobileNumber.equals("") || getMobileNumber.length() == 0
|| getLocation.equals("") || getLocation.length() == 0
|| getPassword.equals("") || getPassword.length() == 0
|| getConfirmPassword.equals("")
|| getConfirmPassword.length() == 0)
new CustomToast().Show_Toast(getActivity(), view,
"All fields are required.");
// Check if email id valid or not
else if (!m.find())
new CustomToast().Show_Toast(getActivity(), view,
"Your Email Id is Invalid.");
// Check if both password should be equal
else if (!getConfirmPassword.equals(getPassword))
new CustomToast().Show_Toast(getActivity(), view,
"Both password doesn't match.");
// Make sure user should check Terms and Conditions checkbox
else if (!terms_conditions.isChecked())
new CustomToast().Show_Toast(getActivity(), view,
"Please select Terms and Conditions.");
// Else do signup or do your stuff
else
Toast.makeText(getActivity(), "User Registered", Toast.LENGTH_SHORT)
.show();
}
}
activity_registration.java
<?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"
android:padding="20dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="#string/signUp"
android:textColor="#color/white_greyish"
android:textSize="25sp"
android:textStyle="bold" />
<EditText
android:id="#+id/fullName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/user"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/fullName"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/userEmailId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/email"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/mobileNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/phone"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/mobileNumber"
android:inputType="phone"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/location"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/location"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/passowrd"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/confirmPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/confirm_password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/confirmPassword"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<CheckBox
android:id="#+id/terms_conditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="#string/terms_conditions"
android:textColor="#color/white"
android:textSize="14sp" />
<Button
android:id="#+id/signUpBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#drawable/loginbutton_selector"
android:padding="3dp"
android:text="Register"
android:textColor="#color/background_color"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
/*app is crashing
expected results: it should navigate to Registration.java
actual results: app is crashing*/
Use fragments to switch between activities
[https://www.youtube.com/watch?v=J8GB_b8qyK8]
this covers with a good explanation. I tried it myself

Conditional statement for spinner

I have some problem for understanding spinner in android
strings.xml
<string-array name="pickformula">
<item>add</item>
<item>multiply</item>
</string-array>
xml file
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/pickformula"
android:layout_marginLeft="10dp"/>
<Spinner
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="calc" />
<EditText
android:id="#+id/input1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"/>
<EditText
android:id="#+id/input2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"/>
Java
package com.aururatech.dropvoltage3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
}
}
This is like simple calculator.
If I pick add in spinner, the formula when I press the button is = input1+input2
If I pick multiply in spinner, the formula when I press the button is = input1*input2
I need the java code for that example..
Thanks
Please follow these code to solve your problem. In this i create a calculator according to you. All operations perform by spinner change.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Spinner operation;
Button calculate;
EditText input1;
EditText input2;
TextView result;
String val;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
operation = findViewById(R.id.spinner1);
calculate = findViewById(R.id.button1);
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.textView);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1 = input1.getText().toString();
String val2 = input2.getText().toString();
if (val.equals("add")) {
result.setText(String.valueOf(Integer.parseInt(val1) + Integer.parseInt(val2)));
}
if (val.equals("sub")) {
result.setText(String.valueOf(Integer.parseInt(val1) - Integer.parseInt(val2)));
}
if (val.equals("multiply")) {
result.setText(String.valueOf(Integer.parseInt(val1) * Integer.parseInt(val2)));
}
if (val.equals("divide")) {
result.setText(String.valueOf(Integer.parseInt(val1) / Integer.parseInt(val2)));
}
}
});
operation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
val = operation.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/pickformula" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="calc" />
<EditText
android:id="#+id/input1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:inputType="number" />
<EditText
android:id="#+id/input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input1"
android:layout_marginTop="28dp"
android:inputType="number" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp" />
</RelativeLayout>
string.xml
<resources>
<string-array name="pickformula">
<item>add</item>
<item>sub</item>
<item>multiply</item>
<item>divide</item>
</string-array>
</resources>

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+".");

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