Android View's touch range is strange... help me...(custom view, inl) - android

I created a Custom View and add setOnClickListener...
View's touch range is strange
how do i fix that?
red square is touch range...
XML file(custom.xml)
<LinearLayout>
<TextView> ~~
<TextView> ~~
</LinearLayout>
JAVA code(custom.java)
public class Custom_Lab_Button extends LinearLayout {
LinearLayout cus_lab_ll;
TextView cus_lab_tv1, cus_lab_tv2;
private void initView(Context context) {
inflate(getContext(), R.layout.custom_lab_button, this);
// String infService = Context.LAYOUT_INFLATER_SERVICE;
// LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(infService);
//// layoutInflater.inflate(R.layout.custom_lab_button, this, false);
// View v = layoutInflater.inflate(R.layout.custom_lab_button, getContent(), false);
// addView(v);
cus_lab_ll = (LinearLayout) findViewById(R.id.cus_lab_ll);
cus_lab_tv1 = (TextView) findViewById(R.id.cus_lab_tv1);
cus_lab_tv2 = (TextView) findViewById(R.id.cus_lab_tv2);
}
public void setCus_lab_ll_back(int cus_ll_resID) {
cus_lab_ll.setBackgroundColor(cus_ll_resID);
}
public void setCus_lab_ll_margin(LayoutParams layoutParams){
cus_lab_ll.setLayoutParams(layoutParams);
}
public LayoutParams getCus_lab_ll_margin(){
return (LayoutParams) cus_lab_ll.getLayoutParams();
}
}
Main Java Code
RelativeLayout cl_img = (RelativeLayout)view.findbyid(R.id.cl_img);
labButton = new Custom_Lab_Button(getActivity());
LinearLayout.LayoutParams tttt = labButton.getCus_lab_ll_margin();
tttt.leftMargin = 300;
tttt.topMargin = 300;
labButton.setCus_lab_ll_back(Color.BLUE);
labButton.setCus_lab_tv1_txt("lab1");
labButton.setCus_lab_ll_margin(tttt);
cl_img.addView(labButton);
labButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "it is lab button 1/", Toast.LENGTH_SHORT).show();
}
});

I had similar problem, use padding instead of margin.

I solved.. myself :)
labButton = new Custom_Lab_Button(getContext());
LinearLayout.LayoutParams tttt = labButton.getCus_lab_ll_margin();
tttt.leftMargin = 300;
tttt.topMargin = 300;
labButton.setCus_lab_tv1_txt("lab1");
labButton.setCus_lab_ll_margin(tttt);
labButton.setCus_lab_ll_back(Color.BLUE);
cl_img.setBackgroundColor(Color.GREEN);
cl_img.addView(labButton);
labButton.cus_lab_ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "it is lab button 1", Toast.LENGTH_SHORT).show();
}
});
[labButton.cus_lab_ll.setOnClickListener] is reason..

Related

Dynamic TextSize for a TextSwitcher in a Fragment

I'm developing simple android app which should have different texts and text size according to the clicks on it. For implementation I have used ConstraintLayout with TextSwitcher to provide animation when texts are changing. Here are sources:
Layout:
<android.support.constraint.ConstraintLayout
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="com.example.android.counter.layout_main">
<TextSwitcher
android:id="#+id/text_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inAnimation="#anim/push_down_in"
android:outAnimation="#anim/push_down_out" />
</android.support.constraint.ConstraintLayout>
Code:
private TextSwitcher mTextBody;
private int mTextSize = 20;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
View view = inflater.inflate(R.layout.layout_main, viewGroup, false);
mTextBody = (TextSwitcher) view.findViewById(R.id.text_body);
mTextBody.setFactory(mViewSwitcherFactory);
mTextBody.setText(getString(R.string.intro_text));
mTextBody.setOnClickListener(mOnSwitcherClickListener);
return view;
}
private ViewSwitcher.ViewFactory mViewSwitcherFactory = new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(mTextSize);
return textView;
}
};
private View.OnClickListener mOnSwitcherClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
mTextSize = 40;
mTextBody.setText(getString(R.string.text_after_click));
}
};
The initial load of intro text and size are ok, but after the performing click on it only text changes not text size. So, the question is how one can change text size (larger) dynamically when it comes setFactory method of the TextSwitcher?
Any help would be appreciated.
private View.OnClickListener mOnSwitcherClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
mTextSize = 40;
mTextBody.setText(getString(R.string.text_after_click));
}
};
in above method you are not changing text size you are changing only text
change with
private View.OnClickListener mOnSwitcherClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
mTextSize = 40;
TextView tv = (TextView)mTextBody.getCurrentView();
tv.setText(getString(R.string.text_after_click));
tv.setTextSize(mTextSize);
}
};

How to disable programmatically button by using xml button

i have 4 button create in programmatically button in for loop, is it possible to disable the onclicklistener function or ontouchlistener function by using button create in xml ? Sorry for my einglish, wish can be understand my question.
Sample Image
Code
public class Tab1 extends Fragment {
TextView textView, test1;
Button button, button2;
LinearLayout rl;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
//Returning the layout file after inflating
//Change R.layout.tab1 in you classest
textView = (TextView) h.findViewById(R.id.textView);
test1 = (TextView) h.findViewById(R.id.test1);
button = (Button) h.findViewById(R.id.button);
button2 = (Button) h.findViewById(R.id.button2);
rl = (LinearLayout) h.findViewById(R.id.tlayout);
for (int i = 1; i < 5; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
return h;
}
}
Add all the buttons you created from the for loop into an array.
ArrayList<Buttons> buttons = new ArrayList<Buttons>();
for(int i =1 ; i<5 ; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
buttons.add(YourDynamicallyCreatedButton)
}
Now in your onClick of xml button:
onClick(){
for(Button button: buttons){
button.setEnable(false)
}
}
Note:
When we add a view(button or text..) via xml we give it an id. We can access these elements with this ID. Similarly, when we create buttons dynamically, we need to set id to the buttons/views.
Call setEnable(false) on your Button reference
Example:
Button b ;
..
..
b.setEnable(false);
Keep reference of your programatically added buttons:
Button[] buttons = new Buttons[5];
for(int i =0 ; i<buttons.length ; i++) {
buttons[i] = new Button(getActivity());
final Button btnAddARoom = buttons[i];
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
Now when you want to disable these buttons:
private disableButtons() {
for(int i=0;i<buttons.length;i++) {
buttons[i].setEnabled(false);
}
}
Hope it will help you!
If you have a dedicated parent for the four buttons then you can eliminate the Arralist or array.
LinearLayout ll = ..//your linear layout or any other parent
final int childcount = ll.getChildCount();
for (int i = 0; i < childcount; i++) {
View view = ll.getChildAt(i);
if (view instanceof Button) {
view.setEnabled(false);
}
}

Can a TextView inside LinearLayout container be collapsed and expand on Button click?

I can dynamically add the text fields in my LinearLayout container.
Now I need to collapse those added fields in click of the Button those added fields should get collapse with any label.
Can it be done? Below is the code that i can add the EditText dynamically.
txtHeading = (EditText)findViewById(R.id.heading);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0)
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.list_view, null);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_icon_select);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textIn.setText(null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);
}}});
btnsave = (Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to insert here?
on click to save button in need to collapse all the textView Elements.
let us consider Heading is the label and it is named as Gender so if i added the options as male and female then after clicking the save button this should get collapsed under the Heading.
Based on what I'm kind of guessing that you want... I would create a custom view class based on a linear layout. Each having two edit texts.
Create and add this class to dynamically when the add button is pressed. When the save button is pressed, you can loop over the parent LinearLayout's children and then call a helper method to collapse the second edit text element.
To Loop over the parent and collapse children. Now the CustomElement and the collapseOptionField() call is custom code you must write.
LinearLayout parentLayout = findViewById(...);
for ( int i = 0; i < parentLayout.getChildCount(); i++ ) {
CustomElement ce = parentLayout.getChildAt(i);
ce.collapseOption();
}
Update
Custom class with collapse-able option:
public class TextWithOption extends LinearLayout {
EditText edit0;
EditText edit1;
public TextWithOption(Context context, AttributeSet attrs) {
this(context);
}
public TextWithOption(Context context) {
super(context);
View view = LayoutInflater.from(context).inflate(R.layout.edit_text_option, null);
edit0 = (EditText) view.findViewById(R.id.edit_text0);
edit1 = (EditText) view.findViewById(R.id.edit_text1);
addView(view);
}
public void collapseOption() {
edit1.setVisibility(View.GONE);
}
public void showOption() {
edit1.setVisibility(View.VISIBLE);
}
}
Activity code that will add new options and then collapse them:
final LinearLayout optionsLayout = (LinearLayout) findViewById(R.id.outer_layout);
Button addButton = (Button) findViewById(R.id.button_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextWithOption to = new TextWithOption(context);
optionsLayout.addView(to);
}
});
((Button) findViewById(R.id.button_save)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for ( int i = 0; i < optionsLayout.getChildCount(); i++ ) {
((TextWithOption)optionsLayout.getChildAt(i)).collapseOption();
}
}
});
public static void collapse(final View v) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
v.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
onclick of button write view.collapse
view equals to whatever view you want collapse

set background color for imageview doesn't work

I am trying to change ImageView background when clicked(like Duolingo)
Here is my code from fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frag_repeat, container, false);
final int[] a1 = {0};
final int[] a2 = {0};
final int[] a3 = {0};
final int[] a4 = {0};
TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);
ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);
ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);
textView1.setText(ss[i]);
wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a1[0] = 1;
a2[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a2[0] = 1;
a1[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a3[0] = 1;
a2[0] = 0;
a1[0] = 0;
a4[0] = 0;
}
});
wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a4[0] = 1;
a2[0] = 0;
a3[0] = 0;
a1[0] = 0;
}
});
if(a1[0] > 0){
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a2[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
}else if(a3[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a4[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if(i1 == 1){
wer1.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 2){
wer2.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 3){
wer3.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 4){
wer4.setImageResource(itemsIcon.getResourceId(i, -1));
}
return rootView;
}
But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.
Please share xml file. Maybe you don't set background for ImageView in xml
Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.
From your code, I think what you are trying to do is this:
Whichever button was clicked - change it's background color from white to #42A5F5.
For the other three buttons not clicked, change the background color back to white.
I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.
class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
ImageView _wer1;
ImageView _wer2;
ImageView _wer3;
ImageView _wer4;
Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:
_wer1 = (ImageView) rootView.findViewById(R.id.imageView);
_wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
_wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
_wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.
_wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(0);
}
});
_wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(1);
}
});
_wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(2);
}
});
_wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(3);
}
});
Then implement ToggleBackgroundColors:
void ToggleBackgroundColors(int which)
{
int highlight = Color.parseColor("#42A5F5");
ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
for (int x = 0; x < views.length; x++)
{
int background = (which == x) ? highlight : Color.WHITE;
views[x].setBackgroundColor(background);
}
}
In your onClickListener, you can also set colorFilter on your imageView when you onClick it.
Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.
ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

Remove the View from Layout of ScrollView

I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.
I have tried parent.removeView(myView)
My Code for Layout adding :
LayoutInflater inflater = getLayoutInflater();
final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);
final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);
Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);
ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);
ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);
final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );
note_Title.setText(note_title);
linear_layout.addView(rel_layout_bottom, 0);
JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");
for (int i = 0; i < jsonArray.length(); i++) {
final View view_middle = inflater.inflate(R.layout.layout_middle, linear_layout, false);
final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);
final CheckBox note_check ;
final TextView note_content ;
note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
btn_remove.setOnClickListener(null);
try {
//Getting data correctly here
linear_layout.addView(view_middle,0);
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
linear_layout.addView(rel_layout_top , 0);
}
Any answer Appreciated...Thks
Something like below will solve your problem.
final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
view_middleArr = new View[jsonArray.length()];
view_middleArr[i] = inflater.inflate(R.layout.layout_middle, linear_layout, false);
// ...
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (View view_middle : view_middleArr) {
linear_layout.removeView(view_middle);
}
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
}
Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.
The code
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
is in a for loop.
It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.

Categories

Resources