android. run two events with an action - android

I have a RelativeLayout in FragmentActivity that looks like this:
<?xml version="1.0" encoding="utf-8"?>
...
<com.my.android.Myview
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</com.my.android.Myview>
I have a OnTouchListener in com.my.android.Myview.
And OnClickListener for RelativeLayout in FragmentActivity .
The problem is that I can not call the two events.
I managed to call one if not the other. But not the two events

Im not sure if what you are trying is a good idea.
Anyways, you can try something like this:
OnClickListener parent= new OnClickListener() {
public void onClick(View v) {
//whatever
}
}
OnClickListener child= new OnClickListener() {
public void onClick(View v) {
parent.onClick(null);
//whatever else
}
}
parentView.setOnClickListener(parent);
childView.setOnClickListener(child);

Related

Scrollto method from ScrollView not working as it should

I have a Linearlayouts, a ScrollView and a button, I am not able to figure out why when I programmatically add a Checkbox to the Linearlayout, at the end of the view, the method from the scrollview
scrollview.scrollto(0, ScrollView.FOCUS_DOWN);
will not focus the last item added.
here the XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
tools:context="com.eidotab.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:padding="10dp"
android:layout_weight="30">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST"/>
</LinearLayout>
and here the java:
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
LinearLayout linearLayout;
Button button;
int num = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroll);
linearLayout = (LinearLayout) findViewById(R.id.linear);
button = (Button) findViewById(R.id.boton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
Im also addind the .rar so you just have to download it and try it.. I have spent several hours trying to make it work.. also tried to get the specific child from the LinearLayout with the
y = Linearlayout.getchildat(N).getBottom();
and works similar to the FOCUS_DOWN... I can never see the very last item.. only the item before the last.
Thanks in advance for the help.
Here is the program
https://www.dropbox.com/home/Public?preview=MyApplication.rar
Well after some research, seems like there is some kind of problem when trying to perform scrolls programmatically and the solution I have found that work best is to add a runable inside the onClick method in my code so I added
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
and the end result was
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
//scrollView.fullScroll(ScrollView.FOCUS_DOWN);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
it is now working flawlessly but that said, I don´t think it is the ideal solution.

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

range bar on click listener dosen't work

Im using this library for range bar:
https://github.com/oli107/material-range-bar
the OnClickListener() does not work. This is my xml:
<com.appyvet.rangebar.RangeBar
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/rangebar"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="60dp"
android:onClick="Click"
/>
with android:onClick="Click" and with this function:
public void Click(View v) {
Log.e(TAG,"come");
userScroll=false;
}
it doesn't come inside function even with this function:
rangeBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
I want to use this function and not a setOnRangeBarChangeListener().
Please try using one either use Click or setOnClickListener.
Dont use Both as they contradict.
Hope it helps.

How to do something like "Touch anywhere to continue"?

How would you do something like "Touch Anywhere to Continue."
Set an onclick listener for the layout of the entire screen
It's as simple as this.
Give an id to "main" layout
set the listener to the main layout
<?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="match_parent"
tools:context=".MainActivity"
android:id="#+id/mainLayout"> //<--- Provide ID
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
In this case, the main element is RelativeLayout. So add the listener to RelativeLayout to implement Touch anywhere to continue.
public class MainActivity extends AppCompatActivity {
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout)findViewById(R.id.mainLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}

Android - change textView at run time

I´m working with a lineal layout and I´m trying to change the text of a TextView but it doesn´t refresh. When I debug I´ve checked that the text have changed correctly.
Thanks
public class Position extends Activity {
Button botonempezar;
Button botonsalir;
TextView text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.position);
botonsalir = (Button) findViewById(R.id.salir);
botonempezar = (Button) findViewById(R.id.Empezar);
text = (TextView) findViewById(R.id.Textoposicion);
botonsalir.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onDestroy();
finish();
}
});
botonempezar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchPosition();
}
});
}
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}
private void determinatePosition(){
....
this.text.setText("New text");
//this.text.invalidate();
this.text.postInvalidate();
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT);// this doesnt work neither.
....
}
Here I post the code of the xml file. Its really silly but it could be the problem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="#+id/Textoposicion"
android:text="Posición desconocida"
android:layout_width="fill_parent"
android:layout_marginTop="20dip"
android:inputType="text">
</TextView>
<Button android:layout_height="wrap_content"
android:id="#+id/Empezar" android:text="Empezar"
android:layout_width="fill_parent"
android:layout_marginTop="20dip">
</Button>
<Button
android:layout_height="wrap_content"
android:id="#+id/salir"
android:layout_marginTop="20dip"
android:text="Finalizar programa"
android:gravity="center" android:layout_width="fill_parent">
</Button>
</LinearLayout>
I have edited the post because I omitted part of the code to make it simpler but I think I may suppressed the problem too. It runs in a endless loop, could be this the mistake?
Try to call invalidate on the view.
http://developer.android.com/reference/android/view/View.html#invalidate()
this.text.invalidate();
This will cause a redraw.
Sorry, too quick, I saw that you already tried that.
Didn't that work? Why did you comment that away?
You forgot to call show() method on Toast :). Try this:
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT).show();
Try not using getBaseContext(). Use Position.this
Well now your infinite loop is caused by
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}
Try moving the toast into public void onClick(View v)
Also, show us your imports

Categories

Resources