Having various possible layouts - java

So I'm trying to create an activity that passes some values into another activity and then, depending on those values, chooses one of several different layouts.
I know that the values are passed through fine, but it won't display the layout (or any layout, it just stays on the previous screen). My code is:
public class ContactDisplay extends GetContact {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
String nameChosen = extras.getString("nameSpinner");
String addressChosen = extras.getString("addressSpinner");
if((nameChosen == "Michael") && (addressChosen == "Michaels Address")){
setContentView(R.layout.contact1_layout);
}
}
}
I've only set it up to with one of the layouts so far, but it should work if Michael and Michaels Address are chosen. Does anyone have any idea where I'm going wrong?

Use this code instead:
if((transportItemChosen.equals("Michael")) && (locationItemChosen.equals("Michaels Address"))){
setContentView(R.layout.contact1_layout);
}
In Java you want to use the equals() function when comparing strings not the == operator.

The first thought is that I would try to not use hard coded values for extras because you can misspell something and it is very easy to get stuck ... if that is not the problem then try to make some logs and see what exactly you receive in transportItemChosen and locationItemChosen
EDIT:
And yes, to compare two Objects you have to use equals() method not the == operator.

Related

How do I conditionally enable a TextView based on another TextView's contents?

I'm new to Android Studio, and programming an application.
I'm experimenting with it, and I want to know how you can enable (for example) Textview2 if Textview1 contains something.
I tried this code but it didn't work:
TextView2.setEnabled(false);
if (TextView1.length() > 0) {
TextView2.setEnabled(true);
}
Thanks in advance!
Welcome to SO, You can use contains method for check if your string has specific text.
if (TextView1.getText().toString().contains("yourText")) {
TextView2.setEnabled(true);
}
for checking if Textview1 has text. you can use method for checking:
//usage:
if(TextView1.getText().toString() != null || TextView1.getText().toString().length() > 0) //then do what you want.
be aware of null pointer exception by checking textview have a text.
I got the answer to my Question!
i did it like this:
if (TextView1.getText().toString().isEmpty()) {
//Do what you want here
}

Cleanest way to create an object, passing parameters from ArrayList of EditText

I've created an ArrayList with my EditText inputs for Android. This is in main activity
First i check if any are empty
for(EditText i: inputs){
if(i.getText().toString() == null){
empty = true;
}
then
if (!empty){
for(EditText i: inputs) {
String input = i.getText().toString();
Person gen = new Person(inputFirstName.getText().toString(),
inputLastName.getText().toString(), inputMaiden.getText().toString(),
inputBirth.getText().toString(), inputBrand.getText().toString());
}else{ createAlertDialog("Alert", "One or more inputs are empty"); }
I'm aware in its current state it won't work, and just create 5 instances of the Person object. That is the layout of the constructor, and I want to find out the cleanest way to construct the object.
The repetition of .getText().toString() is dirty. Surely there is a cleaner way to do this using the ArrayList properties
It may appear to be an advanced approach but you may find Android DataBinding useful.
http://developer.android.com/tools/data-binding/guide.html

Making a TextView visible at runtime in java (Android)

I have two activities; the first activity starts the second one with some data passed through the intent.
Intent i = new Intent(this,BActivity.class);
i.putExtra("identify", "c2f");
startActivityForResult(i, 1);
In the second activity, I want to make some TextViews/EditTexts visible (which are initially set to invisible) based on the information passed from the first activity.
Here's the code for that:
tv1 = (TextView)findViewById(R.id.textView2);
tv2 = (TextView)findViewById(R.id.textView3);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.send_result);
Bundle extras = getIntent().getExtras();
String identifier = extras.getString("identify");
if(identifier == "c2f")
{
tv1.setVisibility(0);
tv1.setText("Celcius");
et1.setVisibility(0);
}
else if(identifier == "f2c")
{
tv1.setVisibility(0);
tv1.setText("Fahrenheit");
et1.setVisibility(0);
}
else if(identifier == "currency")
{
tv1.setVisibility(0);
tv1.setText("Amount");
tv2.setVisibility(0);
tv2.setText("Conv. Rate");
et1.setVisibility(0);
et2.setVisibility(0);
}
Now when the second activity starts, none of these TextViews or EditTexts seem to get visible!
identifier (string) holds the correct value passed from first activity and it even goes into the if conditions, but it doesn't make any view visible.
Am I making any mistake in trying to make these views visible?
Use .equals instead of == to string comparison. You can also use the variable after the quoted string to avoid nullpointer. And you can use TextView.VISIBLE, it's a constant to get it visible.
if("c2f".equals(identifier))
{
tv1.setVisibility(TextView.VISIBLE);
tv1.setText("Celcius");
et1.setVisibility(TextView.VISIBLE);
}
Simply use the View's constants for this.
your_view.setVisibility(View.VISIBLE);
This will make your View visible.
your_view.setVisibility(View.INVISIBLE);
This will make it invisible but still with the layout visible (basically, the space where it goes remains untouched)
your_view.setVisibility(View.GONE);
This will make your View disappear, like it never existed!
As pointed by giacomoni, please use equals for String comparison. Here is a link to explain why.
http://javarevisited.blogspot.in/2012/03/how-to-compare-two-string-in-java.html
Also, try using the standard View.VISIBLE etc constants for showing and hiding views. They are much more easy to use and understand. Happy coding. :)

android setText() multiple textViews

Hello I have 3 Type of textViews in my Layout with same Dynamic text but there is only one Difference between them i.e. . Postion . I wanted to show and hide them according to Button Click . but the problem problem occurs when I have to write the same code for five Different TextViews with only one TEXT . Kindly Suggest me Efficient way which could reduce the number of line in my JAVA code.
temperature.setText(temp);
txt.setText(Name);
descptxt.setText(descp);
temperature3.setText(temp);
txt3.setText(Name);
descptxt3.setText(descp);
temperature4.setText(temp);
txt4.setText(Name);
descptxt4.setText(descp);
the number of TextViews will be increase in Future . I am worried about writing same Boiler Plate Code again and Agian
create a function like this:
void setText(String name,String temp,String descp){
for(int i=0;i<txt.size();i++){
temperature.get(i).setText(temp);
txt.get(i).setText(Name);
descptxt.get(i).setText(descp);
}
}
Also instead of creating different variables for each textview create an array of it.
ArrayList<TextView> temperature=new ArrayList<TextView>();
temperature.add((TextView)findViewById(<id>));
//Same for rest of them
And when you want to change text:
setText("","","");
Store each objects in a array ie. temperature objects in a separate array. txt objects in a separate array and descp objects in a separate array.
create a method like
public void setTexts(String temp,String name,String desc){
for(TextView temperature : temperatureArray){
//Change for loop declaration according to your code
//set text for temperature
temperature.setText(temp);
}
for(TextView txt : txtArray){
//Change for loop declaration according to your code
//set text for txt
txt.setText(name);
}
for(TextView descp : descpArray){
//Change for loop declaration according to your code
//set text for descp
descp.setText(desc);
}
}
If your TextViews are logically grouped together, you might want to create one compound View containing all of them:
public class TemperatureView extends ViewGroup { //or whichever layout you use
private TextView textView1;
private TextView textView2;
private TextView textView3;
public void setData(TemperatureData data) {
textView1.setText(...);
textView2.setText(...);
textView3.setText(...);
}
}
In my opinion, this is the most elegant solution in such situation.
You can use alternative in which u can apply loop over childcount of parent layout instead of finding using findViewById.and then using instance of operator ,you can find textviews on which u want to perform the task.

If statement with booleans android

I have been having a problem with a Boolean in an if statement all day and it is really starting to irritate me now!! I have looked at other Android threads on here and the solutions just don't seem t work.
My code started off like this:
public class MainActivity extends Activity
{
public static boolean isSignedIn = false;
public final static String USERNAME_MESSAGE = "com.example.libnoise.MESSAGE";
Button btnSignIn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (Button) findViewById(R.id.btnSignIn);
Intent intent = getIntent();
String message = intent.getStringExtra(PlayZone.USERNAME_MESSAGE);
if(isSignedIn == false))
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
}
Then I had a thought that made it's not like other languages and I only need one "=" sign so I had it as this:
if(isSignedIn = false)
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
That didn't work and that's when I started looking online, after finding a previous thread on here changed it to the following:
if("false".equals(isSignedIn))
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
Now that doesn't look right to me in the first place but hoped it would work and it didn't.
As this is the MainActivity it loads first however since I added all this, the app crashes before it will even load when I take out the if statement it work as expected.
Any ideas?
This
if (isSignedIn == false)
is perfectly correct. (You could also write if (!isSignedIn), but that's just a matter of style.)
Note that, since you never change the value of isSignedIn (at least not in the code you have shown us), it will always be false.
i think you can simply use
if(!isSignedIn)
{
btnSignIn.setText("SignIn");
}
else
{
btnSignIn.setText(message);
}
the way you followed is also correct i didn't find any mistake in except you are using extra bracket in condition if(isSignedIn == false))
If statements with boolean are same how you do it in Java, == is the right way to compare
The problem in your code is extra bracket
if (isSignedIn == false))
Just to deviate from the question, but point out what is possibly your problem, your null pointer could be because you are accessing a UI object that may well not be ready to have it's text set yet.
While some API versions cope fine with what you're doing, I've found many device/API combos simply aren't ready to have anything changed from what's in the xml until onStart. The general guidance is to load data in onCreate, but not start doing anything until onStart.

Categories