How to open a new activity on click of textview in android? - java

I am new to android programming world, I have designed as a layout with username, password AS edittext control and "forgot password" as textview control, on click/tap of "forgot password", I want to open a new activity.
This is what I am trying, looking forward for your help.
My Application's xml and java code is mentioned below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light">
<TextView
android:id="#+id/txtVwAccountLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Account Login"
android:textColor="#android:color/background_light"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<EditText
android:id="#+id/eTxtUserName"
android:layout_width="288dp"
android:layout_height="48dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.593"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />
<EditText
android:id="#+id/eTxtPassword"
android:layout_width="290dp"
android:layout_height="45dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.606"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<TextView
android:id="#+id/txtVwForgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password ?"
android:textColor="#android:color/background_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.854"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
<Button
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.779" />
<Button
android:id="#+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/holo_blue_light"
android:text="Register"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.662" />
</android.support.constraint.ConstraintLayout>
Login.java
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final TextView tvForgotPwd = (TextView) findViewById(R.id.txtVwForgetPassword);
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this, "you clicked me", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),PatientSearch.class);
startActivityForResult(myIntent, 0);
}
});
final Button btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
startActivityForResult(myIntent,0);
}
});

Even though this question has been asked so many times, I decided to answer anyway. This will work:
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Login.this, PatientSearch.class);
startActivity(myIntent);
}
});

In your onCreate() method, set an OnClickListener callback on your TextView. In the callback, use an Intent to start the desired activity.
Here is an exemple of code:
yourTextView= (TextView) findViewById(R.id.txtVwForgetPassword);
yourTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
}
});

Probably it is necessary to try a method
startActivity (new Intent (Activity.this, Open Activity.class));

Related

Sending Data from Alert Dialog to New Activity

I am new to Android development and I am trying to send data via EditText to TextView from an alert dialog inflated in the MainActivity to a different activity (NewGame.java). I have tried intent.putExtra (see below) however I am getting null pointer exceptions as it seems I cannot access the views from the dialog layout xml file.
I know there are a few other ways to create an alert dialog so perhaps this way is not best for what i am trying to achieve?
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button newGame, prevGames, newSquad, savedSquads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGame = findViewById(R.id.buttonNewGame);
newGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
EditText enterTeam1, enterTeam2, enterDate, enterTime, enterLocation;
enterTeam1 = myView.findViewById(R.id.etTeam1);
enterTeam2 = myView.findViewById(R.id.etTeam2);
enterDate = myView.findViewById(R.id.etDate);
enterTime = myView.findViewById(R.id.etTime);
enterLocation = myView.findViewById(R.id.etLocation);
builder.setTitle("New Game")
.setCancelable(true)
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(MainActivity.this, NewGame.class);
intent.putExtra("TEAM1", enterTeam1.getText().toString());
intent.putExtra("TEAM2", enterTeam2.getText().toString());
intent.putExtra("DATE", enterDate.getText().toString());
intent.putExtra("TIME", enterTime.getText().toString());
intent.putExtra("LOCATION", enterLocation.getText().toString());
startActivity(intent);
}
})
.setNegativeButton("Cancel", null);
builder.create();
builder.show();
}
});
NewGame.java
public class NewGame extends AppCompatActivity {
TextView team1, team2, date, time, location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
team1 = findViewById(R.id.tvTeam1);
team2 = findViewById(R.id.tvTeam2);
date = findViewById(R.id.tvDate);
time = findViewById(R.id.tvTime);
location = findViewById(R.id.tvLocation);
team1.setText(getIntent().getStringExtra("TEAM1"));
team2.setText(getIntent().getStringExtra("TEAM2"));
date.setText(getIntent().getStringExtra("DATE"));
time.setText(getIntent().getStringExtra("TIME"));
location.setText(getIntent().getStringExtra("LOCATION"));
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvNewGame"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp" />
<EditText
android:id="#+id/etTeam1"
android:layout_width="211dp"
android:layout_height="53dp"
android:ems="10"
android:hint="Team 1"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etTeam2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Team 2"
android:inputType="textPersonName"/>
<EditText
android:id="#+id/etDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="#+id/etTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Time"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName"/>
</LinearLayout>
activity_new_game.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewGame">
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="time"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.085" />
<TextView
android:id="#+id/tvTeam1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="team1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.192" />
</androidx.constraintlayout.widget.ConstraintLayout>
I believe you are having this problem because you are actually inflating 2 views. And referencing the wrong one when you use findById.
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
should look like this
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
builder.setView(myView);

How to create a clear button out out imageview?

So im currently doing a project that is due soon.
it consists of making a phone app with number buttons etc..
One of the questions that the teacher asked us is to add a Imageview and use it as a button to clear the TextView when there is 10 characters or over.
any help is appreciated, I'm still very new to Android coding so my knowledge is very bad haha
here is my Java code:
Button no1,no2,no3,no4,no5,no6,no7,no8,no9,no0;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
no1=(Button) findViewById(R.id.no1);
no2=(Button) findViewById(R.id.no2);
no3=(Button) findViewById(R.id.no3);
no4=(Button) findViewById(R.id.no4);
no5=(Button) findViewById(R.id.no5);
no6=(Button) findViewById(R.id.no6);
no7=(Button) findViewById(R.id.no7);
no8=(Button) findViewById(R.id.no8);
no9=(Button) findViewById(R.id.no9);
no0=(Button) findViewById(R.id.no0);
tv1=(TextView)findViewById(R.id.tv1);
no1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"1");
}
});
no2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"2");
}
});
no3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"3");
}
});
no4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"4");
}
});
no5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"5");
}
});
no6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"6");
}
});
no7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"7");
}
});
no8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"8");
}
});
no9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"9");
}
});
no0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"0");
}
});
}
}
And here Is my XML:
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_width="262dp"
android:layout_height="85dp"
android:layout_marginTop="4dp"
android:gravity="center|right"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:maxLength="10"
android:inputType="phone" />
<ImageView
android:id="#+id/btnclear"
android:layout_width="79dp"
android:layout_height="73dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:clickable="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/phone_on" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="345dp"
android:layout_height="404dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.994">
<Button
android:id="#+id/no7"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no4"
app:layout_constraintEnd_toStartOf="#+id/no8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no8"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no5"
app:layout_constraintEnd_toStartOf="#+id/no9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no7"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no9"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no8"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no4"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no1"
app:layout_constraintEnd_toStartOf="#+id/no5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no7" />
<Button
android:id="#+id/no5"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no2"
app:layout_constraintEnd_toStartOf="#+id/no6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no4"
app:layout_constraintTop_toBottomOf="#+id/no8" />
<Button
android:id="#+id/no6"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no5"
app:layout_constraintTop_toBottomOf="#+id/no9" />
<Button
android:id="#+id/no1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/etoile"
app:layout_constraintEnd_toStartOf="#+id/no2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no4" />
<Button
android:id="#+id/no2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no0"
app:layout_constraintEnd_toStartOf="#+id/no3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no1"
app:layout_constraintTop_toBottomOf="#+id/no5" />
<Button
android:id="#+id/no3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/Hashtag"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no2"
app:layout_constraintTop_toBottomOf="#+id/no6" />
<Button
android:id="#+id/etoile"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="*"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/no0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no1" />
<Button
android:id="#+id/no0"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="0"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/Hashtag"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/etoile"
app:layout_constraintTop_toBottomOf="#+id/no2" />
<Button
android:id="#+id/Hashtag"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no0"
app:layout_constraintTop_toBottomOf="#+id/no3" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And heres a Screenshot of my design:
Design part
Thanks for the help
First, you will need to make your ImageView clickable in the XML:
android:clickable="true"
You can then set the OnClickListener on the ImageView like you did for the rest of your Buttons.

Saving edit to listview android studio

I have a list view of restaurants with 4 different strings: Name, Address, description and tags.
When I click a restaurant it leads me to my detailsActivity.java where I let user edit the name, address, description and tags, and then user can save the edited info to be stored in the list view.
So far when I click save it will only save the data for Name... Address, description and tag strings are not saved with new information.
Here is my code :
public class DetailsActivity extends AppCompatActivity {
private int pos;
//RATE RESTAURANT BUTTON
RatingBar ratingbar1;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent i = getIntent();
String name = i.getStringExtra("name");
final String address = i.getStringExtra("address");
String description = i.getStringExtra("description");
String tags = i.getStringExtra("tags");
pos = i.getIntExtra("position", -1); //-1 means not set
EditText ename = findViewById(R.id.editName);
EditText eaddress = findViewById(R.id.editAddress);
EditText edescription = findViewById(R.id.editDescription);
EditText etags = findViewById(R.id.editTags);
ename.setText(name);
eaddress.setText(address);
edescription.setText(description);
etags.setText(tags);
findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = getIntent();
EditText ename = findViewById(R.id.editName);
EditText eaddress = findViewById(R.id.editAddress);
EditText edescription = findViewById(R.id.editDescription);
EditText etags = findViewById(R.id.editTags);
String name = ename.getText().toString();
String address = eaddress.getText().toString();
String description = edescription.getText().toString();
String tags = etags.getText().toString();
i.putExtra("name", name);
i.putExtra("address", address);
i.putExtra("description", description);
i.putExtra("tags", tags);
i.putExtra("position", pos);
setResult(RESULT_OK, i);
finish();
}
});
Here are my xml files in case they are needed:
activity_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsActivity"
tools:layout_editor_absoluteY="25dp">
<EditText
android:id="#+id/editName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="Restaurant Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Restaurant Address (Street #, City) (***)-***-****"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editName" />
<EditText
android:id="#+id/editDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Restaurant Description"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editAddress" />
<EditText
android:id="#+id/editTags"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:ems="10"
android:hint="tags"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editDescription" />
<Button
android:id="#+id/btnSave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTags" />
<Button
android:id="#+id/btnMap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnSave" />
<Button
android:id="#+id/btnBack"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnMap" />
<RatingBar
android:id="#+id/rating1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnBack"/>
<Button
android:id="#+id/btnRate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rating1" />
Here is activity_main.xml where the listview is located:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btnabout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="512dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="about us"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="New Item"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/editItem" />
<ListView
android:id="#+id/itemList"
android:layout_width="match_parent"
android:layout_height="433dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/editItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" >
</ListView>
</android.support.constraint.ConstraintLayout>
and finally mainactivity.java which includes my onActivityResult function:
public class MainActivity extends AppCompatActivity {
private ArrayList<Item> items;
private ItemsAdapter itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvItems = findViewById(R.id.itemList);
items = new ArrayList<>();
items.add(new Item ("Mcdonalds", "108 queen st", "come get a junior chicken", "fast food"));
items.add(new Item("Pizza Pizza", "9 moms st", "wonderful pizza made by mom", "pizza"));
itemsAdapter = new ItemsAdapter(this, R.layout.row_layout, items);
lvItems.setAdapter(itemsAdapter);
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//ADD NEW ITEM TO LIST
EditText et = findViewById(R.id.editItem);
String text = et.getText().toString();
if(!text.isEmpty())
{
itemsAdapter.add(new Item(text, "default address", "default desc", "default tag"));
et.setText("");
}
}
});
//ABOUT PAGE REDIRECT
Button btn1 = findViewById(R.id.btnabout);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,aboutmembers.class);
startActivity(i);
}
});
//INTERACTION WITH LIST ITEMS (LONG CLICK TO DELETE)
lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final int pos = position;
new AlertDialog.Builder(view.getContext()).setTitle("Warning!")
.setMessage("Do you want to remove this item?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
items.remove(pos);
itemsAdapter.notifyDataSetChanged();
}
}).show();
return true;
}
});
//ADD NEW LISTENER TO LIST
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(view.getContext(), DetailsActivity.class);
i.putExtra("position", position);
i.putExtra("name", items.get(position).getName());
i.putExtra("address", items.get(position).getAddress());
i.putExtra("description", items.get(position).getDescription());
i.putExtra("tags", items.get(position).getTags());
startActivityForResult(i, EDIT_ITEM);
}
});
}
//DEFINE OUR CALL OF EDIT ITEM
public static final int EDIT_ITEM = 1;
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode==EDIT_ITEM)
{
if(resultCode==RESULT_OK)
{
int pos = data.getIntExtra("position", -1);
if(pos!=-1)
{
String name = data.getStringExtra("name");
String address = data.getStringExtra("address");
String description = data.getStringExtra("description");
String tags = data.getStringExtra("tags");
Item item = items.get(pos);
item.setName(name);
items.set(pos, item);
itemsAdapter.notifyDataSetChanged();
}
}
}
}
in mainactivity.java in the onActivityResult function set the address/description/tags:
item.setAddress((address));
item.setDescription(description);
item.setTags(tags);

how to pass button intent to make drawable change color in another activity?

I am new to Android Studio and Java and have been working on a project for some time now and would appreciate help with an issue I cannot seem to find any tutorials or information on.
I am using android Studio version 3.1.4 on a computer running Windows 7 pro.
I cannot fit all my buttons and all my drawables onto the MainActivity, so I want all my buttons on the MainActivity and all my drawables on Activity 2.
My Problem is that I cannot work out how to make my drawables change color in the second Activity when I click any buttons in the MainActivity.
My drawables in the form of circles are in svg format with a diameter of 10mm
I have pasted all relevant code for 3 buttons to work with 3 svg drawable on the MainActivity so that you can see how it works on a virtual device or your mobile connected via a usb cable.
I have also added a button in the main activity to take you to Activity 2 where I have put 3 drawable that I need to work in the same way as the main activity but with the buttons from the first activity changing them.
In my project I have many buttons. Each button when clicked changes the color of its related SVG drawable as per my program. The drawable are black to start with. Each click will make them change to the next colors in this sequential order. White, Yellow, Orange, Red, then back to black
So what I need to do is have my buttons in the main activity and the drawables in activity 2.
If Button 1 (attribute is btn1) is clicked in activity_main.xml I want the button information passed to Activity2 to make the drawable with attribute iv1 change color.
So if you were to click Button1, Button2, Button3, Button4 and use the go to activity2 button (attribute btn) the drawable in Activity2 would be white, yellow, white from left to right.
Thanks in advance for any help.
<package com.example.chucky.svg;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button button;
int[] colors = {0xffffffff, 0xffffff00, 0xffff6600, 0xffff0000, 0xff000000};
int counter1 = -1;
int counter2 = -1;
int counter3 = -1;
ImageView iv1;
ImageView iv2;
ImageView iv3;
Button btn;
Button btn1;
Button btn2;
Button btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter3++;
if (counter3> colors.length -1){
counter3 = 0;
}
iv3.setColorFilter(colors[counter3]);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter2++;
if (counter2> colors.length - 1){
counter2 = 0;
}
iv2.setColorFilter(colors[counter2]);
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter1++;
if (counter1> colors.length - 1){
counter1 = 0;
}
iv1.setColorFilter(colors[counter1]);
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2 class
package com.example.chucky.svg;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
Activity_main xml
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text=" Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.895"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Go to Activity 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<ImageView
android:id="#+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
</android.support.constraint.ConstraintLayout>
activity_2 xml
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2">
<ImageView
android:id="#+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<ImageView
android:id="#+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="#drawable/ic_circle_e" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Activity2"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
You have colour codes in integer array int[] colors and selected position in counter variable. By the way set color to imageview | pass it via intent.
Try below code:
Intent i = new Intent(MainActivity.this,Activity2.class);
i.putExtra("COLOR_1",colors[counter1]);
i.putExtra("COLOR_2",colors[counter2]);
i.putExtra("COLOR_3",colors[counter3]);
startActivity(i);
Then in Activity2 get intent value & set to corresponding ImageView.
Add this code in onCreate()
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
iv1.setColorFilter(bundle.getInt("COLOR_1",0));
iv2.setColorFilter(bundle.getInt("COLOR_2",0));
iv3.setColorFilter(bundle.getInt("COLOR_3",0));
}
What you need to do is to pass an information to the second activity, extract, and make use of the information.
With an Intent Extra your goal can be achievable just like this:
First of all, declare a unique constant variable in your second activity that will hold information across the communication.
public static final String EXTRA_USER_ID = "userId";
Now, FirstActivity sending info:
String changeColorToBlack = " black"
Intent intent = new Intent(firstActivty.this, SecondActivity.class);
intent.putExtra(SecondActivity.EXTRA_COLOR_KEY, changeColorToBlack);
startActivity(intent);
SecondActivity can retrieve the information like this
String colorIdKey = getIntent().getStringExtra(EXTRA_COLOR_ID);
if (colorIdKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_COLOR_KEY");
}
//colorIdKey is your color
That is all. All you need is an Intent Extra to pass information between 2 activities
The easiest way to active what you required is pass the color codes to second activity as arguments
in Activity1.class do this
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
// pass the color value as bundle
intent.putExtra("color1", <color_code1>);
intent.putExtra("color2", <color_code2>);
intent.putExtra("color3", <color_code3>);
startActivity(intent); }
in Activity2.class
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Bundle extras = getIntent().getExtras();
if(extras != null){
String color1 = extras.getString("color1");
String color2 = extras.getString("color2");
String color3 = extras.getString("color3");
// set values to image views here
}
}
}
Official documentation here : https://developer.android.com/guide/components/activities/parcelables-and-bundles#kotlin

How Can I Display my Questionnaire and decide what to do based on that Android App

I am working on an App in Android Studios and what I want it to do is it should ask the user would you like to either to listen to Music or watch Videos after they decide what mood they are in. The problem is that when I click the mood it goes straight to the video and when I click the back button it then asks if they would like to music or videos. So it needs to be the other way around. I am working on this in Android Studios. I will paste the code down below.
This is the code for the MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button0 = (Button) findViewById(R.id.HappyButton);
Button button1 = (Button) findViewById(R.id.SadButton);
Button button2 = (Button) findViewById(R.id.MellowButton);
Button button3 = (Button) findViewById(R.id.MotivatedButton);
Button button4 = (Button) findViewById(R.id.AngryButton);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myintent = new Intent(MainActivity.this,theOptionsPage.class);
startActivity(myintent);
openHappy();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSad();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMello();
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMotivated();
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openAngry();
}
});
}
public void showMe(View v){
String button_text;
button_text = ((Button) v).getText().toString();
if(button_text.equals("HAPPY")){
Intent intent = new Intent(this,theOptionsPage.class);
startActivity(intent);
}
}
#Override
public void onClick(View view) {
}
public void openHappy(){
Intent intent = new Intent(this,YoutubeHappy.class);
startActivity(intent);
}
public void openSad(){
Intent sadintent = new Intent(this,YoutubeSad.class);
startActivity(sadintent);
}
public void openMello(){
Intent mellowintent = new Intent(this,YoutubeMellow.class);
startActivity(mellowintent);
}
public void openMotivated(){
Intent motivatedintent = new Intent(this,YoutubeMotivated.class);
startActivity(motivatedintent);
}
public void openAngry(){
Intent angryintent = new Intent(this,YoutubeAngry.class);{
startActivity(angryintent);
}
}
}
This is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.code.mohamedali.hackathon2018.MainActivity">
<Button
android:id="#+id/HappyButton"
android:layout_width="141dp"
android:layout_height="75dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="HAPPY"
android:onClick="showMe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/FeelingView" />
<Button
android:id="#+id/SadButton"
android:layout_width="141dp"
android:layout_height="75dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="SAD"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HappyButton" />
<Button
android:id="#+id/AngryButton"
android:layout_width="141dp"
android:layout_height="75dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="ANGRY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SadButton" />
<Button
android:id="#+id/MotivatedButton"
android:layout_width="141dp"
android:layout_height="75dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="MOTIVATED"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/AngryButton" />
<Button
android:id="#+id/MellowButton"
android:layout_width="141dp"
android:layout_height="75dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:text="MELLOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/MotivatedButton" />
<TextView
android:id="#+id/FeelingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="How Are You Feeling Today?"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Below is the OptionsPage Code and XML
public class theOptionsPage extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_page);
Button button1 = (Button) findViewById(R.id.MusicButton);
Button button2 = (Button) findViewById(R.id.VideoButton);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent in1 = new Intent( MainActivity.this , theOptionsPage.class);
// startActivity(in1);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Would You Like Muisc or Videos"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/MusicButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="80dp"
android:layout_marginTop="116dp"
android:text="Music"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/VideoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="72dp"
android:text="Videos"
app:layout_constraintBaseline_toBaselineOf="#+id/MusicButton"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
Hi if you see your code in button0 click event:
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myintent = new Intent(MainActivity.this,theOptionsPage.class);
startActivity(myintent);
openHappy();
}
});
See that after starting the theOptionsPage activty, immediately u call openHappy(); This is why it starts the "theOptionPage" and then immediately starts the "YoutubeHappy" activity.
So remove the openHappy() call inside the click listener and then start the activity from "theOptionPage"

Categories