NullPointerException trying to find LinearLayout - android

I have some code in an Android app which was working fine but is now throwing an NPE when trying to locate a LinearLayout. The first line of the ReadRecords method below is returning a null for the linearLayoutRecords object, which is causing an NPE on the 2nd line:
public class SavedMealsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_meals);
// enable the home button
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
// Read saved meal records from the database and display them
readRecords();
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
The layout XML is here:
<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:background="#drawable/background"
android:orientation="vertical"
tools:context="com.ian.mealtimer.SavedMealsActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/activity_saved_meals_textview1"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:id="#+id/scrollViewRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRecordCount"
android:layout_margin="5dip"
android:padding="5dip" >
<LinearLayout
android:id="#+id/linearLayoutRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Any ideas??

Related

'void android.databinding.ViewDataBinding.executePendingBindings()' on a null object reference - Algolia

The Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.databinding.ViewDataBinding.executePendingBindings()' on a null object reference
I double checked the names to the references used. Seems fine to me. I don't know why I keep getting this error. The view crashes as soon as I open it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_findfriend);
searcher = Searcher.create(ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX_NAME);
helper = new InstantSearch(this, searcher);
helper.search();
// Get a reference to your Hits widget
final Hits hits = (Hits) findViewById(R.id.hits);
// Add an OnItemClickListener
hits.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView recyclerView, int position, View v) {
JSONObject hit = hits.get(position);
// Do something with the hit
}
});
}
#Override
protected void onDestroy() {
searcher.destroy();
super.onDestroy();
}
}
Here's my Activity:
activity_findfriend.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"
xmlns:algolia="http://schemas.android.com/apk/res-auto"
android:paddingTop="0dp"
android:background="#color/white">
<LinearLayout
android:id="#+id/search_emoji"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/search_drawable"
android:gravity="top"
android:paddingTop="8dp"
android:orientation="horizontal">
<com.algolia.instantsearch.ui.views.SearchBox
android:id="#+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/message_rectangle"
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/search_emoji">
<com.algolia.instantsearch.ui.views.Hits
android:id="#+id/hits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
algolia:itemLayout="#layout/hits_item"
android:layout_below="#+id/search_emoji"/>
</LinearLayout>
To use data binding in your layout, wrap it in a <layout> root tag.
New hits_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:id="#+id/hits_items">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/user_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:civ_border_width="4dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:src="#drawable/profile_thumbnail"
app:civ_border_color="#c42f92"
algolia:attribute='#{"image"}'/>
<TextView
android:id="#+id/user_name"
android:paddingTop="53dp"
android:paddingLeft="20dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
algolia:attribute='#{"username"}'
algolia:highlighted='#{true}'/>
</LinearLayout>
</layout>

Nested LinearLayout doesn't work in Android

I am trying to make layout like in the picture. Project picture I can only link it because I am new. When I run the code with just one LinearLayout it works fine. But when I make nested linear layout it shows an only white screen. And there are no errors. The code adds a score to teams according to their throw s.Thank you.
Java code:
public class MainActivity extends AppCompatActivity {
int scorea=0;
int scoreb=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayforpointb3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforpointb2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb++;
scoreView.setText(""+scoreb);
}
public void displayforpoint3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=3;
scoreView.setText(""+scorea);
}
public void displayforpoint2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=2;
scoreView.setText(""+scorea);
}
public void displayforfreethrow(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea++;
scoreView.setText(""+scorea);
}
}
XML code:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.courtcounter.MainActivity"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#string/zero"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:id="#+id/team_a_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point3"
android:onClick="displayforpoint3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point2"
android:onClick="displayforpoint2"/>
<Button
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:layout_height="wrap_content"
android:text="#string/freethrow"
android:onClick="displayforfreethrow"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/zero"
android:id="#+id/team_b_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="#string/point3"
android:onClick="displayforpointb3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/>
android:onClick="displayforpointb2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/freethrow"
android:onClick="displayforfreethrowb"/>
</LinearLayout>
</LinearLayout>
First of all you should define your TextView as a private attributes in your class as follows
private TextView scoreViewA;
private TextView scoreViewB;
Then in your onCreate() method, you should find the view of those TextView as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scoreViewA = findViewById(R.id.team_a_score);
scoreViewB = findViewById(R.id.team_b_score);
}
Then in your onClick methods, you should update those TextView
public void displayforpointb3(View view){
scoreb+=3;
scoreViewB.setText(""+scoreb);
}
public void displayforpointb2(View view){
scoreb+=2; //You had scoreb+=3 I changed it to 2
scoreViewB.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
scoreb++;
scoreViewB.setText(""+scoreb);
}
public void displayforpoint3(View view){
scorea+=3;
scoreViewA.setText(""+scorea);
}
public void displayforpoint2(View view){
scorea+=2;
scoreViewA.setText(""+scorea);
}
public void displayforfreethrow(View view){
scorea++;
scoreViewA.setText(""+scorea);
}
Also I see something wrong in your activity_main.xml I guess is a misstype
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/> <--- Remove this "/>"
android:onClick="displayforpointb2"/>
Happy coding
Problem
The orientation of the root LinearLayout is horizontal. This results in child views appearing to the right of the Toolbar instead of underneath it.
Solution
Make the root LinearLayout vertical and wrap the two inner view groups in a horizontal LinearLayout.
<?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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Team A -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
<!-- Team B -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
It's not pretty, but it gets the job done. Consider using ConstraintLayout as an alternative to nested view groups if you encounter any performance issues.

Android toolbar change layout

I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case?
MainActivity.class
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
#InjectView(R.id.tool_bar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
activity_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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolBar"
>
<RelativeLayout
android:id="#+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="#mipmap/icon_sliding_menu" />
<ImageView
android:id="#+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/app_logo"
android:padding="5dp">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="#mipmap/icon_my_cart" />
<TextView
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/hotlist_bell"
android:layout_alignTop="#id/hotlist_bell"
android:background="#drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?
There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.
1) First, you create XML files for every toolbar you need.
2) Then you include every toolbar in to your activity_main.xml, like this:
<?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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar1"
android:visibility="visible" />
<include
android:id="#+id/toolbar2"
layout="#layout/tool_bar2"
android:visibility="gone" />
<include
android:id="#+id/toolbar3"
layout="#layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
See how all three toolbar includes have the visibility property?
Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.
For example:
RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);
Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);
Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);
Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);
And just change which one are you making visible, according to your needs.
Hope this helps.
I don`t think the method of #herrmartell is nice;
In my way,I have three Fragment in a Activity,I defined some different layout resources for a common toolbar xml,and load them in toolbar when I click different button; my code :
common_toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
activity_main_toolbar_common_view.xml
<TextView
android:id="#+id/action_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="迪士尼...ˇ"
android:textColor="#color/white"
android:textSize="#dimen/larget_text_size"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
activity_main_toolbar_mine_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mine"
android:textSize="#dimen/big_text_size"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/setting"
android:layout_width="#dimen/toolbar_icon_size"
android:layout_height="#dimen/toolbar_icon_size"
android:src="#drawable/toolbar_setting"
android:layout_alignParentRight="true"
/>
switchToolbar() method in Activity:
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
I render my toolbar view in the onCreate() method of Activity:
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
when I switch toolbar, I call:
switchToolbar(R.layout.activity_main_toolbar_mine_view);

Android getActionBar().setTitle does not work

ActionBar - setTitle seems to not to be called. All other things I'm doing with ActionBar (like HomeIsUp and so on) are going well, but setTitle - not.
Here is the code of a simple activity:
public class GuidelineDetails extends Activity implements OnClickListener {
String guideline_name;
String guideline_attachment;
public static TextView tv_title;
public static TextView tv_intro;
public static TextView tv_test;
public static WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guideline_detail);
getActionBar().setTitle("Privet");
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(R.drawable.listbullet);
///// some code
}
}
Why does it not set the title?
in manifest:
<activity
android:name=".GuidelineDetails"
android:configChanges="orientation"
android:label="Guideline"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light" >
</activity>
in layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/guideline_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="1dp"
android:paddingLeft="10dp"
android:paddingRight="1dp"
android:paddingTop="35dp"
android:textSize="18dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/webviewD"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</TableRow>
</LinearLayout>
</LinearLayout>
because of
getActionBar().setDisplayShowTitleEnabled(false);
change it to
getActionBar().setDisplayShowTitleEnabled(true);
Did you tried to set subTitle? Because in some cases if device screen is small in ActionBar you can see only the ActionBar icon. Try this.getActionBar().setTitle("Privet"); Hope this will help you!
I think it's because the Manifest file over rules it. I was having same issue with my icon, so I just had to change it in manifest file (android:label="Guideline"), or try not setting a label in Manifest if you have to set it grammatically.
Try to extend ActionBarActivity instead of Activity
in the manifest:
<activity android:name=".MessagingActivity"
android:label="MessagingActivity"
android:parentActivityName=".LoginActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
And in the Activity:
public class MessagingActivity extends AppCompatActivity {
private ImageView profileImage;
private TextView username;
private FirebaseUser firebaseUser;
private DatabaseReference referense;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
Toolbar toolbarLcl = findViewById(R.id.toolbar);
setSupportActionBar(toolbarLcl );
ActionBar mActionBarLcl = getSupportActionBar();
mActionBarLcl.setTitle("username");
mActionBarLcl.setDisplayHomeAsUpEnabled(true);
toolbarLcl .setNavigationOnClickListener(v->{
finish();
});.....
and the 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=".MessagingActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent"
>
<include layout="#layout/toolbar_messaging" />
</android.support.design.widget.AppBarLayout>

sliding menu using jfeinstein10 library

I created a sample application to test how sliding menu works. Shown below in the screenshot is what i get as of now. But when I click on the categories button (shown in image below) I should get a secondary menu as shown in the zomato app's screenshot below. How can I do this ? Am I proceeding in the correct way or not?
my SlidingFragmentActivity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
con = this;
setSlidingActionBarEnabled(false);
setContentView(R.layout.main);
sm = getSlidingMenu();
sm.setMode(SlidingMenu.RIGHT);
sm.setShadowDrawable(R.drawable.shadowright);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
sm.setBehindScrollScale(1.0f);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.5f);
//sm.setSecondaryMenu(R.layout.properties);
//sm.setSecondaryShadowDrawable(R.drawable.shadow);
setTitle("Sliding Bar");
// set the Behind View
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
}
my SampleListFragment :
public class SampleListFragment extends SherlockFragment {
private static final String[] Radio_buttons = new String[] { "Distance",
"Rating" };
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
ListView radio_list = (ListView) view.findViewById(R.id.RadioList);
Button categories = (Button) view.findViewById(R.id.sampleButton);
radio_list
.setAdapter(new ArrayAdapter<String>(MainActivity.con,
android.R.layout.simple_list_item_single_choice,
Radio_buttons));
radio_list.setItemsCanFocus(true);
radio_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
categories.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.sm.showSecondaryMenu();
}
});
return view;
}
}
main.xml
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Sliding menu demo...!!!" />
</RelativeLayout>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SEARCH"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:id="#+id/searchTextLayout"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginBottom="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip" >
<ImageButton
android:id="#+id/searchTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#685E5C"
android:scaleType="fitCenter"
android:src="#drawable/abs__ic_search" />
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/searchTextButton"
android:background="#drawable/background_black_border_full"
android:padding="8dp"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SORT BY"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="134dp" >
<ListView
android:id="#+id/RadioList"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
<Button
android:id="#+id/sampleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Categories" />
</LinearLayout>
</ScrollView>
SlidingMenu does NOT do this, zomato uses a custom implementation.
SlidingMenu will let you have a menu on the left and the right, but not two on either side.
I would look at using a view pager or a custom implementation. EitherWay I don't know of anything out of the box to do this. I might be worth looking at Android Views for inspiration.
It's a bit late, but still let me post my answer in case it will help someone in future. If you want to show another menu you can use
setMode(SlidingMenu.LEFT_RIGHT);
setSecondaryMenu(R.layout.yourSecondMenu);
and in your button click
showSecondaryMenu(true);
and perform your actions in that class.

Categories

Resources