Android - Unable to change text in TextView - android

I just started to learning Android and I have a problem. With new Android Studio i have two XML files for each activity, so not to get NullPointerException I had to change my findViewById a little bit. But now I am unable to change text in TextView.
Code:
public class MainActivity extends AppCompatActivity {
LinearLayout layoutMain;
TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) View.inflate(this, R.layout.content_main, null);
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
XML files:
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />

Change
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
to
tvText = (TextView) findViewById(R.id.tvText);
You were getting an incorrect reference to the TextView.

public class MainActivity extends AppCompatActivity {
View includedView;
TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
includedView = (View) findViewById(R.id.includedView);
tvText = (TextView) includedView.findViewById(R.id.tvText);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
Also little bit change in your xml files
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
and second xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"
android:id="#+id/includedView"/>

Copy and Paste your content_main layout inside activity_main xml and delete the content_main xml file.
Then change the code in onCreate() as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) findViewById(R.id.content_main);
//content_main should be the id of the layout you copied!
tvText = (TextView) findViewById(R.id.tvText);
}

Related

Android: block options menu while collapsing toolbar

I have collapsing toolbar with options menu in fragment. When I collapse my toolbar, menu move above status bar. So, after collapse, I doesn't see
menu options. I show it in screenshots:
Here my .xml code:
<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:id="#+id/abl_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/ctl_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
<ImageView
android:id="#+id/iv_avatar"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="#drawable/ic_main_logo"
android:scaleType="centerInside"
android:background="#drawable/shape_profile_avatar_background"
app:layout_collapseMode="parallax"/>
<include layout="#layout/component_transparent_toolbar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="4dp"
app:elevation="4dp"
app:srcCompat="#drawable/vector_photo_camera_black_24dp"
app:layout_anchor="#id/abl_toolbar"
app:layout_anchorGravity="bottom|right|end"/>
<include layout="#layout/component_profile_avatar_bottom_sheet" />
Here my code from fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mActivity = (MainActivity) getActivity();
mAdapter = new ProfileRVAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, parent, false);
mIVAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
LinearLayoutManager manager = new LinearLayoutManager(mActivity);
RecyclerView rvResults = (RecyclerView) view.findViewById(R.id.rv_results);
rvResults.setLayoutManager(manager);
rvResults.setAdapter(mAdapter);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.activity_main_profile_label);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbar.setPadding(0, mActivity.getStatusBarHeight(), 0, 0);
toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + mActivity.getStatusBarHeight();
}
mActivity.setSupportActionBar(toolbar);
view.findViewById(R.id.btn_avatar).setOnClickListener(this);
view.findViewById(R.id.btn_cancel).setOnClickListener(this);
view.findViewById(R.id.btn_gallery).setOnClickListener(this);
view.findViewById(R.id.btn_photo).setOnClickListener(this);
mBottomSheet = BottomSheetBehavior.from(view.findViewById(R.id.ll_bottom_sheet));
mBottomSheet.setState(BottomSheetBehavior.STATE_HIDDEN);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_profile, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
return true;
case R.id.menu_logout:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Any idea why it has happened?
Edited / SOLVED:
I solved my problem. Mistake was in toolbar attributes, I forgot app:layout_collapseMode="pin".
Set design code of Toolbar in layout.xml code in Activity's layout file &
set toolbar as below in Activity Code,
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Add this line of code in Activity file.

How to create a Fragment over the Viewpager in android?

Explanation:
I tried more but i failed to find the how to set the transparent fragment over the Viewpager. I am working in application. The requirement of my client is who will make a application like below url
https://play.google.com/store/search?q=munchery&hl=en
In above url application. In this application they were added more functionality like navigationDrawer, Tabs with viewpager and etc.
One thing is when i click on the actionBar Today's menu it's open something. I am not able to recognized to find what it actually open while i click on actionBar in the Munchery application.
I tried to implement but not implemented as same as the munchery application.
activity_main.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"
android:fitsSystemWindows="true"
tools:context="com.millu.whatsappdemo.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:textSize="20sp"
android:clickable="true"
android:textColor="#FFFFFF"
android:text="Text view"
android:drawableRight="#drawable/ic_up_arrow">
</TextView>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
public TextView action_text;
public Toolbar toolbar;
TabLayout tabs;
ViewPager viewPager;
List<Fragment> fragmentList;
List<String> tab_title;
boolean flag=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabs = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
action_text = (TextView) toolbar.findViewById(R.id.action_text);
fragmentList = new ArrayList<>();
tab_title = new ArrayList<>();
tab_title.add("ONE");
tab_title.add("TWO");
fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
tabs.setupWithViewPager(viewPager);
action_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment=new ThirdFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
if(!fragment.isHidden()) {
fragmentTransaction.replace(R.id.container,fragment);
flag=true;
}
else{
fragmentTransaction.remove(fragment);
flag=false;
}
fragmentTransaction.commit();
}
});
}
#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) {
switch (item.getItemId()){
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
In above code i set the flat onClick of the textview over the actionBar for hiding and showing the fragment. In my demo the problem is it show the tab when my third fragment is open and it does not hide.
Please help me to recognized what is it open on click of the actionBar?
Try the code as below: 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScrollingActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:clickable="true"
android:drawableRight="#mipmap/ic_launcher"
android:gravity="center"
android:text="Text view"
android:textColor="#FFFFFF"
android:textSize="20sp"></TextView>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tabs"
android:background="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/reltv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:background="#80ffffff"
android:visibility="gone">
<RelativeLayout
android:id="#+id/reltv2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Do whatever you like it here."
android:textAppearance="?attr/textAppearanceLargePopupMenu" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
And your ManiActivity.java as below:
public class MainActivity extends AppCompatActivity {
public TextView action_text;
public Toolbar toolbar;
TabLayout tabs;
ViewPager viewPager;
List<Fragment> fragmentList;
List<String> tab_title;
boolean flag = false;
RelativeLayout reltv;
RelativeLayout reltv2;
boolean isShown = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabs = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
action_text = (TextView) toolbar.findViewById(R.id.action_text);
reltv = (RelativeLayout) findViewById(R.id.reltv);
reltv2 = (RelativeLayout) findViewById(R.id.reltv2);
fragmentList = new ArrayList<>();
tab_title = new ArrayList<>();
tab_title.add("ONE");
tab_title.add("TWO");
fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
tabs.setupWithViewPager(viewPager);
action_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isShown) {
isShown=true;
reltv.setVisibility(View.VISIBLE);
}else {
isShown=false;
reltv.setVisibility(View.GONE);
}
}
});
}
#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_scrolling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
First, you should use some translucent or transparent colour for the android: background of parent's attribute in the fragment layout e.g DayFragment . This is the fragment you will inflate in the overlay FrameLayout.
For a smooth inflation of that fragment, you can use custom animation to make the transition smooth like in the Munchery app(uses slide from top animation)
protected void replaceFragment(int containerViewId, Fragment fragment, boolean isAddToBackStack, boolean isAnimateTransition) {
String backStateName = fragment.getClass().getName();
FragmentManager fm = this.getSupportFragmentManager();
boolean fragmentPopped = fm.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && getSupportFragmentManager().findFragmentByTag(backStateName) == null) {
FragmentTransaction ft = fm.beginTransaction();
if(isAnimateTransition) {
animateFragmentTransition(ft);
}
ft.replace(containerViewId, fragment, backStateName);
if (isAddToBackStack) {
ft.addToBackStack(backStateName);
}
ft.commit();
}
}
private void animateFragmentTransition(FragmentTransaction ft) {
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.slide_in_from_top, R.anim.slide_out_to_top);
}
DayFragment fragment = DayFragment.newInstance(null);
replaceFragment(R.id.container, fragment, false, true);
Alternitively, you can use ObjectAnimators to revael and hide views as you wish. For example:-
private void animate() {
View viewToShiftOut = mNormalModelayoutContainer;
View viewToShiftIn = mEditModelayoutContainer;
ObjectAnimator outAnim = ObjectAnimator.ofFloat(viewToShiftOut, "y", 0, -heightOfScreen);
ObjectAnimator inAnim = ObjectAnimator.ofFloat(viewToShiftIn, "y", heightOfScreen , 0);
outAnim.setDuration(1000);
inAnim.setDuration(1000);
outAnim.start();
inAnim.start();
}

How to make Focus to FloatingActionButton Even if the Navigation drawer is open in Android

I want to make focus on FloatingActionButton Even if my Navigation Drawer is open.
I know when the navigation Drawer is open then rest of the screen is unfocusable by using setScrimColor this we can reduce the focus on Navigation Drawer. But I want to make FloatingActionButton is focusable.
Library is below for ActionButton
https://github.com/shell-software/fab
see the below screenshot for my problem.
Normal Screen :
Click on FloatingButton :
What I want like this Focusable :
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity">
<com.example.softeng.animationf.fabdirectory.ActionButton
android:id="#+id/fab_activity_action_button"
style="#style/fab_action_button_style"
fab:type="MINI"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end">
<RelativeLayout
android:id="#+id/right_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4D4D4D">
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigation_view;
int count = 1;
private boolean isOutSideClicked = false;
RelativeLayout relativeLayout;
private ActionButton actionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
/* drawerLayout.setScrimColor(Color.parseColor("#00000000"));*/
navigation_view = (NavigationView)findViewById(R.id.navigation_view);
relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);
actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);
actionButton.setImageResource(R.drawable.fab_plus_icon);
actionButton.setRippleEffectEnabled(true);
actionButton.setShadowRadius(0);
actionButton.setShadowXOffset(0);
actionButton.setShadowYOffset(0);
actionButton.setButtonColor(Color.parseColor("#0072BA"));
actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
actionButton.setShadowRadius(10);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count == 1) {
actionButton.moveLeft(200);
actionButton.setImageResource(R.drawable.close);
drawerLayout.openDrawer(Gravity.RIGHT);
count = count - 1;
}else if(count == 0){
closeFab();
}
else {
}
}
});
}
private void closeFab(){
actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
actionButton.setImageResource(R.drawable.fab_plus_icon);
count = count + 1;
drawerLayout.closeDrawer(Gravity.RIGHT);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (drawerLayout.isDrawerOpen(navigation_view)) {
View content = findViewById(R.id.drawer_layout);
int[] contentLocation = new int[2];
content.getLocationOnScreen(contentLocation);
Rect rect = new Rect(contentLocation[0],
contentLocation[1],
contentLocation[0] + content.getWidth(),
contentLocation[1] + content.getHeight());
if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
isOutSideClicked = true;
} else {
isOutSideClicked = false;
this.closeFab();
}
}
}
return super.dispatchTouchEvent(event);
}
#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);
}
}
The Rounded Image means FloatingActionButton i want to focusable. Any Help be Appreciated.
Found the solution to your problem. I have done some changes in your Layout as well as in Java code. Please refer and let me know if there are any queries.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigation_view;
ImageView view;
int count = 1;
private boolean isOutSideClicked = false;
RelativeLayout relativeLayout;
private ActionButton actionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView)findViewById(R.id.view);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerLayout.setScrimColor(Color.parseColor("#00000000"));
navigation_view = (NavigationView) findViewById(R.id.navigation_view);
relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);
actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);
actionButton.setImageResource(R.drawable.fab_plus_icon);
actionButton.setRippleEffectEnabled(true);
actionButton.setShadowRadius(0);
actionButton.setShadowXOffset(0);
actionButton.setShadowYOffset(0);
actionButton.setButtonColor(Color.parseColor("#0072BA"));
actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
actionButton.setShadowRadius(10);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count == 1) {
actionButton.moveLeft(200);
actionButton.setImageResource(R.drawable.fab_plus_icon);
drawerLayout.openDrawer(Gravity.RIGHT);
view.setVisibility(View.VISIBLE);
actionButton.bringToFront();
count = count - 1;
}else if(count == 0){
closeFab();
}
else {
}
}
});
}
private void closeFab(){
view.setVisibility(View.GONE);
actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
actionButton.setImageResource(R.drawable.fab_plus_icon);
count = count + 1;
drawerLayout.closeDrawer(Gravity.RIGHT);
}
// dispatchTouchEvent is as it was not putting code here..!!
}
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:visibility="gone" />
<com.example.softeng.animationf.fabdirectory.ActionButton
android:id="#+id/fab_activity_action_button"
style="#style/fab_action_button_style"
android:layout_gravity="bottom|right"
fab:type="MINI"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end">
<RelativeLayout
android:id="#+id/right_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4D4D4D">
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Please use this same code. I recommend you do copy-paste because this is working exactly as you wanted..!!
use Frame layout as a parent ViewGroup as it uses z index so place
FAB floating action button at the end
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_search"
android:elevation="50dp"
android:layout_gravity="left|bottom" />
</FrameLayout>

ArrayAdapter not binding data to ListView

I am trying learn Android and building a very basic ListView, the data set up using ArrayAdapter is not showing up. My Activity code is:
MainActivity Class(default no changes)
`public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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);
}
}`
**MainActivityFragment class**
public class MainActivityFragment extends Fragment {
private ListAdapter arrayAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<String> myEntries = new ArrayList<String>();
myEntries.add("Unix");
myEntries.add("Java");
myEntries.add("Android");
arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_forecast_item_textview, myEntries);
ListView listView = (ListView)inflater.inflate(R.layout.fragment_main, container, false).findViewById(R.id.list_view_forecast);
listView.setAdapter(arrayAdapter);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
android:gravity="center_vertical"
android:text=""
android:id="#+id/list_forecast_item_textview">
</TextView>
fragment_main.xml
<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" 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:showIn="#layout/activity_main"
tools:context=".MainActivityFragment"
android:id="#+id/forecastFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_view_forecast" />
<include layout="#layout/list_item_forecast" />
</FrameLayout>
activity_main.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" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
All my application code is as above. I have tried tweaking the code and also tried this link but cannot get this to work. Please help.
The listview you setAdapter is not the listview you return in the fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<String> myEntries = new ArrayList<String>();
myEntries.add("Unix");
myEntries.add("Java");
myEntries.add("Android");
arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_forecast_item_textview, myEntries);
View view =inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView)view .findViewById(R.id.list_view_forecast);
listView.setAdapter(arrayAdapter);
return view ;
}

Replace Fragment: IllegalArgumentException: No view found for id

All of my activities extend MainActivity and share activity_main layout. In 1 of my activities, I'm using the slidinguppanel library, so the layout is different. When I try to attach the fragment after adding the layout view, I get error:
10-16 15:25:14.155 32590-32590/com.myproject.myapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.myapp/com.myproject.myapp.search.SearchActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00ea (com.myproject.myapp/searchContainer) for fragment SearchFragment{2e02f51a #0 id=0x7f0d00ea SearchFragment}
What am I doing wrong?
MainActivity.java oncreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_hamburger_icon);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mMainContainer = (FrameLayout) findViewById(R.id.mainContainer);
mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
mAppBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mUserImageView = (RoundedImageView) navigationView.findViewById(R.id.userImageView);
mRealNameView = (TextView) navigationView.findViewById(R.id.realNameText);
mUserNameView = (TextView) navigationView.findViewById(R.id.userNameText);
showUserInfo(mUser != null);
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/drawer_items" />
</android.support.v4.widget.DrawerLayout>
SearchActivity.java onCreate (I marked the line having the problem):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
mDrawerLayout.removeView(mCoordinatorLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_appbar_layout, mDrawerLayout, false);
mSlidingPanel = (SlidingUpPanelLayout) view.findViewById(R.id.sliding_layout);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
mDrawerLayout.addView(view);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleDrawer();
}
});
SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SearchFragment");
if (searchFragment == null) {
FrameLayout searchContainer = (FrameLayout) view.findViewById(R.id.searchContainer);
searchContainer.setId(R.id.searchContainer);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.searchContainer, SearchFragment.newInstance(), "SearchFragment") // here's the problem
.commit();
}
viewPager = (ViewPager) view.findViewById(R.id.vp_searchResultViewPager);
SearchResultPagerAdapter pagerAdapter = new SearchResultPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabStrip = (TabLayout) view.findViewById(R.id.tl_searchResultTabs);
tabStrip.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_SETTLING) {
switch (viewPager.getCurrentItem()) {
case 1:
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchMapEvent(foursquareResultsObject));
}
break;
default: //case 0
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchListEvent(foursquareResultsObject));
}
break;
}
}
}
});
}
search_appbar_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoDragView="#+id/toolbar"
sothree:umanoInitialState="expanded"
sothree:umanoOverlay="true"
sothree:umanoPanelHeight="?attr/actionBarSize"
sothree:umanoParallaxOffset="0dp"
sothree:umanoShadowHeight="4dp">
<!-- MAIN CONTENT -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="?attr/actionBarSize">
<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:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="#+id/tl_searchResultTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/vp_searchResultViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<!-- SLIDING LAYOUT -->
<LinearLayout
android:id="#+id/ll_searchDragView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:bar="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="end|center_vertical"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
bar:navigationIcon="#drawable/ic_action_hamburger_icon"
bar:theme="#style/ThemeOverlay.AppCompat.ActionBar"
bar:title="#string/search_venues"
bar:titleTextAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/searchContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
In your SearchActivity you are using the activity_main layout instead of the search_app_bar_layout where you have the fragment container.
Change from this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_appbar_layout);

Categories

Resources