Where should i declare the button of different layout? - java

I've made a calculator apps and I'm trying to create an about page showing some information.
The OK button will be coded setContentView(originallayout.xml) to return to the calculator layout.
Where should i put these codes to declare the OK button?
private Button btnOK;
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
I tried to put these code just below where i did for the buttons at the main layout but the apps just stopped after launch.
07-18 09:39:43.290: E/AndroidRuntime(6984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hychentsa.calculator/com.hychentsa.calculator.CalculatorActivity}: java.lang.NullPointerException

Instead of using setContentView() to change screens, you should have separate activities. Then, in your about activity, you can simply call finish() on button click to go back to the main activity.
http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)

if your layout doesn't content the id of the button (in your case btnOK), Eclipse throws NullPointerException - it can not find it in your layout's content.
So when you set your layout (or menu), it must content the id btnOK. Check it!
Put your button initialization after setContentView(R.layout.your_about_layout_name);
Put all this code in
Button btnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
}
Update:
Look at the answer of invertigo:
It's wrong to change the layout when you click on the button.
You have to do it this way:
CalculatorActivity
public class CalculatorActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator_black);
// initialization of your views stays here
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.calculator_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.your_id_to_go_in_about_activity:
Intent intent = new Intent(CalculatorActivity.this, AboutActivity.class);
// put some extras if you need to send information from this page to the
// AboutActivity page with this code: intent.putExtra();
startActivity(intent); // with this code you go to AboutActivity
return true;
case R.id.theme:
// Do Something with the theme
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now, the place for your initialization of OKButton is in new class, lets call it
AboutActivity
here you can put my earlier code:
public class AboutActivity extends Activity{
Button btnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
}
// and the listener for your OK button have to look like this:
OnClickListener OKListener = new OnClickListener() {
#Override
public void onClick(View v) {
// Do something here if you need
finish(); // with finish() you are returning to the previous page
// which is CalculatorActivity
}
};
}

Related

Create child Intent from Fragment

I'm using FragmentActivity for switching between Fragment. But I would like to have a Admin Button on a fragment, and when I click on it, a new fragment or activity appears like a child (with the back button in action bar).
How can I make it ?
Here is my code, that works, but the back button doesn't appear in action bar :
Fragment :
public class Reports extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null) {
return null;
}
public void onClick(View v) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
getActivity().startActivity(intent);
}
});
}
}
Activity (for the moment... but maybe Fragment if we need ?) :
public class LoginActivity extends ActionBarActivity {
public static final String TAG = LoginActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView emailText = (TextView) findViewById(R.id.emailText);
TextView passwordText = (TextView) findViewById(R.id.passwordText);
ParseUser.logInInBackground(emailText.getText().toString(), passwordText.getText().toString(), new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.i(TAG, "Yeahhh Login OK");
finish();
} else {
runOnUiThread();
}
}
});
}
});
}
Maybe I have to change something in Manifest ?
All you need to do is enable it inside the activity you're currently at.
When inside a FragmentActivity: getActionBar().setHomeAsUpEnabled(boolean).
Otherwise, inside a Fragment: getActivity().getActionBar().setHomeAsUpEnabled(boolean).
U need to override the onCreateOptionsMenu and onOptionsItemSelected. In the onCreateOptionsMenu method do the following : Inflate the menu into the action bar. You can define the contents of the menu item under res/menu folder.
Next in the onOptionsItemSelected method, you can handle the clicks of the back button added in the action bar. Also keep in mind one thing. In the manifest please use a theme which has action bar in it.
Example : Under the application tag use
android:theme="#android:style/Theme.Light" and not anything like android:theme="#android:style/Theme.Light.NoTitleBar
Well if you are starting a new Activity you can enable the back button in it by writing shouldDisplayHomeUp(); in the onCreate() method and on back should take you to the previous activity in the back stack.
And in the other case of adding a new Fragment you can take a look on this answer for reference as it mentions that when you add a new Fragment you add it to the back stack like this
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
this will make the back button take you to your previous Fragment

can't repeat text in editText using a button

I have a simple activity in android with a button and an editText widget.
I have referenced the resource for the button, created an instance variable for the button, and invoked the listener method on it.
When I run the application, the button prints to the edit text widget, but only once.
I cannot continue to type "0000000", which is the ideal output that I want, rather than being stuck on "0".
The code I have used is:
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn0);
// set an onclick listener to listen for when the buttons are clicked
toe.setOnClickListener(this);
text = (EditText) findViewById(R.id.editTextSimple);
}
#Override
public void onClick(View v) {
text.setText(btn1.getText().toString());
}
#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;
}
}
Is there a method or a work around that I could implement in order to print more digits to the edit text view?
Thank you for your help
There is no need to reinvent the wheel here. You don't even have to use direct Strings.
Your onClick could simply be:
#Override
public void onClick(View v) {
text.append(btn.getText());
}
Also, I don't know what "toe" is in
toe.setOnClickListener(this);
but you should be setting the clicklistener on btn1
Replace
text.setText(btn1.getText().toString());
with
String t=text.getText().toString();
text.setText(t+btn1.getText().toString())

Android/Java passing value from one screen to another and displaying value

I am a beginner, and having trouble.
I have two pages. On logon.java user enters name and clicks button. On second screen I need to have "Hello"+ the name they entered on the first page.
So for the first page I have:
public class LogonActivity extends Activity
{
private EditText enterName;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//associate view with activity
setContentView(R.layout.logon);
//set the button
Button button=(Button)findViewById(R.id.button);
//access the field where the user entered the name
final EditText editTextName= (EditText)findViewById(R.id.enterName);
//button listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//save the editTextName to a new variable -enterName
String enterName=editTextName.getText().toString();
//create explicit intent
Intent nameResultsIntent = new
Intent(getApplicationContext(),
ConfirmationActivity.class);
//pass that to next activity
nameResultsIntent.putExtra("PERSON_NAME",enterName);
startActivity(nameResultsIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logon_main, menu);
return true;
On the second page is:
public class ConfirmationActivity extends Activity {
EditText enterName;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//associate the layout with this activity using setContentView
setContentView(R.layout.confirmation);
//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");
findViewById(R.id.confirmationText);
String confirmationText = enterName;
//confirmationText.setText(enterName);
finish();
}
}
}
}
So... its the last lines on the ConfirmationPage.
The Text field on the 1st page is : android:id="#+id/enterName"
The field on the 2nd page where I want the text to appear is :
Can someone help with the last line to show the text on the second page?
Try something like this
//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");
EditText confirmationText = (EditText)findViewById(R.id.confirmationText);
confirmationText.setText(enterName);
finish();

Display editText and a Button in Another Activity

I am using Linear Layout(Horizontal) for my Android application. I am using two buttons for my screen which I have called as Chat and Draw. I want to display a second activity on clicking on Chat button in which I have an area for editText and a corresponding button called as Enter for entering the text.
In the DisplayMessageActivity class which I am using for Chat button, I have created the layout for editText and Enter button too. But however, on clicking on Chat I am not being able to see the area for editText and the button Enter.
Code in MainActivity.java :
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.appfirst.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void chatMessage(View view) {
Intent intent_chat = new Intent(this, DisplayMessageActivity.class);
startActivity(intent_chat);
}
public void drawing(View view) {
Intent intent_draw = new Intent(this, DisplayMessageActivity.class);
startActivity(intent_draw);
}
}
code in DisplayMessageActivity.java :
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
What code should I write in the OnCreate function of DisplayMessageActivity class so that I am able to get the desired view ?
Can someone help me with the code. I am totally new to Android Development Framework. Thanks and Regards.
From your post i understand that u have 2 layouts 1 with 2 buttons "Chat" and "Draw".And when clicking chat u have to call another activity with Edittext and Enter button in it.If this is the case you simply call the Intent.
Intent intent=new Intent(this,yourclass.class);
startactivity(intent);
In Oncreate of your DisplayMessage do the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourXml.Xml);
}
In your second activity you need to put the following line setContentView(R.layout.whatEverYourLayoutIsCalled);, put it right after super.onCreate(...);
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityxml);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
For more information look the below example link

Button Handlers in Android App

I am trying to add multiple button handlers for my app. I have successfully created one button that links to a web page. I am running into trouble with my next button, however. I want to go to a new screen on click. I have create the new screen xml file and the corresponding java file. I have also added the activity to the manifest. I'm just not sure how to add multiple button handlers to the main java page. I have attached how I did my first button. Any advice on how to add a second handler to this page for a button that will change view?
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.noblenet.org/eg/opac/home?locg=1"));
startActivity(browserIntent);
}
});
}
#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;
}
If I understand it correctly then buttons appear through onCreateOptionsMenu logic.
Best way to handle the click events in this case would be to override onOptionsItemSelected API. Look here for more details
One of the simplest ways of handling button click is to use android:onClick attribute in xml file for a button view. For example in Button View of your xml add android:onClick="clickMe"
and in your java code write Public void clickMe(View view) { //do something on button click }

Categories