I have a problem with the Checkmark of my ListView row layout. The checkmark doesn't show up when the ListItem is clicked even though the ListView works (the interaction works). How can i fix this problem?
You will want to make you custom row layout that is checkable.
First you need to create a custom layout that implements Checkable:
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private Checkable mCheckable;
public CheckableLinearLayout(Context context) {
this(context, null);
}
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean isChecked() {
return mCheckable == null ? false : mCheckable.isChecked();
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
// Find Checkable child
int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
View v = getChildAt(i);
if (v instanceof Checkable) {
mCheckable = (Checkable) v;
break;
}
}
}
#Override
public void setChecked(boolean checked) {
if(mCheckable != null)
mCheckable.setChecked(checked);
}
#Override
public void toggle() {
if(mCheckable != null)
mCheckable.toggle();
}
}
After this layout is inflated it looks through it's children for one that is checkable (like a CheckedTextView or CheckBox), other than that it is quite simple.
Next use it in a layout:
<your.package.name.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckedTextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/textCheckMark"
android:gravity="center_vertical"
android:paddingLeft="6dp"
android:paddingRight="6dp" />
</your.package.name.CheckableLinearLayout>
Notice that you cannot just use CheckableLinearLayout, since it is not a built-in Android View you need tell the compiler where it is. Save this as checkable_list_row.xml, for example.
Lastly use this new layout like you would with any other custom layout.
adapter = new MySimpleCursorAdapter(this, R.layout.checkable_list_row, cursor,
new String[] { Database.KEY_DATE , Database.KEY_NAME },
new int[] {R.id.text1, R.id.text2}, 0);
Hope that helps!
Related
I want to make widget which includes EditText and TextView and looks like this:
.
To achieve this I've created RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp">
<EditText
android:id="#+id/editTextHint"
style="#style/CustomEditTextTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="14dp"
android:textColor="#color/textColor"
android:textSize="16sp"
android:hint="To my first character"
tools:text="Тип клиента">
</EditText>
<TextView
android:id="#+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextHint"
android:layout_alignParentEnd="true"
android:layout_above="#id/editTextHint"
tools:text="Продавец"
android:fontFamily="#font/proxima_nova_regular"
android:textSize="16sp"
android:layout_marginEnd="4dp"
android:textColor="#color/colorPrimary"/>
</RelativeLayout>
The above RelativeLayout works great and it looks as planned. To make Compound view I've removed </RelativeLayout> tag and wrapped it into the </merge> (I don't apply code as it's identical except of RelativeLayout tag)
To work with view I've written MyCustomEditText class which extendes RelativeLayout
public class CustomEditText extends RelativeLayout {
private EditText editTextHint;
private TextView textViewEntry;
private String hintText;
private String inputText;
private String starterText;
private OnCustomEditTextListener listener;
public String getHintText() {
return hintText;
}
public String getInputText() {
return inputText;
}
public void setHintText(String hintText) {
editTextHint.setText(hintText);
}
public void setInputText(String inputText) {
textViewEntry.setText(inputText);
}
public CustomEditText(Context context) {
super(context);
initializeViews(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray;
typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
hintText = typedArray.getString(R.styleable.CustomEditText_hintText);
inputText = typedArray.getString(R.styleable.CustomEditText_inputText);
starterText = typedArray.getString(R.styleable.CustomEditText_starterText);
typedArray.recycle();
initializeViews(context);
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_edit_text, this);
setMinimumHeight(48);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
editTextHint = findViewById(R.id.editTextHint);
textViewEntry = findViewById(R.id.textViewContent);
bindDataToChildViews();
editTextHint.setOnClickListener(v -> listener.onClick());
}
private void bindDataToChildViews() {
editTextHint.setText(hintText);
if ((inputText == null)||(inputText.isEmpty())) {
textViewEntry.setText(starterText);
} else {
textViewEntry.setText(inputText);
}
}
public void setHintClickListener(OnCustomEditTextListener listener) {
this.listener = listener;
}
public interface OnCustomEditTextListener {
void onClick();
}
}
I'm trying tu use this view in other layouts but the positioning of text view is ugly:
I thought that extending view from RelativeLayout would be enough for correct positioning. So I need your help to define where I'me getting wrong. How to position elements correctly?
P.S. The replacement of RelativeLayout for merge tag was made to optimize drawing of views and avoiding unnecessary nesting layouts
First of all you dont need to have
android:layout_above="#id/editTextHint"
And add
android:layout_gravity="center_vertical"
to editTextHint and textViewContent
Also possibly you should remove
android:layout_marginEnd="4dp"
or change a bit it's value
The onChildClickListener is not working on my expandable list.
The child layout is made from a custom LinearLayout by code and it adds some other child views into it from xml.
For the xml, all views are set to clickable=false.
The extended layout (The parent layout of the other views) is not set to anything clickable - wise since it's in code.
I believe I should set the custom linear layout to clickable false as well but since it's in code I'm not sure to do it
Here is my getChildView method:
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = new ExtendedContentLinearLayout(context);
convertView).addViewToContentLayout(text);
ImageView leftImage = (ImageView) convertView.findViewById(R.id.child_left_image);
leftImage.setImageDrawable(context.getResources().getDrawable(R.drawable.folder_));
TextView childTextView = (TextView) convertView.findViewById(R.id.childTextView);
childTextView.setText(childText);
return convertView;
}
Extended LinearLayout:
public class ExtendedContentLinearLayout extends LinearLayout implements View.OnClickListener {
private View mTrigger;
private LinearLayout mContent;
public ExtendedContentLinearLayout(Context context) {
super(context);
mContent = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTrigger = inflater.inflate(R.layout.nav_menu_board_expandable_children_outer, this, true);
this.addView(mContent);
mTrigger.setOnClickListener(this);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
}
#Override
public void onClick(View v) {
App.disableTouchEventForDefaultDuration();
toggleVisibility();
}
public void addViewToContentLayout(View view) {
mContent.addView(view);
toggleVisibility();
}
private void toggleVisibility() {
if (mContent.getVisibility() == View.GONE) {
mContent.setVisibility(View.VISIBLE);
} else {
mContent.setVisibility(View.GONE);
}
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mContent = null;
mTrigger = null;
}
}
The children views xml which I add to the custom layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:layout_gravity="center_vertical"
android:clickable="false"
android:gravity="center_vertical"
android:id="#+id/triggerLayout"
android:background="#color/da_nav_drawer_dark"
android:layout_height="60dp">
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#android:color/black" />
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="20dp"
android:clickable="false"
android:id="#+id/child_left_image"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textColor="#color/da_white"
android:id="#+id/childTextView" />
</LinearLayout>
Have you tried setOnChildClickListener listener on listview? Get more help Here, check this also.
I had to set the parent layout to clickable through code:
parent.setClickable(true);
that solved it.
thanks you for all the help
I have a listfragment that manages row(s) of 2 radio buttons per row, where only one button is allowed to be checked, but I've not been able to manage the radio button states properly.
I'm pretty familiar with listview's recycling of views. I've tried many different avenues to no success, I realize I have to keep the states of the radio buttons, list position and probably whether the button has been initialized yet separately, which I can to with an interface. It's where and when to set the radio button states within the adapter that I've been wrestling with. Anything I've tried has one condition or another interfering with the other. Like setting the radio button from the database initially, but when there is a recycled view with no radio button to be check and I do a radiogroup.clearCheck() that seems to override when the user chooses a button.
The state of the radio buttons should be controlled by 2 sources;
Database source:
1) Database column has a value I'd like to set for the radio button initially, which I'd like to show as checked (.setChecked(true)) when the user hasn't had any interaction with that row yet, besides scrolling/no clicks. OR the Database column is 0 and no radio button should be checked.
User Action:
2) The users selects one of the two buttons, this should override the value of the database column.
Following is the rough framework for the fragments and adapters that I have:
The listview row xml file stackoverflow_2_radiobuttons_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:divider="#color/gray"
android:dividerHeight="5dp" >
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_text_left_margin"
android:layout_marginRight="#dimen/default_text_left_margin"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb_1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/radiogroup"
android:layout_weight="1"
android:gravity="top"
android:paddingBottom="#dimen/default_text_top_margin"
android:text="RadioButton 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:checked="false" />
<RadioButton
android:id="#+id/rb_2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/rb_1"
android:layout_weight="1"
android:gravity="top"
android:paddingBottom="#dimen/default_text_top_margin"
android:text="RadioBUtton 2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:checked="false"/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
The list fragment: StackOverFlow.java
public class StackOverFlow extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter adapter;
Activity activity;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] columns = new String[] { Mydb.COL_RB_STATE };
int[] viewfields = new int[] { };
adapter = new StackOverFlowAdapter(getActivity(), R.layout.stackoverflow_2_radiobuttons_row.xml,
null, columns, viewfields);
setListAdapter(adapter);
getLoaderManager().initLoader(0, null, this);
}
Handler handler = new Handler();
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = new String[] {
Mydb.COL_RB_STATE };
CursorLoader loader = new CursorLoader(getActivity(),
MYProvider.CONTENT_URI, projection, null, null, null);
String[] argz = new String[] { "'text'" };
return loader;
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
((SimpleCursorAdapter) this.getListAdapter()).swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
((SimpleCursorAdapter) this.getListAdapter()).swapCursor(null);
}
}
The custom list fragment adapter: StackOverFlowAdapter.java
public class StackOverFlowAdapter extends SimpleCursorAdapter {
private Context context;
private final LayoutInflater inflater;
private int pos = 0;
RadioGroup radiogroup;
int iRowID = -1;
String row_Id;
Activity TestActivity;
protected static class ViewHolder {
protected TextView h_rb1;
protected TextView h_rb2;
protected TextView h_radiogroup;
protected int h_position;
}
public StackOverFlowAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public boolean hasStableIds() {
return true;
}
ViewHolder holder;
public View newView(Context context, Cursor cursor, ViewGroup parent) {
holder = new ViewHolder();
View view = inflater.inflate(R.layout.stackoverflow_2_radiobuttons_row, parent, false);
holder.h_rb1 = (RadioButton) view.findViewById(R.id.rb_1);
holder.h_rb2 = (RadioButton) view.findViewById(R.id.rb_2);
holder.h_radiogroup = (RadioButton) view.findViewById(R.id.radiogroup);
holder.h_position = (pos);
if (cursor != null) {
bindView(view, context, cursor);
}
view.setTag(holder);
return view;
}
public void bindView(View view, final Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
final RadioButton rb_1 = (RadioButton) view
.findViewById(R.id.rb_1);
final RadioButton rb_2 = (RadioButton) view
.findViewById(R.id.rb_2);
final RadioGroup radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup);
radiogroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == rb_1.getId()) {
rb_1.setChecked(true);
} else if (checkedId == rb_2.getId()) {
rb_2.setChecked(true);
}
}
});
}
}
I've seen a lot of answers using getView, but I haven't been able to find a good example or theory using bindView and newView in a custom SimpleCursorAdapter with radio button states. I haven't been successful manipulating anything in newView, I've been working mostly in bindView. So at this point I'm not sure how to proceed. Any help or guidance would be appreciated.
Have a great day.
I want Radio Group implementation on GridView, so that only single item can be selected among the elements of grid.
Please help.
The purpose to restrict the select of element from the grid can be accomplished as follows:
1.Creation of Grid element.
<LinearLayout
android:id="#+id/item_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<RadioButton
android:id="#+id/radiobtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image" />
</LinearLayout>
2.Inflating this xml in the getView() method of customize adapter.
public class MyAdapter extends BaseAdapter {
Context mCtx;
int[] mImg;
LayoutInflater layoutInflater;
RadioGroup rgp;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
public MyAdapter(Context context, int[] img) {
this.mCtx = context;
this.mImg = img;
rgp = new RadioGroup(context);
layoutInflater = (LayoutInflater) mCtx
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mImg.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
Holder holder;
if (view == null) {
view = layoutInflater.inflate(R.layout.element, null);
holder = new Holder();
holder.image = (ImageView) view.findViewById(R.id.imageView);
holder.radioButton = (RadioButton) view
.findViewById(R.id.radiobtn);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((position != mSelectedPosition && mSelectedRB != null)) {
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
holder.radioButton.setChecked(false);
} else {
holder.radioButton.setChecked(true);
if (mSelectedRB != null && holder.radioButton != mSelectedRB) {
mSelectedRB = holder.radioButton;
}
}
return view;
}
}
private class Holder {
ImageView image;
RadioButton radioButton;
}
An alternative approach to this is to create your own subclass of RadioButton which has an extra XML attribute (such as group). This specifies (as a string) to which group the button belongs. In the subclass, you then ensure that within any particular group, only one radio button is selected.
You can do this as follows:
First create the file res/values/attrs.xml which contains something like the following:
<resources>
<declare-styleable name="GroupedRadioButton">
<attr name="group" format="string"/>
</declare-styleable>
</resources>
Then create your subclass, GroupedRadioButton:
public class GroupedRadioButton extends RadioButton {
public GroupedRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
processAttributes(context, attrs);
setOnClickListener(internalListener, true);
}
...
}
Once fleshed out (see below), you can then use this new class as follows in your layout files:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 3"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option A"
custom:group="group2" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option B"
custom:group="group2" />
...
The radio buttons can be anywhere in your layout (e.g. in a GridView). Note the xmlns:custom tag is required since we are using a custom attribute.
The layout above will make options 1, 2 and 3 mutually exclusive and options A and B mutually exclusive.
This is achieved by keeping track (statically) of which GroupedRadioButton is currently selected within each group:
public class GroupedRadioButton extends RadioButton {
private static Map<String, WeakReference<GroupedRadioButton>> buttonMap;
static {
buttonMap = new HashMap<String, WeakReference<GroupedRadioButton>>();
}
...
}
Note that we have to be careful here to ensure that we don't keep strong references to the buttons otherwise they will never be garbage collected.
The processAttributes() method specified in the constructor above digs out the group attribute from the XML we specified and sets this as instance data:
private void processAttributes(Context context, AttributeSet attrs) {
TypedArray attributes = context.obtainStyledAttributes(attrs,
R.styleable.GroupedRadioButton);
int attributeCount = attributes.getIndexCount();
for (int i = 0; i < attributeCount; ++i) {
int attr = attributes.getIndex(i);
switch (attr) {
case R.styleable.GroupedRadioButton_group:
this.groupName = attributes.getString(attr);
break;
}
}
attributes.recycle();
}
We define the main OnClickListener for this class.
private OnClickListener internalListener = new OnClickListener() {
#Override
public void onClick(View view) {
processButtonClick(view);
}
};
which calls:
private void processButtonClick(View view) {
if (!(view instanceof GroupedRadioButton))
return;
GroupedRadioButton clickedButton = (GroupedRadioButton) view;
String groupName = clickedButton.groupName;
WeakReference<GroupedRadioButton> selectedButtonReference = buttonMap.get(groupName);
GroupedRadioButton selectedButton = selectedButtonReference == null ? null : selectedButtonReference.get();
if (selectedButton != clickedButton) {
if (selectedButton != null)
selectedButton.setChecked(false);
clickedButton.setChecked(true);
buttonMap.put(groupName, new WeakReference<GroupedRadioButton>(clickedButton));
}
if (externalListener != null)
externalListener.onClick(view);
}
This does two things. It ensures that it deselects the old group button before selecting the new one (assuming the old and new buttons are different). It then calls onClick() on an externalListener which is provided so that users of the class can add their own 'on-click' functionality.
The setOnClickListener() call in the constructor is to our own method as follows:
private void setOnClickListener(OnClickListener listener, boolean internal) {
if (internal)
super.setOnClickListener(internalListener);
else
this.externalListener = listener;
}
This sets the internalListener as the official OnClickListener and sets instance data as appropriate for the external listener. The View.setOnClickListener() method can then be overridden as follows:
#Override
public void setOnClickListener(OnClickListener listener) {
setOnClickListener(listener, false);
}
Apologies for the length of this answer but I hope it helps you and others trying to do the same thing. It would of course not be needed at all if a RadioGroup applied recursively to its children!
When you select an element of your grid, check that none of the other elements that you want to be in your radio group aren't selected and if they are deselect them...
I tried to build my first custom gui element but I get problems when I try to change appearance or adapter (using Gallery) later in code.
My Problem: I can't change Custom Gallery properties
My actual Code:
First I create an XML which is the widget customGallery.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="#+id/toLeft"
android:background="#drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginBottom="1dip" />
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_toRightOf="#+id/toLeft"
android:spacing="40dip"
android:scrollbars="horizontal"/>
<ImageButton android:id="#+id/toRight"
android:background="#drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_toRightOf="#+id/gallery" />
</merge>
Later i create a test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.Test.GallerySlider
android:id="#+id/choose"
android:layout_span="2"
android:layout_width="300dip"
android:layout_height="wrap_content" />
</LinearLayout>
My next Step was to include this custom Widget into my project and change the adapter from my Widget:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout);
ViewStub st3 = new ViewStub(TestwidgetActivity.this);
LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineLayout.addView(st3,paramst3);
st3.setLayoutResource(R.layout.test);
View st3InflaView =st3.inflate();
GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose);
gSlider.setNewAdapter( new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"}));
}
This is the Widgetclass I wrote:
public class GallerySlider extends RelativeLayout implements OnClickListener {
private ArrayAdapter<String> adapter;
private Gallery gallery;
private ImageButton toLeftBtn = null;
private ImageButton toRightbtn = null;
public GallerySlider(Context context) {
super(context, null);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
public void init(Context ctx){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
inflater.inflate(R.layout.customGallery, this, true);
toLeftBtn = (ImageButton) findViewById(R.id.toLeft);
toLeftBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() > 0){
gallery.setSelection(gallery.getSelectedItemPosition()-1);
}
}
});
toRightbtn = (ImageButton) findViewById(R.id.toRight);
toRightbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){
gallery.setSelection(gallery.getSelectedItemPosition()+1);
}
}
});
adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1",
"1")});
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setBackgroundResource(R.drawable.h_sliderl);
gallery.setAdapter(adapter);
}
#Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case R.id.toLeft: gallery.setSelection(gallery.getFocusedChild().getId()-1);
break;
case R.id.toRight: gallery.setSelection(gallery.getFocusedChild().getId()+1);
break;
}
}
public void setNewAdapter(ArrayAdapter<String> _adapter){
gallery.setAdapter(_adapter);
((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged ();
}
}
If I call setNewAdapter(ArrayAdapter _adapter) nothing change.. . I also tried to change the size of the gallery but it also fails(nothig happen). Is my approach false?
greetings marcel
The first thing I can detect is that you are creating your custom view two times.
The first creation occurs with the ViewStub when you set the layout.
And the second one, which doesn't get added to the contentView when you inflate R.layout.test.
You are setting the adapter to the second custom view which it isn't added to the view hierarchy.