How to change activity on bottom navigation button click? - android

I want to use bottom navigation bar in my existing android app but the problem is all screen are activity ,is it possible to load activity without hiding the bottom navigation bar.
example:
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/myScrollingContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your loooooong scrolling content here. -->
</android.support.v4.widget.NestedScrollView>
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
app:bb_tabXmlResource="#xml/bottom_bar"
app:bb_behavior="shy"/>
</android.support.design.widget.CoordinatorLayout>
this is my base activity,
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomBar bottomBar;
bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
#Override
public void onTabSelected(#IdRes int tabId) {
if (tabId == R.id.matching) {
Log.i("matching","matching inside "+tabId);
Intent in=new Intent(getBaseContext(),Main2Activity.class);
startActivity(in);
}else if (tabId == R.id.watchlist) {
Log.i("matching","watchlist inside "+tabId);
Intent in=new Intent(getBaseContext(),Main3Activity.class);
startActivity(in);
}
}
});
}
}
Main2Activity
public class Main2Activity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main2);
NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
View wizard = getLayoutInflater().inflate(R.layout.activity_main2, null);
dynamicContent.addView(wizard);
Main3Activity
public class Main3Activity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main3);
NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
View wizard = getLayoutInflater().inflate(R.layout.activity_main3, null);
dynamicContent.addView(wizard);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bottom.bottomnavigation">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".Main3Activity"></activity>
</application>
</manifest>

I have solved this problem in following way:
1.Create one BaseActivity with bottom nav bar.
package com.example.apple.bottomnavbarwithactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class BaseActivity extends AppCompatActivity {
RadioGroup radioGroup1;
RadioButton deals;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
deals = (RadioButton)findViewById(R.id.deals);
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Intent in;
Log.i("matching", "matching inside1 bro" + checkedId);
switch (checkedId)
{
case R.id.matching:
Log.i("matching", "matching inside1 matching" + checkedId);
in=new Intent(getBaseContext(),MatchingActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.watchList:
Log.i("matching", "matching inside1 watchlistAdapter" + checkedId);
in = new Intent(getBaseContext(), WatchlistActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.rates:
Log.i("matching", "matching inside1 rate" + checkedId);
in = new Intent(getBaseContext(),RatesActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.listing:
Log.i("matching", "matching inside1 listing" + checkedId);
in = new Intent(getBaseContext(), ListingActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.deals:
Log.i("matching", "matching inside1 deals" + checkedId);
in = new Intent(getBaseContext(), DealsActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
default:
break;
}
}
});
}
}
BaseActivity layout named base_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="10dp"
android:background="#color/white"
android:id="#+id/bottonNavBar"
android:paddingTop="5dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:foregroundGravity="bottom">
<RadioGroup
android:id="#+id/radioGroup1"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RadioButton
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:text="Matching"
android:layout_weight="1"
android:button="#null"
android:padding="2dp"
android:checked="false"
android:textSize="12sp"
android:drawableTop="#drawable/selector_matching"
android:textColor="#drawable/selector_nav_text"
android:id="#+id/matching"/>
<RadioButton
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:button="#null"
android:layout_weight="1"
android:padding="2dp"
android:checked="false"
android:textSize="12sp"
android:drawableTop="#drawable/selector_watchlist"
android:textColor="#drawable/selector_nav_text"
android:id="#+id/watchList"
android:text="Watchlist"/>
<RadioButton
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:id="#+id/rates"
android:button="#null"
android:paddingTop="5dp"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:layout_weight="1"
android:checked="false"
android:textSize="12sp"
android:drawableTop="#drawable/selector_rates"
android:textColor="#drawable/selector_nav_text"
android:text="Rates"/>
<RadioButton
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:button="#null"
android:padding="2dp"
android:checked="false"
android:layout_weight="1"
android:textSize="12sp"
android:drawableTop="#drawable/selector_deals"
android:textColor="#drawable/selector_nav_text"
android:id="#+id/deals"
android:text="Deals"/>
<RadioButton
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:button="#null"
android:padding="2dp"
android:checked="false"
android:layout_weight="1"
android:textSize="12sp"
android:drawableTop="#drawable/selector_listing"
android:textColor="#drawable/selector_nav_text"
android:id="#+id/listing"
android:text="Listing"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dynamicContent"
android:orientation="vertical"
android:layout_marginBottom="56dp"
android:background="#color/white"
android:layout_gravity="bottom">
</LinearLayout>
2.extend BaseActivity in all the activity that you want to open on bottom nav click and also need to inflate the activity layouts
For example, I have created five sample activity.
i] MatchingActivity.
package com.example.apple.bottomnavbarwithactivity;
import android.graphics.Color;
import android.os.Bundle;
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.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
//extends our custom BaseActivity
public class MatchingActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_matching);
//dynamically include the current activity layout into baseActivity layout.now all the view of baseactivity is accessible in current activity.
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_matching, null);
dynamicContent.addView(wizard);
//get the reference of RadioGroup.
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.matching);
// Change the corresponding icon and text color on nav button click.
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_matching_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
ii]WatchlistActivity
package com.example.apple.bottomnavbarwithactivity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class WatchlistActivity extends AppCompatActivity {
LinearLayout dynamicContent,bottonNavBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_watchlist1);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_watchlist1, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.watchList);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.favourite_heart_selected, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
iii]RatesActivity
package com.example.apple.bottomnavbarwithactivity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class RatesActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_rates);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_rates, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.rates);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_rate_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
iv] ListingActivity
package com.example.apple.bottomnavbarwithactivity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class ListingActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_listing);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_listing, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.listing);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_listing_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
v] DealsActivity
package com.example.apple.bottomnavbarwithactivity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class DealsActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_deals);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_deals, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.deals);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_deals_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
Note: make sure that you handle back press properly.I have not written any code for back press.

With Activity, you have to declare and init that BottomBar each time you load that activity.
With your problem, my answer is NO.
Btw, you can use Fragment which helps you solve this pretty good.
Time to learn something new, bro.
EDIT
You bring Fragment inside only 1 Activity. And let BottomBar inside Activity while all others view and data set inside Fragment.
Just try it!

Related

Click using RadioButton

fragment2_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="#+id/G1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/R1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="By Specialist" />
<RadioButton
android:id="#+id/R2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="By Doctor"/>
</RadioGroup>
</LinearLayout>
Fragment2.java
package com.example.tabbedapp;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment2 extends Fragment {
public Fragment2(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
Intent intent = new Intent(getActivity(), Specialist.class);
RadioButton button = (RadioButton) rootView.findViewById(R.id.R1);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(intent);
}
});
return rootView;
}
}
By clicking R1 I need to move to Specialist.class page, clicking R2 I need to move to Doctor.class page. How do I implement the code in java file? I have implemented the above code while clicking the RadioButton, it closes the app. Please Can anyone resolve my problem? Is there any mistake in Intent.
enter image description here
You can use like this
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(this,
Specialist.class);
}
Or
final RadioGroup group= (RadioGroup) findViewById(R.id.radioGroupId);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int id = group.getCheckedRadioButtonId();
switch (id) {
case R.id.r1:
// Your code
break;
case R.id.r2:
// Your code
break;
case R.id.r3:
// Your code
break;
case R.id.r4:
// Your code
break;
default:
// Your code
break;
}
}});
I hope, that your classes like Speacialist and Doctor are Activities, not Fragments, so probably you forgot to register your activities in the manifest.
<activity
android:name=".Specialist"/>
<activity
android:name=".Doctor"/>

fragment stuck - can't go to previous fragment

I have a fragment(A) , where another fragment(B) is being opened . When I press back button it just refreshes the fragment(B) instead of exiting from it and returning to fragment A.
I tried poping back the fragment A in the Activity's onBackPressed callback method but it didn't change a thing :
#Override
public void onBackPressed() {
if(B.active)
{
mFragmentManager.popBackStack( A.TAG , 0);
B.active = false;
}
}
** the active boolean is just something I added as part of the solution. It's initialized to TRUE once the fragment is instantiated.
I don't know how to commit a project!
package com.example.a;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/9.
*/
public class ActivityA extends AppCompatActivity{
#InjectView(R.id.c)
LinearLayout c;
#InjectView(R.id.btn)
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
ButterKnife.inject(ActivityA.this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.e, FragA.newInstance(), "A");
// fragmentTransaction.addToBackStack("A");
fragmentTransaction.commitAllowingStateLoss();
}
});
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragA extends Fragment{
private View rootView;
public static FragA newInstance() {
return new FragA();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragA() {
}
#InjectView(R.id.a)
LinearLayout a;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fraga,container,false);
ButterKnife.inject(this,rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.e, FragB.newInstance(), "B");
fragmentTransaction.addToBackStack("B");
fragmentTransaction.commitAllowingStateLoss();
}
});
return rootView;
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragB extends Fragment{
private View rootView;
public static FragB newInstance() {
return new FragB();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragB() {
}
#InjectView(R.id.b)
LinearLayout b;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragb,container,false);
ButterKnife.inject(this, rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
});
return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/c"
android:background="#4b14b1"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/e"
android:layout_width="match_parent"
android:layout_height="300dp">
</FrameLayout>
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="c"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/a"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="A"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/b"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="B"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

App crash using intent to switch activity

It's a simple login activity but when it must switch activity, the app crash.
I tried use also the normal form pubic void onClick() {...} but it doesn't work.
Login.java
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
private Player player;
private Button login;
private EditText Username;
private EditText Password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login=(Button)findViewById(R.id.button_sign_in);
Username=(EditText)findViewById(R.id.nickname);
Password=(EditText)findViewById(R.id.password);
String a = Username.getText().toString();
String b = Password.getText().toString();
player.inserisciCredenziali(a,b); //insert into a player class
//username and password
signIn();
}
public void signIn() {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}
}
activity_login.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.corrado_mattia_danny.face_offbrains.Login"
android:background="#drawable/sfondo_custom" >
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPersonName"
android:hint="Nickname"
android:ems="10"
android:id="#+id/nickname"
android:linksClickable="false"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_below="#+id/nickname"
android:layout_alignStart="#+id/nickname"
android:layout_marginTop="30dp"
android:hint="password"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/button_sign_in"
android:background="#ffffeb00"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/nickname"
android:layout_marginLeft="10dp"
android:onClick="signIn"/>
</RelativeLayout>
Home.java
the buttons in Home doesn't work yet.
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Intent intent = getIntent();
Button bottone_sfida_amico = (Button)findViewById(R.id.button_sfida_amico);
Button bottone_avversario_casuale = (Button)findViewById(R.id.button_avversario_casuale);
}
}
}
When you set a method through the android:onClick attribute, your method signIn() must have a parameter View, which is the source of the event. So, declare the method as follows:
public void signIn(View view) {
...
}
can be 2 things:
1did you declare it on the manifest?
<activity android:name=".SampleActivity" android:screenOrientation="landscape"
android:label="SampleActivity"
/>
and the other reason must be the declaration of your function, when you call it for the layout it should be like this (View v) as param
public void signIn(View v) {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}

Get error value of LinearLayout Height? The value is doubled. Android 4.2.2

I try to get LinearLayout Height on Activity, but I get the height value is doubled. PS:height is 60 but I get 120.
Someone can help?
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pull_to_refresh_head1"
android:layout_width="200dip"
android:layout_height="60dip"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world" />
</LinearLayout>
Java code:
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GdomRefreshableActivity extends Activity {
private LinearLayout l1;
TextView t1;
#Override
//create
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gdom_refreshable);
l1=(LinearLayout)findViewById(R.id.pull_to_refresh_head1);
t1=(TextView)findViewById(R.id.t1);
ViewTreeObserver vto2 = l1.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
l1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int h = l1.getHeight();
int ht = t1.getHeight();
Log.d("GdomRefreshableActivity2",String.valueOf(h));
}
});
}
}

layout elements not showing up

I have two activities and the first is generating properly however the second is not. only the first element in the xml file will show up. here is my xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alternateActivity"
android:shadowColor="#color/shadowColor"></TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/oldActivityButton"
android:text="#string/oldActivityButton"></Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/changeText"
android:text="#string/changeTextButton"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/originalText"
android:id="#+id/textToChange"></TextView>
</LinearLayout>
and here is my activity that corresponds:
package fsg.dev.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class newActivity extends Activity {
private Button oldActivityButton;
private Button changeText;
private TextView textToChange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alternate);
oldActivityButton = (Button) findViewById(R.id.oldActivityButton);
changeText = (Button) findViewById(R.id.changeText);
textToChange = (TextView) findViewById(R.id.textToChange);
oldActivityButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeActivity();
}
});
changeText.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeText();
}
});
}
private void changeActivity(){
Intent i = new Intent(this, activityTest.class);
startActivity(i);
}
private void changeText(){
if(textToChange.equals(R.string.originalText)){
textToChange.setText(R.string.newText);
} else {
textToChange.setText(R.string.originalText);
}
}
}
has anyone else seen this problem? or know of a way to fix it?
Try to add android:orientation="vertical" to your LinearLayout
replace
Intent i = new Intent(this, activityTest.class);
by
Intent i = new Intent().setClass(this, activityTest.class);

Categories

Resources