Action bar, navigation drawer with listview - android

I am novice to android programming and try to implement a listview which gets populated from the database. I want to have an actionbar with navigation drawer in my application. However as mainactivity.java extends listview, it's not possible to extend appcompatibility class.
I am trying to find solution on internet, but unable to find anything concrete. Is there anyway in which we can implement this? If there is any resource please provide that.
below is code of my activity_main.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"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
I have one more layout file app_custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"/>
//android:src="#drawable/facebook" />
<TextView
android:id="#+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/appIcon"
android:layout_toRightOf="#id/appIcon"
android:layout_marginLeft="3dp"
android:text="Application Title"
android:textSize="22dp"/>
<TextView
android:id="#+id/dlTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/titleTxt"
android:layout_marginLeft="10dp"
android:layout_marginRight="3dp"
android:text="description"
android:textSize="18dp"/>
</RelativeLayout>
My main_activity.java file is below:
package com.example.gurje.beyondteaching1;
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;
private String[] mPlanetTitles;
private ListView mDrawerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.0.29:8080/connect.php";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
#Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
#Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
public void NavigationDrawer(){
}
}
Now i want to add actionbar to it, but as i am already extending listactivity, I cant extend appcompat which is required to implement actionbar

Don't extend ListActivity. You can add list to your xml and get it using findViewById(<id>). See example below.
main_activity.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main_activity);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
ListView listView = (ListView)findViewById(R.id.listView);
}
}

Related

Probelm with spinner and context - Android Java

I'm having trouble launching the app (the app freezes) after adding a Spinner to it.
It probably doesn't get me "Context" properly - probably.
Maybe the problem lies elsewhere, but I cannot verify it at my stage
Before adding Spinner everything worked ok.
Git app code: https://github.com/RemekLago/project_Factory2.git
I will be grateful for any help.
code of main activity:
package com.example.projecttech_v4;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
private PagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager2 = findViewById(R.id.viewPager2);
tabLayout.addTab(tabLayout.newTab().setText("Maszyna01"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna02"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna03"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna04"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna05"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna06"));
FragmentManager fragmentManager = getSupportFragmentManager();
adapter = new PagerAdapter(fragmentManager, getLifecycle());
viewPager2.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position){
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
Spinner spinner1 = findViewById(R.id.spinner1_operator);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource
(this, R.array.operator, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
}
}
code: XML mainactivity:
<?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">
<TableRow
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner1_operator"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2_data"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3_godzina"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/button_zatwierdz_operator"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Zatwierdź"
android:textSize="11sp"
android:textColor="#color/black"
android:layout_weight="1"/>
</TableRow>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorGravity="stretch"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
TableRow should be a child of TableLayout only. exchange <TableRow to <LinearLayout with android:orientation="horizontal" attribute in your 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">
<!-- TableRow becomes LinearLayout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp">
<!-- spinners and rest of code -->
powodzenia

How to access a Button inside a Toolbar in Android

I'm pretty new to coding so I might not express myself correctly, please bear with me.
I have a fragment inflated inside a layout that includes a toolbar. When I open the fragment, I want to add a button (not a menu, just a button to navigate to another fragment).
My issue is I don't know how to instantiate the button from my Fragment class to add an icon and an onClickListener.
Here's my xml files:
The layout that contains my fragment:
<?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"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
android:orientation="vertical">
<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="#color/blue"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<Button
android:id="#+id/toolbar_button"
android:layout_width="#dimen/margin"
android:layout_height="#dimen/margin"
android:layout_gravity="right"
android:layout_marginRight="#dimen/innerLargeMargin"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</LinearLayout>
The layout in which the fragment is inflated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
tools:showIn="#layout/app_bar_main"
android:background="#color/orange">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f1f1f1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="#menu/bottom_nav_items"
android:background="#color/orange"
android:backgroundTint="#color/white"
design:itemIconTint="#color/white"
design:itemTextColor="#color/white"
design:itemBackground="#drawable/bottom_bar_tint_selector"
/>
</LinearLayout>
So what I want to do is something like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (???).findViewById(R.id.toolbar_button)
}
activity_main.xml to add Button inside android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFA000">
<Button
android:id="#+id/toolbarbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button toolBarBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Toolbar with button example");
toolbar.setSubtitle("Toolbar subtitle");
toolbar.setLogo(android.R.drawable.ic_menu_info_details);
toolBarBtn = (Button)findViewById(R.id.toolbarbtn);
toolBarBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
"Button in ToolBar clicked",
Toast.LENGTH_LONG).show();
}
});
}
}
Set This onClick Listener in your parent activity of Fragment.
try this to access your button of toolbar from activity
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) toolbar.findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);
toolbar_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ChatActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
you have to write like below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (Button) view.findViewById(R.id.toolbar_button);
where view is your view which you bind at your onCreateView in your Fragment.
What I do is to bind the View inside the activity and then create an interface who works with that View.
public class Activity implements ButtonListener{
//findViewbyId or Butterknife
void doSomething(){
toolBarButton.something()
}
Interface
}
public interface ButtonListener{
void doSomething();
}
Fragment
public class Fragment extends Fragment{
public ButtonListener mButton;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
mButton = (ButtonListener)getActivity();
super.onCreate(savedInstanceState);
}
}
You can access button directly without toolbar using (Button) findViewById(R.id.toolbar_button).
Toolbar toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);

add my main activity toolbar to another layout

in my main activity i have a toolbar. also i have a layout with a Custom list view. i want to show that toolbar here too. but whatever i do i cant. what is the problem? i want the same toolbar from my main activity to be shown here too.
About.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class About extends AppCompatActivity{
ListView list;
String[] text = {
"Version"
} ;
Integer[] imageId = {
R.drawable.ic_info_black_24dp
};
private Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Set a Toolbar to replace the ActionBar. (doesn't work)
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomList adapter = new
CustomList(About.this, text, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(About.this, "You Clicked at " + text[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
about.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:background="#drawable/about_background">
</ListView>
</RelativeLayout>
I think your ListView and Toolbar layout overlapping each other, that's why you are not able to see that. If you want to show ListView below the Toolbar then you have to set android:layout_below="#+id/toolbar" property in ListView like that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_below="#+id/toolbar
android:background="#drawable/about_background">
</ListView>
</RelativeLayout>

Scrolling to view in CoordinatorLayout shows only partially the view

I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">

How to reference ID from other layout

Following this tutorial I moved my ActionBar (Toolbar) from my main layout to another layout and I include the toolbar to my main layout like this:
<?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"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/my_toolbar">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
And my toolbar layout is this:
<?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="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/CustomActionBarTheme"/>
</LinearLayout>
But I got the following error
Why I cant make reference of my toolbar if it has the same id? Can I include my toolbar and make reference so I can put elements below the toolbar?
Edit 1 - Activity Code
package com.marcelo.notemuevas;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] itemname ={
"Carpinteria"
};
String[] descripcion={
"Reparación"
};
Integer[] imgid={
R.mipmap.icono1
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.setLogo(R.mipmap.ic_launcher);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, descripcion);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, carpinteria.class);
myIntent.putExtra("key", 5); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Edit 2 - Solution
Now I make it work, I added a new Id to the include then I make reference to it like James Lockhart and Phan Văn Linh suggest (dont know why the first time didnt work) so my new question is, the IDs works like variables? only exist where it has been declared?
This is the working 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"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarLayout"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbarLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
In your XML
<include
android:id=#"+id/toolBarLayout" // set the id for layout that contains toolbar
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolBarLayout"> // change my_toolbar to toolBarLayout
Then you access to your Toolbar by change
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
to
LinerLayout myLinear = (LinerLayout) findViewById(R.id.toolBarLayout);
Toolbar myToolbar = (Toolbar) myLinear.findViewById(R.id.my_toolbar);
Please try this:
In your toolbar layout file, in the LinearLayout tag, set the 'android:id' attribute to a unique layout id name, for example: android:id="#+id/toolbarLayout"
Once you have completed (1), in your main layout refer to your toolbar layout file as follows: android:layout_below="#id/toolbarLayout"
Hope this helps.

Categories

Resources