Selected item not work in listview - java

I have listview (android.support.v7.widget.RecyclerView ) in my HomeLayout.
Code in HomeLayout
<LinearLayout
p1:orientation="horizontal"
p1:minWidth="25px"
p1:clickable="true"
p1:minHeight="25px"
p1:layout_width="wrap_content"
p1:layout_height="45dp"
p1:id="#+id/linearLayout2"
p1:layout_alignParentTop="true"
p1:layout_alignParentLeft="true"
p1:layout_marginLeft="70dp"
p1:layout_marginTop="15dp"
p1:layout_marginRight="310dp">
<android.support.v7.widget.RecyclerView
p1:id="#+id/categoriesListView"
p1:duplicateParentState="true"
p1:state_checked="true"
p1:focusable="true"
p1:focusableInTouchMode="true"
p1:clickable="true"
p1:scrollbars="horizontal"
p1:state_activated="true"
p1:layout_width="match_parent"
p1:layout_height="45dp" />
</LinearLayout>
List view have custom adapter. Adapter code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:orientation="horizontal"
p1:focusable="true"
p1:focusableInTouchMode="true"
p1:clickable="true"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/linearLayout1">
<TextView
p1:text="Medium Text"
p1:background="#drawable/designforselected"
p1:textAppearance="?android:attr/textAppearanceMedium"
p1:layout_width="wrap_content"
p1:layout_height="45dp"
p1:id="#+id/txtCategoryName"
p1:textSize="20dp"
p1:focusable="true"
p1:focusableInTouchMode="true"
p1:clickable="true"
p1:textColor="#drawable/textcolorarticle"
p1:gravity="center"
p1:fontFamily="Myriad"
p1:paddingRight="20dp"
p1:paddingLeft="20dp"
p1:paddingTop="10dp"
p1:paddingBottom="10dp"
p1:layout_marginLeft="5dp" />
</LinearLayout>
Textview have custom design for press p1:background="#drawable/designforselected" Code for designforselected is
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="rectangle"/>
<solid android:color="#333333" />
</item>
<item android:state_pressed="true" >
<shape android:shape="rectangle"/>
<solid android:color="#333333" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#b3b3b3"
android:endColor="#e6e6e6"
android:angle="90"/>
<corners
android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp" >
</corners>
</shape>
</item>
</selector>
State_pressed is work but state_selected no work. Why ? Ty
I wish the solution to XML file

You need to implement the listView onClick like this:
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ...;
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});

I had similar issue not long ago with state_selected. Try state_checked or state_activated instead.

In your adapter write this:
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
final GridItem nature = mItems.get(i);
viewHolder.tvspecies.setText(nature.getName());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
viewHolder.imgThumbnail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("CLick",nature.toString());
}
});
}
This is a way that works to me. Of course you need use your ids

Related

how to use transition to change the color of a circular button in android studio

I'm a very basic beginner of android studio and im struggling a lot with adding transitions to my toggle button. As it is circular, once i change the background color, it turns into a square. Would be highly appreciated if someone could help me out. Thanks!
This is my layout XML for the button
<ToggleButton
android:id="#+id/button2"
android:layout_width="279dp"
android:layout_height="279dp"
android:layout_centerInParent="true"
android:background="#drawable/roundcircle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.012"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
This is my XML file that i used to create my circle (roundcircle)
<size android:height="283dp" android:width="283dp"/>
<solid android:color="#32CD32"/>
<corners android:radius="278dp"/>
This is the MainActivity
public class MainActivity extends AppCompatActivity {
private ToggleButton Remote;
private TextView Text;
DatabaseReference database;
TransitionDrawable transitiondrawable;
ColorDrawable[] BackGroundColor = {
new ColorDrawable(Color.parseColor("#ff0000")),
new ColorDrawable(Color.parseColor("#56ff00"))
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Remote = (ToggleButton) findViewById(R.id.toggleButton);
Button AppEffect = (Button) findViewById(R.id.button2);
transitiondrawable = new TransitionDrawable(BackGroundColor);
AppEffect.setBackground(transitiondrawable);
Remote.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
transitiondrawable.startTransition(3000);
}
else{
transitiondrawable.startTransition(3000);
}
}
});
}
}
You can directly define a TransitionDrawable in a xml, e.g. button_bg.xml as follows.
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#ff0000"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#56ff00"/>
</shape>
</item>
</transition>
Then set the background of the button to button_bg.
transitiondrawable = (TransitionDrawable) getResources().getDrawable(R.drawable.button_bg);

Android background color transition from left to right

I was wondering if there is a way to animate the background from a relativelayout , so that the color changes from e.g blue to yellow, from left to right in about 1 second. (This animation should be triggered onclick).
Appreciate any help,
ty
I think that you can use AnimationDrawable, the most basic way it's to have three color : blue, a gradient blue-yellow and yellow :
In res/drawable create a gradient.xml file (left to right):
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#FFFF00"
android:startColor="#0000FF" />
</shape>
Then in the same folder create a animalion-list.xml file :
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#0000FF" android:duration="333"/>
<item android:drawable="#drawable/gradient" android:duration="333"/>
<item android:drawable="#FFFF00" android:duration="333"/>
</animation-list>
Apply the animation-list as background to your relative layout :
<RelativeLayout:
android:...
android:...
android:drawable="#drawable/animation-list"
android:... >
...
</RelativeLayout>
In your code :
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_id);
AnimationDrawable animationDrawable = (AnimationDrawable) relativeLayout.getBackground();
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable.start();
}
});
I'm not sur that this code give you that your exactly expected but with some imagination, you should get that you want.
Hope this helps.
Drawable gradient_2
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/deep_purple_500"
android:endColor="#color/blue_700"
android:angle="0"/>
</shape>
and usage inside 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_2"
android:orientation="vertical">
</LinearLayout>
You can change agle of colorfull 0 , 45 , 90 , 180
android:angle="0"
You can also add centerColor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:centerColor="#D8EDFD"
android:endColor="#D8EDFD "
android:startColor="#F6F9FF " />
</shape>

Changing color of the border of the background of a list item

I was going through this tutorial and had the idea of making it so when you press a button, one of the cards borders changes color.
I looked at this solution, but nothing happens.
Here is what I have done:
I have an xml in drawable for the shape (feed_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:id="#+id/feedsquare">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#color/white" >
</solid>
<!-- view border color and width (I WANT TO CHANGE THIS) -->
<stroke
android:width="1dp"
android:color="#color/white" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
</shape>
</item>
</layer-list>
Then I have a layout that is basically how each card/list item looks (list_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="#drawable/feed_item"
android:id="#+id/card"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/title"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMM"
android:textSize="#dimen/name3"
android:textColor="#color/black" />
<!--android:scaleType="fitXY"-->
<ImageView android:id="#+id/list_image"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:layout_width="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:src="#mipmap/navigation_refresh"/>
<TextView android:id="#+id/description"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
android:textSize="#dimen/description3"
android:textColor="#color/black" />
</LinearLayout>
Now, I have a fragment with a listview. The fragment does exactly what the tutorial does (show 8 cards). I've added a button to the fragment that I want to change a specific cards border color (in the fragments onactivitycreated):
newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
shape.setStroke(1 ,R.color.green);
}
});
Nothing happens when I press the button. Any ideas as to what I can do?
To show selected item (row) in listview :
create a variable in adapter class :
private int selectedPosition = 0;
Create method in side adapter :
public void setSelectedItem(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
Add check in getView methode
if (position == selectedPosition)
{
convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg));
}
else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
selector_rect_black_trans_bg.xml :
<solid android:color="#color/gray1" />
<stroke
android:width="2dp"
android:color="#color/gray6" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
In Activity, only you need to pass position in adapter to show highlighted.
its tested code and it will work.

Android: Layer drawable loses information

I am messing around with creating my own dialog class, but I've run into an interesting issue. I use a layer-list drawable for the dialog's close button, but for some reason, the shadow layer disappears after the first time I show the dialog. You can see in the image below:
I really have absolutely no idea what could be causing this. I've tried debugging, but I wasn't able to find any leads. Does anyone have any ideas? My code is below.
TwoColorDialog.java
public class TwoColorDialog extends DialogFragment {
private static final String TAG = "TwoColorDialog";
private ViewGroup _root;
private TextView _textTitle;
private ImageView _buttonClose;
private Button _button1;
private Button _button2;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
_root = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.two_color_dialog, null);
_textTitle = (TextView) _root.findViewById(R.id.dialog_title);
_buttonClose = (ImageView) _root.findViewById(R.id.dialog_close_button);
_button1 = (Button) _root.findViewById(R.id.dialog_button1);
_button2 = (Button) _root.findViewById(R.id.dialog_button2);
dialog.requestWindowFeature(STYLE_NO_TITLE);
View decorView = dialog.getWindow().getDecorView();
decorView.setBackgroundResource(getResources().getColor(android.R.color.transparent));
dialog.setContentView(_root);
_buttonClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
_button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Button 1 pressed");
}
});
_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Button 2 pressed");
}
});
return dialog;
}
}
two_color_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/dialog_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/dialog_background">
<LinearLayout android:id="#+id/dialog_title_spacing"
android:layout_width="match_parent"
android:layout_height="24dp"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button android:id="#+id/dialog_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/button"
android:text="Button1" />
<Button android:id="#+id/dialog_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/button"
android:text="Button2" />
</LinearLayout>
</LinearLayout>
<TextView android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="6dp"
android:background="#drawable/dialog_title_box"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="TITLE" />
<ImageView android:id="#+id/dialog_close_button"
android:layout_width="42dp"
android:layout_height="44dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="top|right"
android:src="#drawable/dialog_close_button"/>
</FrameLayout>
dialog_title_box.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#88888888" />
<corners android:radius="1dp" />
<padding
android:left="1dp"
android:top="0dp"
android:right="1dp"
android:bottom="3dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<corners android:radius="1dp" />
<padding
android:left="15dp"
android:top="5dp"
android:right="15dp"
android:bottom="5dp" />
</shape>
</item>
</layer-list>
dialog_close_button.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#88888888" />
<padding
android:left="1dp"
android:top="0dp"
android:right="1dp"
android:bottom="4dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/white" />
</shape>
</item>
<item android:drawable="#drawable/ic_close" />
</layer-list>
Well, I've found the issue. The problem only shows up when you use the "padding" tag. I have no idea why, maybe it is a bug. I was able to work around it by changing my dialog_close_button.xml accordingly:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#88888888" />
</shape>
</item>
<item
android:left="1dp"
android:right="1dp"
android:bottom="4dp">
<shape android:shape="oval">
<solid android:color="#color/white" />
</shape>
</item>
<item
android:drawable="#drawable/ic_close"
android:left="1dp"
android:right="1dp"
android:bottom="4dp" />
</layer-list>

TextView with shape or line

I want to draw a simple line on top of some text in a TextView. I have looked at various examples which seem to override the onDraw() function but my understanding is that onDraw() is called when something is drawn.
I would like a vertical line in my TextView and at this moment in time I dont really care where it is, once I have the line I am sure I will be able to manipulate it to the position I would like.
I have a TextViewWithLines class extending TextView where the code will go:
public class TextViewWithLines extends TextView {
public TextViewWithLines(Context context){
super(context);
}
public TextViewWithLines(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithLines(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
and I also have a fragment where I would like the drawing of the line to be done when I create the view.
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
t1 = (TextViewWithLines)v.findViewById(R.id.display1);
.................
}
Any help would be appreciated
As you describe, you simply need to do your drawing in onDraw().
Here is a (working) example of someone that wants all text in the Textview underlined;
How can I have a row separating line in TextView
One possible solution (just a simple XML layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/small_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="#dimen/define_your_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<LinearLayout
android:id="#+id/text_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="vertical"
android:background="#color/define_your_color"
android:layout_alignParentTop="true"/>
</RelativeLayout>
If you want to draw a shape around a TextView (like a rectangle), you can define a drawable background and set the colors you want:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<stroke android:color="#FFCC00" android:width="3dp"></stroke>
<corners android:radius="5dp"/>
<solid android:color="#FAFFA8"/>
</shape>
</item>
<item android:state_enabled="false">
<shape>
<stroke android:color="#FFFFFF" android:width="2dp"></stroke>
<corners android:radius="5dp"/>
<solid android:color="#00FFFFFF"/>
</shape>
</item>
<item>
<shape>
<stroke android:color="#DADADA" android:width="2dp"></stroke>
<corners android:radius="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
</item>
</selector>

Categories

Resources