Display editText and a Button in Another Activity - java

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

Related

From second activity when press home back button do not came back same fragment in a activity

In my project 2 activity given - MainActivity & Second Activity.
in my main activity, I have four fragments, In the 3rd fragment, A button uses to go to the second activity.
I will implement the below code to go back
This code came back to me on MainActivity at home Fragment. but I went through the third fragment.so I want to come back to my third fragment. Please coders help me to solve this.
Also, help me when I go from fragment to fragment and want to come back to the same fragment.
In Manifest
<activity
android:name=".SecondActivity"
android:parentActivityName=".MainActivity"
android:exported="false" />
ThirdFragment
binding.goToSecondActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent a = new Intent(getActivity(), SecondActivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
});
In Second activity
public class SecondActivity extends AppCompatActivity {
ActivitySecondBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySecondBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setTitle("Second Page");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// code here
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
You should define a class, with a method getInstance accessible globally, that contains the last fragment visited, when you press back you should use the fragment manager to recreate the last fragment (found in the new class) and then, looking at the model of the selected fragment, repopulate it with the old data (if there are inputs/non static datas in the fragment)

onTouchEvent and onClickListener can't work together on the same page

For example, if I have some buttons with onclick listener and the rest of the screen recieves ontouch event.The ontouch event will jam the onclick event unless you lift up the ontouch finger the onclick will not respond.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAttackButton = findViewById(R.id.attack);
mJumpButton = findViewById(R.id.jump);
mAttackButton.setOnClickListener(this);
mJumpButton .setOnClickListener(this);}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.attack:
attack();
break;
case R.id.jump;
jump();
break;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
mTouchPoint.x=event.getX();
mTouchPoint.y=event.getY();
return true;
}
}
You can always split the onClickListener(). Instead of implementing the entirety of the activity. Try adding onClickListener() to each view individually. Same with the OnTouchEvent().

Implement OnClickListener

I'm reading an Android's tutorial and I have a class that implements abstract class OnClickListener. The problem is that in tutorial when it override method onClick,this has only one parameter but my eclipse show me an error because the onClick method need two parameters.
Below my wrong code by tutorial,how Can I fix it?
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.form_button);
button.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.form_button:
final EditText edit_name = (EditText) findViewById(R.id.edit_name);
final EditText edit_lastname = (EditText) findViewById(R.id.edit_lastname);
Bundle bundle = new Bundle();
bundle.putString("name", edit_name.getText().toString());
bundle.putString("lastname", edit_lastname.getText().toString());
Intent form_intent = new Intent(getApplicationContext(), Form.class);
form_intent.putExtras(bundle);
startActivity(form_intent);
break;
}
}
}
You need to import
import android.view.View.OnClickListener
and not
import android.content.DialogInterface.OnClickListener
I guess you have the wrong import of DialogInterface.OnClickListener
onClick(DialogInterface dialog, int which) takes 2 param
But View.OnClickListener's onClick takes only 1 param ie View
http://developer.android.com/reference/android/view/View.OnClickListener.html
You may mistakenly imported the onClickListener on DialogInterface. You have to import android.view.onClickListener, because Button is a sub-class of View.
For more details check this link.
or do as below
implements View.OnClickListener

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();

Where should i declare the button of different layout?

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
}
};
}

Categories