No view found for id 0x7f0a005b - android-studio

I've met the problem that trouble me for days.
I have 2 fragments: theFirstFragment & theSecondFragment.
In MainActivity, i call theFirstFragment and it load ok. After that, i make event click on theFirstFragment to change theSecondFragment. It shows error:
java.lang.IllegalArgumentException: No view found for id 0x7f0a005b
This is my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Fragment fragment = new TheFirstFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.activeMain, fragment);
transaction.commit();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activeMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
TheFirstFragment.java
public class TheFirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
return view;
}
fragment_first.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/fragment_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/background"
tools:context=".TheFirstFragment">
TheSecondFragment.java
public class TheSecondFragment extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
return view;
}
fragment_second
<?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/fragment_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/background"
tools:context=".TheSecondFragment">

Related

Recycle View android Studio search well but just shows 1 item

Here a beginner at Android studio.
I'm creating a simple agenda app, where you can add, edit, delete and search contacts.
I used a recycle view but after adding the search option, the recycle view just shows me one contact.
The contacts are being added as appear when I search for them.
Any idea on how to fix it?
It tried changing the layout but nothing works.
My main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
Here is the Adapter: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/adaptadores/ListaContactosAdapter.java
Main Java: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/MainActivity.java
Thanks a lot.
I have used the below code it's working perfectly. Please check it. Currently, I'm using dummy data for testing purposes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
lista_item_contacto.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/viewNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
contactos
public class contactos {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ListaContactosAdapter.java
public class ListaContactosAdapter extends RecyclerView.Adapter<ListaContactosAdapter.ContactoViewHolder> {
ArrayList<contactos> listaContactos;
ArrayList<contactos> listaOriginal;
public ListaContactosAdapter(ArrayList<contactos> listaContactos){
this.listaContactos = listaContactos;
listaOriginal = new ArrayList<>();
listaOriginal.addAll(listaContactos);
}
#NonNull
#Override
public ContactoViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_item_contacto, null , false);
return new ContactoViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ContactoViewHolder holder, int position) {
holder.viewName.setText(listaContactos.get(position).getName());
}
public void filtrado(String txtBuscar){
int longitud = txtBuscar.length();
if (longitud == 0){
listaContactos.clear();
listaContactos.addAll(listaOriginal);
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
List<contactos> collecion = listaContactos.stream().filter(i->i.getName().toLowerCase().contains(txtBuscar.toLowerCase()))
.collect(Collectors.toList());
listaContactos.clear();
listaContactos.addAll(collecion);
} else {
for(contactos c: listaOriginal){
if (c.getName().toLowerCase().contains(txtBuscar.toLowerCase())){
listaContactos.add(c);
}
}
}
}
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return listaContactos.size();
}
public class ContactoViewHolder extends RecyclerView.ViewHolder {
TextView viewName, viewTelephone, viewEmail;
public ContactoViewHolder(#NonNull View itemView) {
super(itemView);
viewName = itemView.findViewById(R.id.viewNombre);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context = view.getContext();
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
SearchView txtBuscar;
RecyclerView listaContactos;
ArrayList<contactos> listaArrayContactos;
ListaContactosAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtBuscar = findViewById(R.id.txtBuscar);
listaContactos = findViewById(R.id.listacontactos);
listaContactos.setLayoutManager(new LinearLayoutManager(this));
listaArrayContactos = new ArrayList<>();
contactos c1 = new contactos();
c1.setName("Hello");
contactos c2 = new contactos();
c2.setName("Android");
contactos c3 = new contactos();
c3.setName("Studio");
listaArrayContactos.add(c1);
listaArrayContactos.add(c2);
listaArrayContactos.add(c3);
adapter = new ListaContactosAdapter(listaArrayContactos);
listaContactos.setAdapter(adapter);
//Revisar si no funciona y cambiar de lugar
txtBuscar.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
adapter.filtrado(s);
return false;
}
}

android layout animation bottom to top

I have created a layout in Android . how to give animation from bottom to top and top to bottom when i pressed button?
Please find my code below.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.Theme_AppCompat);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
overridePendingTransition( R.anim.slide_in_top, R.anim.slide_in_bottom);
bottomSheetDialog.show();
}
});
}
Bottom Layout
<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"
android:id="#+id/bottomSheetContainer"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:background="#color/white">

Clickable URL in Android-Studio TextView does not work

My Link looks like a link (itÅ› color is blue) but when I click, nothing happens. Do you have any ideas? I use an external string where i want to put in the clickable URL.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/anthrazit">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="#color/lightwhite"
android:text="#string/info"
android:linksClickable="true"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My java:
public class info extends MainActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
final TextView infoTextView = (TextView) findViewById(R.id.textinfo);
infoTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode ==
KeyEvent.KEYCODE_BACK) {
intent = new Intent(info.this, MainActivity.class);
startActivity(intent);
finish();
}
return false;
}
}
And my string. I found the code online, but in my Version it does not work :-( :
<string name="info">
<i>https://stackoverflow.com/questions/ask</i>
</string>

cannot resolve symbol ListView and ArrayAdapter

I am getting cannot resolve symbol for ListView and ArrayAdapter ,trying to resolve for a long time,suggestions plz.
res/layout contains activity_main.xml,fragment.xml and layout_textview.xml
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sunshine.BlankFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_vertical" >
</ListView>
</FrameLayout>
layout_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:id="#+id/text_view">
</TextView>
BlankFragment.java
public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
List<String> weekforecast=new ArrayList<String>(Arrays.asList(forecastArray));
itemAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.layout_textview,R.id.text_view, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(weekforecast);
return rootview;
}
}
public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
final List<String> weekforecast=new ArrayList<String>();
for (int i = 0; i < forecastArray.length; ++i)
{
weekforecast.add(forecastArray[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(adapter);
return rootview;
}
Try this code

Fragment overlapping appbar

I have a possible overlapping fragment containing listView. The layout looks fine in the preview window, however, when I run it on device or Emulator the appbar goes missing.
Main XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".GuiseMainActivity"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:id="#+id/fragmentProps"></fragment>
</RelativeLayout>
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:gravity="top|center"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/propCategoryListView"></ListView>
</LinearLayout>
Main Class
public class GuiseMainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentProps fragmentProps = new FragmentProps();
fragmentTransaction.replace(android.R.id.content,fragmentProps);
fragmentTransaction.commit();
}}
Fragment Class
public class FragmentProps extends Fragment{
private View PropsFragmentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
PropsFragmentView = inflater.inflate(R.layout.fragment_props, container, false);
String[] propsCategories ={"Recently used", "Popular", "New", "Personalities", "Moustaches / Beard", "Caps/ Hats", "Glasses", "Banners"};
ListAdapter propsCategoryListAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, propsCategories);
ListView propCategoryListView = (ListView) PropsFragmentView.findViewById(R.id.propCategoryListView);
propCategoryListView.setAdapter(propsCategoryListAdapter);
propCategoryListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String categoryPicked = "You Picked: " + String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getActivity(), categoryPicked, Toast.LENGTH_LONG).show();
}
});
return PropsFragmentView;
}}
Result
I found the solution, MainActivity Extends AppCompatActivity then add these lines in onCreate method:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.applogo);
getSupportActionBar().setTitle("");
Thanks guys.

Resources