I am trying to use a rating bar in Android Studio but it just isn't appearing in my app.
I am not receiving any errors and can't understand what I am doing wrong.
Has anyone else experienced this issue?
Here is my java file;
package com.example.myopenlounge;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.analytics.FirebaseAnalytics;
public class RatingActivity extends AppCompatActivity {
private RatingBar mRatingBar;
private TextView mRatingScale;
private EditText mFeedback;
private Button mSendFeedback;
private int star1 = 0;
private int star2 = 0;
private int star3 = 0;
private int star4 = 0;
private int star5 = 0;
public static final String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mFeedback = (EditText) findViewById(R.id.etFeedback);
mSendFeedback = (Button) findViewById(R.id.btnSubmit);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
mRatingScale.setText(String.valueOf(rating));
switch((int) ratingBar.getRating()){
case 1:
mRatingScale.setText("Awful!");
star1++;
break;
case 2:
mRatingScale.setText("Not great...");
star2++;
break;
case 3:
mRatingScale.setText("Good");
star3++;
break;
case 4:
mRatingScale.setText("Great");
star4++;
break;
case 5:
mRatingScale.setText("Fantastic!");
star5++;
break;
default:
mRatingScale.setText("");
}
}
});
mSendFeedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mFeedback.getText().toString().isEmpty()){
Toast.makeText(RatingActivity.this, "Please fill in feedback text box",
Toast.LENGTH_LONG).show();
}
else{
mFeedback.setText("");
mRatingBar.setRating(0);
Toast.makeText(RatingActivity.this, "Thank you for sharing your feedback :) ",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
and then my xml file looks like this;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".RatingActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="We hope you enjoyed your meal with us today"
android:textSize="18sp"
android:textStyle="italic" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:rating="5"/>
<TextView
android:id="#+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Awesome. I love it"
android:textSize="16sp"
android:textStyle="bold"/>
<EditText
android:id="#+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:hint="Tell us a little about why you have given us this rating"
android:gravity="top" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Send feedback"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="46dp" />
</LinearLayout>
The text "We hope you enjoyed your meal with us today" is all that appears on the screen when the onCreate method is called. I can't seem to find a reason why.
This is because you sets orientation horizontal
android:orientation="horizontal"
you need to update it to vertical
android:orientation="vertical"
Here is your updated 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RatingActivity"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="We hope you enjoyed your meal with us today"
android:textSize="18sp"
android:textStyle="italic" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:rating="5"/>
<TextView
android:id="#+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Awesome. I love it"
android:textSize="16sp"
android:textStyle="bold"/>
<EditText
android:id="#+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:hint="Tell us a little about why you have given us this rating"
android:gravity="top" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Send feedback"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="46dp" />
</LinearLayout>
<LinearLayout
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=".RatingActivity"
android:orientation="vertical">
...
Related
i'm beginner in android coding and im trying to switch activities using Intent class
but it do nothing on all cases (Putting data or inputting data) can someone resolve my problem ? here is my layout.xml :
i already added the other activity in the manifiest xml
i think the problem is in where i get data from the spinner or from the radioButton
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Formulaire d'inscription"
android:textAlignment="center"
android:textSize="30dp"
android:textColor="#FF0000"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Nom" />
<EditText
android:id="#+id/txtNom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Saisir votre nom"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Prenom :" />
<EditText
android:id="#+id/txtPrenom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Saisir votre prénom"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Email :" />
<EditText
android:id="#+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Saisir votre email"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Mot de passe" />
<EditText
android:id="#+id/txtPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Saisir le mot de passe"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Confirmation :" />
<EditText
android:id="#+id/txtPassConfirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Confirmation"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Genre" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Homme"
android:checked="true">
</RadioButton>
<RadioButton
android:id="#+id/rbf"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Femme">
</RadioButton>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250px"
android:layout_height="wrap_content"
android:text="Pays :" />
<Spinner
android:id="#+id/spnPays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/pays" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="INSCRIRE"
/>
</LinearLayout>
</LinearLayout>
and my java code is :
package com.fach.app1;
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;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static String nomInscrit;
public static String prenomInscrit;
public static String emailInscrit;
public static String passwordInscrit;
public static String payInscrit;
public static String genreInscrit;
Button btnSign ;
EditText nom ;
EditText prenom ;
EditText email ;
EditText password ;
EditText passConf;
Spinner spnPay;
RadioGroup genre;
RadioButton gen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSign = findViewById(R.id.btnSignUp);
nom = findViewById(R.id.txtNom);
prenom = findViewById(R.id.txtPrenom);
email = findViewById(R.id.txtEmail);
password = findViewById(R.id.txtPass);
passConf = findViewById(R.id.txtPassConfirm);
spnPay = findViewById(R.id.spnPays);
genre = findViewById(R.id.radiogroup);
int selectedId = genre.getCheckedRadioButtonId();
gen = findViewById(selectedId);
btnSign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nomInscrit = nom.getText().toString();
prenomInscrit = prenom.getText().toString();
emailInscrit = email.getText().toString();
passwordInscrit = password.getText().toString();
payInscrit = spnPay.getSelectedItem().toString();
genreInscrit = gen.getText().toString();
if(nom.getText().equals("") || prenom.getText().equals("") || email.getText().equals("") || password.getText().equals("")){
Toast.makeText(MainActivity.this,"Veuillez remplir tous les champs", Toast.LENGTH_SHORT);
}
else if(!password.getText().equals(passConf.getText())){
Toast.makeText(MainActivity.this,"Les mots de passe doivent etre identique", Toast.LENGTH_SHORT);
}
else{
switchActivities();
}
}
});
}
private void switchActivities() {
Intent ActivityIntent = new Intent(this, DataActivity.class);
startActivity(ActivityIntent);
}
}
Thanks for reading and resolving my problem
You need to pass the data using the ActivityIntent you have created in switchActivity function amd then get that data in your second Activity from getIntent().getString(key) function.
Please refer to this,
How do I pass data between Activities in Android application?
I am try to save my roll no and name in main activity but when my screen is rotated the activity data has been lose ,
finally i fixed it , below i adding source code
consider any layout , here i am using this layout structure
In activity_xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/rollnoID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Rollno : "
android:textSize="25dp" />
<EditText
android:hint="Roll Number"
android:id="#+id/rollnoID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="number"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nameID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Name : "
android:textSize="25dp" />
<EditText
android:hint="Student Name"
android:id="#+id/nameID_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:ems="8"
android:inputType="textCapWords"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<Button
android:onClick="saveData"
android:id="#+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save this Data"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:hint="~RollNo~"
android:textColor="#color/colorPrimary"
android:id="#+id/display_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="~Name~"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
In MainActivity :
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String KEY_ROLL = "rollno_key";
private static final String KEY_NAME = "name_key";
int rollno;
String name;
EditText roll_et , name_et;
TextView roll_tv , name_tv;
Button saveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll_et = (EditText) findViewById(R.id.rollnoID_edit);
name_et = (EditText) findViewById(R.id.nameID_edit);
roll_tv = (TextView) findViewById(R.id.display_rollno);
name_tv = (TextView) findViewById(R.id.display_name);
if (savedInstanceState != null){
String save_RollNO = savedInstanceState.getString(KEY_ROLL);
roll_tv.setText(save_RollNO);
String save_Name = savedInstanceState.getString(KEY_NAME);
name_tv.setText(save_Name);
} else {
Toast.makeText(getApplicationContext(),"Entry your data",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSaveInstanceState(Bundle saveData) {
saveData.putString(KEY_ROLL , roll_et.getText().toString());
saveData.putString(KEY_NAME , name_et.getText().toString());
super.onSaveInstanceState(saveData);
}
public void saveData(View v){
roll_tv.setText(roll_et.getText().toString().trim());
name_tv.setText(name_et.getText().toString().trim());
}
}
I am trying to create radio buttons that allows the users to select one of the three options. Once selected, the option should have a border, while the others do not.
Here is an image of what I am trying to create:
I'm not sure how to go about this. Any suggestions are greatly appreciated.
You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected.
selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached. here's the structure:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card
so here's the code:
activity_main.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"
android:background="#color/parentBackground"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="#+id/public_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/public_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="visible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card1Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_public" />
<TextView
android:id="#+id/card1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card1Icon"
android:text="Public Profile"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card1Label"
android:layout_alignStart="#id/card1Label"
android:text="Everyone can see your account. \nIncluding non followers."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/friends_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/public_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/friends_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card2Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_friends" />
<TextView
android:id="#+id/card2Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card2Icon"
android:text="Friends Only"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card2Label"
android:layout_alignStart="#id/card2Label"
android:text="Connections nly. Only your friends \nand friends of friends can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/private_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/friends_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/private_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card3Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_private" />
<TextView
android:id="#+id/card3Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card3Icon"
android:text="Private"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card3Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card3Label"
android:layout_alignStart="#id/card3Label"
android:text="Only you can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public MaterialCardView cPublic, cFriends, cPrivate;
public FrameLayout sPublic, sFriends, sPrivate;
public ArrayList<MaterialCardView> cards;
public ArrayList<FrameLayout> strokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
strokes = new ArrayList<>();
cPublic = findViewById(R.id.public_card);
cFriends = findViewById(R.id.friends_card);
cPrivate = findViewById(R.id.private_card);
cards.add(cPublic);
cards.add(cFriends);
cards.add(cPrivate);
sPublic = findViewById(R.id.public_stroke);
sFriends = findViewById(R.id.friends_stroke);
sPrivate = findViewById(R.id.private_stroke);
strokes.add(sPublic);
strokes.add(sFriends);
strokes.add(sPrivate);
cPublic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPublic);
}
});
cFriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cFriends);
}
});
cPrivate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPrivate);
}
});
}
public void setChecked(MaterialCardView selected) {
int index = cards.indexOf(selected);
FrameLayout stroke = strokes.get(index);
stroke.setVisibility(View.VISIBLE);
for (FrameLayout s : strokes) {
if (!s.equals(stroke)) {
s.setVisibility(View.INVISIBLE);
}
}
}
public MaterialCardView checkedCard() {
int index = 0;
for (FrameLayout s : strokes) {
if (s.getVisibility() == View.VISIBLE) {
index = strokes.indexOf(s);
}
}
return cards.get(index);
}
}
Hope it could help you (:
I have a simple LinearLayout with several buttons, whos state color/text change based on the state of an underlying service, thats working fine.
However the buttons, are only clickable on the right corner ???
The button allSystemServicesToggleButton which i have included the implementation for in this post and only be clicked on the right side/right corner???
Here is my fragment xml layout & Actual screen shot with “Show Layout bounds” set to true:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="All System Services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/allSystemServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="#string/shutdown_all_services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/shutdownAllServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/shutdown"
android:layout_weight="1"
android:layout_width="0dp"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Networks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Bluetooth Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/btServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--TODO: get requirements for showing paired devices & pairing devices-->
<TextView
android:id="#+id/textPairedText"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Paired Bluetooth Devices"
android:textColor="#000000"
android:singleLine="true"
android:textSize="20sp"
android:layout_width="0dp"
/>
<TextView
android:id="#+id/textViewNumberOfConnectedDevices"
android:layout_height="wrap_content"
android:text="0"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btDevicesToggleButton"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="2"
android:text="Pair"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="MQTT Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/MQTTserviceToggleButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:text="#string/stopped" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Location Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider3"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="GPS"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/gpsServiceToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Command Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider4"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Voice Recognition"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/voiceRecognitionToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
/>
</LinearLayout>
Relevant fragment java:
package x.core.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;
public class ServicesFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "ServicesFragment";
public static ServicesFragment newInstance() {
return new ServicesFragment();
}
private static Button btServicesToggleButton;
private static Button mqttServicesToggleButton;
private static Button gpsServiceToggleButton;
private static Button voiceServiceToggleButton;
private static Button allServiceToggleButton;
private static String stopped = null;
private static String running = null;
private static int runningColorId, stoppedColorId = -1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container, false);
btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
stopped = getResources().getString(R.string.stopped);
running = getResources().getString(R.string.running);
runningColorId = getResources().getColor(R.color.runningServiceColor);
stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
allServiceToggleButton.setEnabled(true);
allServiceToggleButton.setClickable(true);
allServiceToggleButton.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.allSystemServicesToggleButton:
if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.restarting_services),Toast.LENGTH_SHORT).show();
//restartingServices();
}
else
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.all_already_running),Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/activity_main_tabLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity.java, I only included relevant code:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
private static final int CHECK_BT_CODE = 1;
private static final int CHECK_TTS_CODE = 2;
//global boolean flags that will communicate the state of the system at all times
//bluetooth related flags
public boolean isBleSupported = false;
public boolean isBluetoothEnabled = false;
public boolean accessBluetoothManager= false;
public boolean nearbyDevices = false;
//configuration data related
public boolean isConfigurationLoadedCorrectly = false;
//text to speech related
public boolean isTextToSpeechSupported = false;
private Context context = null;
private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
private ViewPager mainViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity started!!");
context = this;
setContentView(R.layout.activity_main);
MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());
mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
mainViewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.activity_main_tabLyout);
tabLayout.setupWithViewPager(mainViewPager );
}
}
The adapter for my fragments, FragmentStatePagerAdapter:
package x.core.views;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;
public class MainActivityViewPager extends FragmentStatePagerAdapter {
public MainActivityViewPager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment returnFragment;
switch(position) {
case 0:
returnFragment = ServicesFragment.newInstance();
break;
case 1:
returnFragment = BiometricsFragment.newInstance();
break;
default:
return null;
}
return returnFragment;
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position) {
case 0:
title = "Services";
break;
case 1:
title = "Biometrics";
break;
default:
return null;
}
return title;
}
}
Thanks
only for corner clicking use this kind of logic
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/colorAccent" />
<TextView
android:id="#+id/tvtttt"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="right"
android:background="#F00" />
</FrameLayout>
I created an app that dynamically adds an edittext field to the view by clicking an Add button and deletes it by clicking a delete button. I created a separate xml file called field.xml that is called up when a new edittext field is added to the view. But the delete button does not remove this edittext field when clicked. What am I missing in the code I have tried most options but to no avail.
Here is the main java file
SchoolSearchSetup.java
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
The main XML file
activity.school_search_setup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the Field.xml file for the edit text to be added. Both the field.xml and the activity.school_search_setup.xml share the same IDs.
Field.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:id="#+id/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
Have you tried using
setVisibility(View.GONE);
Update:
Try this code block:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(80, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});