I use an Android 4.4 and having trouble implementing a spinner.
Problem: the Spinner is not setting the selected item when it's choosen from the list.
according to this and houndred other posts I tried no solution seems to work for me, am I missing something very important that I'm just not realizing?.
What the program should do: I do want to scan for wifi networks and want the user to select a wifi connection from the spinner and set it.
How it is right now: The Spinner shows the available wifi networks but when I click at one it is not selected.
public class SetupActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener
{
private WifiManager wifiManager;
private ListView listView;
private Button buttonScan;
private int size = 0;
private List<ScanResult> results;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter adapter;
private Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_setup );
buttonScan = findViewById(R.id.btnScan);
buttonScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
scanWifi();
}
});
spinner = findViewById(R.id.spinnerSSID);
listView = findViewById(R.id.wifiList);
wifiManager = (WifiManager) getApplicationContext().getSystemService( Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(this, "WiFi is disabled ... We need to enable it", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
scanWifi();
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(this);
}
private void scanWifi() {
arrayList.clear();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
Toast.makeText(this, "Scanning WiFi ...", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
results = wifiManager.getScanResults();
unregisterReceiver(this);
for (ScanResult scanResult : results) {
arrayList.add(scanResult.SSID + " - " + scanResult.capabilities);
adapter.notifyDataSetChanged();
}
}
};
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Here the xml from the layout:
<?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:tint="?attr/colorControlNormal"
tools:context=".SetupActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DeviceID: 1234abcd"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Setup"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Content Installed!"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose which Setup"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="240dp"
android:layout_marginTop="196dp"
android:text="Local"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="196dp"
android:layout_marginEnd="240dp"
android:text="Online"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="432dp"
android:layout_height="297dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView11"
app:srcCompat="#drawable/usbstick" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="11dp"
android:layout_height="252dp"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:srcCompat="#color/colorPrimary" />
<EditText
android:id="#+id/etxtContentId"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="63dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Content ID"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/textView12" />
<EditText
android:id="#+id/etxtWifiPassword"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Wifi Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/spinnerSSID" />
<Spinner
android:id="#+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtContentId" />
<Button
android:id="#+id/btnTestConnection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:text="Test Connection"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<Button
android:id="#+id/btnWifiConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:text="Connect"
app:layout_constraintStart_toEndOf="#+id/btnTestConnection"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<Button
android:id="#+id/btnFinish"
android:layout_width="392dp"
android:layout_height="45dp"
android:layout_marginStart="13dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="43dp"
android:text="Finish and perform online update"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/btnTestConnection" />
<Button
android:id="#+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="25dp"
android:text="Scan"
app:layout_constraintStart_toEndOf="#+id/btnWifiConnect"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<ListView
android:id="#+id/wifiList"
android:layout_width="412dp"
android:layout_height="99dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The Important parts are there in the activity and layout.xml
private Spinner spinner;
...
spinner = findViewById(R.id.spinnerSSID);
...
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
parent.getItemAtPosition(0);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(0).toString(),
Toast.LENGTH_SHORT).show();
}
} );
And in the XML:
<Spinner
android:id="#+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtContentId" />
Thanks for any help in advance!
EDIT: I noticed this error in the console:
E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method androidx.appcompat.widget.AppCompatSpinner$DropDownAdapter.<init>
Also in the debugger I noticed the listener is not getting called...
Use
spinner.setAdapter(adapter);
When you are setting adapter to spinner.
I have checked it.
Note- There is something wrong with your adapter " adp " . I don't know why you have used that.
Related
I am making chat app but when I send message recycler view does not show first 2 messages because it is up I want something like whatsapp if I open keyboard recycler view is shown from start
I tried following but it sticks chats to end even after I close keyboard:
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
. following is my code:
public class UserChat extends AppCompatActivity {
private RecyclerView recyclerView;
private Button btnSend;
private EditText messageBox;
private TextView name_txt;
final ArrayList<ChatModel> chatModels = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_chat);
recyclerView = findViewById(R.id.chat_list);
messageBox = findViewById(R.id.et_chat_box);
btnSend = findViewById(R.id.btn_chat_send);
name_txt = findViewById(R.id.name_txt);
String username = "username not set";
Bundle extras = getIntent().getExtras();
if(extras!=null){
username = extras.getString("username");
}
name_txt.setText(username);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = 1;
i++;
ChatModel chatModel = new ChatModel();
chatModel.setId(i);
chatModel.setMe(true);
chatModel.setMessage(messageBox.getText().toString().trim());
chatModels.add(chatModel);
ChatAdapter chatAdapter = new ChatAdapter(chatModels, getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(chatAdapter);
messageBox.setText("");
}
});
}
}
following is my 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=".UserChat">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chat_list"
android:layout_width="406dp"
android:layout_height="611dp"
android:paddingStart="15dp"
android:paddingTop="15dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="0.974" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e0e0e0"
app:layout_constraintBottom_toTopOf="#+id/layout_gchat_chatbox" />
<RelativeLayout
android:id="#+id/layout_gchat_chatbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="#+id/et_chat_box"
android:layout_width="333dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="0dp"
android:layout_toStartOf="#+id/btn_chat_send"
android:background="#android:color/transparent"
android:hint="Enter Message"
android:inputType="text"
android:maxLines="6"
tools:ignore="Autofill" />
<Button
android:id="#+id/btn_chat_send"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:background="?attr/selectableItemBackground"
android:text="Send"
android:textColor="#color/teal_700" />
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="408dp"
android:layout_height="64dp"
android:background="#2E2A2A"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/name_txt"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#FAF8F8"
android:textSize="21sp"
app:layout_constraintEnd_toEndOf="#+id/toolbar"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="67dp"
android:layout_height="53dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="#+id/name_txt"
app:layout_constraintHorizontal_bias="0.673"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/female" />
</androidx.constraintlayout.widget.ConstraintLayout>
int i = 1;
i++;
always u send i=2
There was a problem in height of the recycler view. I saw it here: https://github.com/stfalcon-studio/ChatKit/issues/103. #andriizhumela said that you need to put match parent in width and height of recycler view . so now it is solved
I am receiving the following error: error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
Here is my code:
PropertyHome.java:
public class PropertyHome extends AppCompatActivity
{
private RecyclerView recyclerView;
private RecyclerView.Adapter recyclerAdapter;
private RecyclerView.LayoutManager recyclerLayoutManager;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
recyclerView = (RecyclerView)findViewById(R.id.get_property_entry_property_home);
String[] myRecycler = new String[5];
myRecycler[0] = "Recycler 1";
myRecycler[1] = "Recycler 2";
myRecycler[2] = "Recycler 3";
myRecycler[3] = "Recycler 4";
myRecycler[4] = "Recycler 5";
recyclerAdapter = new GetPropertyRecyclerViewAdapater(myRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerAdapter);
}
catch(Exception e)
{
Toast.makeText(PropertyHome.this, "Error: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
...
}
activity_property_homr.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="AppLogout"
android:orientation="vertical"
android:textAlignment="gravity" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/get_property_entry_property_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="0" />
</LinearLayout>
</ScrollView>
Layout for displaying each row of the RecyclerView:
GetPropertyRecyclerViewAdapter.java:
public class GetPropertyRecyclerViewAdapater extends RecyclerView.Adapter<GetPropertyRecyclerViewAdapater.ViewHolder>
{
private String[] dataSet;
public static class ViewHolder extends RecyclerView.ViewHolder
{
public final TextView mTextView;
public ViewHolder(View v)
{
super(v);
mTextView = (TextView)v.findViewById(R.id.location_property_home);
}
public TextView getTextView()
{
return mTextView;
}
}
public GetPropertyRecyclerViewAdapater(String[] myDataSet)
{
dataSet = myDataSet;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_item, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position)
{
holder.getTextView().setText(dataSet[position]);
}
#Override
public int getItemCount()
{
return dataSet.length;
}
}
recycler_view_item.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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Price:"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image_property_home" />
<TextView
android:id="#+id/price_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="0"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/textView"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="#+id/textView" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:text="Location:"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/location_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="N/A"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/textView6"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toTopOf="#+id/textView6" />
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/location_property_home"
app:srcCompat="#drawable/bedroom" />
<TextView
android:id="#+id/bedrooms_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="0"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toEndOf="#+id/bedrooms_property_home"
app:layout_constraintTop_toBottomOf="#+id/location_property_home"
app:srcCompat="#drawable/bathroom" />
<TextView
android:id="#+id/bathrooms_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="0"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toEndOf="#+id/bathrooms_property_home"
app:layout_constraintTop_toBottomOf="#+id/location_property_home"
app:srcCompat="#drawable/garage" />
<TextView
android:id="#+id/garages_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="0"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView3"
app:layout_constraintStart_toEndOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toEndOf="#+id/garages_property_home"
app:layout_constraintTop_toBottomOf="#+id/location_property_home"
app:srcCompat="#drawable/area" />
<TextView
android:id="#+id/area_property_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="0"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView4"
app:layout_constraintStart_toEndOf="#+id/imageView4"
app:layout_constraintTop_toTopOf="#+id/imageView4" />
<ImageView
android:id="#+id/image_property_home"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/placeholder_image" />
<ImageButton
android:id="#+id/edit_property_home"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="center"
app:layout_constraintEnd_toEndOf="#+id/image_property_home"
app:layout_constraintTop_toTopOf="#+id/image_property_home"
app:srcCompat="#android:drawable/ic_menu_edit" />
<ImageButton
android:id="#+id/delete_property_home"
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintEnd_toStartOf="#+id/edit_property_home"
app:layout_constraintTop_toTopOf="#+id/image_property_home"
app:srcCompat="#android:drawable/ic_menu_delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
I've tried my best to find where the error is, but cannot find it.
You missed an important part in your code, setContentView()
add it in you onCreate() right after super() call
Should be working then
public class PropertyHome extends AppCompatActivity
{
private RecyclerView recyclerView;
private RecyclerView.Adapter recyclerAdapter;
private RecyclerView.LayoutManager recyclerLayoutManager;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_property_homr); // add setContentView here
try {
recyclerView = (RecyclerView)findViewById(R.id.get_property_entry_property_home);
String[] myRecycler = new String[5];
myRecycler[0] = "Recycler 1";
myRecycler[1] = "Recycler 2";
myRecycler[2] = "Recycler 3";
myRecycler[3] = "Recycler 4";
myRecycler[4] = "Recycler 5";
recyclerAdapter = new GetPropertyRecyclerViewAdapater(myRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerAdapter);
}
catch(Exception e)
{
Toast.makeText(PropertyHome.this, "Error: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
...
}
So....i am trying to make this button change from main activity to Disciplinas_Activity. But whenever I try to run the app and click the button, the app crashes.
Keep in mind i have other 3 button completly identical ,(other then the fact that they direct to different activities), in the same activity and they all work with the same base code.
Here is the .xml code for Main Activity
<TextView
android:id="#+id/textView_emailnotverified"
android:layout_width="172dp"
android:layout_height="34dp"
android:background="#color/white"
android:text="Email não Verificado!"
android:textColor="#E41F1F"
android:textSize="18dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.066"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.516" />
<TextView
android:id="#+id/textView_studentEmail"
android:layout_width="237dp"
android:layout_height="40dp"
android:text="Email do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.856"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.395" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="332dp"
android:layout_height="168dp"
android:scaleX="2"
android:scaleY="1.3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.028"
app:srcCompat="#drawable/etpr" />
<TextView
android:id="#+id/welcommingtextview5"
android:layout_width="337dp"
android:layout_height="39dp"
android:text="ESCOLA TÉCNICA E PROFISSIONAL DO RIBATEJO"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.459"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />
<TextView
android:id="#+id/ETPRTitle5"
android:layout_width="258dp"
android:layout_height="30dp"
android:text="BEM-VINDO À ETPR"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.105" />
<Button
android:id="#+id/Button_LOGOUT"
android:layout_width="114dp"
android:layout_height="59dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<Button
android:id="#+id/button_Testes"
android:layout_width="108dp"
android:layout_height="47dp"
android:text="Testes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.867"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
<Button
android:id="#+id/button_pdf"
android:layout_width="121dp"
android:layout_height="41dp"
android:text="PDF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.894"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.753" />
<Button
android:id="#+id/button_historia"
android:layout_width="191dp"
android:layout_height="47dp"
android:text="Sobre o criador"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.149"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.76" />
<ImageView
android:id="#+id/imageView_perfil"
android:layout_width="122dp"
android:layout_height="118dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.055"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.376"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView_studentname"
android:layout_width="236dp"
android:layout_height="33dp"
android:text="Nome do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.857"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.329" />
<Button
android:id="#+id/button_emailverification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verificar agora"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.88"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.527" />
<Button
android:id="#+id/changeprofileBTN"
android:layout_width="245dp"
android:layout_height="41dp"
android:text="Mude a imagem de perfil"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.897"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.453" />
<Button
android:id="#+id/button_disciplinas"
android:layout_width="194dp"
android:layout_height="55dp"
android:text="Disciplinas"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.106"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
Here is my .java code for main activity
public class MainActivity extends AppCompatActivity {
TextView fullname, email, verifymessage;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
String userID;
Button resendVerification, LogoutBTN, changeprofileBTN, tests, createrInfo, pdfdatabase, buttonDisciplinas;
ImageView profileImage;
StorageReference storageReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullname = findViewById(R.id.textView_studentname);
email = findViewById(R.id.textView_studentEmail);
LogoutBTN = (Button) findViewById(R.id.Button_LOGOUT);
tests =(Button) findViewById(R.id.button_Testes);
createrInfo =(Button) findViewById(R.id.button_historia);
pdfdatabase =(Button) findViewById(R.id.button_pdf);
buttonDisciplinas = findViewById(R.id.button_disciplinas);
buttonDisciplinas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Disciplinas_Activity.class));
}
});
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginScreen.class));
finish();
}
});
profileImage = findViewById(R.id.imageView_perfil);
changeprofileBTN = findViewById(R.id.changeprofileBTN);
fAuth = FirebaseAuth.getInstance();
fstore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
StorageReference profileRef = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
resendVerification = findViewById(R.id.button_emailverification);
verifymessage = findViewById(R.id.textView_emailnotverified);
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fstore.collection("utilizadores").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot, #Nullable FirebaseFirestoreException error) {
fullname.setText(documentSnapshot.getString("NomeCompleto"));
email.setText(documentSnapshot.getString("Email"));
}
});
FirebaseUser user = fAuth.getCurrentUser();
if (!user.isEmailVerified()){
verifymessage.setVisibility(View.VISIBLE);
resendVerification.setVisibility(View.VISIBLE);
resendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d("tag","Erro: Email de verificação não enviado " + e.getMessage());
}
});
}
});
}
changeprofileBTN.setOnClickListener(new View.OnClickListener() { //mudar imagem de perfil
#Override
public void onClick(View v) {
Intent openGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGalleryIntent,1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 ){
if (resultCode == Activity.RESULT_OK){//se o resultado for igual então abre galeria
Uri imageUri = data.getData();
//profileImage.setImageURI(imageUri); //insere imagem escolhida na galeria
uploadImageToFirebase(imageUri);
}
}
}
private void uploadImageToFirebase(Uri imageUri) { //upload da imagem para base de dados
StorageReference fileReference = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
fileReference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"Erro", Toast.LENGTH_SHORT).show();
}
});
}
}
Here is .xml code for DisciplinasActivity
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/Relative_Layout_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="32dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/Menu_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disciplinas"
android:textColor="#color/white"
android:textSize="22sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Menu_Title"
android:layout_marginTop="6dp"
android:text="ETPR"
android:textColor="#color/white"
android:textSize="14sp">
</TextView>
<TextView
android:id="#+id/Name_Data"
android:textColor="#color/white"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/user_icon"
android:text="Nome">
</TextView>
<TextView
android:id="#+id/Email_Data"
android:textColor="#color/white"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Name_Data"
android:layout_toStartOf="#id/user_icon"
android:text="Email">
</TextView>
<ImageView
android:id="#+id/user_icon"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/cara">
</ImageView>
</RelativeLayout>
<GridLayout
android:id="#+id/MainGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#+id/Relative_Layout_Title"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="2">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutSdac"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/sdac_icon"/>
<TextView
android:layout_width="56dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="SDAC"
android:textColor="#color/black"
android:textSize="18sp">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutEletronica"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/eletronica_icon"
app:srcCompat="#drawable/eletronica_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="Eletrónica"
android:textColor="#color/black"
android:textSize="18sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutCD"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/cd_icon"
app:srcCompat="#drawable/cd_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="CD"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutIMEI"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/imei_icon"
app:srcCompat="#drawable/imei_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="IMEI"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:id="#+id/ExitButton"
android:layout_marginTop="20dp"
android:layout_width="121dp"
android:layout_height="69dp"
android:layout_gravity="center"
android:layout_marginBottom="#+id/RelativeLayout"
android:text="Sair"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
Here is my .Java code for DisciplinasActivity
public class Disciplinas_Activity extends AppCompatActivity {
GridLayout gridPrincipal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disciplinas);
gridPrincipal = (GridLayout)findViewById(R.id.MainGrid); //identifica qual a grelha
//ação
setSingleEvent(gridPrincipal);
}
private void setSingleEvent(GridLayout gridPrincipal) {
for (int i =0;i<gridPrincipal.getChildCount();i++)
{
CardView cardView = (CardView)gridPrincipal.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),TeacherLoginScreen.class));//muda para Disciplina de Sdac
}
});
}
}
}
What am i missing...? if all the other 3 buttons work...why doesnt this one work? (I am a total newbie but i
I really need the help)
Consider your code
button = (Button)findViewById(R.id.button1);
You do not need to explicitly cast widgets anymore unless it's a special case like a RadioButton from a resource ID.
Please check that the button you're clicking is actually the button you set all this up for.
Add
android:clickable="true";
android:focusable="true";
to your buttons in the XML layout.
I'm guessing ConstraintLayout is creating this problem, you shouldn't come across this issue if you use LinearLayout.
In your .xml code for DisciplinasActivity you have some mistakes on the dimensions that you are using:
<GridLayout
...
android:id="#+id/MainGrid"
...
android:layout_marginBottom="#+id/Relative_Layout_Title" <!-- E.G 20dp -->
>
<Button
android:id="#+id/ExitButton"
...
android:layout_marginBottom="#+id/RelativeLayout" <!-- E.G 20dp -->
... />
For margins, padding, etc you have to use a dimension, like dp,in,mm,etc not a reference to other view, or you could use dimmens.xml maybe you are confusing android:layout_marginBottom with android:layout_constraintBottom_toBottomOf or another property. Check for this in all the elements in your .xml
This answer is due to this log that you posted:
ComponentInfo{studying.app.tkappv6/studying.app.tkappv6.Disciplinas_Activity}:
java.lang.UnsupportedOperationException: Can't convert to dimension:
type=0x12
In the MainActivity
Change the to "v.getContext()" istead of "MainActivity.this"
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(v.getContext(),LoginScreen.class));
finish();
}
});
I was Looking into the code and i've never used this method, but it's strange for me so it's good to pay attention, I think is wrong to set a "Void", if the method is not going to receive any value, but since is a #Override method if it was that way from the beginning you don't need to change;
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
Give a try and let's see if works!
To summarize, I'm building a social media app that displays:
A Newsfeed that shows posts from all user profiles in a given account
A Timeline that shows posts from a specific user profile in the account
I've built a custom BaseAdapter to populate a custom cell within each ListView. The Newsfeed ListView (that populates posts from all users on the account) is populating correctly.
The Timeline ListView (that populates posts from one profile on the account) is not showing. I've set breakpoints to ensure that my ArrayList is not null or empty when populating the Timeline ListView. In addition, breakpoints verify that my custom adapter is, in fact, pulling data from the ArrayList and inflating cells. However, the ListView is simply not visible.
Here is the layout file for my Newsfeed fragment (that is working correctly):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/input_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:backgroundTint="#color/colorSkyPrimary"
android:ems="10"
android:gravity="start|top"
android:hint="#string/what_s_going_on"
android:importantForAutofill="no"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="o" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="#+id/input_post"
app:layout_constraintEnd_toEndOf="#+id/input_post"
app:srcCompat="#drawable/camera_icon" />
<Button
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="8dp"
android:text="#android:string/cancel"
android:textColor="#color/colorGrassPrimary"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="48dp"
android:backgroundTint="#color/colorGrassPrimary"
android:text="#string/update"
android:textColor="#color/colorButtonText"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="300dp"
android:contentDescription="#string/post_image"
android:scaleType="fitCenter"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input_post"
tools:srcCompat="#tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/picker_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.ConstraintLayout
android:id="#+id/picker_image"
android:layout_width="0dp"
android:layout_height="75dp"
android:background="#android:color/white"
android:elevation="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageButton
android:id="#+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/camera_icon_large" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.375"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
Here is the layout file for my Profile fragment (in which the ListView is not showing at runtime)
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:src="#drawable/male_icon_large"
app:civ_border_color="#android:color/transparent"
app:civ_border_width="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/display_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/brelynn_mack"
android:textAlignment="viewStart"
android:textColor="#color/colorTextDark"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/button_delete_profile"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toTopOf="#+id/profile_photo" />
<TextView
android:id="#+id/display_timestamp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/may_14_2019_9_59_pm"
android:textAlignment="viewStart"
android:textColor="#color/colorTextLight"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_name" />
<TextView
android:id="#+id/display_last_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="#string/_8741_grouse_run_lane_28314"
android:textAlignment="viewStart"
android:textColor="#color/colorTextPrimary"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_timestamp" />
<ImageButton
android:id="#+id/button_delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#android:color/white"
android:contentDescription="#string/trash_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash_icon" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/camera_icon" />
<ImageButton
android:id="#+id/button_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="#android:color/white"
android:contentDescription="#string/family_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/family_icon_small" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_posts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
Here is my custom adapter:
public class NewsfeedAdapter extends BaseAdapter {
// Class properties
private static final String TAG = "NewsfeedAdapter";
public static final String EXTRA_POSTS = "extra_posts";
public static final String EXTRA_POSITION = "extra_position";
private final Context context;
ArrayList<Post> posts;
Account account;
// Constructor
public NewsfeedAdapter(Context context, ArrayList<Post> posts, Account account) {
this.context = context;
this.posts = posts;
this.account = account;
}
// System generated methods
#Override
public int getCount() {
return posts.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return posts.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Post post = posts.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.cell_newsfeed, null);
}
TextView profileNameDisplay = convertView.findViewById(R.id.display_profile_name);
String name = post.getPosterName() + " " + account.getFamilyName();
profileNameDisplay.setText(name);
TextView timestampDisplay = convertView.findViewById(R.id.display_timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy # hh:mm a", Locale.getDefault());
String timestamp = dateFormat.format(post.getTimeStamp());
timestampDisplay.setText(timestamp);
TextView postMessageDisplay = convertView.findViewById(R.id.display_post_message);
postMessageDisplay.setText(post.getPostMessage());
if (post.getHasImage()) {
AccountUtils.loadProfilePhoto(context, convertView, post.getPosterId());
}
else {
ImageView postImage = convertView.findViewById(R.id.display_post_image);
postImage.setVisibility(View.GONE);
}
PostUtils.loadPostImage(convertView, post.getPostId());
ImageButton deleteButton = convertView.findViewById(R.id.button_delete_post);
ImageButton editButton = convertView.findViewById(R.id.button_edit_post);
toggleButtons(post, editButton, deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(context.getString(R.string.delete_post));
alertBuilder.setMessage(context.getString(R.string.delete_post_message));
alertBuilder.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
posts.remove(position);
PostUtils.deletePost(context, post.getPostId(), post.getPosterId());
notifyDataSetChanged();
PostUtils.listenForNews(context);
}
});
alertBuilder.setNegativeButton(context.getString(R.string.cancel), null);
AlertDialog alert = alertBuilder.create();
alert.show();
}
});
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent editIntent = new Intent(context, EditPostActivity.class);
editIntent.putExtra(EXTRA_POSTS, posts);
editIntent.putExtra(EXTRA_POSITION, position);
context.startActivity(editIntent);
}
});
return convertView;
}
// Custom methods
private void toggleButtons(Post post, ImageButton editButton, ImageButton deleteButton) {
long twoMinutes = System.currentTimeMillis() - (2 * 60 * 1000);
long fiveMinutes = System.currentTimeMillis() - (5 * 60 * 1000);
if (post.getTimeStamp().getTime() < fiveMinutes) {
editButton.setVisibility(View.GONE);
}
else {
editButton.setVisibility(View.VISIBLE);
}
if (post.getTimeStamp().getTime() < twoMinutes) {
deleteButton.setVisibility(View.GONE);
}
else {
deleteButton.setVisibility(View.VISIBLE);
}
}
Here are the lifecycle methods from the Newsfeed fragment that load my data and set the adapter:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newsfeed, container, false);
PostUtils.listenForNews(getActivity());
mPosts = PostUtils.loadNewsfeed(getActivity());
mNewsfeed = view.findViewById(R.id.list_newsfeed);
mImagePicker = view.findViewById(R.id.picker_image);
mPhotoView = view.findViewById(R.id.post_image);
AccountUtils.listenForUpdates(getActivity());
setClickListener(view);
setFocusListener(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadNewsfeed(getActivity());
Account account = AccountUtils.loadAccount(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, account);
mNewsfeed.setAdapter(mNewsfeedAdapter);
}
Here are the lifecycle methods from my Profile fragment where the same functionality should work for a slightly different set of data:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
try {
if(getActivity().getIntent() != null && getActivity().getIntent().getAction().equals(FamilyProfileFragment.ACTION_EDIT_PROFILE)) {
mEditingSelf = false;
mIsParent = true;
mProfile = (Profile) getActivity().getIntent().getSerializableExtra(FamilyProfileFragment.EXTRA_PROFILE);
String selectedName = mProfile.getFirstName();
String loadedName = AccountUtils.loadProfile(getActivity()).getFirstName();
if(selectedName.equals(loadedName)) {
mEditingSelf = true;
}
}
else {
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
}
catch (Exception e) {
e.printStackTrace();
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
mAccount = AccountUtils.loadAccount(getActivity());
AccountUtils.loadProfilePhoto(getActivity(), view, mProfile.getProfileId());
mPhotoView = view.findViewById(R.id.profile_photo);
PostUtils.listenForTimeline(getActivity(), mProfile);
mPosts = PostUtils.loadTimeline(getActivity());
mTimeline = view.findViewById(R.id.list_posts);
setClickListener(view);
setTextDisplay(view);
populateProfile(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadTimeline(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, mAccount);
mTimeline.setAdapter(mNewsfeedAdapter);
}
My constraints were set up incorrectly in the Profile fragment. Updated the XML to the following:
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
I'm trying to use a spinner selection to change a TextView. Where I think my error is, is that "onItemSelected" "is never used". I'm very new to android/java so i'm having a hard time figuring out why this is happening.
public class activity_game extends AppCompatActivity {
public String myString1 = "Counter will increase every 3 seconds";
public TextView myCounterText;
private Spinner mySpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) {
TextView myCounterText = (TextView)findViewById(R.id.myCounter);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
if (mySpinner.getSelectedItem().equals("Easy")){
myCounterText.setText("myString1");
}
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
startActivity(toActivityPlay);
}
}
Thanks in advance.
Edit: Here is my XML (with unnecessary stuff edited out so that I can post without having "mostly code".
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
tools:context="com.example.newpc.fizzbuzz.activity_game">
<TextView
android:id="#+id/counterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="#string/counter_text"
android:textAlignment="center"
android:textColor="#color/text_shadow"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="#+id/startButton"
android:layout_width="239dp"
android:layout_height="73dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/FizzBuzz_yellow"
android:text="#string/start_game"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="36sp"
android:textStyle="bold"
android:shadowColor="#color/text_shadow"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="3"
android:onClick="toActivityPlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.863"
app:layout_constraintHorizontal_bias="0.531"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="240dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:dropDownWidth="match_parent"
android:entries="#+id/difficulty"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</android.support.constraint.ConstraintLayout>
try this use setOnItemSelectedListener of spinner
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text=spin.getSelectedItem().toString();
if (text.equals("Easy")){
myCounterText.setText("myString1");
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});