Adding PreferenceScreen to linearlayout - java

Hi I am new to Android and Java and have run into a problem. My problem is that i am trying to add a PreferenceScreen to my linear layout so I am able to set an setOnSeekBarChangeListener() on the seekbar in res/layout/activity_main.xml. At the moment the PreferenceScreen and the LinearLayout get added to the view but they are on top of each other which is not what I want. Instead I want the PreferenceScreen to go in place of the ListView inside my layout file.
res/xml/settings.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Category"
android:key="category_preference">
<SwitchPreference
android:key="switch_preference"
android:title="Title"
android:summary="Summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="checkbox_preference"
android:title="Title"
android:summary="Summary"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_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" >
<SeekBar
android:id="#+id/main_seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
setContentView(R.layout.activity_main);
SeekBar seekBar = (SeekBar) findViewById(R.id.main_seek_bar);
seekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
});
}
public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
onSharedPreferenceChanged(null, "");
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("switch_preference")) {
Toast.makeText(getActivity(), "Switch", Toast.LENGTH_LONG).show();
}
}
}
}

in your res/layout/activity_main.xml file change
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
to
<fragment android:id="#+id/fragment_settings"
android:layout_width="fill_parent"
android:name="yourpackagename.MainActivity$SettingsFragment"
android:layout_height="fill_parent" />
and then in your MainActivity.java file remove the following line
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();

Related

On implementing Material Search View, Textview is not shown at first launch

I'm implementing searchview for my activity. It works well but I also want to show textview at first launch. But when i run app, then textview is not shown but only search menu item appears and when I search anything and click search button, then only textview appears and I can similarly search any item as before. But problem is on First launch the item in my xml don't appear and only toolbar with search menu item appears. What is the cause of it?
Here is my code
public class MainActivity extends AppCompatActivity {
MaterialSearchView searchView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchViewCode();
}
private void searchViewCode() {
searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));
searchView.setEllipsize(true);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
#Override
public void onSearchViewShown() {
}
#Override
public void onSearchViewClosed() {
}
});
} /*click alt+insert key */
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.material_search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (searchView.isSearchOpen()) {
searchView.closeSearch();
} else {
super.onBackPressed();
}
}
}
and my xml is
<Linearlayout>
<FrameLayout
android:id="#+id/toolbar_container"
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="#color/appcolor"
android:elevation="4dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:id="#+id/ev_title"
android:text="uis sed nisi arcu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</Linearlayout>
Thank in advance.
You should Constraint Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/appcolor"
android:elevation="4dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:id="#+id/ev_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="uis sed nisi arcu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar_container" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to add Toolbar to PreferenceActivity

I am creating an app which have a setting PreferenceActivity with actionbar and that's working fine. I want to use another custom PreferenceActivity with actionbar which is using a layout (activity_about.xml) and preference layout (about_preferences.xml). But when i run the app it's causing my app to crash on calling that activity. Probably the actionbar is returning the Null, i can't figure it out where is the problem. Please help.
My ActivityAbout.java
public class ActivityAbout extends PreferenceActivity {
private AppCompatDelegate mDelegate;
private ActionBar actionBar;
private SharedPref sharedPref;
private View parent_view;
private TextView ver;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.about_preferences);
setContentView(R.layout.activity_about);
View lv = findViewById(android.R.id.list);
if (lv != null) lv.setPadding(0, 0, 0, 0);
final Preference prefPrivacy = (Preference) findPreference(getString(R.string.pref_title_privacy));
final Preference prefTerm = (Preference) findPreference(getString(R.string.pref_title_term));
final Preference prefBuild = (Preference) findPreference(getString(R.string.pref_title_build));
final Preference prefCopyright = (Preference) findPreference(getString(R.string.pref_title_copyright));
String versionName = BuildConfig.VERSION_NAME;
ver = (TextView) findViewById(R.id.version);
ver.setText("Version "+versionName);
prefBuild.setSummary("Version "+versionName);
int y = Calendar.getInstance().get(Calendar.YEAR);
prefCopyright.setSummary("Copyright © "+y+" Get Rid Remedy.\nAll Right Reserved.");
prefPrivacy.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
dialogPrivacy(ActivityAbout.this);
return false;
}
});
prefTerm.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
dialogTerm(ActivityAbout.this);
return false;
}
});
initToolbar();
}
public void dialogPrivacy(Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(activity.getString(R.string.pref_title_privacy));
builder.setMessage(activity.getString(R.string.content_privacy));
builder.setPositiveButton("OK", null);
builder.show();
}
public void dialogTerm(Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(activity.getString(R.string.pref_title_term));
builder.setMessage(activity.getString(R.string.content_term));
builder.setPositiveButton("OK", null);
builder.show();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
private void initToolbar() {
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(R.string.activity_title_settings);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
#Override
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
#Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
#Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
#Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
#Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
#Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
#Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
}
Here is the about_preferences layout.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="#string/pref_title_dev_email"
android:summary="#string/developer_email"
android:title="#string/pref_title_dev_email"/>
<Preference
android:key="#string/pref_title_copyright"
android:summary="#string/copyright"
android:title="#string/pref_title_copyright"/>
<Preference
android:key="#string/pref_title_build"
android:summary="#string/app_version"
android:title="#string/pref_title_build"/>
<Preference
android:key="#string/pref_title_privacy"
android:title="#string/pref_title_privacy"
android:widgetLayout="#layout/ic_about"/>
<Preference
android:key="#string/pref_title_term"
android:title="#string/pref_title_term"
android:widgetLayout="#layout/ic_about"/>
Here is activity_about.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_about"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.overridecode.getridremedy.ActivityAbout">
<android.support.design.widget.AppBarLayout
android:id="#+id/tool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_below="#+id/tool"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imageView" />
<TextView
android:text="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#73000000"
android:id="#+id/app_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="#+id/version" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The error i am getting:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.overridecode.getridremedy/
com.overridecode.getridremedy.ActivityAbout}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
Add an ID to your Toolbar:
<include
android:id="#+id/toolbar_prefs"
layout="#layout/toolbar" />
And call setSupportActionBar() (passing your Toolbar instance) before calling getSupportActionBar():
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_prefs);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(R.string.activity_title_settings);
}
Otherwise getSupportActionBar() will return null.
As per my experience, I found there is no ActionBar in preference activity. So,
There are following steps to add toolbar in your activity
Add in xml
<include layout="#layout/toolbar" />
Set Toolbar in java class using following code.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarId);
setSupportActionBar(toolbar);
NOTE: here you need to use this code in starting of intitToolbar() method.
Import proper class/package for your toolbar. From your comments seems you need to import
import android.support.v7.widget.Toolbar;
Hope this may help you. If helps, please approve it as right answer.

ViewPager + TabLayout with NavigationDrawer in every Activity - Issue

I encountered a problem that I didn't managed to solve: I have a MainActivity where there's a NavigationDrawer that allows me to go to three different activities. Those extend the MainActivity so I get the Drawer in every activity.
In the same MainActivity, I put a TabLayout with three tab Fragments.
The problem I'm facing is that whenever I go to one of the three activity from the drawer layout, I don't get the layout xml attached to Activity1 but instead I get again the TabLayout with the Fragments.
How can I solve this?
The result should look like Google Play app.
Here's my MainActivity:
public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(R.string.app_name);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
Button button1 = (Button)findViewById(R.id.button1); //this is inside the drawer layout
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
null,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View v) {
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
mCollectionPagerAdapter = new CollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
}
public class CollectionPagerAdapter extends FragmentPagerAdapter {
//final int NUM_ITEMS = 3; // number of tabs
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
}
return null;
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return getString(R.string.tab1);
case 1:
return getString(R.string.tab2);
case 2:
return getString(R.string.tab3);
}
return null;
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Activity1:
public class Activity1 extends MainActivity { //extends MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.classe1); //setContentView before super.onCreate(savedInstanceState) allows me to get drawer in each activity
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("Activity 1");
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Tab1 (Fragment):
public class Tab1 extends Fragment {
View view;
public Tab1() {
}
#SuppressLint("InflateParams")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab1, null);
return view;
}
and my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginBottom="60dp"
android:id="#+id/pager">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tabs"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextColor="#color/tab_text"
app:tabIndicatorColor="#android:color/transparent"
app:tabBackground="#drawable/selected_tab_color"
style="#style/MyCustomTabLayout"/>
<include layout="#layout/toolbar" android:id="#+id/toolbar_layout"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:background="#ffffff">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/uno"
android:text="Button to Activity1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
classe1.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginBottom="30dp"
android:id="#+id/content_frame">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="CLASSE 1"
android:textSize="35sp"
android:gravity="center"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<include layout="#layout/toolbar" android:id="#+id/toolbar_layout"/>
</RelativeLayout>
</RelativeLayout>
Your problem is that the super.onCreate() call in Activity1 is calling setContentView() again in MainActivity, which is completely replacing Activity1's layout set with its call to setContentView().
Since you want tabs in MainActivity but not the other Activities, your other Activities shouldn't extend MainActivity. Instead, you should create a base Activity with the DrawerLayout that all of your Activities extend, including MainActivity, and then add whichever Views you need in the individual subclasses.
In the base Activity, we'll override the setContentView() method to first set the base layout, setup the drawer and toggle, and then inflate the subclass's layout into the DrawerLayout's content View. Note that we do not call setContentView() in the base Activity's onCreate() method.
public abstract class BaseActivity extends AppCompatActivity {
protected Toolbar toolbar;
protected DrawerLayout mDrawerLayout;
protected ActionBarDrawerToggle mDrawerToggle;
protected TextView mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
#Override
public void setContentView(int layoutResID) {
super.setContentView(R.layout.activity_base);
toolbar = (Toolbar) findViewById(R.id.toolbar_layout);
mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(R.string.app_name);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//this is inside the drawer layout
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BaseActivity.this, Activity1.class);
startActivity(intent);
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
null,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View v) {
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
getLayoutInflater().inflate(layoutResID,
(ViewGroup) findViewById(R.id.content));
}
}
The base layout is pretty much the same, except everything specific to MainActivity is removed.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="#layout/toolbar" android:id="#+id/toolbar_layout"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:background="#ffffff">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:text="Button to Activity1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In MainActivity, we no longer need to setup the drawer and toggle.
public class MainActivity extends BaseActivity {
private CollectionPagerAdapter mCollectionPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCollectionPagerAdapter = new CollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
}
...
}
And the layout for MainActivity is now basically just the ViewPager and TabLayout.
<LinearLayout 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:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/pager">
...
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tabs"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextColor="#color/tab_text"
app:tabIndicatorColor="#android:color/transparent"
app:tabBackground="#drawable/selected_tab_color"
style="#style/MyCustomTabLayout" />
</LinearLayout>
Then, to accomplish everything in Activity1 that your posted code is doing, all we need is this, since the Toolbar and title TextView are now in BaseActivity.:
public class Activity1 extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.classe1);
mTitle.setText("Activity 1");
}
}
And the layout for Activity1 can be pared down significantly:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="CLASSE 1"
android:textSize="35sp"
android:gravity="center"/>
</LinearLayout>
</ScrollView>
You call setContentView two times in the Activity1 onCreate method, the first time with R.layout.classe1 and the second time with R.layout.activity_main (when you call the super.onCreate). The last setContentView wins, your problem is here.

Android animation fade in / fade out is not setting the visibility of the view in the callback function

I have android view that I want to fadeIn and Fadeout when users click on a button. The animation itself is being called and the callback is being called but when the animation triggers the view cannot be seen. I think the problem is with the xml layout, I have been stuck on this for hours if someone can help it would be greatly appreciated.
My Main Layout: - Just view in question
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|top"
android:background="#color/whiteOverlay"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/uploadOptions"
android:alpha="0"
android:visibility="gone">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<Button
style = "#style/basic_button"
android:text="#string/selectFromGallery"
android:id="#+id/gallery_select"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
style = "#style/basic_button"
android:text="#string/selectFromCamera"
android:id="#+id/camera_select"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
FadeIn
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500" />
</set>
FadeOut
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500" />
</set>
Activity Java -
public class regPP extends Activity {
private String lang = "";
private Boolean optionsActive = false;
private LinearLayout options = null;
private void fade (final View view, final Boolean Toggle){
Animation anim;
view.setVisibility(View.VISIBLE);
if(Toggle){
//fadeOut
anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadeout);
Log.d("FADE","FadeOut");
} else {
//fadeIn
anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadein);
Log.d("FADE","FadeIn");
}
view.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
if(Toggle){
view.setVisibility(View.GONE);
Log.d("FADE", "FadeOut Callback");
} else {
view.setVisibility(View.VISIBLE);
Log.d("FADE", "FadeIn Callback");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.lang = getResources().getString(R.string.lang);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final Context context = regPP.this;
setContentView(R.layout.reg_pp);
ImageButton uploadTog = (ImageButton) findViewById(R.id.uploadTog);
options = (LinearLayout) findViewById(R.id.uploadOptions);
uploadTog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fade(options,false);
optionsActive = true;
}
});
}
#Override
public void onBackPressed() {
if(optionsActive){
fade(options,true);
optionsActive = false;
} else {
super.onBackPressed();
}
}
}
remove this line may help u
android:visibility="gone"
visibility is gone that's why your view is not visible
android:visibility="gone"

onKeydown is not allowed to go back when the dialog is start

i have create simple dialog that showing when i click on the button
every thing is working fine
but i want the onKeydown is not allowed to go back
this is my activity class
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog);
dialog.show();
}
}
and this is the Activity 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"
android:background="#000424"
tools:context=".MainActivity" >
<Button
android:id="#+id/showdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="showdialog" />
</RelativeLayout>
and this is the dialog xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:layout_marginTop="200dp"
android:text="this is the dialog"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="100sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Take a look at onKeyListener:
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.dialog);
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//DO NOTHING
}
return true;
}
});
dialog.show();
}
}

Categories

Resources