Check the radiobutton from the page fragment - android

I have a button , I want when I click it goes to the page fragment The radiobutton is checked , If, for example, it was a hello , The word hello is printed on the textview in page MainActivity
page fragment
<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="com.example.java.frag.BlankFragment">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:text="nice to meet you"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_weight="1" />
<RadioButton
android:text="welcome"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="1" />
<RadioButton
android:text="hello"
android:textStyle="bold"
android:layout_gravity="center"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
page MainActivity
package com.example.java.frag;
import android.app.FragmentTransaction;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView x = (TextView)findViewById(R.id.textView);
}
public void changetext(View view) {
FragmentManager frag = getFragmentManager();
FragmentTransaction tran = frag.beginTransaction();
BlankFragment s = new BlankFragment();
tran.replace(R.id.con,s);
tran.commit();
}
}
How do I do it?
I hope to get an example

First of all, you should be very clear and precise about your query. Next, you should use EventBus here. It really comes handy when callbacks between different components are needed. To use EventBus, you should add following dependency in the app's build.gradle:
compile 'org.greenrobot:eventbus:3.0.0'
Then, you can create a simple POJO class to refer a callback or event. In this case, you can create a class like this:
class OptionItemEvent {
private String option;
public OptionItemEvent(String option){
this.option = option;
}
public String getOption(){
return option;
}
}
In your BlankFragment.java, you've to make a call to the event in the appropriate listener method, for example in this case:
#Override
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) {
RadioButton radioButton = (RadioButton)view.findViewById(radioButtonId);
EventBus.getDefault().post(new OptionItemEvent(radioButton.getText().toString()));
}
In your MainActivity.java, add the following code:
#Override
protected void onResume(){
super.onResume();
EventBus.getDefault().register(this);
}
#Override
protected void onPause(){
EventBus.getDefault().unregister(this);
super.onPause();
}
#Subscribe (threadMode = ThreadMode.MAIN)
public void onOptionItemSelected(OptionItemEvent event){
//Set the value for your TextView
x.setText(event.getOption());
}
I hope this solves your problem.

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

Unfortunately, FindWorkOutActivity has stopped working. How to solve this error?

I am new in Android Learning and try to complete my task, in which i need to create the Workout simple app, with dropdown, button and text. In app preview, app is looking fine but when i run on device it gives me an error "Unfortunately, FindWorkOutActivity has stopped working."
I have searched on internet about this error and unable to understand the Logcat information as i didn't learn about this. Kindly help me to solve this error.
Below are the Files of my app:
activity_find_work_out.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rj.findworkout.FindWorkOutActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_workout_here"
android:id="#+id/text_hello"
android:layout_centerHorizontal="true"
android:layout_below="#+id/button_id"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_id"
android:text="New Button"
android:layout_centerHorizontal="true"
android:layout_below="#+id/spinner_id"
android:layout_marginTop="20dp" />
<Spinner
android:id="#+id/spinner_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:entries="#array/work_out"
android:onClick="onClickWorkOut"
>
</Spinner>
</RelativeLayout>
FindWorkOutActivity.java
package com.example.rj.findworkout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
/**
* Created by Rj on 5/25/2017.
*/
public class FindWorkOutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_work_out);
}
public void onClickWorkOut(View view){
TextView workouts = (TextView) findViewById(R.id.text_hello);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
String workout = String.valueOf(workouttype.getSelectedItem());
workouts.setText(workout);
}
}
Your problem is the onClick property in the Spinner. The proper way is to add setOnItemSelectedListener on it instead. So first you need to remove the
android:onClick="onClickWorkOut"
and in the Activity code, add a OnItemClickListener instead.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
workouttype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

onClickListener not working in Fragment

trying to run a fragment with a button and getting error after the execution:
Following this S.O. answer How to handle button clicks using the XML onClick within Fragments , but not working with me.
DetalhesFragment.java
package com.example.waitersoriginal;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
public class DetalhesFragment extends Fragment implements OnClickListener{
TextView nomeEntrada,descrEntrada,valorEntrada,nmArm,dsArm,vlArm;
String txtNome, txtDescr;
View view,v;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.detalhes_fragment, container, false);
Button mButton = (Button) view.findViewById(R.id.button1);
nomeEntrada= (TextView) view.findViewById(R.id.textView1);
descrEntrada= (TextView)view.findViewById(R.id.textView2);
valorEntrada= (TextView)view.findViewById(R.id.textView3);
mButton.setOnClickListener((android.view.View.OnClickListener) this);
return view;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
}
public void change(String txt, String txt1){
//public void change(String txt, String txt1, String txt2){
//public void change(String txt){
nomeEntrada.setText(txt);
descrEntrada.setText(txt1);
txtNome = txt;
txtDescr = txt1;
//valorEntrada.setText(txt2);
}
}
But this row contains errors:
public class DetalhesFragment extends Fragment implements OnClickListener{
Error: The type DetalhesFragment must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface,int)
I've tried to implement the method above, no success.
Can somebody help me with this?
Ty!
EDIT:
detalhes_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="14dp"
android:textSize="25dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="455dp"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="78dp"
android:textSize="18dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="285dp"
android:layout_y="19dp"
android:textSize="22dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="395dp"
android:text="Adicionar aos Pedidos"
android:visibility="visible" />
</AbsoluteLayout>
Remove this code:
mButton.setOnClickListener((android.view.View.OnClickListener) this);
return view;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
}
Replace this code...
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
});
return view;
}
As the error says your have to implement (add in your class) a method with the name onClick with a parameter of type DialogInterface and another one of type int.
That is because your are implementing an interface which is a kind of contract. All methods of the interface you implement it is mandatory to you to create it on your class even if it has no code at all.
Like:
#Override
public void onClick(DialogInterface dialogInterface, int i){}
EDIT
Sorry, I just saw your code and see the the method is there what you need to do is to add the right parameters. Right now you are passing just a view.
Can you use Linear Layout.
Why did use absolute layout?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" ><TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="14dp"
android:textSize="25dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="455dp"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="78dp"
android:textSize="18dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="285dp"
android:layout_y="19dp"
android:textSize="22dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="395dp"
android:text="Adicionar aos Pedidos"
android:visibility="visible" />
</LinearLayout>`
Can You try this...

Base Activity Button Click's not working in child activities

I am new to android. In my app, having common header.
I have created common header.xml and include that header.xml in all the activites.
But the button clicks [listener] not working.
How to solve the issue. this is the code.
Header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Home" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Accounts" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="History" />
</LinearLayout>
Home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
<EditText
android:id="#+id/txtOTPCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="verify" />
</LinearLayout>
BaseHeaderActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class BaseHeaderActivity extends Activity
{
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
System.out.println("Home button Action");
}
});
}
}
Home Activity.java
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends BaseHeaderActivity
{
Button btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btnVerify = (Button)findViewById(R.id.btnVerify);
}
}
base activities button clicks not working..!
Kindly suggest the solution.
Thanks in advance,
Arun
You don't have to create two different activities for this. Create one activity and use all buttons (including button in header.xml) similarly
public class MainActivity extends Activity {
Button btn1,btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Home button Action", Toast.LENGTH_SHORT).show();
}
});
btnVerify = (Button)findViewById(R.id.btnVerify);
btnVerify.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Button Verify Action", Toast.LENGTH_SHORT).show();
}
});
}
}
You can not use one layout xml file with single activity only.
Note - If you still want to create two different activities - one base and one child then you need to use setContentView() method in base activity with main layout file (Home.xml in your case) and extend it with child activity. Remember in this case dont use setContentView() method in Child Activity. This scenario is not preferred generally unless we want to create several child of same activity.

Bring a view on selecting one view

hii i want to create a ui in which as i select a radio button there should come a textview.
when that button is not selected text view should not be visible. and as button got selected it should be visible..can i implement this??
Inside the listener where you check if the radio button is selected or not:
findViewById(R.id.yourtextview).setVisibility(View.INVISIBLE);
and
findViewById(R.id.yourtextview).setVisibility(View.VISIBLE);
You can choose between INVISIBLE and GONE.
Tutorial to manage radio buttons
Here is sample codes for you...
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioButton radioButton1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioButton1 = (RadioButton) findViewById(R.id.RadioButton1);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(radioButton1.isChecked()) {
findViewById(R.id.textView).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.textView).setVisibility(View.GONE);
}
}
});
}
}
And here is the xml layout: Main.xml
Hope it will help you a lot...
public class _alefon_radio extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private TextView tx;
private RadioGroup rg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx = (TextView) findViewById(R.id.tvv);
rg = (RadioGroup) findViewById(R.id.rgroup);
rg.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
// for R.id.option1
case R.id.option1:
tx.setText("option one is checked");
//tx.setVisibility(0); //visible
break;
default:
tx.setText("");
//tx.setVisibility(4); //invisible
}
}
}
and layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/tvv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
</LinearLayout>
I used edit text instead of setting visbile/invisible but also I included visibilty controls (commented) if you`d like to use this way.
I hope this is what you are looking for.
good luck,

Categories

Resources