I was working on a different code site before Android studio, and how they made buttons in a second activity open was different than here. So far I have my second activity button and it opens..
My fifthactivity.java is below
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
I understand I need to make a new .java and a new layout to direct the button, I just need help with the code to put into my fifth activity.java
Below is my layout for the other button that i need to open.
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
Button button;
Button anotherButton; // the second button OP required
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
anotherButton = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(FifthActivity.this, AnotherActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
}
}
Add new Button to your xml file and style it how do you like and add new id like
android:id="#+id/button6" :
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
<Button
android:id="#+id/button6"
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SomeText"
android:textSize="35sp" />
In your fifthactivity.java add new Button:
Button button, button2;
and, like your previous button, add click listener to that button. Create new Java class with its own layout and open it thru Intent with that button.
Related
In this program, I want to delete ll2 (LinearLayout) when mButton is pressed. I.e I don't want this layout to appear the second time I enter this activity. When I push the button, the layout is gone for as long as I am in the activity, but when I come back into the activity, the layout is there.
How do I permanently delete it? Thanks in advance!
LinearLayout ll,ll2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
location_btn = (Button)findViewById(R.id.location_btn);
menu_btn = (Button)findViewById(R.id.bt_menu);
mButton = (Button) findViewById(R.id.buttone);
mEdit = (EditText) findViewById(R.id.edittexte);
ll2 = (LinearLayout)findViewById(R.id.llayout);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
number = mEdit.getText().toString();
mEdit.setText("");
ll2.setVisibility(View.GONE);
ll2.removeAllViewsInLayout();
}
});
}
My layout file
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/llayout"
android:visibility="visible">
<EditText
android:id="#+id/edittexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SAVE"
android:id="#+id/buttone"/>
</LinearLayout>
Just keep a SharedPreference and save the state that you have pressed the button earlier. Then each time you enter the activity, check the value stored in your SharedPreference and if its found that the button is already pressed before, just hide the LinearLayout.
LinearLayout ll,ll2;
SharedPreference pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
pref = getSharedPreferences("MyApplication", Activity.MODE_PRIVATE);
location_btn = (Button)findViewById(R.id.location_btn);
menu_btn = (Button)findViewById(R.id.bt_menu);
mButton = (Button) findViewById(R.id.buttone);
mEdit = (EditText) findViewById(R.id.edittexte);
ll2 = (LinearLayout)findViewById(R.id.llayout);
// Check the preference value when activity is launched each time and hide of the button was pressed before
if(pref.getBoolean("ButtonPressed", false)) ll2.setVisibility(View.GONE);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
number = mEdit.getText().toString();
mEdit.setText("");
// Save the sate of the button pressed in the SharedPreference
pref.edit().putBoolean("ButtonPressed", true).apply();
ll2.setVisibility(View.GONE);
}
});
}
You can try saving a boolean in SharedPreferences...
For e.g.
Keep the boolean as false in the beginning.
As soon as you press the button, remove the View (LinearLayout), change the boolean to true & save it to SharedPreferences...
In the onCreate(),
try as
if(booleanisTrue) {
ll2.setVisibility(View.GONE);
ll2.removeAllViewsInLayout();
}
i have the edit view and button in main activity. Here is the button code:
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="#{handler::onButtonClick}"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
how can i change activity in onButtonClick method?
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="onButtonClick"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
And add this function to your java code:
public void onButtonClick(View view) {
Intent intent = new Intent(context, YourActivityClass.class);
context.startActivity(intent);
}
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you can edit the view and change activity
// Change Activity using Intent
startActivity(new Intent(CurrentActivity.this, SecondActivity.class));
}
});
}
Hope this answer helps you.
In your main Activity
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getContext(),YOURACTIVITYNAME.class);
startActivity(intent);
}
});
I need help in the MainActivity.java coding such that the value of the text field is stored into a variable on button click so that i could use it for search query later.
The activity_main.xml contains the following:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:text="Search"
android:ems="10"
android:id="#+id/SearchText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
Could anyone suggest what the MainActivity.java file should contain as to recieve the text value as i am new to coding?
Add onClick on your button
onClick:"btnClicked"
Like
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2"
android:onClick="btnClicked" />
Add these code in Java file
public void btnClicked(View view){
EditText et = (EditText)findViewById(R.id.SearchText);
String value = et.getText().toString();// your edit text value
}
//Variable to store value.
String variable;
//References to the views.
Button btnClick = (Button)findViewById(R.id.button2);
EditText etText = (EditText)findViewById(R.id.SearchText);
//Setting click to the button
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// Do Your stuff.
variable = etText.getText().toString();
}
});
what the MainActivity.java file should contain as to receive the text value as i am new to coding?
Write This Code in Your MainActivity.java Whatever you will give in Text field and click Button , It will update and show in Toast Message.
public class MainActivity extends Activity {
EditText et;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
et = (EditText)findViewById(R.id.SearchText);
bt = (Button)findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String eText = et.getText().toString();
Toast.makeText(getApplicationContext(),"Your Text is here"+eText,Toast.LENGTH_SHORT).show();
}
});
}
String YOUR_VARIABLE = "";
Button btn= (Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText edtText = (Edittext)findViewById(R.id.SearchText);
if(!edtText.isEmpty()){
YOUR_VARIABLE = edtText.getText().toString();
}
});
This will store your EditText text in your Variable
if you want to use That variable Globally you should make if public static
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = SearchText.getText().toString();
}
});
Im building an application that requires when you press the button I.E. (search) then it opens the search page, click the (home) button then it goes back to the home view. what I'm missing is the knowledge to make that connection between those two views. this is what the home page looks like in XML.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//Title
//Search Student Button
<Button
android:id="#+id/button1"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Search Student" />
//New Student Button
<Button
android:id="#+id/button2"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="New Studetn " />
//Legal Button
<Button
android:id="#+id/button3"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Legal Info" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Student Registration "
android:textAppearance="?android:attr/textAppearanceLarge" />
some pictures of the application.
http://imgur.com/aVpqUCZ
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
Don't forget to add your new activity in the AndroidManifest.xml:
<activity android:label="#string/app_name" android:name="searchStudentActivity"/>
<activity android:label="#string/app_name" android:name="newStudentActivity"/>
<activity android:label="#string/app_name" android:name="legalActivity"/>
When dealing with multiple views and/or buttons like you are, I usually prefer using only one instance of onClickListener for all the views, to keep the code cleaner.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
}
I would recommend that you change the android:id attribute of your buttons to a more meaningful name. This makes it easier to see what you're referencing in the code. I personally prefer prepending my views with an abreviation of the view class such as btn_ for Button and tv_ for TextView. Remember to update your calls to findViewById() and the id's used in the switch statement.
Finally, don't forget to add your activities to the AndroidManifest.xml file as Sagar Pilkhwal posted, if you haven't already.
I am a completely new programmer and I am looking to make a sort of quiz app as my first app. Just Questions with the right answer that will send the user to the next activity. I figured out how to edit buttons but I am not sure what to add to the JAVA file or XML file that will allow the next activity (screen) to be opened up.
This is my Layout so far. I have decided the password input but the answer to the question will be stored within the app. Not sure if this is the right approach?
`
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Answer" />
`
Thanks in advance for any help rendered!
#kanwaljit Sngh
I am getting multiple errors like "button cannot be resolved to a type" and "R cannot be resolved to a variable" What does these mean?
`import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Startscreen extends Activity {
EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
`
Get the value of edittext
check it with desired answer
If true, redirect to next activity
Eg :
if(editText1.getText().toString().equalsIgnoreCase("desiredanswer")
{
startActivity(new Intent(this,nextActivity.class));
}
else
{
Toast.makeText(this,"Wrong answer",2000).show();
}
EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
and write new two activity CorrectAnswerActivty and WrongAnswerActivity
and do actions what to display in these activties
and add the following line to the xml file in button tag-
android:id="#+id/button"