Android DataBinding onClick event not working - java

Tries to add actions to the onClick() event button (with DataBinding). I do everything like in the materials found on the web, but it doesn't work. The onSetLocationClick() method is not called.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="location"
type="com.example.mytracker.model.Location" />
<variable
name="activity"
type="com.example.mytracker.MainActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/tvLongitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text='#{location.longitudeText}'
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/bSetLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:onClick="#{() -> activity.onSetLocationClick()}"
android:text="SET LOCATION"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvLongitude" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
Location location = new Location();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLocation(location);
TextView tvLabel = binding.tvLabel;
location.longitude = 1.1;
}
public void onSetLocationClick() {
location.longitude=99;
}
}
In build.gradle file i was added:
buildFeatures {
dataBinding true
}
I found a few similar threads but found no solution in any of them.
I will add that after changing the declaration
android:onClick="#{() -> activity.onSetLocationClick()}"
to
android:onClick="onSetLocationClick"
and declaration method from
public void onSetLocationClick()
to
public void onSetLocationClick(View view)
the code works fine.
However, I would like to use:
android:onClick="#{() -> activity.onSetLocationClick()}"

You have to call setActivity() on your binding to actually initialize your activity variable.

setContentView(R.layout.activity_main);
Remove this line in your code and check it,

Related

Canhub image is not shown

I use canhub to crop image. Even I load the image, it is not shown. When I log the uri or canhub imageviewer, it is not null. I added the permissions and checked manually, there is no problem about permissions.
My main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.canhub.cropper.CropImageView
android:id="#+id/CropImageView"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_weight="1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="5dp"
tools:layout_editor_absoluteY="61dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
and MainActivity.java
public class MainActivity extends AppCompatActivity {
CropImageView mCropImageView;
Uri mCropImageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCropImageView = (CropImageView)findViewById(R.id.CropImageView);
//ImageView test = findViewById(R.id.test);
Uri uri = Uri.parse("android.resource://com.example.testaygaz/drawable/bird.jpg");
Log.i("MainActivity",mCropImageView.toString());
//test.setImageURI(uri);
mCropImageView.setImageUriAsync(uri);
}
}

Problem with locating button on map (on same activity) in Android

I'm writing an application in Android where background is a map and I want to put on this background some buttons like "Search", "Login".
I have created xml like below:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonLogReg"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="top"
android:layout_weight="10"
android:baselineAligned="false"
android:text="#string/log_amp_reg"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<org.osmdroid.views.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MissingConstraints" >
</org.osmdroid.views.MapView>
<org.osmdroid.views.MapView
android:id="#+id/mapview"
tilesource="Mapnik"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and also Java code like:
public class MainActivity extends Activity {
MapView map = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_main);
map = (MapView)findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(12.5);
GeoPoint startPoint = new GeoPoint(49.20, 19.94);
mapController.setCenter(startPoint);
Button logreg = findViewById(R.id.buttonLogReg);
logreg.setOnClickListener(onClickListener);
}
public void onResume(){
super.onResume();
map.onResume();
}
public void onPause(){
super.onPause();
map.onPause();
}
private View.OnClickListener onClickListener = new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), LoginRegister.class);
startActivity(intent);
}
};
}
So my question is:
Why I can't see the button on android screen, I see only moveable map?
Anybody could help me with this issue?
There are two problem maps in the map sizes. You can edit them.......
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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonLogReg"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="top"
android:layout_weight="10"
android:text="#string/log_amp_reg" />
<org.osmdroid.views.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:ignore="MissingConstraints" >
</org.osmdroid.views.MapView>

setOnItemClickListener() not working on ExpandableHeightGridView in Fragment

Hello good morning in my view i cant handled the setOnItemClickListener
I am trying to create an application which dynamically lists installed applications in a gridview.I am able to display the applications in a gridview form but not able to make these items clickable. Nothing happens when i click these items. The code inside the setOnItemClickListener does not get triggered on click event.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Tab2profile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
//my scrollview
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/MainRLyout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/girislayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/usernameTv"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:textSize="#dimen/text_size_Orta"
android:textColor="#color/cardview_dark_background"
android:layout_height="wrap_content"
tools:ignore="PrivateResource" />
<Button
android:id="#+id/Login_LogOut_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/logout_rounded"
android:drawableRight="#drawable/logout_icon"
android:padding="0dp"
android:text="#string/Logout_Text_str"
android:textColor="#android:color/white"
android:textSize="12sp"
tools:ignore="RelativeOverlap,RtlHardcoded" />
</RelativeLayout>
//in here i cant handled the item click funktşon
<syr13.comapp4.Custum_Adapters.ExpandableHeightGridView
android:id="#+id/Your_Apps_Li"
android:numColumns="3"
android:layout_below="#id/Your_Apps_Tv"
android:horizontalSpacing="6dp"
android:layout_margin="5dp"
android:verticalSpacing="6dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
</syr13.comapp4.Custum_Adapters.ExpandableHeightGridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</FrameLayout>
I think you can Change your Code
from
button.setOnItemClickListener(this);
To
button.setOnClickListener(this);
for example :-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
//Do Something
break;
}
}
}

Custom media player in android

I am trying to make a custom media player where when ever the music start a loader will start and when the music is paused or ends the the loader goes invisible .This is what i am trying to get when the button is clicked ..How to set the desired output.I am using the following gradel
compile 'com.leo.simplearcloader:simplearcloader:1.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
my xml code
<?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"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.union.playerfront1.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/load1">
<com.leo.simplearcloader.SimpleArcLoader
android:visibility="visible"
android:id="#+id/loader"
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_style="simple_arc"
custom:arc_speed="medium"
custom:arc_margin="3dp"
>
</com.leo.simplearcloader.SimpleArcLoader>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_marginBottom="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
my java code
public class MainActivity extends AppCompatActivity {
boolean isPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer =MediaPlayer.create(this,R.raw.song);
final RelativeLayout real=(RelativeLayout)findViewById(R.id.load1);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed){
button.setBackgroundResource(R.drawable.profile);
real.setVisibility(View.VISIBLE);
mediaPlayer.start();
}
}
});
}
}
How I can change the visibility of the loader on the base that a song is playing or it is stopped I am able to set the button visibility properly.How i can set the visibility on the basis of song pause,song start,song stop?

Android Sliding Drawer Open On Create

I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here

Categories

Resources