How to fix this margin gap? - java

I am creating an Faq page using TextViews, So the textviews become visible and hide automatically when clicked, but this was overlapping the upcoming question so I researched and found marginlayoutparams solution but this code won't reset back when clicked again.
Initial state:
After I click Fare Charges:
After I click on it again:
The code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class faq extends AppCompatActivity {
TextView mfaq,mAns,mfaq2,mAns2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq1);
mAns=findViewById(R.id.ans);
mfaq2=findViewById(R.id.faq2);
mAns2=findViewById(R.id.ans2);
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns.getVisibility()== View.VISIBLE){
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
-400, //setting it back to 0 this part isnt working
marginParams.rightMargin,
marginParams.bottomMargin);
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
400, //only changing top margin
marginParams.rightMargin,
marginParams.bottomMargin);
}
});
mfaq2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns2.getVisibility()== View.VISIBLE){
mAns2.setVisibility(View.GONE);
}
else
mAns2.setVisibility(View.VISIBLE);
}
});
}
}
I don't know how to set it back to initial state like first image. Please help. Note- I am a beginner so please explain me so I can learn and understand.

Can you print the xml ? try with "wrap_content" in parent layout

You can use Cardview to encapsulate your Faq element :
https://developer.android.com/guide/topics/ui/layout/cardview
Then change the visibility of the component that you want to hide as gone and set a listner to change the visibility if the textView (or your cardView ) is clicked
Textview label = (TextView ) myPosts.findViewById(R.id.cancel);
cancelButton.setOnClickListener(view->{
if(newPostCard.getVisibility() == View.VISIBLE){
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.GONE);
newPostButton.setVisibility(View.VISIBLE);
}
else{
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.VISIBLE);
newPostButton.setVisibility(View.GONE);
}
});

The way I got around to this problem was to add a linear layout to the questions and answer fields. here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#android:color/holo_orange_dark"
tools:context=".faq">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="300dp"
tools:context=".faq">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/Welcome"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="36sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/frequentlyaskedquestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/faqs"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/clickonthem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/clickonthem"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/frequentlyaskedquestions" />
<LinearLayout
android:layout_width="409dp"
android:layout_height="294dp"
android:layout_marginTop="28dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clickonthem">
<TextView
android:id="#+id/faq1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
<TextView
android:id="#+id/faq2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Related

Android app keeps stopping while building using android studio

When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing,
It show me 'app keeps stopping'
Code in MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Intent outIntent;
EditText edtPhone;
EditText edtMessage;
Button btnNext;
String tempText="";
public static final String PHONE = "PHONE";
public static final String MESSAGE = "MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
outIntent = new Intent(MainActivity.this, ActivityTwo.class);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMessage = (EditText) findViewById(R.id.edtMessage);
tempText = edtPhone.getText().toString();
outIntent.putExtra(PHONE,tempText);
tempText = edtMessage.getText().toString();
outIntent.putExtra(MESSAGE,tempText);
startActivity(outIntent);
}
});
}
public void closeMethod(View view) {
finish();
}
}
ActivityTwo.java
package com.example.fir;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
Intent incomingIntent;
TextView txtPhone;
TextView txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
incomingIntent = getIntent();
txtPhone = (TextView) findViewById(R.id.txtPhone);
txtMessage = (TextView) findViewById(R.id.txtMessage);
txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
Activitytwo.xml
////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping".
I cannot get the text and am unsure of why the app keeps crashing.
you have defined your Edit texts in the first activity with id's named
edtPhone
edtMessage
but there was no edtPhone or edtMessage in your xml.
this issue also repeats in your second activity
you have defined txtPhone and txtMessage
but again there are no such ids in your second activity XML.
keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you

How to create a component and place on xml page using a button?

Basically, this is the code I have right now...
ViewCard activity
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import com.team46.stubank.R;
public class DisplayCards extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_cards);
}
public void viewCard(View view){
Intent intent = new Intent(this, ViewCard.class);
startActivity(intent);
}
#SuppressLint("SetTextI18n")
public void toggleDetails(View view){
Switch simpleSwitch = findViewById(R.id.switch1);
TextView tv = findViewById(R.id.textView4);
TextView tv1 = findViewById(R.id.textView);
TextView tv2 = findViewById(R.id.textView5);
TextView tv3 = findViewById(R.id.textView6);
if (simpleSwitch.isChecked()){
tv.setText("card.getCardNumber");
tv1.setText("user.getName");
tv2.setText("card.getSortCode");
tv3.setText("card.getAccountNumber");
//needs to call getter methods from card and user classes
}
else{
tv.setText("0000-0000-0000-0000");
tv1.setText("Joe Bloggs");
tv2.setText("11-11-11");
tv3.setText("3411-4885-6324-7195");
}
}
}
xml page:
?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:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".card_activities.DisplayCards">
<androidx.cardview.widget.CardView
android:layout_width="362dp"
android:layout_height="172dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="viewCard"
app:cardBackgroundColor="#6C4095"
app:cardCornerRadius="20dp"
app:cardElevation="15dp"
app:cardMaxElevation="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="viewCard">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/card_number_default"
android:textColor="#FFFFFF"
android:textSize="22sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/name_default"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="59dp"
android:layout_height="47dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/mastercard_small" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/roboto_medium"
android:onClick="toggleDetails"
android:text="#string/toggle_card_details"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="14dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/sortcode_default"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/account_number_default"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="79dp"
app:cardBackgroundColor="#6d0095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/cards_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/cards_menu"
android:textColor="#FFFFFF"
android:textSize="36sp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
The code here is basically a screen with a card, made up of various components, in order to resemble a credit/debit card containing the user's information. Similar to this. What I want to do, is to allow the user to add multiple "cards" by clicking onto a button. I'm not sure how to go about this exactly so any help would be much appreciated.
You have 3 (I think) basic ways to do it - they all involve pulling your single card layout into its own XML file:
Use the layout as a list item in a ListView, or (preferably) a ViewHolder in a RecyclerView. Each item gets a card layout, and you set the details on the things - the actual data for each card is stored in a list and you just display the appropriate stuff
Create a custom View (compound controls really, just bundling some Views together) which inherits from something like FrameLayout, and inflate your XML layout into that. That way each card is its own object and you can use them as widgets in any other layout
Make each card a Fragment which is a little simpler than creating a View (since it handles all the layout inflation stuff for you) but is probably a bit harder to work with in terms of adding them to a layout.
Really it depends what you want - a simple list with a RecyclerView might be best, if you want flexibility you could make the custom View and stick one of those in a basic layout and use that as your ViewHolder layout

I am having problems programming java on aide app

The app is supposed to call one screen from another and use the other screen's layout components. Whenever I try to call a component, such as ImageButton, it says that member id is unknown, I told my teacher and he says that it shouldn't happen. I have tried renaming the components and even made a new project.
How can I fix it?
the error is in R.id
package com.movil.ejemploventanas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class DatosPersonales extends Activity {
EditText vEdtNom,vEdtEdad;
ImageButton vBtnOrigen,vBtnSantuario;
Intent intnEnvio;
Bundle bndContenedorEnvio;
String strNom;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.datospersonales);
vEdtNom=(EditText)findViewById(R.id.edtNom);
vEdtEdad=(EditText)findViewById(R.id.edtEdad);
vBtnOrigen=(ImageButton)findViewById(R.id.btnOrigen);
vBtnSantuario=(ImageButton)findViewById(R.id.btnSantuario);
intnEnvio=new Intent(DatosPersonales.this,Caracteristicas.class);
bndContenedorEnvio=new Bundle();
vBtnOrigen.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad", vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","1");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
vBtnSantuario.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad",vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","2");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtLetrero"
style="style/titulo"
android:layout_centerHorizontal="true"
android:text="#string/letMariposa"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLetrero"
android:id="#+id/linearlayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp">
<TextView
android:id="#+id/txtNom"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="string/nom"/>
<EditText
android:id="#+id/edtNom"
style="style/datos"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:id="#+id/linearlayout2"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/txtEdad"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/edad"
android:textAlignment="gravity"/>
<EditText
android:id="#+id/edtEdad"
style="style/datos"
android:layout_width="220dp"
android:ems="10"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/origen"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
android:id="#+id/textView4"
style="style/letreros"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="#id/textView4"
android:layout_marginLeft="80dp"
android:id="#+id/btnOrigen"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sant"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/btnOrigen"
android:layout_marginLeft="120dp"
android:id="#+id/textView5"
style="style/letreros"/>
<ImageButton
android:id="#+id/btnSantuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_marginLeft="180dp"/>
</RelativeLayout>

How to use spinners in android?

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

How to make Views with an Invisible attribute 'Visible' after clicking a button

I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}

Categories

Resources