I want to set an image to ImageView as soon as I call a new activity. But for some reason, I can't do it, although seems to be alright
Here is an XML snippet
<?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">
<ImageView
android:id="#+id/magneticImgBox"
android:layout_width="327dp"
android:layout_height="211dp"
android:src="#drawable/magneticimg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/magneticNameBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/magneticImgBox"
android:text="Набор Magnetic"
android:textSize="35sp"/>
<TextView
android:id="#+id/boxDiscrition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/magneticNameBox"
android:text="Бюджетный комплект, который не имеет каких-то дополнительных функций, но до сих пор пользуется спросом и выполняет свои функции без проблем.
\n\nВ комплекте:\n- Антенна\n- Гарнитура с микрофоном\n- Извлекатель-внедритель\n- Магнитные динамики - 2шт.\n- Батарейка Крона"/>
<VideoView
android:id="#+id/magneticVideoPlayerBox"
android:layout_width="wrap_content"
android:layout_height="450dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/boxDiscrition"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is a Java snippet
public void openExamLight(View view) {
MagneticActivity mag = new MagneticActivity();
Intent intent = new Intent(this, MagneticActivity.class);
startActivity(intent);
mag.changePicture();
}
public void changePicture() {
ImageView img = findViewById(R.id.magneticImgBox);
img.setImageResource(R.drawable.magneticimg);
}
Here I showed the method of clicking in which I call a new activity (open Magnetic) and the method in which I try to change the picture (changePicture, yes, I know that it could not be taken out in a separate method, it's just an attempt to fix everything)
I solved the problem, I had to do a type conversion in the changePicture method
public void changePicture() {
ImageView img = (ImageView)findViewById(R.id.magneticImgBox);
img.setImageResource(R.drawable.magneticimg);
}
Related
I want this drawerLayout(bellow photo) to always open like Samsung multiwindow sidebar.
Till now I am able to open this drawerLayout(sidebar) within the app. please give the instruction to be able to open this draweLayout(sidebar) from anywhere(I mean from any other app or even from home screen of a device).
Thanks in advance.
Here is my drayerLayout XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#color/cardview_light_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swipe Right"
android:textSize="15dp"
android:textColor="#D50000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open App In\nMultiWIndow Mode"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#color/black"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity2">
<ListView
android:id="#+id/listView"
android:layout_width="280dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
This cannot be achieved by making something global you to do some modification in every activity
you can create a new class that will have a method to initialize the navigation, move navigation view to separate xml file and include that file in every activity, and call initialization method in every activity
You can try this library https://github.com/mikepenz/MaterialDrawer to move most xml code to java so you won't have to add DrawerLayout in every activity but choice is your, it always better to skip using 3rd party libraries if you can
if you are using library than here's some example you can follow
Constant.java
private void setupNavigationDrawer() {
ProfileDrawerItem section = new ProfileDrawerItem()
.withSetSelected(false)
.withSetSelected(false)
.withEnabled(false);
PrimaryDrawerItem joyType0DrawerItem = new PrimaryDrawerItem().withName("Basic Gamepad").withTag(KEY_BASIC_GAMEPAD_1).withLevel(2);
PrimaryDrawerItem joyType1DrawerItem = new PrimaryDrawerItem().withName("Assault Horizon").withTag(KEY_ASSAULT_HORIZON).withLevel(2);
PrimaryDrawerItem joyType2DrawerItem = new PrimaryDrawerItem().withName("Awesomenauts").withTag(KEY_AWESOMENAUTS).withLevel(2);
PrimaryDrawerItem joyType3DrawerItem = new PrimaryDrawerItem().withName("BombSquad").withTag(KEY_BOMBSQUAD).withLevel(2);
PrimaryDrawerItem joyType4DrawerItem = new PrimaryDrawerItem().withName("Final Fantasy XIII").withTag(KEY_FINAL_FANTASY_XIII).withLevel(2);
SectionDrawerItem sectionDrawerItem = new SectionDrawerItem().withName("Joystick Type").withTextColorRes(R.color.colorAccent);
PrimaryDrawerItem deviceDrawerItem = new PrimaryDrawerItem().withName("Device Connection").withTag(KEY_DEVICE_CONNECTION).withIcon(GoogleMaterial.Icon.gmd_devices);
PrimaryDrawerItem settingsDrawerItem = new PrimaryDrawerItem().withName("Settings").withTag("settings").withIcon(GoogleMaterial.Icon.gmd_settings);
ndMenu = new DrawerBuilder()
.withActivity(this)
.withToolbar(tbMain)
.withCloseOnClick(true)
.withFireOnInitialOnClick(true)
.withSelectedItemByPosition(6)
.withDelayDrawerClickEvent(300)
.withDisplayBelowStatusBar(true)
.withTranslucentStatusBar(false)
.withOnDrawerItemClickListener(this)
.addDrawerItems(section, deviceDrawerItem, settingsDrawerItem, sectionDrawerItem, joyType0DrawerItem, joyType1DrawerItem, joyType2DrawerItem, joyType3DrawerItem, joyType4DrawerItem)
.build();
findViewById(R.id.nav_button).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
// Check these functions from library example to check drawer state and open close drawer
if(ndMenu.isDrawerOpen){
ndMenu.closeDrawer();
} else {
ndMenu.openDrawer();
}
});
}
MainActivity.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<include
layout="#layout/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout>
nav_view.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
orientation="horizontal">
<Image
id="#+id/nav_button"
android:layout_width="30dp"
android:layout_height="30dp"/>
<LinearLayout>
Trying to make two login, one for admin, one for user. They share the same layout, but admin has additional TextFields and Buttons to add data to the menu. So when logging in with user credentials, the TextFields and Buttons will be setVisibility(View.GONE), but it's not working at all, in fact it's showing up like setVisibility(View.GONE) was added at all. Any advice would be appreciated.
loginpage.java
DatabaseHelper myDB;
EditText LoginEMail;
EditText LoginPassword;
Button LoginBtn;
LinearLayout linearLayout;
View add_image;
View add_name;
View add_desc;
View add_data;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
LoginEMail = findViewById(R.id.LoginEMail);
LoginPassword = findViewById(R.id.LoginPassword);
LoginBtn = findViewById(R.id.LoginBtn);
myDB = new DatabaseHelper(this);
linearLayout = findViewById(R.id.linearLayout);
linearLayout = new LinearLayout(this);
add_image = new View(this);
add_name = new View(this);
add_desc = new View(this);
add_data = new View(this);
}
public void Login(View view) {
Intent intent = new Intent(loginpage.this, MenuSelection.class);
if (LoginEMail.getText().toString().equals("admin") && (LoginPassword.getText().toString().equals("admin"))) {
startActivity(intent);
linearLayout.setVisibility(View.VISIBLE);
}
else if(LoginEMail.getText().toString().equals("user") && (LoginPassword.getText().toString().equals("user"))) {
startActivity(intent);
linearLayout.setVisibility(View.GONE);
add_image.setVisibility(View.GONE);
add_name.setVisibility(View.GONE);
add_desc.setVisibility(View.GONE);
add_data.setVisibility(View.GONE);
}
else
Toast.makeText(loginpage.this, "Incorrect E-mail or Password.", Toast.LENGTH_SHORT).show();
}
loginpage.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">
<EditText
android:id="#+id/LoginEMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="E-Mail"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />
<EditText
android:id="#+id/LoginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.338" />
<Button
android:id="#+id/LoginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602"
android:onClick="Login"/>
</androidx.constraintlayout.widget.ConstraintLayout>
menu_selection.xml
<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:gravity="center_horizontal"
android:orientation="vertical">
<ListView
android:id="#+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/add_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#color/black" />
<EditText
android:id="#+id/add_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="#+id/add_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Description" />
<Button
android:id="#+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of add_image = new View(this); you need to associate the variable add_image to the view in your XML layout using the findViewById(R.id.add_image) method. Similarly for the other 3 views that you have initialized this way.
You also need to delete the line linearLayout = new LinearLayout(this);. The previous line setting this variable is correct.
So I've been trying to figure this out for quite a while now and cannot get this to work. I have a RecyclerView which has a child added to it upon a button press (this is a test case at the moment). As you will see in the code below, When onBindView is called, the text is changed. However as the title says, the text does not auto resize to fill the box in question.
How would I be able to do this?
RecyclerViewEntry.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="wrap_content"
android:id="#+id/ma_rv_ple_layout_root"
android:background="#drawable/background_border_rc"
android:padding="15dp"
android:clickable="true"
android:focusable="true"
android:layout_margin="5dp"
>
<TextView
android:text="#string/ma_rv_ple_project_label"
android:layout_width="0dp"
android:layout_height="0dp" android:id="#+id/am_rv_ple_project_title_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:gravity="center"
app:layout_constraintDimensionRatio="1"
android:breakStrategy="balanced"
app:autoSizeTextType="uniform"
android:maxLines="1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
ProjectListAdapter.java
#Override
public void onBindViewHolder(#NonNull ProjectListAdapter.ViewHolder holder, int position) {
// TODO: Create and build our ViewHolder.
String entry = ProjectManager.getInstance().getProjectItem(position).getProjectName();
holder.getProjectNameTextView().setText(entry);
}
Hello the code below shows a spinner in an activity my problem is that the dropdown does not work, only the 1 element of the array is displayed.
In the spinner, only the first element inserted in the arraylist is displayed. What can this be due to? I state that I am using this code in other activities and it does not give me any problems
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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=".View.ComponentiCantiere.CreazioneCantiereActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.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" />
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/include3"
layout="#layout/content_creazione_cantiere"
app:layout_anchor="#+id/include3"
app:layout_anchorGravity="left|top" />
<Button
android:id="#+id/buttonSelezionaCliente"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="450dp"
android:background="#drawable/clienti" />
<Button
android:id="#+id/buttonNuovoCantiere"
android:layout_width="48dp"
android:layout_height="50dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="300dp"
android:background="#drawable/nuovo_cantiere"
android:text="" />
<TextView
android:id="#+id/textviewCreazioneCantiere"
android:layout_width="200dp"
android:layout_height="29dp"
android:layout_marginLeft="120dp"
android:layout_marginTop="310dp"
android:text="Creazione Cantiere"
android:textColor="#color/Black"
android:textStyle="bold"
android:textSize="18dp" />
<TextView
android:id="#+id/textViewSelezioneCliente"
android:layout_width="200dp"
android:layout_height="29dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="470dp"
android:text="Seleziona cliente"
android:textColor="#color/Black"
android:textStyle="bold"
android:textSize="18dp" />
<TextView
android:id="#+id/textViewTipologia"
android:layout_width="81dp"
android:layout_height="29dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:text="Tipologia:"
android:textColor="#color/Black"
android:textSize="18dp" />
<Spinner
android:id="#+id/spinnertipologiacantiere"
android:layout_width="153dp"
android:layout_height="32dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="100dp"
android:background="#color/Gray" />
<TextView
android:id="#+id/textView_NomeCantiere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
android:text="Nome Cantiere:"
android:textColor="#color/Black"
android:textSize="18dp" />
<EditText
android:id="#+id/editextNomeCantiere"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="200dp"
android:background="#color/Gray" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
Java Code:
public class CreazioneCantiereActivity extends AppCompatActivity {
private EditText txtNomeCantiere;
private String TipologiaCantiereSelezionato="Consuntivo";
private Button btnCreaCantiere, btnSelezionacliente;
private ClienteController c;
private Spinner spinnerTipologie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creazione_cantiere);
//Intent che contiene il cliente e l'utente
Intent i = this.getIntent();
final UtenteController u = (UtenteController) i.getSerializableExtra("UtenteController");
c = (ClienteController) i.getSerializableExtra("ClienteController");
btnCreaCantiere = findViewById(R.id.buttonNuovoCantiere);
btnSelezionacliente = findViewById(R.id.buttonSelezionaCliente);
txtNomeCantiere = findViewById(R.id.editextNomeCantiere);
//Inizializzazione spinner tipologie cantiere
spinnerTipologie = findViewById(R.id.spinnertipologiacantiere);
spinnerTipologie.setEnabled(true);
//Inizio Configurazione Spinner delle tipologia del cantiere con Consuntivo e Preventivo
List<String> arraySpinner = new ArrayList<String>();
arraySpinner.add("Consuntivo");
arraySpinner.add("Preventivo");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTipologie.setAdapter(adapter);
//Configurazione Button Creazione CantiereController
btnSelezionacliente.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent tab = new Intent(CreazioneCantiereActivity.this, ClienteActivity.class);
tab.putExtra("UtenteController", u);
tab.putExtra("Stato", "Selezione");
startActivity(tab);
}
});
//Configurazione Button Creazione CantiereController
btnCreaCantiere.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (txtNomeCantiere.getText().toString().length() > 0 && c != null) {
CantiereController ctemp = new CantiereController();
ArrayList readvalue = new ArrayList();
readvalue = ctemp.Creazione(txtNomeCantiere.getText().toString(), spinnerTipologie.getSelectedItem().toString(),u, c);
if (readvalue.isEmpty() == false) {
Intent intent = new Intent(CreazioneCantiereActivity.this, GestioneCantieriActivity.class);
ctemp = (CantiereController) readvalue.get(0);
intent.putExtra("CantiereController", ctemp);
intent.putExtra("UtenteController", u);
startActivity(intent);
} else {
Support.Notification(CreazioneCantiereActivity.this, "Errore", "creazione cantiere non riuscita");
}
} else {
Support.Notification(CreazioneCantiereActivity.this, "Errore", "compila tutti i campi");
}
}
});
}
}
So you wanna create an static array and show in your spinner? Tell me if the code bellow helps you:
activity_main.xml
<Spinner
android:id="#+id/booksSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/books_array"/>
strings.xml:
<string-array name="books_array">
<item>Hamlet</item>
<item>Harry Potter</item>
<item>The Great Gatsby</item>
</string-array>
Doing that, you will remove the responsibility for creating the array of your java class and just needs to get the spinner value in onCreate() method. Example:
findViewById (R.id.booksSpinner);
EDIT 1:
You said that did not work but i tested and it worked. Check the full code:
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/mainLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginLeft="84dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="313dp"
android:ems="10"
android:text="Spinner test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.148"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032" />
<Spinner
android:id="#+id/spinnertipologiacantiere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mainLabel"
android:entries="#array/books_array"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Spinner spinnerTipologie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerTipologie = findViewById(R.id.spinnertipologiacantiere);
}
}
strings.xml
<resources>
<string name="app_name">MyApplication</string>
<string-array name="books_array">
<item>Hamlet</item>
<item>Harry Potter</item>
<item>The Great Gatsby</item>
</string-array>
</resources>
Result:
EDIT 2
Hello, again! I added more books in my books_array of strings.xml to test the scroll and it worked as you can see bellow:
<string-array name="books_array">
<item>Hamlet</item>
<item>Harry Potter</item>
<item>The Great Gatsby</item>
<item>Don Quixote</item>
<item>Moby Dick</item>
<item>War and Peace</item>
<item>Ulysses</item>
<item>The Odyssey</item>
<item>Beloved</item>
<item>The Hobbit</item>
<item>Les Misérables</item>
<item>The Magic Mountain</item>
<item>The Lord of the Rings</item>
<item>Animal Farm</item>
<item>The Castle</item>
</string-array>
If you are using my code correctly, your spinner should work. Maybe there is something missing for you, I suggest you edit your post and add your updated code so I can understand what is happening
I'm working on a music player application, and I've created a FrameLayout which works as a "card," displaying information about each song (album art, title, artist, etc.) I'm trying to add checkboxes in this card, which I plan to customize to create upvote and downvote buttons. However, the checkboxes within the card do not respond to clicks. If I draw a checkbox in the main activity of the application (where I'm drawing the cards), it works fine.
Here is the onDraw() method of the card view ("MusicCardView"):
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw the voting controls
try {
CheckBox upvoteButton = new CheckBox(getContext());
upvoteButton.setX(20);
upvoteButton.setTop(-60);
upvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "upvote clicked");
}
});
CheckBox downvoteButton = new CheckBox(getContext());
downvoteButton.setX(20);
downvoteButton.setTop(-150);
downvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "downvote clicked");
}
});
upvoteButton.draw(canvas);
downvoteButton.draw(canvas);
} catch (Exception e) {
Log.e("MusicCardView", "exception", e );
}
}
The xml of the main activity ("PartyActivity"):
<?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:id="#+id/main_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.musique.PartyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_linear_view"
android:layout_marginBottom="56dp">
<com.musique.MusicCardView
android:layout_width="match_parent"
android:id="#+id/now_playing_view"
android:layout_height="80dp"
android:background="#ccc"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:exampleColor="#000000"
app:songTitle="Money"
app:artist="Pink Floyd"
app:album="The Dark Side of the Moon"
app:exampleDimension="18sp"
app:exampleDrawable="#drawable/darkside"
app:exampleString="example" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/controller_space"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/query"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchBtn"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search"
android:background="#5eca99"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="#+id/main_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scroll_child">
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
And an example of a MusicCardView being the draw in PartyActivity
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);
I've had a look at this question and this question, but had no luck with the solutions posted, and would greatly appreciate any advice you might have.
If you draw the controls directly to the canvas they are basically just bitmaps, you need to add them to the view hierarchy (activity/fragment layout) for them to receive touch events.
As a side note, it's a big no-no to instantiate things in onDraw, especially views.