I'm using the umano SlidingUpPanel library and I used an imagebutton in the panel but nothing happens when I click the imagebutton, I tried the imagebutton in another activity which worked perfectly. "next" is supposed to be printed to the logs when the button is pressed. I've searched all over but can't find any solution
Here's my code:
#SuppressLint("Registered")
public class NowPlaying extends Activity {
ImageButton play, pause, play_main, pause_main, next, imgbtn;
private MusicService musicSrv;
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.now_playing);
play = findViewById(R.id.play_button);
imgbtn = findViewById(R.id.imgbtn);
imgbtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ShowToast")
#Override
public void onClick(View v) {
Log.i("oladapos", "next");
}
});
SlidingUpPanelLayout now_playing =
findViewById(R.id.slideup_nowplaying);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playNext();
}
});
}
//play next
private void playNext(){
musicSrv.playNext();
}
//play previous
private void playPrev(){
musicSrv.playPrev();
}
#Override
public void start() {
}
#Override
public void pause() {
}
#Override
public int getDuration() {
return 0;
}
#Override
public int getCurrentPosition() {
return 0;
}
#Override
public void seekTo(int pos) {
}
#Override
public boolean isPlaying() {
return false;
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public boolean canPause() {
return false;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public int getAudioSessionId() {
return 0;
}
}
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/slideup_nowplaying"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".Fragments.songsFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/songs_cover"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true"
android:orientation="horizontal"
android:focusable="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/songs_cover_one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:src="#drawable/songs_cover"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="202dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout3"
app:layout_constraintStart_toEndOf="#+id/songs_cover_one"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/songs_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:lines="1"
android:text="#string/havana_camila_cabello_song"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<TextView
android:id="#+id/songs_artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:text="#string/camila_cabello" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.974"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<Button
android:id="#+id/play_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:scaleType="centerInside"
android:src="#drawable/round_play_arrow_black_48dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/forword_button"
android:id="#+id/imgbtn"/>
</RelativeLayout>
</RelativeLayout>
Related
so I am trying to create an activity, and then add onclick listeners to it, but it wont let me refer. So the moment I open this activity in my app, my app crashes, saying 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference'
I have no idea why this is happening. I have correctly names them in accordance with the xml file as well.
Please help.
public class SubtaskActivity extends AppCompatActivity {
EditText etSubtaskName;
Button btnDone;
RadioGroup radgrpPri, radgrpTime;
RadioButton radbtnPriHigh, radbtnPriMed, radbtnPriLow, radbtnTimeMore, radbtnTimeMed, radbtnTimeLess;
boolean priHigh, priMed, priLow, timeMore, timeMed, timeLess;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnDone = findViewById(R.id.btnDone);
radgrpPri = findViewById(R.id.radgrpPri);
radgrpTime = findViewById(R.id.radgrpTime);
radbtnPriHigh = findViewById(R.id.radbtnPriHigh);
radbtnPriMed = findViewById(R.id.radbtnPriMed);
radbtnPriLow = findViewById(R.id.radbtnPriLow);
radbtnTimeMore = findViewById(R.id.radbtnTimeMore);
radbtnTimeMed = findViewById(R.id.radbtnTimeMed);
radbtnTimeLess = findViewById(R.id.radbtnTimeLess);
etSubtaskName = findViewById(R.id.etSubtaskName);
radgrpPri.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnPriHigh.isChecked())
{
priHigh = true;
priLow = false;
priMed = false;
}
else if (radbtnPriMed.isChecked())
{
priHigh = false;
priLow = false;
priMed = true;
}
else if (radbtnPriLow.isChecked())
{
priHigh = false;
priLow = true;
priMed = false;
}
}
});
radgrpTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnTimeMore.isChecked())
{
timeMore = true;
timeMed = false;
timeLess = false;
}
else if (radbtnTimeMed.isChecked())
{
timeMore = false;
timeMed = true;
timeLess = false;
}
else if (radbtnTimeLess.isChecked())
{
timeMore = false;
timeMed = false;
timeLess = true;
}
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = etSubtaskName.getText().toString().trim();
Intent intent = new Intent(SubtaskActivity.this, TaskInfo.class);
intent.putExtra("subtaskName", name);
intent.putExtra("priHigh", priHigh);
intent.putExtra("priMed", priMed);
intent.putExtra("priLow", priLow);
intent.putExtra("timeMore", timeMore);
intent.putExtra("timeMed", timeMed);
intent.putExtra("timeLess", timeLess);
startActivity(intent);
}
});
}
}
XML File :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/it"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:orientation="vertical">
<TextView
android:id="#+id/tvSubtaskPriorityHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/priority_of_subtask"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<RadioGroup
android:id="#+id/radgrpPri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnPriHigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/high"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriLow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/low"
android:textColor="#color/white" />
</RadioGroup>
<TextView
android:id="#+id/tvTimeWeightHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/time_this_subtask_may_consume"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/floating_hint_time_minutes"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:hintTextAppearance="#style/FlotatingHintStyle">
<EditText
android:id="#+id/etSubtaskName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:fontFamily="#font/roboto"
android:hint="#string/name_your_subtask"
android:inputType="textPersonName"
android:maxLength="20"
android:textColor="#color/white"
android:textColorHint="#B8AEAE"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<RadioGroup
android:id="#+id/radgrpTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnTimeMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/more"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeLess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/less"
android:textColor="#color/white" />
</RadioGroup>
</LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:gravity="center_horizontal"
android:text="#string/done"
app:backgroundTint="#color/orange_accent" />
</LinearLayout>
You didn't call in onCreate method setContentView(R.layout.youractivity). If you didn't, Android doesn't know what to render, so there are no views for you to provide.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
}
just set your (xml)layout file to setContentView in onCreate()
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
}
The program is supposed to run getCurrentLocation and then set the textView "coordinates" to the value of the coordinates called. That text view is part of a pager view that draws from a separate .xml file than the main layout "activity_main".
public class MainActivity extends AppCompatActivity
{
CardAdapter CardAdapter;
String coordinatesFinal;
FusedLocationProviderClient fusedLocationProviderClient;
TextView coordinates;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
getCurrentLocation();
}else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);
}
setupCardItems();
ViewPager2 CardViewPager = findViewById(R.id.cardViewPager);
CardViewPager.setAdapter(CardAdapter);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults)
{
if (requestCode == 100 & grantResults.length > 0 && (grantResults[0] + grantResults[1]) == PackageManager.PERMISSION_GRANTED)
{
getCurrentLocation();
}else
{
Toast.makeText(getApplicationContext(), "Permission denied.",Toast.LENGTH_SHORT).show();
}
}
#SuppressLint("MissingPermission")
public void getCurrentLocation()
{
coordinates = findViewById(R.id.coordinates);
LocationManager locationManager = (LocationManager) getSystemService
(
Context.LOCATION_SERVICE
);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(task ->
{
Location location = task.getResult();
if (location != null)
{
coordinates.setText(location.getLatitude()+", "+location.getLongitude());
}else
{
LocationRequest locationRequest = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(10000).setFastestInterval(1000).setNumUpdates(1);
LocationCallback locationCallback = new LocationCallback()
{
#Override
public void onLocationResult(LocationResult locationResult)
{
Location location1 = locationResult.getLastLocation();
coordinatesFinal = location1.getLatitude()+", "+location1.getLongitude();
}
};
fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback, Looper.myLooper());
}
});
}else
{
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
//card view
private void setupCardItems()
{
List<CardItem> CardItems = new ArrayList<>();
CardItem itemPageOne = new CardItem();
itemPageOne.setLocation("Home");
itemPageOne.setTemp("60°f");
itemPageOne.setWeatherDesc("Cloudy");
itemPageOne.setWeatherIcon(R.drawable.cloud);
itemPageOne.setHum("75%");
itemPageOne.setVis("10");
itemPageOne.setPrecip("50%");
itemPageOne.setDew("40°f");
itemPageOne.setCloud("80%");
itemPageOne.setFog("75%");
CardItem itemPageTwo = new CardItem();
itemPageTwo.setLocation("Madison");
itemPageTwo.setTemp("60°f");
itemPageTwo.setWeatherDesc("Sunny");
itemPageTwo.setWeatherIcon(R.drawable.sun);
itemPageTwo.setHum("75%");
itemPageTwo.setVis("10");
itemPageTwo.setPrecip("50%");
itemPageTwo.setDew("40°f");
itemPageTwo.setCloud("80%");
itemPageTwo.setFog("75%");
CardItem itemPageThree = new CardItem();
itemPageThree.setLocation("Milwaukee");
itemPageThree.setTemp("60°f");
itemPageThree.setWeatherDesc("Rainy");
itemPageThree.setWeatherIcon(R.drawable.rain);
itemPageThree.setHum("75%");
itemPageThree.setVis("10");
itemPageThree.setPrecip("50%");
itemPageThree.setDew("40°f");
itemPageThree.setCloud("80%");
itemPageThree.setFog("75%");
CardItems.add(itemPageOne);
CardItems.add(itemPageTwo);
CardItems.add(itemPageThree);
CardAdapter = new CardAdapter(CardItems);
}
}
I have narrowed it down to the line that doesn't work properly:
coordinates.setText(location.getLatitude()+", "+location.getLongitude());
I have tested the exact same line as above, only using a textView from inside activity main, which works just fine. Any help is greatly appreciated.
Edit: The file actually does crash, I mistakenly said it didn't, and I added both .xml Files:
activity_main.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"
android:background="#566D8F"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="15dp"
android:paddingBottom="15dp">
<ImageButton
android:id="#+id/settingsBar2"
android:layout_width="27sp"
android:layout_height="27sp"
android:layout_gravity="left"
android:layout_marginStart="20sp"
android:layout_marginEnd="70sp"
android:background="#color/background"
android:cropToPadding="true"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/homeName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="#string/app_name"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:typeface="normal" />
<ImageButton
android:id="#+id/settingsBar"
android:layout_width="27sp"
android:layout_height="27sp"
android:layout_gravity="right"
android:layout_marginStart="70sp"
android:layout_marginEnd="20sp"
android:background="#drawable/settings_handle"
android:cropToPadding="true"
android:scaleType="fitCenter" />
</LinearLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/cardViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/topBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_container_card.xml:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<androidx.cardview.widget.CardView
android:id="#+id/pagerCard"
android:layout_width="match_parent"
android:layout_height="440dp"
android:layout_margin="20dp"
app:cardBackgroundColor="#EAC0A0"
app:cardCornerRadius="30dp"
app:cardElevation="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/locationName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-medium"
android:gravity="center"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/coordinates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center"
android:textColor="#color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/temperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:gravity="center"
android:textColor="#color/white"
android:textSize="50sp" />
<TextView
android:id="#+id/weatherDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageView
android:id="#+id/imageOnboarding"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="15dp"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="#+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-light"
android:paddingLeft="30sp"
android:paddingBottom="5sp"
android:text="Humidity"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/humVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingEnd="15sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:paddingStart="15sp"
android:paddingEnd="10sp"
android:paddingBottom="5sp"
android:text="Visibility"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/visVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingRight="30sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="#+id/precipitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-light"
android:paddingLeft="30sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:text="Precip. "
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/precipVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingTop="5sp"
android:paddingEnd="15sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/dewPoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:paddingStart="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:text="Dew Point"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/dewVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingTop="5sp"
android:paddingRight="30sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="#+id/cloud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-light"
android:paddingLeft="30sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:text="Cloud"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/cloudVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingTop="5sp"
android:paddingEnd="15sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/fog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:paddingStart="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:text="Fog"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="#+id/fogVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:fontFamily="sans-serif-condensed-medium"
android:paddingStart="10sp"
android:paddingTop="5sp"
android:paddingEnd="30sp"
android:paddingBottom="5sp"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</TableRow>
</TableLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Edit 2: CardAdapter.java and CardItem.java
CardAdapter.java:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.cardViewHolder>
{
private List<CardItem> CardItems;
public CardAdapter(List<CardItem> CardItems) {
this.CardItems = CardItems;
}
#NonNull
#Override
public cardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new cardViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_container_card, parent, false
)
);
}
#Override
public void onBindViewHolder(#NonNull cardViewHolder holder, int position) {
holder.setCardData(CardItems.get(position));
}
#Override
public int getItemCount() {
return CardItems.size();
}
class cardViewHolder extends RecyclerView.ViewHolder
{
private TextView locationText;
private TextView weatherDesc;
private ImageView weatherIcon;
private TextView tempVal;
private TextView humVal;
private TextView visVal;
private TextView precipVal;
private TextView dewVal;
private TextView cloudVal;
private TextView fogVal;
public cardViewHolder(#NonNull View itemView)
{
super(itemView);
locationText = itemView.findViewById(R.id.locationName);
weatherDesc = itemView.findViewById(R.id.weatherDesc);
weatherIcon = itemView.findViewById(R.id.imageOnboarding);
tempVal = itemView.findViewById(R.id.temperature);
humVal = itemView.findViewById(R.id.humVal);
visVal = itemView.findViewById(R.id.visVal);
precipVal = itemView.findViewById(R.id.precipVal);
dewVal = itemView.findViewById(R.id.dewVal);
cloudVal = itemView.findViewById(R.id.cloudVal);
fogVal = itemView.findViewById(R.id.fogVal);
}
void setCardData(CardItem CardItem)
{
locationText.setText(CardItem.getLocation());
weatherDesc.setText(CardItem.getWeatherDesc());
weatherIcon.setImageResource(CardItem.getWeatherIcon());
tempVal.setText(CardItem.getTemp());
humVal.setText(CardItem.getHum());
visVal.setText(CardItem.getVis());
precipVal.setText(CardItem.getPrecip());
dewVal.setText(CardItem.getDew());
cloudVal.setText(CardItem.getCloud());
fogVal.setText(CardItem.getFog());
}
}
}
CardItem.java:
package com.example.viewpagertest;
public class CardItem
{
private int weatherIcon;
private String location;
private String temp;
private String weatherDesc;
private String hum;
private String vis;
private String precip;
private String dew;
private String cloud;
private String fog;
//weather icon
public int getWeatherIcon() {
return weatherIcon;
}
public void setWeatherIcon(int weatherIcon) {
this.weatherIcon = weatherIcon;
}
//location title
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
//temperature value
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
//weather description
public String getWeatherDesc() {
return weatherDesc;
}
public void setWeatherDesc(String weatherDesc) {
this.weatherDesc = weatherDesc;
}
//humidity value
public String getHum() {
return hum;
}
public void setHum(String hum) {
this.hum = hum;
}
//visibility value
public String getVis() {
return vis;
}
public void setVis(String vis) {
this.vis = vis;
}
//precipitation value
public String getPrecip() {
return precip;
}
public void setPrecip(String precip) {
this.precip = precip;
}
//dew point value
public String getDew() {
return dew;
}
public void setDew(String dew) {
this.dew = dew;
}
//cloud value
public String getCloud() {
return cloud;
}
public void setCloud(String cloud) {
this.cloud = cloud;
}
//fog value
public String getFog() {
return fog;
}
public void setFog(String fog) {
this.fog = fog;
}
}
Here's the Console when running a debugger with the breakpoint at
coordinates.setText(location.getLatitude()+", "+location.getLongitude());
The whole console breaks the character limit so here's a document with the console text.
Console Text
if (location != null)
{
// coordinates.setText(location.getLatitude()+", "+location.getLongitude());
cardAdapter.setCoordinates(0 /*the cardItem number you want to update*/, location.getLatitude()+", "+location.getLongitude());
}
Since the coordinates TextView is in your item xml, you will not be able to address it from the root of your MainActivity, but from your item.
Update
Thanks for adding the Adapter code:
Easiest would be to create a method in your adapter like the following:
setCoordinates(int position, String coords) {
holders.get(position).itemView.findViewById(R.id.coordinates).setText(coords);
}
To be able to access the holders on runtime, add the following:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.cardViewHolder>
{
private List<CardItem> cardItems;
private List<CardViewHolder> holders;
....
#Override
public cardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
CardViewHolder holder = new cardViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_container_card, parent, false
)
);
holders.add(holder);
return holder;
}
Another option would be to add the field coordinates to your CardItem and write the setCoordinates method like so:
setCoordinates(int position, String coords) {
cardItems.get(position).setCoordinates(coords);
notifyDataSetChanged(); // to reload the holders, only notifying the specific position will be faster to reload only the changed holder
}
You need to update the adapter items by creating a method in CardAdapter and calling it from main activity as below -
Create this method in your adapter class
public void updateAdapter(List<CardItem> CardItems) {
this.CardItems = CardItems;
notifyDataSetChanged();
}
Replace below line
CardAdapter = new CardAdapter(CardItems);
With this one
CardAdapter.updateAdapter(CardItems);
I've been playing with bindings (in actions and fragments) until I managed to get it (partially) working. I am binding six variables (and they are properly shown in the application), but generated BR file has only three definitions (and only one of them corresponds to one of my variables)
My main layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content main:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layout of the fragment:
<?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="data"
type="Data" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentFloor0">
<TextView
android:id="#+id/label_floor0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:layout_marginEnd="#dimen/margin_small"
android:text="#string/label_floor0"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageGroundFloor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/label_floor0"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/ground_floor_v2"
android:scaleType="centerInside"
android:contentDescription="#string/blueprint_0th_floor" />
<TextView
android:id="#+id/liviTempStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_liviTempStr}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.788"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.32" />
<TextView
android:id="#+id/liviTempTel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_liviTempTel}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<TextView
android:id="#+id/kitcTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_kitcTempSin}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.32"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<TextView
android:id="#+id/bathTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_bathTempSin}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.8" />
<TextView
android:id="#+id/hallTempSin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallTempEx1}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.62"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.39" />
<TextView
android:id="#+id/hallTempEx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallTempEx1}"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
android:rotation="90"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.55" />
<TextView
android:id="#+id/hallHummEx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.fl0_hallHummEx1}"
android:rotation="90"
android:textSize="#dimen/textSizeRegular"
app:layout_constraintBottom_toBottomOf="#id/imageGroundFloor"
app:layout_constraintEnd_toEndOf="#id/imageGroundFloor"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toStartOf="#id/imageGroundFloor"
app:layout_constraintTop_toTopOf="#id/imageGroundFloor"
app:layout_constraintVertical_bias="0.65" />
<Button
android:id="#+id/button_floor0_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_small"
android:layout_marginBottom="#dimen/margin_small"
android:text="#string/button_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Data binding class:
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
public class HomematicData extends BaseObservable {
private float fl0_liviTempStr;
private float fl0_liviTempTel;
private float fl0_kitcTempSin;
private float fl0_bathTempSin;
private float fl0_hallTempEx1;
private float fl0_hallHummEx1;
#Bindable
public float getFl0_liviTempStr() { return fl0_liviTempStr; }
public float getFl0_liviTempTel() { return fl0_liviTempTel; }
public float getFl0_kitcTempSin() { return fl0_kitcTempSin; }
public float getFl0_bathTempSin() { return fl0_bathTempSin; }
public float getFl0_hallTempEx1() { return fl0_hallTempEx1; }
public float getFl0_hallHummEx1() { return fl0_hallHummEx1; }
public void setFl0_liviTempTel(float fl0_liviTempTel) { this.fl0_liviTempTel = fl0_liviTempTel; /* notifyPropertyChanged(BR.fl0_liviTempTel); */ }
public void setFl0_liviTempStr(float fl0_liviTempStr) { this.fl0_liviTempStr = fl0_liviTempStr; /* notifyPropertyChanged(BR.fl0_liviTempStr); */ }
public void setFl0_kitcTempSin(float fl0_kitcTempSin) { this.fl0_kitcTempSin = fl0_kitcTempSin; /* notifyPropertyChanged(BR.fl0_kitcTempSin); */ }
public void setFl0_bathTempSin(float fl0_bathTempSin) { this.fl0_bathTempSin = fl0_bathTempSin; /* notifyPropertyChanged(BR.fl0_ballTempSin); */ }
public void setFl0_hallTempEx1(float fl0_hallTempEx1) { this.fl0_hallTempEx1 = fl0_hallTempEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }
public void setFl0_hallHummEx1(float fl0_hallHummEx1) { this.fl0_hallHummEx1 = fl0_hallHummEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }
Main activity:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Activity:
public class FragmentFloor0 extends Fragment {
private FragmentFloor0Binding binding;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_floor0, container, false);
View rootView = binding.getRoot();
HomematicData hd = new HomematicData();
hd.setFl0_liviTempStr(1.0f);
hd.setFl0_liviTempTel(2.0f);
hd.setFl0_bathTempSin(3.0f);
hd.setFl0_kitcTempSin(4.0f);
hd.setFl0_hallTempEx1(5.0f);
hd.setFl0_hallHummEx1(6.0f);
binding.setData(hd);
return rootView;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_floor0_next).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NavHostFragment.findNavController(FragmentFloor0.this)
.navigate(R.id.action_FragmentFloor0_to_FragmentFloor1);
}
});
}
}
Binding Adaptor:
public class BindingUtils {
#BindingAdapter("android:text")
public static void setFloat(TextView view, float value) {
if (Float.isNaN(value)) view.setText("");
else view.setText(String.format("%.1f", value));
}
#InverseBindingAdapter(attribute = "android:text")
public static float getFloat(TextView view) {
String num = view.getText().toString();
if(num.isEmpty()) return 0.0F;
try {
return Float.parseFloat(num);
} catch (NumberFormatException e) {
return 0.0F;
}
}
and generated BR:
package androidx.databinding.library.baseAdapters;
public class BR {
public static final int _all = 0;
public static final int data = 1;
public static final int fl0_liviTempStr = 2;
}
where are all other constants?!
I have clean-build and rebuild project, as well as invalidated the cache and rebuild the project.
It looks to me that #Bindable needs to be added before every getter
I want to show horizontal items in recyclerview, but I've tried but no work it all, only show me horizontal items with enlarge widh.
In Android Studio show as I want
But in runtime in my movile, show me
The items is width wraped but the other items appear after scroll right separated of my previus item.
This is my code.
LIST_ITEM
<?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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearFondo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/item_list_sel"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ruta"
tools:srcCompat="#drawable/ruta" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And My Xml to show the recyclerview
<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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="sitetech.NFCcheckPoint.ui.operador.CheckFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/SuccessColor"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/tfecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Fecha"
android:textSize="20sp" />
<Button
android:id="#+id/bpruebas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasar Tarjeta" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ruta para el registro" />
<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
android:id="#+id/rlista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#888"
android:dividerHeight="0dp"
android:fadeScrollbars="true"
android:orientation="horizontal"
android:overScrollMode="never"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="horizontal"
app:itemSpace="0dp"
tools:listitem="#layout/ruta_sel_template">
</com.omega_r.libs.omegarecyclerview.OmegaRecyclerView>
</LinearLayout>
</LinearLayout>
And my Fragment code to load
dataAdapter = new rutaSelAdapter(lista, new onItemClick() {
#Override
public void onClickItemList(View v, int position) {
ToastHelper.info(lista.get(position).getNombre().toString());
}
});
rlista.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rlista.setAdapter(dataAdapter);
And my dataAdapter class
public class rutaSelAdapter extends OmegaRecyclerView.Adapter<rutaSelAdapter.ViewHolder> {
public List<Ruta> lista;
private onItemClick onItemClick;
private Ruta selectedItem;
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
}
#Override
public int getItemCount() {
return lista.size();
}
#Override
public rutaSelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new rutaSelAdapter.ViewHolder(parent);
}
#Override
public void onBindViewHolder(rutaSelAdapter.ViewHolder holder, int position) {
Ruta dato = lista.get(position);
holder.display(dato);
}
public void setSelectedItem(Ruta rx){
selectedItem = rx;
}
public Ruta getSelectedItem(){
return selectedItem;
}
public void updateData(Ruta bx) {
boolean nuevo = true;
for (Ruta rx : lista) {
if (rx.getId() == bx.getId()) {
lista.set(lista.indexOf(rx), bx);
nuevo = false;
}
ToastHelper.info("Se a modificado el horario.");
}
if (nuevo) {
lista.add(bx);
ToastHelper.exito("Horario asignado.");
}
notifyDataSetChanged();
}
public void deleteData(Ruta rx) {
//ToastHelper.normal("Se a eliminado el horario " + rx.getHora());
lista.remove(rx);
notifyDataSetChanged();
}
public class ViewHolder extends SwipeViewHolder implements View.OnClickListener {
private final TextView tnombre;
private LinearLayout linearFondo;
HorarioDao horarioManager = AppController.daoSession.getHorarioDao();
private Ruta currentItem;
public ViewHolder(ViewGroup itemView) {
super(itemView, R.layout.ruta_sel_template, SwipeViewHolder.NO_ID, SwipeViewHolder.NO_ID);
tnombre = findViewById(R.id.tnombre);
linearFondo = findViewById(R.id.linearFondo);
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setSelectedItem(currentItem);
onItemClick.onClickItemList(v, getAdapterPosition());
}
});
}
private Ruta backItem;
private void setSelection(Ruta rx){
//if (backItem == null)
}
#Override
public void onClick(final View v) {
}
public void display(Ruta rx) {
currentItem = rx;
if (rx.getNombre() == null) tnombre.setText("");
else tnombre.setText(rx.getNombre().toString());
}
}
}
In Android Studio show me right as I want.
But in runtime, in my mobile the items have full width but wrap content.enter image description here
i think you just add notifyDataSetChanged in your class adapter
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
notifyDataSetChanged();
}
In your recyclerview item layout update width of these 2 textviews [tnombre and thorarios] to wrap_content
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
If someone else have this issue, I fixed adding this line in the fragment class.
rlista.setHasFixedSize(true);
I have a floating action button in the following layout file activity_register :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<FrameLayout
android:paddingTop="70dp"
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<android.support.v7.widget.CardView
android:id="#+id/cv_RegisterAdd"
app:cardBackgroundColor="#009688"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="320dp"
app:cardCornerRadius="6dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:text="REGISTER"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
/>
<LinearLayout
android:layout_marginTop="10dp"
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#f0f7f4"
android:layout_width="match_parent"
android:theme="#style/TextLabel"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="Username"
android:textColor="#f0f7f4"
android:id="#+id/txtCurUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#FFFFFF"
android:theme="#style/TextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="Password"
android:textColor="#f0f7f4"
android:id="#+id/txtNewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#f0f7f4"
android:theme="#style/TextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f0f7f4"
android:hint="Repeat Password"
android:id="#+id/txtRepeatPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:stateListAnimator="#drawable/state_list_animator_z"
android:id="#+id/btn_Register"
android:text="REGISTER"
android:textColor="#009688"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#drawable/register_btnshape"
>
</Button>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fb_Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="#drawable/plus_x"
android:transitionName="loginFab"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</RelativeLayout>
In the activity I have set a Click event on the floating action button.
I have been debugging and checking to see if the event is ever fired and the click event is never fired. The RegisterActivity code is as follows:
public class RegisterActivity extends AppCompatActivity
{
#BindView(R.id.fb_Cancel)
FloatingActionButton mClose_fb;
#BindView(R.id.btn_Register)
Button mRegister_btn;
#BindView(R.id.cv_RegisterAdd)
CardView mRegister_cv;
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
getSupportActionBar().hide();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
showEnterAnimation();
}
mClose_fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
animationRevealClose();
}
});
}
public void showEnterAnimation()
{
Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
getWindow().setSharedElementEnterTransition(transition);
transition.addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
mRegister_cv.setVisibility(View.GONE);
}
#Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
animateRevealShow();
}
#Override
public void onTransitionCancel(Transition transition) {
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});
}
public void animationRevealClose()
{
Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv,mRegister_cv.getWidth()/2,0, mRegister_cv.getHeight(), mClose_fb.getWidth() / 2);
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter()
{
#Override
public void onAnimationEnd(Animator animation)
{
mRegister_cv.setVisibility(View.INVISIBLE);
super.onAnimationEnd(animation);
mClose_fb.setImageResource(R.drawable.plus);
RegisterActivity.super.onBackPressed();
}
#Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
}
public void animateRevealShow()
{
Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv, mRegister_cv.getWidth()/2,0, mClose_fb.getWidth() / 2, mRegister_cv.getHeight());
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter()
{
#Override
public void onAnimationEnd(Animator animation)
{
super.onAnimationEnd(animation);
}
#Override
public void onAnimationStart(Animator animation)
{
mRegister_cv.setVisibility(View.VISIBLE);
super.onAnimationStart(animation);
}
});
mAnimator.start();
}
#Override
public void onBackPressed() {
animationRevealClose();
}
the method which annotated with #OnClick should be out of onCreate.
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
//--------------------------TODO---------------------//
//----------Set a click event for the floating action button
}
#OnClick({R.id.btn_Login, R.id.fb_Register})
public void onClick(View view)
{
}
#OnClick(R.id.take_photo) void openCameraIntent()
{
openCameraIntent1();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialize();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
}
private void initialize()
{
btnSavePhoto = findViewById(R.id.btn_save);
photoView = findViewById(R.id.imageView);
photoViewUnuse = findViewById(R.id.imageViewUnuse);
previewPhotoText = findViewById(R.id.preview_text);
btnSavePhoto.setOnClickListener(view -> submitDataToServer());
ButterKnife.bind(this);
}