I just started programming a little password manager but every time I run it in the emulator, it crashes and I get the following error:
android.widget.TextView cannot be cast to android.widget.EditText
Is everyone seeing the mistake?
MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpassfield = (EditText) findViewById(R.id.masterpass);
masterpass = "password";
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString() == masterpass) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
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" >
<EditText
android:id="#+id/masterpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/login" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="#string/masterpasstext"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Project -> Clean seems to be the only solution to me.
Trying cleaning your project a few times and then build it.
Try Project -> Clean and also check if there are any warnings in any .xml file. If there are any the R.class will not be generated correctly.
Related
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+".");
Im trying to get a dialog to display on my app, I followed the exact dialog tutorial on the android website https://developer.android.com/guide/topics/ui/dialogs.html
(with some slight name changes) however it is rather ambiguous about where to put certain portions of code leading to my code not working. Im not sure what to do at this point.
Dialog Fragment
public class CreatePlayerDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.player_name_dialog)
.setPositiveButton(R.string.player_name_dialog_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.player_name_dialog_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
XML File The button in question is id: button 6
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_start_new_game"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.hunte.assassin.StartNewGameActivity"
tools:showIn="#layout/activity_start_new_game">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:textAlignment="center"
android:hint="Name Of Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColorLink="?attr/colorPrimaryDark"
android:selectAllOnFocus="true" />
<ListView
android:layout_marginTop="15dp"
android:layout_height="300dp"
android:layout_width="250dp"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:id="#+id/Players" />
<TextView
android:text=" Players:"
android:id="#+id/textView3"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="62dp"
android:layout_alignTop="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:text="Button"
android:id="#+id/button6"
android:foreground="#android:drawable/ic_input_add"
android:layout_marginTop="6dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="confirmFireMissles"/>
<ImageButton
android:text="Button"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button7"
android:layout_below="#+id/button6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:foreground="#android:drawable/ic_delete" />
<Button
android:text="Begin!"
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/button3"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Activity W/ show dialog method
package com.example.hunte.assassin;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class StartNewGameActivity extends AppCompatActivity {
private ListView lv;
public void confirmFireMissiles() {
DialogFragment newFragment = new CreatePlayerDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
ArrayList<Player> players = new ArrayList<Player>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_new_game);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.Players);
// Instanciating an array list (you don't need to do this,
// you already have yours).
List<String> game_players = new ArrayList<String>();
for(int i = 0; i < players.size(); i++){
game_players.add(players.get(i).name);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
game_players );
lv.setAdapter(arrayAdapter);
}
}
This is what I thought would be the most logical implementation however my XML file does not seem to recognize my "confirmFireMissles" method and my activity file says that my method isnt used. Sorry for including so much code I just cannot identify where the problem may lie. Im only trying to get this to work so I can understand how to display a dialog
You need to pass View as parameter to confirmFireMissiles() method.
public void confirmFireMissiles(View view) {
DialogFragment newFragment = new CreatePlayerDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
From looking online, I know there is some problem with my XML file but not sure what it is. I've tried remaking the entire button from scratch but the error persists. The following is the code in the activity.
package com.example.linked1n;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.linked1n.R;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewbyId(R.Id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.Id.login:
loginClick();
break;
}
}
This is the 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.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="#string/yayaya" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
Any help will be appreciated!
Edit: adding doesn't fix it
It is not R.Id.login, it should be R.id.login
You wrote ID in caps in your switch statement. id is a reserved word and needs to be in small letter, change the R.Id.login to R.id.login, then run :)
End tag is missing for the parent layout ...
Add </RelativeLayout> tag at the end. If the problem still persists, let us know.
Remove the last line - </EditText>
Below is your XML file which i build successfully. Try this.
<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.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="Sample" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" />
<requestFocus />
</RelativeLayout>
java file
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
}
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"
}
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);