This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.
Related
Getting error when I use button setOnClickListener, also getting same error when use ViewBinding.
My Mainactivity.java is looks like this
setContentView(R.layout.activity_select_player);
button=(Button) findViewById(R.id.play);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});
findViewById is showing error when I call to set in on button, and thats why I am also cant use setOnClickListener to button.
my activity_main.xml is looks like this
<Button
android:id="#+id/play"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/button_bg"
android:text="#string/play"
android:textColor="#color/white"
android:textSize="34sp"
app:cornerRadius="30dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.672" />
This is the error,
void android.view.View.setOnClickListener(android.view.View$OnClickListener)
The view you are setting as activity's view doesn't match with the one that contains the button you are trying to interact with.
Just replace:
setContentView(R.layout.activity_select_player);
With:
setContentView(R.layout.activity_main);
I know a there is a lot of similar questions out there, but none of them work for me. I want to make a EditText that acts like a Button. I want it to be not editable (and no keyboard should popup). However, when I set an onClickListener(), the EditText doesn't react to my onClick() event. How should I do this correctly?
In my xml I disabled the focusableInTouchMode and I set the clickable to be true. My resulting EditText is not editable, but it doesn't response to my onClickListener().
My xml of the EditText:
<EditText
android:id="#+id/taskviewer_date"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textColorHint="#color/colorShadow"
android:inputType="none"
android:hint="No Due Date"/>
My Code:
EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("taskviewer", "OnClickListener called !");
}
}
);
Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. Make FrameLayout's visibility Gone when you want your edittext to be editable
Use of EditText's setOnClickListener will work if you set setTextIsSelectable to true.
In the XML:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
In the appropriate fragment or the activity:
mTextInpuEditText.setEnabled(true);
mTextInpuEditText.setTextIsSelectable(true);
mTextInpuEditText.setFocusable(false);
mTextInpuEditText.setFocusableInTouchMode(false);
mTextInpuEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Edit Text is clicked");
}
});
Have tried various solutions including those in here:
Have to click a button twice for it to work in Android Studio and here:
I have to click the button twice for it to work
And have tried " android:focusableInTouchMode="false" " on the buttons in the xml file which did not work either.
My current code is as follows:
SplashActivity.java
//THIS BIT IS IN THE ONCREATE METHOD
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(OnClick);
Button bookButton = (Button) findViewById(R.id.book_button);
bookButton.setOnClickListener(OnClick);
}
//THIS BIT IS OUTSIDE OF THE ONCREATE METHOD
private OnClickListener OnClick = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
switch (v.getId()) {
case R.id.enter_button:
handler.removeCallbacksAndMessages(null);
startActivity(intent);
break;
case R.id.book_button:
handler.removeCallbacksAndMessages(null);
intent.putExtra("source", "onClick");
startActivity(intent);
break;
default:
break;
}
}
};
}
activity_splash.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/enter_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Enter"
android:stateListAnimator="#null"
/>
<Button
android:id="#+id/book_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Book"
android:stateListAnimator="#null"
/>
</LinearLayout>
Really can't work out how to work around this!
UPDATE to - activity_splash.xml
So I've narrowed this down to the Java file, it doesn't seem to be the xml code causing the issue as I've trimmed down the xml file to the following and the symptoms are the same:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/enter_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
<Button
android:id="#+id/book_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
I would normally extend SplashActivity to include View.OnClickListener and implement
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
#Override
public void onClick(View view_) {
...
}
Not sure if this would make a difference or not?
I think the focus is the parent view so add the android:descendantFocusability= attribute to your parent view
So after all of the testing I've managed to find out what the problem was.
It was this line of code here which helps with display the image as full screen:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
The SYSTEM_UI_FLAG_HIDE_NAVIGATION flag does not register touch events so you need to change it to SYSTEM_UI_FLAG_IMMERSIVE so it reads as follows:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
Note however this flag was only released after Android 4.4 API Level 19 so won't work in Android versions before this.
Thanks guys for looking into this as well for me, most appreciated.
i wanted to create a terminal/console, where the user can enter commands. I know java, but i'm new to xml, so i wanted to know how i can spawn text under text and if it gets to long it should be scrollable, here's a picture:
and here's my xml cpde for it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d1d1d1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="175dp"
android:layout_height="wrap_content"
android:id="#+id/consoleText"
android:layout_gravity="bottom"
android:hint="Command"
android:layout_weight="0.99" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>
and here's my code for getting the text:
public class FragmentConsole extends Fragment{
EditText text;
Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.console,
container, false);
button = (Button) view.findViewById(R.id.sendButton);
text = (EditText)view.findViewById(R.id.consoleText);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("EditText", text.getText().toString());
}
});
return view;
}
}
So i will use some photoshopped pictures to show what i want to do, so each time i enter a command in this case i will use the example "command one","command two" & "command three", so and if i click the send button after each of them they should apper like this:
so and if the text reaches this black bar:
the above added text should get pushed in a scrollview and every new text will also get part of the scrollview, so that you can scroll through your commands later on.
I know this is a long post and i hope it is clear what i want to do and someone will know how i could do this. So thanks in advance :)
For this task you should consider using ListView and add every command as new row in this view. It'll also take care for scrolling and you'r text wont colide with your EditText.
I have been playing around a lot with the ExpandableListView and I cannot figure out where to add the button listeners for the button that will be the children in the view. I did manage to get a button listener working that uses getChildView() below, but it seems to be the same listener for all the buttons.
The best case scenario is that I would be able to implement the button listeners in the class that instantiates the ExpandableListAdapter class, and not have to put the listeners in the actual ExpandableListAdapter class. At this point I don't even know if that is possible
I have been experimenting with this tutorial/code: HERE
getChildView()
#Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
ChildHolder childHolder;
if (view1 == null)
{
view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);
childHolder = new ChildHolder();
childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
view1.setTag(childHolder);
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();
}
});
}else {
childHolder = (ChildHolder) view1.getTag();
}
childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");
childHolder.section_btn.setTypeface(tf);
return view1;
}
Any help would be much appreciated. Thank you and I will be standing by.
If the buttons are in the ExpandableListView, their listener needs to be in the adapter.
I'm not sure the thrust of your question, but if you are asking how do you relate the button to the contents of the child row, I can answer that. :p
I'll assume a somewhat simple child row layout for demonstration purposes.
child_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="7dip"
android:paddingRight="7dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Then, to get the contents of the row when your button is pressed, you use the button to backtrack to the parent vieew and then get the necessary child views and their contents:
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout ll = (LinearLayout) v.getParent(); // get the view containing the button
TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1); // get the reference to the first widget
TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2); // get the reference to the second widget
String text1 = tv1.getText.toString(); // Get the contents of the first widget to a string
String text2 = tv2.getText.toString(); // Get the contents of the second widget to a string
}
});
If this isn't what you were looking for clarify your question and I'll take another shot at it.