Android TV ImageCardView title wrapping - java

I am developing app for Android TV and using leanback library.
In example from google they are using public class CardPresenter extends Presenter to display content. The problem is, that I want to display all text in titles, i.e. dont cut it.
I already tried:
1) To fix this problem by programmatically setting LayoutParams: cardView.findViewById(R.id.title_text).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
2) I looked inside lb_image_card_view.xml file for ImageCard. Funny moment, that TextView with id "title_text" already has android:layout_height="wrap_content" parameter, but looks like it doesn't work?? Is it a bug?
3) For a backup plan I can create my own class for ImageCardView, but this solution seems 2 hard.
Thx.
Update.
I used the answer below, but I have to modificative code for better performance:
cardView.setOnFocusChangeListener((view, isFocused) -> {
if (isFocused) {
((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(5);
}
else {
((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(1);
}
});

Try to check this tutorial if it can help you. it helps you to show or display all the text in title.
Here is the code use for this tutorial
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
final ImageCardView cardView = new ImageCardView(mContext);
cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, final boolean isFocused) {
final View infoField = view.findViewById(R.id.info_field);
final TextView contentField = (TextView)view.findViewById(R.id.content_text);
final TextView titleField = (TextView)view.findViewById(R.id.title_text);
final Drawable mainImage = ((ImageView)view.findViewById(R.id.main_image)).getDrawable();
if (isFocused) {
((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(3);
FrameLayout.LayoutParams infoLayout = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
infoField.setLayoutParams(infoLayout);
RelativeLayout.LayoutParams contentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
contentLayout.addRule(RelativeLayout.BELOW, R.id.title_text);
contentField.setLayoutParams(contentLayout);
}
else {
((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(1);
}
}
});
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
return new ViewHolder(cardView);
}
And here is the output of this code.
For more information, check this thread also.

Related

Set color seekbar

I'm creating a small paint android app using android studio java. I create in the menu an icon for changing the color of the drawing. Once the user click on it a new AlertDialog should appear showing 4 seekbars for ARGB colors. I ended with this code but I still don't know why it's not working. Can someone please help me?
private Drawing draws;
private SeekBar alphaSeekBar;
private SeekBar redSeekBar;
private SeekBar greenSeekBar;
private SeekBar blueSeekBar;
private void showColorDialog(){
currentAlertDialog = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
alphaSeekBar = view.findViewById(R.id.alphaSeekBar);
redSeekBar = view.findViewById(R.id.redSeekBar);
greenSeekBar = view.findViewById(R.id.greenSeekBar);
blueSeekBar = view.findViewById(R.id.blueSeekBar);
colorView = view.findViewById(R.id.colorView);
alphaSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
redSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
greenSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
blueSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
int color = draws.getDrawingColor();
alphaSeekBar.setProgress(Color.alpha(color));
redSeekBar.setProgress(Color.red(color));
greenSeekBar.setProgress(Color.green(color));
blueSeekBar.setProgress(Color.blue(color));
Button setColorButton = view.findViewById(R.id.setColorButton);
setColorButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pikassoView.setDrawingColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
colorDialog.dismiss();
}
});
currentAlertDialog.setView(view);
currentAlertDialog.setTitle("Choose color");
colorDialog = currentAlertDialog.create();
colorDialog.show();
} private SeekBar.OnSeekBarChangeListener colorSeekBarChanged = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
draws.setBackgroundColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
colorView.setBackgroundColor(Color.argb(
alphaSeekBar.getProgress(),
redSeekBar.getProgress(),
greenSeekBar.getProgress(),
blueSeekBar.getProgress()
));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
First, this alphaSeekBar.setProgress(Color.alpha(color)); is wrong. setProgress is meant for setting int value to show how much progress seekbar has.
If your seekBar Max is set to 100 (alphaSeekBar.setMax(100);) then this alphaSeekBar.setProgress(50); means seekBar is progressed half way.
Second, to set colors use this:
alphaSeekBar.setProgressTintList(Color.alpha(color));
alphaSeekBar.setThumbTintList(Color.alpha(color));
Or, use this:
alphaSeekBar.getProgressDrawable().setColorFilter(Color.alpha(color), PorterDuff.Mode.MULTIPLY);
try different tint modes in place of PorterDuff.Mode.MULTIPLY to get your desired result
good luck
EDIT:
For setting a custom view to Alert Dialog
currentAlertDialog = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
currentAlertDialog.setView(view);
then you can get those seekbar references like below:
alphaSeekBar = currentAlertDialog.findViewById(R.id.alphaSeekBar);
redSeekBar = currentAlertDialog.findViewById(R.id.redSeekBar);
greenSeekBar = currentAlertDialog.findViewById(R.id.greenSeekBar);
blueSeekBar = currentAlertDialog.findViewById(R.id.blueSeekBar);
colorView = currentAlertDialog.findViewById(R.id.colorView);

Click listener inside OnBindViewHolder

I have the following code for the recyclerview adapter for an android app that I'm working on right now:
#Override
public void onBindViewHolder(final FeedViewHolder contactViewHolder, final int i) {
final FeedInfo ci = feedInfoList.get(i);
//Set the text of the feed with your data
contactViewHolder.feedText.setText(ci.getFeed());
contactViewHolder.surNameText.setText(ci.getSurName());
contactViewHolder.nameText.setText(ci.getFirstName());
contactViewHolder.feedDate.setText(ci.getDate());
contactViewHolder.numberOfGoingText.setText(ci.getNumber_of_going());
contactViewHolder.numberOfInterestedText.setText(ci.getNumber_of_interested());
//seteaza fotografia de profil in postare
new ProfilePictureDownloadImage(contactViewHolder.profilePicture).execute(ci.getProfileImageURL());
ImageButton interestedButton = contactViewHolder.interestedButton;
interestedButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = i;
FeedInfo fi = feedInfoList.get(position);
int displayedNumberOfInterested = Integer.parseInt(ci.getNumber_of_interested()) + 1;
contactViewHolder.numberOfInterestedText.setText(Integer.toString(displayedNumberOfInterested));
System.out.println("emilutzy interested from within" + fi.getPostID());
contactViewHolder.surNameText.setText("kk");
}
});
}
The problem is the click listener. In theory the button I press should increment the number right next to it. However, since I have to declare onBindViewHolder's arguments as final, only the first click works, the rest of the clicks do not change the value of the number. I am new to Android, so could you please help me find a better solution?
There's a nice method called getAdapterPosition() that you can use in your RecyclerView's ViewHolder.
Instead of setting the click listener in onBindViewHolder, set it in the constructor of your ViewHolder like so:
public class FeedViewHolder extends RecyclerView.ViewHolder {
private TextView feedText;
private TextView surNameText;
private Button interestedButton;
// ... the rest of your viewholder elements
public FeedViewHolder(View itemView) {
super(itemView);
feedtext = itemView.findViewById(R.id.feedtext);
// ... find your other views
interestedButton.setOnClickListener(new View.OnClickListener() {
final FeedInfo fi = feedInfoList.get(getAdapterPosition());
int numInterested = Integer.parseInt(ci.getNumber_of_interested()) + 1;
// setting the views here might work,
// but you will find that they reset themselves
// after you scroll up and down (views get recycled).
// find a way to update feedInfoList,
// I like to use EventBus to send an event to the
// host activity/fragment like so:
EventBus.getDefault().post(
new UpdateFeedInfoListEvent(getAdapterPosition(), numInterested));
// in your host activity/fragment,
// update the list and call
// notifyDatasetChanged/notifyDataUpdated()
//on this RecyclerView adapter accordingly
});
}
}
Don't set your position in onBindViewHolder to final (Android Studio will warn you why).
I'm not sure how the object FeedInfo looks like but you could also at a method called for example increaseNumberOfInterested() which would increase the value of Number_of_interested by one and would persist in the object when the recyclerview recycle the cell. it would like kind of like below
#Override
public void onBindViewHolder(final FeedViewHolder contactViewHolder, final int i) {
final FeedInfo ci = feedInfoList.get(i);
//Set the text of the feed with your data
contactViewHolder.numberOfInterestedText.setText(ci.getNumber_of_interested());
contactViewHolder.interestedButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Increase the number of interested in the object, so it can be persisted when cell is reclycled
ci.setNumberOfInterested(ci.getNumber_of_interested()) + 1);
//Get new value and display
contactViewHolder.numberOfInterestedText.setText(Integer.toString(ci.getNumber_of_interested()));
}

Setting image resourse by obtaining tag position for an imageview android

I am making use of recycler view. I have a layout that is highlighted in light red,this layout is included for each item in the recycler view. The light red layout is placed over the background image. I am using setTag method to identify the clicks of the buttons in red layout. That is working properly when i click i get the position. The problem is i want to change the image at specific position.
For example : Consider the heart button. I have set a tag on it like this.
heartButton = findViewById(id);
heartButton.setTag(position);
now i get the position by using the getTag method. But now i want to change the image of the heartButton at the a specific position. Is there something like
heartButton.getTag(position).setImageResouce(drawable);
If not how do i do this then.
use setBackgroundResource(R.drawable.XXX)
http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)
Proper way to do this is,
You have to keep the state of the heart button stored in the model(POJO) which is passed to custom adapter.
e.g.
class ModelListItem{
public static final int HEART=1,BROKEN_HEART=2;
int heartButtonState;
}
Now in onClick() of heart button, get that object from adapter using position,cosidering you have already figured it out on how to preserve position from heart button
ModelListItem item = (ModelListItem)adapter.getItem(position)
Change the state of heart button;
item.setHeartButtonState(ModelListItem.BROKEN_HEART);
adapter.notifyDatasetChanged();
You already know below explaination but just in case
To work this properly,in your getView methode of adapter you need to put the check on heartButtonState(); and use appropriate image resource.
getView(BOILERPLATE){
BOILERPLATE
switch(item.getheartButtonState()){
case ModelItemList.HEART:
heartbutton.setImageResource(heart_image);
break;
case ModelItemList.BROKEN_HEART:
heartbutton.setImageResource(broken_heart_image);
break;
}
I made a custom click listener and updated the like in the setter getter.But this works only when the view has been moved out of the view (i think it is the scrapeview)
The Setter Getter Class
public class DemoData {
int background;
boolean liked;
public DemoData(int background) {
this.background = background;
}
public int getBackground() {
return background;
}
// public void setBackground(int background) {
// this.background = background;
// }
public boolean isLiked() {
return liked;
}
public void setLiked(boolean liked) {
this.liked = liked;
}
}
The onBindViewHolder function of the recycler view
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
background = (ImageView) holder.view.findViewById(R.id.image);
layout = (LinearLayout) holder.view.findViewById(R.id.layout);
delete = (ImageView) layout.findViewById(R.id.delete);
lock = (ImageView) layout.findViewById(R.id.lock);
delete.setTag("delete_"+position);
lock.setTag("lock_"+position);
if(Constants.demoDatas.get(position).isLiked()){
delete.setImageResource(R.drawable.ic_launcher);
}
else{
delete.setImageResource(android.R.drawable.ic_delete);
}
delete.setOnClickListener(new CustomClickListener(position));
lock.setOnClickListener(new CustomClickListener(position));
}
The custom click listener is as below
public class CustomClickListener implements View.OnClickListener {
int position;
public CustomClickListener(int position) {
this.position = position;
}
#Override
public void onClick(View v) {
String tag = (String) v.getTag();
String identifier[] = tag.split("_");
// this line saves my state in the Setter Getter Class
Constants.demoDatas.get(position).setLiked(true);
}
}

Android, How to pass the name of int variable to a popupwindow?

How can I pass the name of an int variable to a popupwindow when an image is clicked? I have set an int per image and I have a lot of images that I had set.
This is how I'm using the int in a textView on a PopupWindow.
public boolean onLongClick(View v) {
// v.setTag(v);
case R.id.hsv1iv1:
ImageView ivpopup = (ImageView) popupView.findViewById(R.id.pv1);
intcount1++; // I would like to pass this int name to the popup window.
break;
case R.id.hsv2iv1:
ImageView ivpopup = (ImageView) popupView.findViewById(R.id.pv1);
intcount2++; // I would like to pass this int name to the popup window.
break;
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.update(0, 0, 800, 500);
ColorDrawable dw = new ColorDrawable(-005500);
popupWindow.setBackgroundDrawable(dw);
tvpwlikectr = (TextView) popupView.findViewById(R.id.liketv);
Button pwlikebtn = (Button) popupView.findViewById(R.id.pwlikebtn);
Button btnDismiss = (Button)popupView.findViewById(R.id.cancel);
pwlikebtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
intcount1++;
tvpwlikectr.setText(Integer.toString(intcount1)); // this code doesn't work with the intcount1
}});
btnDismiss.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
popupWindow.dismiss();
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
}
}
Could you explain how you are setting the INT per image? Copying and pasting the code on how you set an INT per image would be helpful, because it's unclear what you mean by you are setting an INT per image.
Also, are you interested in the value of the int variable or the name of the variable? Showing how you are settings lots of images with int per image would help clarify what you are trying to do.
-- adding answer after seeing the updated post with code --
I would create an object that has the name you are interested in (i.e. intcount1) and an int to keep the actual value. After that, you can associate each button/ImaveView with that object with the view.setTag method, and get the value via view.getTag method. Here's an example:
private class MyTag {
String mTagName;
int mCount;
MyTag(String tagName) {
mTagName = tagName;
mCount = 0;
}
}
// in your onCreate or initializaion code somewhere
ImageView view1 = (ImageView) popupView.findViewById(R.id.hsv1iv1);
MyTag imageTag = new MyTag("intcount1");
view1.setTag(imageTag);
ImageView view2 = (ImageView) popupView.findViewById(R.id.hsv1iv1);
// this will go wherever you handle the onLongClick
public boolean onLongClick(View v) {
Object tag = v.getTag();
if (tag instanceof MyTag) {
MyTag myTag = (MyTag) tag;
myTag.mCount++;
}
}
// I'm assuming you are setting the text from the actual clicked object
// so this will go wherever you are setting the text/handling the click
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof MyTag) {
MyTag myTag = (MyTag) tag;
myTag.mCount++;
tvpwlikectr.setText(myTag.mTagName);
}
}
The bottom line is, creating an object with name/count value, associate each View with its own object using the view.setTag() function, and when you need to read the values, use the view.getTag() to get the object and read the mTagName (the "variable" name) and the mCount (the "variable" value).

findviewbyid returns null in a dialog

I have a custom dialog and when I try to get the value of an EditText it returns null.
This line returns null
EditText et = (EditText)findViewById(R.id.username_edit);
Here is the code in its entirety.
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(TicTacToe.this)
//.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(getTitleText())
.setView(textEntryView)
.setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
EditText et = (EditText)findViewById(R.id.username_edit);
playerName = et.getText().toString();
}
catch (Exception e)
{
}
}
})
.create();
}
return null;
}
In my case:
First I must call the
dialog.show(),
and only after it I was able to use
dialog.findviewById(R.id.myID).
If I missed to call the show(), than I got a null back with findViewByID.
Try this:
EditText et = (EditText)textEntryView.findViewById(R.id.username_edit);
You have to tell in which view to find the id. Otherwise it will try to find the id in the view from the xml layout inflated by setContentView (usually declared in onCreate)
I faced a similar problem. In my case, I had a dialog with custom layout and in this layout had a radioButton. In order to solve that, I used the follow code:
View dialogLayout = factory.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setView(dialogLayout);
RadioButton radiobt = (RadioButton) dialogLayout.findViewById(R.id.radioBt);
I was having the same problem, which presented itself in the following code snippet:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.addbank_dialog);
dialog.show();
Button btnSaveBank = (Button)dialog.findViewById(R.id.btnSaveBank);
final EditText etBankName = (EditText)dialog.findViewById(R.id.etBankName);
btnSaveBank.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
String bank = etBankName.getText().toString();
SharedCommonData.dbOps.saveBankInDB(bank);
}
catch(Exception e){
e.printStackTrace();
}
Toast.makeText(SharedCommonData.context, "Bank Saved", Toast.LENGTH_SHORT);
refreshBanks();
dialog.dismiss();
}
});
etBankName was returning null value, but then I used dialog.findviewbyid(R.id.etBankName) and it worked.
In my case I had this error because I was redeclaring an initialized variable
In main Activity I had:
EditText cityName;
And in onCreate:
EditText cityName = (EditText)findViewById(R.id.cityName);
Just removed EditText and smooth sailing!
None of the existing answers worked for me, so I started trying different lifecycle hooks, and the one that worked for me was onViewCreated, which seems like a good choice semantically as well.
In my case what I did was that I was passing an ID of a view which was in different fragment and in that case the compiler gave me same error.
So try checking that the ID use pass is in the same xml which is connect to the java file.
I hope it helps this is my first ever solution given in this community.

Categories