Click using RadioButton - android

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"/>

Related

How to change activity on bottom navigation button click?

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!

App Crashes While Trasitioning One Fragment To Another

I am trying to jump from one fragment to another fragment. But whenever i click the button to jump on second fragment my app crashes.
Here are my source files. Please help !
I want to Navigate Tab1 to AlertScreen.
Here is Tab1.java
package com.example.bavarian.sos;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Tab1 extends Fragment {
ImageButton HelpButton ;
private boolean playing = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
HelpButton = (ImageButton) rootView.findViewById(R.id.imageButton);
final MediaPlayer Alarm = MediaPlayer.create(getContext(),R.raw.sound);
//private boolean playing = false ;
HelpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new AlertScreen();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView ;
}
}
AlertScreen.java
package com.example.bavarian.sos;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AlertScreen extends Fragment {
public AlertScreen(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alert_screen,container,false);
return rootView;
}
}
alert_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Alerrt Screen"
android:id="#+id/alerttext"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="176dp" />
</RelativeLayout>
What i am missing here ???

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>

Android ImageButton onClick

I have problem with button in activity_main.xml,
I need to launch by click on "imageButton8" a webView (website URL) in application.
MainActivity.Java
package com.XX.app;
import com.XX.app.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
WebView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Empêcher le téléphone de passer en mode veille
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//When User click on the button imageButton8 the Activity2 launch
Button imageButton8 = (Button) findViewById(R.id.imageButton8);
setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
}
private void setOnClickListener(OnClickListener onClickListener) {
// TODO Auto-generated method stub
}
//loads RETURN URL on lastpage
#Override
public void onBackPressed() {
if(this.view.canGoBack())
{
this.view.goBack();
} else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("إغلاق تطبيق جمعية البر و التعاون")
.setMessage("هل أنت متأكد أنك تريد إغلاق تطبيق جمعية البر و التعاون ؟")
.setPositiveButton("نعم", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("لا", null)
.show();
}
}
}
activity_main.xml for imageButton8 is
<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="#drawable/background_1">
<ImageButton
android:id="#+id/imageButton8"
android:onClick="click"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="380dp"
android:background="#0000"
android:scaleType="fitXY"
android:src="#drawable/btn_www" />
</RelativeLayout>
Activity2.java
package com.XX.app;
import com.XX.app.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Application;
import android.os.Bundle;
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
}
}
activity_webview.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="#drawable/background_1">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
In the same project,
2. Others Issues: I have 08 buttons. How do I execute each button in its template xml?
For example: Button 1 => about_obama.xml. When user click (in this file) it will find the description of obama.
Button 2 => ....xml...etc!
Button 3......
You need to set the onclickListener for the ImageButton using the ImageButton's setOnClickListener() method. So, in your MainActivity, inside your onCreate() method, do the following:
Button imageButton8 = (Button) findViewById(R.id.imageButton8);
// Set the OnClickListener on the button itself
imageButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class); // Use the MainActivity context
startActivityForResult(intent, 0);
}
});
// You should delete the private setOnClickListener() method as it does not
// have any purpose
To specify the onClick() in the xml when the Button is clicked, set the:
android:onClick:"some_method"
property for your button. Then declare and define this method in your Activity.

Acessing the TextView in Fragment from FragmentActivity

I need to setText in TextView inside the Fragment from FragmentActivity.
((TextView) findViewById(R.id.textview_in_fragment)).setText("Text")
in FragmentActivity doesnt works. I found out how to setText in Fragment, but I need to setText in Fragment layout from FragmentActivity. I searched a lot, but I couldnt find a direct answer.
you can try to code below,
just create and method in your fragmet to set the value of it
public void setText(String text)
{
button.setText(text);
}
BasicFragment
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class BasicFragment extends Fragment {
Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_basic, container, false);
button = (Button) view.findViewById(R.id.fragment_button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
"toast_you_just_clicked_a_fragment",
Toast.LENGTH_LONG).show();
}
}
});
return view;
}
public void setText(String text ){
button.setText(text);
}
}
BasicFragmentActivity
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BasicFragmentActivity extends FragmentActivity {
BasicFragment b;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
b.setText("test");
}
});
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
b = new BasicFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, b);
ft.commit();
}
}
}
activity_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_in_fragmentactivity" />
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
fragment_button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#ffffaa">
<Button
android:id="#+id/fragment_button"
android:text="BtnFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Categories

Resources