I am trying to achieve a screen like this in Android. I have taken the same code from the site & tried it, but it's not animating the buttons. I'm getting by running the following code
My content_main.xml contains the following code: -
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="meetutu.juspay.com.meetutu.MainActivity"
tools:showIn="#layout/activity_main"
android:background="#2980B9">
<ImageView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/meetutuImageView"
android:src="#drawable/meetutuimage"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/meetutuImageView"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="The joy of meeting an expert teacher"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="for a craving learner"
android:textAlignment="center"
android:textColor="#A3E4D7"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"/>
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/loginButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="LOGIN"
android:layout_marginTop="44dp"
android:layout_below="#+id/textView2"
android:layout_alignLeft="#+id/textView1"
android:layout_alignStart="#+id/textView1" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:id="#+id/signupButton"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="SIGN UP"
android:layout_alignTop="#+id/loginButton"
android:layout_alignRight="#+id/textView1"
android:layout_alignEnd="#+id/textView1" />
</RelativeLayout>
</RelativeLayout>
And my MainActivity.java contains following code:
package meetutu.juspay.com.meetutu;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.*;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView logoImage;
private TextView textView1, textView2;
private ViewGroup container;
public static final int STARTUP_DELAY = 300;
public static final int ANIM_ITEM_DURATION = 1000;
public static final int ITEM_DELAY = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
animation();
super.onWindowFocusChanged(hasFocus);
}
#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 animation()
{
logoImage = (ImageView) findViewById(R.id.meetutuImageView);
container = (ViewGroup) findViewById(R.id.container);
ViewCompat.animate(logoImage)
.translationY(-200)
.setStartDelay(STARTUP_DELAY)
.setDuration(ANIM_ITEM_DURATION)
.setInterpolator(
new DecelerateInterpolator(1.2f)).start();
for(int i = 0; i < container.getChildCount(); i++)
{
View v = container.getChildAt(i);
ViewPropertyAnimatorCompat viewAnimator;
if(!(v instanceof Button))
{
viewAnimator = ViewCompat.animate(v)
.translationY(50).alpha(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(1000);
}
else
{
viewAnimator = ViewCompat.animate(v)
.scaleY(1).scaleX(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(500);
}
viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
}
}
}
style.xml contains the following code:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
I'm new to Android, so I don't know much about types of animation and how they can be implemented. I want to achieve the animation and smoothness given in the link. Please let me know what else I should add to my code to make it work like that.
Thank you for your time!!
Here is example :-
Use these xml and put these 4 into drawable/anim folder
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"
android:startOffset="500" >
</scale>
move_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="10%p"
android:toYDelta="0%p"
android:duration="800"
android:startOffset="0" />
</set>
move_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="-15%p"
android:toYDelta="0%p"
android:duration="500"
android:startOffset="300"/>
</set>
move_down_slow.xml
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="-10%p"
android:toYDelta="0%p"
android:duration="300"
android:startOffset="500" />
</set>
Use this code for the animation in Oncreate() method..
Declare it as globally ......
private Animation animation,animation1,animation2,animation3,animation4;
and this is code ......
animation3 = new AlphaAnimation(0.0f, 1.0f);
animation3.setFillAfter(true);
animation3.setDuration(1200);
mLinear.startAnimation(animation3);
mText.setText("Meetutu");
mText1.setText("The joy of meeting an expert teacher");
mText2.setText("for a craving learner");
animation = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_up);
mText.startAnimation(animation);
animation1 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down);
mText1.startAnimation(animation1);
animation2 = AnimationUtils.loadAnimation(Full_XML_Practise.this, R.anim.move_to_down_slow);
mText2.startAnimation(animation2);
animation4 = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
mButton.startAnimation(animation4);
NOTE:- mText,mText1,mText2 is as TextView , mButton is Button and mLinear is Parent root in the layout.xml file.....
enjoy coding......
Related
I'm trying to make a clickable star button which will have a fill in color of red when clicked on. Before I click the build button, Android Studio does not highlight and error in my program. However, it is after I click the build button that Android Studio shows that everything is wrong with my styles.xml file but I am not sure how I can fix it.
MainActivity.java
package com.pace.importantbutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onDefaultToggleClick(View view){
Toast.makeText(this,"DefaultToggleClick", Toast.LENGTH_SHORT).show();
}
public void onCustomToggleClick(View view){
Toast.makeText(this,"CustomToggle", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="onDefaultToggleClick"
/>
<ToggleButton
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:background="#drawable/toggle_selector"
android:onClick="onCustomToggleClick"
/>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<Style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme will be customized here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</Style>
</resources>
toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_baseline_star_outline_24" android:state_checked="false"></item>
<item android:drawable="#drawable/ic_baseline_star_24" android:state_checked="true"></item>
</selector>
ic_baseline_star_outline_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
ic_baseline_star_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
I do it in my project by using an ImageView.
ImageView favorite = findViewById(R.id.favoriteButton);
//Here you put the logic to start with the outline or the filled star
if (user.isFavorite(productKey)) favorite.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
else favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//Finally you set your listener to change the image
favProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (favorite.getDrawable().getID()==R.drwable.ic_filled_star){
favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//The other things you want to do
}
else{
favProduct.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
//The other things you want to do
}
}
});
I cannot get the bottom navigation to show up in androidx at all. I have even copied examples from other answers that are supposed to work, and they show up in the preview. activity.xlm:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/frameLayout"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/homeButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="a funny item"/>
<item
android:id="#+id/favoritesButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_info_details"
android:title="favorites"/>
<item
android:id="#+id/ideaButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="ideas"/>
</menu>
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
activity code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
What am I missing? Also why would it show up in the preview in android studio and not show when the app runs? Anyhelp is greatly appreciated
Yor forgot add setContentView(R.layout.activity_main); in your MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Im wondering why my bottom navigation bar is not showing up. Can anyone help me out please. Im still learning Android so i apologise in advance if i seem to be asking the wrong questions!
This is my home.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_action_home"
/>
<item
android:id="#+id/Login"
android:title="Login"
android:icon="#drawable/ic_action_login"
/>
<item
android:id="#+id/About"
android:title="About"
android:icon="#drawable/ic_action_about"
/>
</menu>
This is my fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listViewDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This is HomeFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
String[] menuItems = {"Do Something!", "Do something else!", "Do yet another thing!"};
ListView listView = (ListView) view.findViewById(R.id.listViewDetails);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems
);
listView.setAdapter(listViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(getActivity(),"Hi! You CLikced the first item", Toast.LENGTH_LONG).show();
}else if (position == 1) {
Toast.makeText(getActivity(),"Hi! You CLikced the Second item", Toast.LENGTH_LONG).show();
}else if (position == 2) {
Toast.makeText(getActivity(),"Hi! You CLikced the Third item", Toast.LENGTH_LONG).show();
}
}
});
// Inflate the layout for this fragment
return view;
}
}
This is my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sg.edu.rp.c346.a3pdwork">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is LoginFragment.java
package sg.edu.rp.c346.a3pdwork;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
Button b1, b2;
EditText ed1, ed2;
TextView tx1;
int counter = 3;
public LoginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
b1 = findViewByID(R.id.button);
ed1 = findViewByID(R.id.editText);
ed2 = findViewByID(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
This is my fragment_about.xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
This is my fragment_login.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=".LoginFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:text = "Tutor Seeker"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/textview"
android:textSize = "35dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/editText"
android:hint = "Enter Name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Enter Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textview"
android:textSize="25dp"
android:layout_toRightOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textview"
android:layout_toStartOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textview"
android:layout_toEndOf="#+id/textview" />
</RelativeLayout>
This is my AboutFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class AboutFragment extends Fragment {
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
}
This is MainActivity.java file
package sg.edu.rp.c346.a3pdwork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
app:menu="#menu/home"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try to use RelativeLayout instead of LinearLayout like below:
<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=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
...
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
The fragment in your activity_main has android:layout_height="match_parent" and LinearLayout has vertical orientation. This means that your fragment takes all the place on the screen and your bottom navigation menu placed below your fragment. Also, you shouldn't set navigation menu height as match_parent. I suggest you to try to use ConstraintLayout as layout root.
NOTE! I use AndroidX, but it also works for AppCompat.
<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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_menu_height"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
<fragment
android:id="#+id/fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I need to open my phone settings by a click of a menu item "Open Device Settings" Here I had tried something, But when I try to run the project,the app crashes when I clicked the menu item, when I checked the Logcat it shows No activity to handle intent. Should I need to create another activity? Even if I create another activity, I don't know how to proceed further. Please help me friends.
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
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) {
Intent intent = new Intent(Settings.ACTION_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
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"
tools:context="arun.com.androidmenu.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" />
</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"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="arun.com.androidmenu.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
menu_main.xml:
<menu 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"
tools:context="arun.com.androidmenu.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Open Device Settings"
app:showAsAction="never" />
<item
android:id="#+id/action_exit"
android:orderInCategory="100"
android:title="Exit"
app:showAsAction="never" />
</menu>
Here's my coding., I need to open my device settings when I click the Open
Device settings menu item. Please let me know the mistakes what I've done, Thanks.
Use this
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
This would solve your problem.
You can try simple intent:
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I have been working on various Android apps for quite some time now and am trying to finalize them. One of these apps involves creating a text field with a button. Below the button and the text field is some text (aligned left) and a spinner(calendar) which aligned right. Here is my code...
MainActivity.java
package com.miller.lab2;
import android.R;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
Spinner spinner = (Spinner) findViewById(R.id.listofmonths);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.monthlist, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view) {
EditText input = (EditText) findViewById(R.id.text);
String string = input.getText().toString();
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
Resources res = getResources();
String[] monthlist = res.getStringArray(R.array.monthlist);
}
activity_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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.miller.lab2.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="#+id/text"
android:text="#string/enter_message" />
<TextView
style="#+style/CustomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toLeftOf="#+id/button"
android:text="#string/text"
android:textColor="#347C17" />
<!-- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:text="#string/text2" /> -->
<Spinner
android:id="#+id/monthlist"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_message"
android:inputType="text"
android:text="#string/text_field" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text"
android:layout_centerInParent="true"
android:text="#string/button" />
</RelativeLayout>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.miller.lab2.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
I'm getting the following errors in my MainActivty.java:
listofmonths cannot be resolved or is not a field
monthlist cannot be resolved or is not a field
main cannot be resolved or is not a field
action_settings cannot be resolved or is not a field
text cannot be resolved or is not a field
Any help would be greatly appreciated
You are importing the R class from android
Delete this import import android.R; and reimport the R class from your project