Get Chip Text on click chip - java

I am new to android and trying to play with chips. I have chip group like this in xml
<com.google.android.material.chip.ChipGroup
android:padding="10dp"
android:id="#+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:chipSpacing="10dp">
</com.google.android.material.chip.ChipGroup>
I am dynamically adding chip like this in it.
Chip chip = new Chip(getLayoutInflater().getContext());
for(String genre : tags_text) {
chip.setText(genre);
tags.addView(chip);
}
Adding chip is working fine. Now I want get chip text of clicked chip. I am trying to get it like this
chip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int chipsCount = tags.getChildCount();
String TagText ="";
int i = 0;
while (i < chipsCount) {
Chip chip = (Chip) tags.getChildAt(i);
if (chip.isChecked() ) {
TagText = chip.getText().toString();
}
i++;
};
Log.e("CHIPS", TagText+"-OK");
}
});
But I am getting empty text always. let me know if someone can help me for solve it.
Thanks!

You can use:
chip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = ((Chip) view).getText().toString();
//...
}
});

Related

Deletion an element of an `ArrayList` and setting the info to a layout in `MainActivity` by the command of a custom `Dialog`

I am new working with this customized Dialog in android. I may not be able to clarify my problem properly by writing, So it humble request to understand what i want from given code.
In MainActivity i have Button to delete an element of an ArrayList and setting the info from the ArrayList to a Layout.
Code from CustomDialog:
public class PermissionToDelete extends Dialog implements View.OnClickListener {
public Button btnYes, btnNo;
public TextView txtPermToDelete;
public PermissionToDelete(Context context){
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.permission_delete);
initialize();
}
public void initialize(){
btnYes = findViewById(R.id.btnYes);
btnNo = findViewById(R.id.btnNo);
txtPermToDelete = findViewById(R.id.txtPermToDelete);
btnYes.setOnClickListener(this);
btnNo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnNo){
dismiss();
}
else if(v.getId() == R.id.btnYes){
MainActivity.deleteOrErase = "delete";
dismiss();
}
}
}
There is a static String variable in MainActivity named deleteOrErase.
From dialog it returns the string delete if Button Yes is clicked.
Code from MainActivity:
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
String text = tv.getText().toString();
PermissionToDelete permissionToDelete = new PermissionToDelete(MainActivity.this);
permissionToDelete.show();
if(deleteOrErase.equals("delete")){
String index = "";
for(int i = 1; text.charAt(i) != ')'; i++){
index += text.charAt(i);
}
tasksList.remove(Integer.parseInt(index) - 1);
File rootFile = new File(rootFolder);
if(rootFile.exists()){
String[]entries = rootFile.list();
for(String s: entries){
File currentFile = new File(rootFile.getPath(),s);
currentFile.delete();
}
rootFile.delete();
}
dailyTasksLayout.removeAllViews();
makePrimaryFileAndFolder();
saveDataToFile();
setTaskList();
deleteOrErase = "";
}
But the problem is before button from dialog is clicked, if(deleteOrErase.equals("delete")) is encountered when the value of deleteOrErase is still null.
Therefore data isn't deleted at the first click.
Its need second click to delete, because now the value of deleteOrErase is "delete".
How can i delete it from first click?
Or is there any other way???
thanks dear honorable members! <3
(I don't know either i could clarify my problem or not.
Even i can't understand from mty writings :P )

Changing Background color for TextView on click in Android using Java

In my Android app, I have category titles shown in picture. When I click the title below content will be changed depending upon the category title. At that same time title, the background color should change but other title background colors should remain the same. How to do that in Android Studio using Java?
You can change the background colors of your TextViews like this:
yourTextViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yourContentTextView.setBackgroundColor(getResources()
.getColor(R.color.yourColor));
}
});
For every view, there is a setBackgroundColor method
yourView.setBackgroundColor(Color.parseColor("#ffffff"));
Try like below:
...
title1, title2, title3, selectedTitle;
...
title1.setOnClickListener(v -> {
updateTitleBackground(title1);
});
title2.setOnClickListener(v -> {
updateTitleBackground(title2);
});
title3.setOnClickListener(v -> {
updateTitleBackground(title3);
});
...
private void updateTitleBackground(title) {
if(selectedTitle != null) {
selectedTitle.setBackgroundColor(Color.WHITE);
}
selectedTitle = title;
title.setBackgroundColor(Color.RED);
}
...
Here are instruction and code for you to complete your target -
String titleContent1 = "Content 1", titleContent2 = "Content 2", titleContent3 = "Content 3";
textView1.setOnClickListener(v -> {
changeBackgroundColorAndTitle(titleContent1, getResources().getColor(R.color.red));
});
textView2.setOnClickListener(v -> {
changeBackgroundColorAndTitle(titleContent2, getResources().getColor(R.color.green));
});
textView3.setOnClickListener(v -> {
changeBackgroundColorAndTitle(titleContent3, getResources().getColor(R.color.white));
});
private void changeBackgroundColorAndTitle(String titleContent, int color) {
selectedTitle = title;
contentTextView.setBackgroundColor(color);
}
Try like this
// initialize your views inside your onCreate()
title1 = findViewById("yourtextviewid");
title2 = findViewById("yourtextviewid");
title3 = findViewById("yourtextviewid");
title1.setOnClickListener(this);
title2.setOnClickListener(this);
title3.setOnClickListener(this);
In your onClick method
#Override
public void onClick(View view) {
if(view == title1)
// here i assumed yourContentView and contenTextview you should use yours.
yourContentView.setBackgroundColor(Color.RED);
contentTextView.setText("Title1");
}else if(view == title2){
yourContentView.setBackgroundColor(Color.GREEN);
contentTextView.setText("Title2");
}else if(view == title3){
yourContentView.setBackgroundColor(Color.BLACK);
contentTextView.setText("Title3");
}
``

Java-BackGround color never applying

I'm trying to change the background color of buttons programatically in my Android application, but it does not work.
Whatever methods I am using work fine, (i.e text size, text, text color) but I cannot manage to change the background color of the buttons.
What am I doing wrong ?
XML description of the button :
<Button
android:id="#+id/jeton103"
android:layout_column="0"
android:layout_row="28"
android:layout_height="88dp"
android:layout_width="88dp"
android:backgroundTint="#2aa17b" />
Java code :
int i;
int j = 103;
Button changer;
private EditText changerlettres;
String choix;
public void BtnClick() {
changer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Thrown out letters
choix = changerlettres.getText().toString(); // EditText
for (int i = 0, n = choix.length(); i < n; i++) {
char letter = choix.charAt(i);
final Button jetoninvisible;
int ressourceId2 = getResources().getIdentifier("jeton"+j, "id", getPackageName());
jetoninvisible = (Button) findViewById(ressourceId2);
// jetoninvisible.getBackground().setAlpha(200); // Not working
// jetoninvisible.setBackgroundColor(Color.WHITE); Not working
// jetoninvisible.setTextColor(Color.parseColor("#eeceac")); // Not working
jetoninvisible.setTextSize(40); // Working fine
jetoninvisible.setText(String.valueOf(letter)); // Working fine
j = j+1;
}
}
});
}
Thanks a lot for your answers or suggestions

favorite button in android not working properly

Hii i'm making an app where i have small heart image onclick of that image i've set an onclick listener so when i click on that image it is replaced by another red coloured heart image.
so my problem is when i again click on red heart image it doesnt turn to its normal state which is blank heart vector image as i'm new in android i dont have much knowledge please if someone can guide.
my code for image onclick
#Override
public void onBindViewHolder(FeedViewHolder holder, int position) {
int image_id = images[position];
//holder.background_image_layout.setImageDrawable(null);
//holder.background_image_layout.setImageResource(image_id);
holder.background_image.setBackgroundResource(image_id);
}
#Override
public int getItemCount() {
return images.length;
}
public static class FeedViewHolder extends RecyclerView.ViewHolder {
CustomTextViewMedium first_text,second_text,third_text,fourth_text,fifth_text,sixth_text,
seventh_text;
ImageView favourite_image;
CardView primary_card;
LinearLayout background_image;
ImageView background_image_layout;
CircleImageView profile_image;
public FeedViewHolder(View itemView) {
super(itemView);
background_image = (LinearLayout)itemView.findViewById(R.id.background_image);
primary_card = (CardView)itemView.findViewById(R.id.primary_card);
first_text = (CustomTextViewMedium)itemView.findViewById(R.id.first_text);
second_text = (CustomTextViewMedium)itemView.findViewById(R.id.second_text);
third_text = (CustomTextViewMedium)itemView.findViewById(R.id.third_text);
fourth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fourth_text);
fifth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fifth_text);
sixth_text = (CustomTextViewMedium)itemView.findViewById(R.id.sixth_text);
seventh_text = (CustomTextViewMedium)itemView.findViewById(R.id.seventh_text);
favourite_image = (ImageView)itemView.findViewById(R.id.favourite_image);
background_image_layout = (ImageView) itemView.findViewById(R.id.background_image_layout);
profile_image = (CircleImageView)itemView.findViewById(R.id.profile_image);
favourite_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(favourite_image.isPressed()){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
});
}
}
Use if condition like this :-
first globally define like this :-
boolean imageChange = true;
then do this in setOnClickListener like this :-
favourite_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(imageChange ){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageChange = false;
}else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageChange = true;
}
}
});
Problem is that favourite_image.isPressed() is not persistent state of the View. It will be true only while user holds its finger on the view.
To overcome this you have two similar options:
Option #1
Instead of checking isPressed you can check some other state, for example isSelected. Since this is ImageView you probably need to set the state manually on click.
public void onClick(View v) {
if(favourite_image.isSelected()){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
favourite_image.setSelected(false)
}
else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
favourite_image.setSelected(true)
}
}
Options #2
Use some simple boolean to keep track of the selected state, and set/chec its values similarity to above code.

How to get data from many EditTexts when Its added in layout programmatically in android

I'm adding EditText in linear layout and it gives a view like that in image.
I'm getting this view by using this code.
public class SearchRecipe extends AppCompatActivity {
LinearLayout parentLayout;
ImageButton searchRecipe;
private int EDITTEXT_ID = 1;
private List<EditText> editTextList;
EditText editTextItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_recipe);
setActionBar();
init();
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editTextItem = (EditText) parentLayout.findViewById(EDITTEXT_ID);
for (int i = 0; i < editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
Toast.makeText(SearchRecipe.this, editTextItem.getText().toString() + " ", Toast.LENGTH_SHORT).show();
}
}
});
}
public void init() {
parentLayout = (LinearLayout) findViewById(R.id.parent_layout); //make sure you have set vertical orientation attribute on your xml
searchRecipe = (ImageButton) findViewById(R.id.search_button);
editTextList = new ArrayList<EditText>();
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editTextItem = new EditText(SearchRecipe.this);
editTextItem.setId(EDITTEXT_ID);
editTextList.add(editTextItem);
EDITTEXT_ID++;
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
}
Now the only problem I'm facing is that. I'm not getting the text from edittext properly. Let me Explain what I want to do.
Click on Add More TextView will add one more edit text.
After adding all edittexts I will click on Search button.
By clicking search button will get the data from edittexs and save in arraylist. I tried a lot but can't do this properly. will you please help me to do this thing ? I'm stuck in from many days.
if you are createing edit text run time only for this purpose then there is no need of below tow lines
editTextItem.setId(EDITTEXT_ID);
EDITTEXT_ID++;
To retrive data from each edit box follow below things
for (EditText editText : editTextList) {
/* now you can get the value from Edit-text and save in the ArrayList
or you can append it in same string*/
yourArraList.add(editText.getText().toString()));
}
Get the editext from your list editTextList
String data = editTextList.get(index).getText().toString();
Add check for editTextList should not be null or empty.
You can iterate over list using for-each loop
for (EditText editText : editTextList) {
// now you can get the value from Edit-text and save in the ArrayList
yourArraList.add(editText.getText().toString()));
}
you can do like below if view inside fragment.
public static String getText(final Activity activity) {
final StringBuilder stringBuilder=new StringBuilder();
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
EditText editText = (EditText) innerLayout.findViewById(R.id.meeting_dialog_et);
msg.add(editText.getText().toString());
}
for (int j=0;j<msg.size();j++)
{
stringBuilder.append(msg.get(j)).append(";");
}
Toast t = Toast.makeText(activity.getApplicationContext(), stringBuilder.toString(), Toast.LENGTH_SHORT);
t.show();
return stringBuilder.toString();
}
there is another way is make arraylist Edittexes and add each edittext when it added to layout. then you can get like below:
for (int i = 0; i < Edittexes.size(); i++) {
if (Edittexes.get(i) == view)
{
String text=Edittexes.get(i).getText();
}
}

Categories