This question already has answers here:
"cannot resolve symbol R" in Android Studio
(100 answers)
Closed 5 years ago.
I'm new to android and saw other people having the same question but the solutions didn't work.
i tried multiple times to clean, sync and build the project after changes but every time is says that it can't resolve R
adding: import nl.test123.R; didn't work because than the next error popsup:Error:attribute 'nl.test123.soundboard:layout_background' not found.
also this didn't work
import android.R
this is the code:"MainActivity.java"
package nl.test123.testsoundboard;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
//import nl.test123.R;
public class MainActivity extends AppCompatActivity {
MediaPlayer mysound1;
MediaPlayer mysound2;
MediaPlayer mysound3;
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_top:
// mTextMessage.setText(R.string.title_top);
return true;
case R.id.navigation_latest:
// mTextMessage.setText(R.string.title_latest);
return true;
case R.id.navigation_favorite:
// mTextMessage.setText(R.string.title_favorite);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mysound1 = MediaPlayer.create(this, R.raw.bird);
mysound2 = MediaPlayer.create(this, R.raw.bomb);
mysound3 = MediaPlayer.create(this, R.raw.dog);
}
public void sound1(View view){
mysound1.start();
}
public void sound2(View view){
mysound2.start();
}
public void sound3(View view){
mysound3.start();
}
}
can anyone see what i'm doing wrong or forgetting to do.
see the xml below:"activity_main.xml"
<?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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nl.marvinboontjes.cucumbersoundboard.MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92dp"
android:layout_marginTop="128dp"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:onClick="Sound1"
android:text="Bird"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sound2"
android:text="Bomb"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onclick="sound3"
android:text="Dog"
tools:layout_editor_absoluteX="269dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:layout_background="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
Error images:
picture1
picture2
use yourpackagename.R.
in your case it will be nl.test123.testsoundboard.R
I think you have an error in your layout. BottomNavigationView doesn't have app:layout_background attribute. remove it and rebuild again.
Related
I'm a beginner, but I want to learn and I'm developing my first app!!I would also like to use the "toaster" function in the app. Unfortunately no longer works !!
Not only in my app, but no matter where I want to use it. Should I reinstall android studio?
Thank you, Stefan
Hi, no error message! Just not work!!
MainActivity:
package de.havadinagy.toaster_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void info(View view){
Toast.makeText(this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="243dp"
android:layout_height="172dp"
android:onClick="info"
android:text="ToasterTest"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this
Toast.makeText(requireContext(), "message", Toast.LENGTH_SHORT).show()
do try this
try
public void info(View view){
Toast.makeText(MainActivity.this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
and change "info"
i have a custom navigation drawer that opens from left to right using fragment. i want to move my content and action bar to right when navigation opens, i read some sources, they override onDrawerSlide method to do so, but since i using fragment to open and close navigation, i do not have such a method to override, I'll be grateful someone could help me (:
I tried this link to move the contents
and used this to create a custom navigation view
there is main activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
<TextView
android:id="#+id/homeContents"
android:text="this is gonna be ur main page"
android:layout_width="200dp"
android:layout_height="46dp" />
</FrameLayout>
</LinearLayout>
and this is drawer :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/rootLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/nav_profile"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_width="77dp"
android:layout_height="77dp"
android:src="#drawable/ic_user"
/>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:drawableLeft="#drawable/ic_user_plus"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:id="#+id/sign_in"
android:layout_width="91dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:background="#drawable/more_btn_coop_req"
android:text="SIGNUP"
android:textColor="#color/blue" />
<Button
android:drawableLeft="#drawable/ic_sign_in_alt"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:id="#+id/sign_up"
android:layout_width="91dp"
android:layout_height="40dp"
android:layout_marginStart="25dp"
android:background="#drawable/more_btn_coop_req"
android:text="ENTER"
android:textColor="#color/blue" />
</LinearLayout>
<Spinner
android:layout_marginTop="16dp"
android:background="#drawable/spinner_background"
android:id="#+id/cities_spinner"
android:layout_gravity="center"
android:layout_width="190dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="250dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:drawableLeft="#drawable/ic_check"
android:drawablePadding="25dp"
android:id="#+id/bookmark_centers"
android:layout_gravity="center"
android:background="#drawable/drawer_buttons_style"
android:text="تخفیف های نشان شده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:drawableLeft="#drawable/ic_store"
android:id="#+id/followed_centers"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:background="#drawable/drawer_buttons_style"
android:text="مراکز دنبال شده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/tems"
android:drawableLeft="#drawable/ic_info"
android:background="#drawable/drawer_buttons_style"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="شرایط استفاده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/contact_us"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:drawableLeft="#drawable/ic_phone"
android:background="#drawable/drawer_buttons_style"
android:text="ارتباط با ما"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/share_us"
android:layout_gravity="center"
android:text="پیشنهاد به دوستان"
android:layout_marginTop="10dp"
android:drawableLeft="#drawable/ic_share_alt"
android:background="#drawable/drawer_buttons_style"
android:layout_width="186dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/EXIT"
android:layout_width="72sp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:background="#drawable/drawer_buttons_style"
android:drawableLeft="#drawable/ic_sign_out_alt"
android:text="خروج" />
<Button
android:id="#+id/edit"
android:layout_width="105dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:background="#drawable/drawer_buttons_style"
android:drawableLeft="#drawable/ic_cog"
android:text="ویرایش" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
there is a custom toolbar and base activity that merged both toolbar and
drawer, but i think it is not necessary.
this is menu fragment class :
package TestNavigationDrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.example.deathstroke.uniqueoff1.R;
public class MenuFragment extends Fragment implements
View.OnTouchListener {
GestureDetector gestureDetector;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.slider_menu, container,
false);
RelativeLayout root = rootView.findViewById(R.id.rootLayout);
/*gestureDetector=new GestureDetector(getActivity(),new
OnSwipeListener(){
#Override
public boolean onSwipe(Direction direction) {
if (direction==Direction.up){
//do your stuff
((MainActivity ) getActivity()).hideFragment();
}
if (direction==Direction.down){
//do your stuff
}
return true;
}
});
root.setOnTouchListener(this);*/
return rootView;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
}
and there is mainActivity class :
package com.example.deathstroke.uniqueoff1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import TestNavigationDrawer.*;
public class MainActivity extends BaseActivity {
boolean isFragmentLoaded;
Fragment menuFragment;
TextView title;
ImageView menuButton;
RelativeLayout rootLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("onCreate", "onCreate: " );
super.onCreate(savedInstanceState);
initAddlayout(R.layout.activity_main);
Log.e("onCreate", "onCreate: " );
rootLayout = findViewById(R.id.rootLayout);
textView = findViewById(R.id.homeContents);
title = findViewById(R.id.title_top);
menuButton = findViewById(R.id.menu_icon);
title.setText("Menu Activity");
menuButton.setOnClickListener((View)-> {
try {
if (!isFragmentLoaded) {
loadFragment();
title.setText("Profile");
} else {
if (menuFragment != null) {
if (menuFragment.isAdded()) {
hideFragment();
}
}
}
}catch (Exception e){
Log.e("Menu", "onCreate: menu on click, e: " + e);
}
});
}
public void hideFragment(){
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
R.anim.slide_right);
fragmentTransaction.remove(menuFragment);
fragmentTransaction.commit();
menuButton.setImageResource(R.drawable.ic_menu);
isFragmentLoaded = false;
title.setText("Main Activity");
}
public void loadFragment(){
FragmentManager fm = getSupportFragmentManager();
menuFragment = fm.findFragmentById(R.id.container);
menuButton.setImageResource(R.drawable.ic_up_arrow);
if(menuFragment == null){
menuFragment = new MenuFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
R.anim.slide_right);
fragmentTransaction.add(R.id.container,menuFragment);
fragmentTransaction.commit();
}
isFragmentLoaded = true;
}
}
as you can see, i am using loadFragment and hideFragment to open and close drawer, so how can i move main content (there is a simple textview but consider it whole lots of stuff)
Hey you can use this I guess you want the same output
The below link
I'm currently creating an Android app for school but still want to give my best. I'm pretty new to Android development and coding in general.
The app is supposed to be a stock market game.
(Btw, I'm German, so there might be some German variables)
The app should have a similar style to Google's recent one with the Pixel 2 and Android Pie. Thus, the toolbar should have no drop shadow when created but it should appear when scrolling, like in the Pixel 2's settings app (e.g. the battery tab).
The drop shadow appears when scrolling and disappears when arriving at the top, but the toolbar starts with a drop shadow although I have set the android:elevation to 0 in the XML and also toolbar.setElevation(0) in the onCreate method.
Why does this happen? This method works in the OnScrollChangedListener!
The Java code:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<JSONObject> jObjList = new ArrayList<>();
private FloatingActionButton fab;
private TextView moneyTxt;
private TextView sharesTxt;
private TextView sumTxt;
private Toolbar toolbar;
private AppBarLayout toolbarLayout;
private RecyclerView recyclerShares;
private SharesAdapter sAdapter;
private NestedScrollView scrollMain;
private SwipeRefreshLayout refreshMain;
private float money;
private float sharesWorth;
private boolean isRefreshing;
private JSONObject jObj = new JSONObject();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.abMain);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimary));
setSupportActionBar(toolbar);
fab = findViewById(R.id.fabMain);
moneyTxt = findViewById(R.id.moneyTxt);
sharesTxt = findViewById(R.id.sharesTxt);
sumTxt = findViewById(R.id.sumTxt);
toolbarLayout = findViewById(R.id.abMainLayout);
recyclerShares = findViewById(R.id.recyclerShares);
scrollMain = findViewById(R.id.scrollMain);
scrollMain.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
refreshMain = findViewById(R.id.refreshMain);
isRefreshing = false;
try {
jObj.put("name", "BMW");
jObj.put("worth", 143.23);
jObj.put("count", 5);
jObj.put("change", -1.5);
jObjList.add(jObj);
} catch (JSONException e) {
}
sAdapter = new SharesAdapter(jObjList);
RecyclerView.LayoutManager cLayoutManager = new CustomGridLayoutManager(getApplicationContext()) {
#Override
public boolean canScrollVertically() {
return false;
}
};
recyclerShares.setLayoutManager(cLayoutManager);
recyclerShares.setItemAnimator(new DefaultItemAnimator());
recyclerShares.setAdapter(sAdapter);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SharesActivity.class);
startActivity(intent);
}
});
toolbarLayout.setElevation(0); //TODO: Stackoverflow nach Lösung fragen
scrollMain.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scroll = scrollMain.getScrollY();
if (scroll == 0) {
toolbarLayout.setElevation(0);
} else {
toolbarLayout.setElevation(8);
}
}
});
refreshMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refresh(MainActivity.this);
}
});
refresh(MainActivity.this);
}
private void refresh(Context context) {
isRefreshing = true;
SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor sharedPrefEdit = sharedPref.edit();
if (sharedPref.getBoolean("isFirstRun", true)) {
sharedPrefEdit.putBoolean("isFirstRun", false);
sharedPrefEdit.putFloat(getString(R.string.moneyShared), 5000);
sharedPrefEdit.apply();
}
float shareWorth = 0;
for (int i = 0; i < jObjList.size(); i++) {
try {
shareWorth += jObjList.get(i).getDouble("worth") * jObjList.get(i).getDouble("count");
} catch (JSONException e) {
}
}
sharedPrefEdit.putFloat(getString(R.string.sharesWorthShared), shareWorth);
sharedPrefEdit.commit();
money = sharedPref.getFloat(getString(R.string.moneyShared), 0);
sharesWorth = sharedPref.getFloat(getString(R.string.sharesWorthShared), 0);
moneyTxt.setText(String.format("%.2f€", money));
sharesTxt.setText(String.format("%.2f€", sharesWorth));
sumTxt.setText(String.format("%.2f€", money + sharesWorth));
if(isRefreshing) {
isRefreshing = false;
refreshMain.setRefreshing(isRefreshing);
}
Toast.makeText(MainActivity.this, "Alles neugeladen", Toast.LENGTH_SHORT).show();
}
public void onShareClick(View v) {
Intent i = new Intent(MainActivity.this, CompanyActivity.class);
try {
i.putExtra("name", jObj.getString("name"));
i.putExtra("worth", jObj.getDouble("worth"));
} catch (JSONException e) {
i.putExtra("name", "Fehler");
i.putExtra("worth", 0);
}
startActivity(i);
}
}
The XML 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/abMainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/abMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBackgroundAccent"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:backgroundTint="#color/colorBackground"
android:src="#drawable/ic_note_add"
app:borderWidth="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refreshMain"
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/abMainLayout">
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
app:layout_constraintTop_toBottomOf="#+id/abMainLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:id="#+id/gridMoney"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundAccent"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/abMain">
<TextView
android:id="#+id/sumTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="5"
android:layout_columnWeight="1"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
android:layout_row="1"
android:textColor="#color/colorDarkText"
android:textSize="50sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/moneyImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="right|center_vertical"
android:layout_row="2"
android:background="#android:color/transparent"
android:scaleX="0.5"
android:scaleY="0.5"
app:srcCompat="#drawable/ic_money" />
<TextView
android:id="#+id/moneyTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:background="#android:color/transparent"
android:textColor="#color/colorLightText"
android:textSize="15sp" />
<ImageView
android:id="#+id/sharesImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_gravity="right|center_vertical"
android:layout_row="2"
android:background="#android:color/transparent"
android:scaleX="0.5"
android:scaleY="0.5"
app:srcCompat="#drawable/outline_assessment_black_36" />
<TextView
android:id="#+id/sharesTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_columnWeight="1"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:background="#android:color/transparent"
android:textColor="#color/colorLightText"
android:textSize="15sp" />
</GridLayout>
<View
android:id="#+id/dividerMoney"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
app:layout_constraintTop_toBottomOf="#id/gridMoney" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/recycler_horizontal_margin"
android:layout_marginStart="#dimen/recycler_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="Aktien"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#+id/dividerMoney"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/gridMoney" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerShares"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/recycler_title_bottom_margin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
So #NileshRathod answered it:
It's app:elevation, not android:elevation.
But I still don't get why the method in onCreate() doesn't work.
Of course, setElevation(0); won't work.
But I still don't get why the method in onCreate() doesn't work.
AppbarLayout uses StateListAnimator from v24.0.0 and that is why setElevation will has no effect on it: https://stackoverflow.com/a/37992366/4409113
So:
StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);
Or:
toolbarLayout = findViewById(R.id.abMainLayout);
toolbarLayout.StateListAnimator = null;
In your case.
I'm new on this. Just want to show a webpage through WebView, but it
continuously crashing while launching this particular activity. Please help.
Here is my code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
public class vistaweb extends AppCompatActivity implements View.OnClickListener {
String url= "https://myschoolserver.com/apps_login.php";
WebView vWeb = (WebView) findViewById(R.id.MyWeb);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_vistaweb);
vWeb.clearCache(true);
vWeb.clearHistory();
vWeb.getSettings().setJavaScriptEnabled(true);
// vWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
vWeb.loadUrl(url);
Button back = (Button)findViewById(R.id.back);
Button forward = (Button)findViewById(R.id.forward);
Button reload = (Button)findViewById(R.id.relode);
back.setOnClickListener(this);
forward.setOnClickListener(this);
reload.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.back:
vWeb.canGoBack();
break;
case R.id.forward:
vWeb.canGoForward();
break;
case R.id.relode:
vWeb.reload();
break;
}
}
}
And here is my xml file
<?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"
tools:context="com.myschoolserver.myschoolserver_beta.vistaweb">
<Button
android:id="#+id/back"
style="#style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="BACK"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_editor_absoluteY="461dp"
android:layout_marginStart="3dp" />
<Button
android:id="#+id/relode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RELOAD"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="462dp" />
<Button
android:id="#+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forward"
tools:layout_editor_absoluteY="462dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
<WebView
android:id="#+id/MyWeb"
android:layout_width="0dp"
android:layout_height="445dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
And while launching I noticed the report bug showing error at line 16 which is
WebView vWeb = (WebView) findViewById(R.id.MyWeb);
Please help. Thanks in advance.
1.You should call findViewById in onCreate method .
2.Because you use vWeb in onClick method .So you should set vWeb as global variable .
3.Don't forget add permission .
<uses-permission android:name="android.permission.INTERNET"/>
4.Set webView.setWebViewClient and webview.setWebChromeClient in your code .
private WebView vWeb;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_vistaweb);
vWeb = (WebView) findViewById(R.id.MyWeb);
...
}
I'm trying to make an app where you can add an item to a list using an EditText and a ListView. I was using this website to help because I don't have much android coding experience, but I had to change the code a bit because I'm using two activities instead of one. But my ListView has disappeared, and I don't know why.
Questions.java (the ListView activity)
package com.example.sylvie.dogwise;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Questions extends AppCompatActivity {
ListView listview;
String[] ListElements = new String[] {
"Question 1",
"Question 2"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
Bundle newQuestion = getIntent().getExtras();
if (newQuestion==null){
return;
}
String QuestionName = newQuestion.getString("QuestionName");
listview = (ListView) findViewById(R.id.QuestionsList);
final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(Questions.this, android.R.layout.simple_list_item_1, ListElementsArrayList);
ListElementsArrayList.add(QuestionName);
adapter.notifyDataSetChanged();
}
public void backHomeOnClick(View view){
Intent b = new Intent(this, HomeScreen.class);
startActivity(b);
}
public void askAQuestionOnClick(View view){
Intent i = new Intent(this, AskAQuestion.class);
startActivity(i);
}
}
activity_questions.xml (the ListView activity)
<?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"
tools:context="com.example.sylvie.dogwise.Questions">
<Button
android:id="#+id/qBackButton"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="backHomeOnClick"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<ListView
android:id="#+id/QuestionsList"
android:layout_width="384dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/qBackButton" />
<Button
android:id="#+id/AskAQuestionButton"
android:layout_width="384dp"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:text="Ask a Question"
android:onClick="askAQuestionOnClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
AskAQuestion.java (the EditText activity)
package com.example.sylvie.dogwise;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class AskAQuestion extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_aquestion);
}
public void backQuestionsOnClick(View view){
Intent b = new Intent(this, Questions.class);
startActivity(b);
}
public void okOnClick(View view){
Intent o = new Intent(this, Questions.class);
final EditText QuestionInput = (EditText) findViewById(R.id.editText);
String Question = QuestionInput.getText().toString();
o.putExtra("QuestionName", Question);
startActivity(o);
}
}
activity_ask_aquestion.xml (the EditText activity)
<?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"
tools:context="com.example.sylvie.dogwise.AskAQuestion">
<Button
android:id="#+id/aaqBackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:onClick="backQuestionsOnClick"
android:text="Back"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Question"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="100dp" />
<Button
android:id="#+id/okButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:text="OK"
android:onClick="okOnClick"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
You need to call listview.setAdapter(adapter)
public class Questions extends AppCompatActivity {
// fields
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// code ...
// no need of notifyDataSetChanged
listview.setAdapter(adapter);
// from this moment on, use adapter.notifyDataSetChanged
// to indicate changes in dataset
}
// other methods
}