I have a tabbed activity and in one of the fragments I have a radio group with 3 radio buttons. I am trying to figure out how to determine which radio buttons is selected. I suspect I needed to make a java file for the fragment. I am not sure if that is correct, though. I followed an example for the java file. I modified it and it is not working. Nothing happens when I click on a radio button.
fragment_calories_want.xml (omitted code not relevant to the question)
<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="layout.CaloriesWant">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="100px"
android:id="#+id/radioGroup"
android:orientation="horizontal"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:text="Grams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonGrams"
android:layout_weight="1"
tools:text="Grams" />
<RadioButton
android:text="Ounces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonOunces"
android:layout_weight="1" />
<RadioButton
android:text="Pounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonPounds" />
</RadioGroup>
</RelativeLayout>
CaloriesWantFragment.java
package com.example.crims.caloriecalculator;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
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;
public class CaloriesWantFragment extends Fragment {
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioGroup radioGroupOne;
String buttonSelected;
Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_calories_want, container, false);
radioButton1 = (RadioButton) view.findViewById(R.id.radioButtonGrams);
radioButton2 = (RadioButton) view.findViewById(R.id.radioButtonOunces);
radioButton3 = (RadioButton) view.findViewById(R.id.radioButtonPounds);
radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup);
radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.radioButtonGrams:
buttonSelected = "button one selected";
break;
case R.id.radioButtonOunces:
buttonSelected = "button two selected";
break;
case R.id.radioButtonPounds:
buttonSelected = "button three selected";
break;
default:
}
Toast.makeText(activity, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
Initialize Radio Group
radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup);
java file
public class CaloriesWantFragment extends AppCompatActivity {
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioGroup radioGroupOne;
String buttonSelected;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
radioButton1 = (RadioButton) findViewById(R.id.radioButtonGrams);
radioButton2 = (RadioButton) findViewById(R.id.radioButtonOunces);
radioButton3 = (RadioButton) findViewById(R.id.radioButtonPounds);
radioGroupOne = (RadioGroup) findViewById(R.id.radioGroup);
radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.radioButtonGrams:
buttonSelected = "button one selected";
break;
case R.id.radioButtonOunces:
buttonSelected = "button two selected";
break;
case R.id.radioButtonPounds:
buttonSelected = "button three selected";
break;
default:
}
Toast.makeText(CaloriesWantFragment.this, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show();
}
});
}
Xml file
<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">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="100px"
android:id="#+id/radioGroup"
android:orientation="horizontal"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:text="Grams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonGrams"
android:layout_weight="1"
tools:text="Grams" />
<RadioButton
android:text="Ounces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonOunces"
android:layout_weight="1" />
<RadioButton
android:text="Pounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonPounds" />
</RadioGroup>
</RelativeLayout>
Related
Assume a group of 4 radio buttons. The user clicks on the first one, then he for example realizes that he made a mistake and he clicked on the 4th one. Is there a way to memorize just the last one clicked and to make the app forgets that the 1st one was clicked too?
Here is the simple example using SharedPreferences. Easier, less cumbersome than Sqlite.
Layout
demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rank_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/first_radio"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/first_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First" />
<RadioButton
android:id="#+id/second_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second" />
<RadioButton
android:id="#+id/third_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third" />
<RadioButton
android:id="#+id/fourth_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fourth" />
</RadioGroup>
</LinearLayout>
Activity
DemoAppActivity.java
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
public class DemoAppActivity extends ActionBarActivity implements
OnCheckedChangeListener {
private RadioButton mFirstRadioButton = null;
private RadioButton mSecondRadioButton = null;
private RadioButton mThirdRadioButton = null;
private RadioButton mFourthRadioButton = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_app);
initializeRadioGroup();
}
private void initializeRadioGroup() {
mFirstRadioButton = (RadioButton) findViewById(R.id.first_radio);
mSecondRadioButton = (RadioButton) findViewById(R.id.second_radio);
mThirdRadioButton = (RadioButton) findViewById(R.id.third_radio);
mFourthRadioButton = (RadioButton) findViewById(R.id.fourth_radio);
// Fetching last checked position in preferences
int lastCheckedPosition = PreferenceManager
.getDefaultSharedPreferences(this).getInt("last_checked", 0);
Log.d("TAG", "Fetching last saved position " + lastCheckedPosition);
switch (lastCheckedPosition) {
case 1:
mFirstRadioButton.setChecked(true);
break;
case 2:
mSecondRadioButton.setChecked(true);
break;
case 3:
mThirdRadioButton.setChecked(true);
break;
case 4:
mFourthRadioButton.setChecked(true);
break;
default:
mFirstRadioButton.setChecked(true);
break;
}
mFirstRadioButton.setOnCheckedChangeListener(this);
mSecondRadioButton.setOnCheckedChangeListener(this);
mThirdRadioButton.setOnCheckedChangeListener(this);
mFourthRadioButton.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int newCheckedPos = 0;
if (isChecked) {
switch (buttonView.getId()) {
case R.id.first_radio:
newCheckedPos = 1;
break;
case R.id.second_radio:
newCheckedPos = 2;
break;
case R.id.third_radio:
newCheckedPos = 3;
break;
case R.id.fourth_radio:
newCheckedPos = 4;
break;
}
}
if (newCheckedPos > 0) {
Log.d("TAG", "Saving new checked position " + newCheckedPos);
// Saving checked position in preferences
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt("last_checked", newCheckedPos).commit();
}
}
}
I'm getting an NPE on my code. I'm trying to get the value of the selected RadioButton. When I click the Button the Exception gets fired, don't know why, I could imagine that it takes the wrong view...
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class CategoryDialog extends DialogFragment{
Button btnDone;
RadioGroup rg;
RadioButton radioBtn;
String selectedCategory;
Communicator communicator;
public static interface Communicator{
public void onCategorySelected(String category);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
communicator = (Communicator) activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
btnDone = (Button) view.findViewById(R.id.category_done_btn);
rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
btnDone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
radioBtn = (RadioButton) v.findViewById(selectedId);
selectedCategory = radioBtn.getText().toString();
if(v.getId() == R.id.category_done_btn){
communicator.onCategorySelected(selectedCategory);
dismiss();
}
}
});
return view;
}
}
XML-FILE
<?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="wrap_content" >
<RadioGroup
android:id="#+id/category_radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<RadioButton
android:id="#+id/category_radioGroup_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/z2_videodetail_category_dialog_radioButton1" />
<RadioButton
android:id="#+id/category_radioGroup_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/z2_videodetail_category_dialog_radioButton2" />
<RadioButton
android:id="#+id/category_radioGroup_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/z2_videodetail_category_dialog_radioButton3" />
<RadioButton
android:id="#+id/category_radioGroup_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/z2_videodetail_category_dialog_radioButton4" />
</RadioGroup>
<Button
android:id="#+id/category_done_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/category_radioGroup"
android:text="#string/z2_videodetail_category_dialog_done_btn" />
</RelativeLayout>
Thank you very much, highly appreciating your help! :)
You named your fragment View as view, but you also have the variable view inside of the onClick method referring to the button itself, so when you are finding the radio button by id, the program is looking into the wrong view. Is looking inside the button instead of looking into your fragment. Rename the variable view on the onClick method to avoid this confusion
And you have to declare the view object as final so it's available in the anonymous inner class!
final View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
final RadioGroup rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
#Override
public void onClick(View v) { //Change it from view to v or whatever you want
int selectedId = rg.getCheckedRadioButtonId();
radioBtn = (RadioButton) view.findViewById(selectedId);
selectedCategory = radioBtn.getText().toString();
if(v.getId() == R.id.category_done_btn){ //And update this value
communicator.onCategorySelected(selectedCategory);
dismiss();
}
}
Hi I'm trying to develop an application which runs for every interval time, lets say for every 1 minute it will display some Toast message.
But problem is I'm using RadioButton functionality is perfect but when I tap on one radio button it will be green, but when I close and re-open the activity I'll get as none of the radio buttons selected.
Here is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_one_min:
if (checked)
{
//some code
}
break;
case R.id.radio_ten_min:
if (checked)
{
//some code
}
break;
case R.id.radio_disable:
if (checked)
{
//some code
}
break;
}
}
}
and here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio">
<RadioButton android:id="#+id/radio_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_one_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 minute"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ten_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10 minute"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Please help me to solve this riddle.
Thanks in advance...
This code is useful for store the ratingbar state, when we start new activity, you will see the previous rating state..
package com.example.ratingbar;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class RatingbarMainActivity extends Activity {
RatingBar ratingbarClick;
Button sub_btn;
TextView textRatingView , textRatingViewSave;
Boolean val = true;
float ans = (float) 0.0;
//--------------------------------------------------------------------------------------------
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ratingbar_main);
ratingbarClick = (RatingBar) findViewById(R.id.ratingBar1);
ratingbarClick.setOnRatingBarChangeListener(rateObj);
SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
(RatingbarMainActivity.this);
ans = sharePref.getFloat("Get_Rating", 0.0f);
System.out.println("--------------------------------------ans = " + ans);
if(val) {
ratingbarClick.setRating(ans);
}
else {
ratingbarClick.setRating(ans);
}
textRatingView = (TextView) findViewById(R.id.ratingView);
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
RatingBar.OnRatingBarChangeListener rateObj = new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
//textRatingView.setText(String.valueOf(rating));
ans = ratingbarClick.getRating();
SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
(RatingbarMainActivity.this);
SharedPreferences.Editor edit = sharePref.edit();
edit.putFloat("Get_Rating", ans);
edit.commit();
val = false;
}
};
//--------------------------------------------------------------------------------------------
}
---------------------------------------------------------------------------------------------------
activity_ratingbar_main.xml file
<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" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ratingBar1"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="23dp"
android:text="Select Your Rating Bar Here"
tools:context=".RatingbarMainActivity" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="63dp" />
<TextView
android:id="#+id/ratingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ratingBar1"
android:text="TextView" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Click To Save Rating In TextBox" />
</RelativeLayout>
it is the simplest way to do so,no need of sharedpreference at all.you will get confused while using it.keep the things simple like this
public class MainActivity extends Activity {
Public static int flag=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(flag==1)
radio_one_min.setChecked(true);
else if(flag==2)
radio_ten_min.setCheckek(true);
else if(flag==3)
radio_disable.setCheckek(true);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_one_min:
if (checked)
{
flag =1;
//some code
}
break;
case R.id.radio_ten_min:
if (checked)
{
flag=2 ;
//some code
}
break;
case R.id.radio_disable:
if (checked)
{
flag=3;
//some code
}
break;
}
}
}
[EDITED]
I found developer.android.com/training/basics/activity-lifecycle/recreating.html document first, so my first guess was using Bundles and on Save/Resume Instace State methods. However this does not seem to work well. Here's my final attempt at a working solution (using SharedPreferences class, as suggested by some users):
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends Activity {
final String PREFERENCES = "prefs";
final String RADIO_BUTTON = "prefsval";
SharedPreferences sp;
Editor e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = this.getSharedPreferences(PREFERENCES, MODE_PRIVATE);
e = sp.edit();
}
#Override
protected void onResume() {
super.onResume();
if (sp != null) {
if (sp.getInt(RADIO_BUTTON, 0) != 0) {
RadioButton rb;
rb = (RadioButton) findViewById(sp.getInt(RADIO_BUTTON, 0));
rb.setChecked(true);
}
}
}
public void onRadioButtonClicked(View view) {
e.putInt(RADIO_BUTTON, view.getId());
e.apply();
}
}
hi i want to show the result in edittext 2 only after click the convert button
also
the second button (clear) not shown when i run the program how to show it as the screen size is small
here is the code
package converter.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class now extends Activity {
/** defining variables */
String[] currencys1,currencys2;
String e,l,f,u,d;
double EURO=5,dollar=6,franc=7,LE=1,UAE=8,txtvalue;
EditText txt1,txt2;
Button convert,clear;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 =(EditText)findViewById(R.id.txt1);
txt2 =(EditText)findViewById(R.id.txt2);
convert=(Button)findViewById(R.id.btn1);
clear=(Button)findViewById(R.id.clear_btn);
currencys1=getResources().getStringArray(R.array.currency);
final Spinner s1=(Spinner)findViewById(R.id.sp1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys1);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
switch(arg2)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
currencys2=getResources().getStringArray(R.array.currency1);
final Spinner s2=(Spinner)findViewById(R.id.sp2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys2);
s2.setAdapter(adapter1);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
txtvalue = Double.parseDouble(txt1.getText().toString());
switch(arg2)
{
case 0:
final String fromCurrencyCode = s1.getSelectedItem().toString();
final String toCurrencyCode = s2.getSelectedItem().toString();
if (fromCurrencyCode.equals(toCurrencyCode)) {
txt2.setText(txt1.getText());
}
else
{
double EURO1=txtvalue*EURO;
txt2.setText(String.valueOf(EURO1));
}
break;
case 1:
final String fromCurrencyCode1 = s1.getSelectedItem().toString();
final String toCurrencyCode1 = s2.getSelectedItem().toString();
if (fromCurrencyCode1.equals(toCurrencyCode1)) {
txt2.setText(txt1.getText());
}
else
{
double LE1=txtvalue*LE;
txt2.setText(String.valueOf(LE1));
}
break;
case 2:
final String fromCurrencyCode2 = s1.getSelectedItem().toString();
final String toCurrencyCode2 = s2.getSelectedItem().toString();
if (fromCurrencyCode2.equals(toCurrencyCode2)) {
txt2.setText(txt1.getText());
}
else
{
double franc1=txtvalue*franc;
txt2.setText(String.valueOf(franc1));
}
break;
case 3:
final String fromCurrencyCode3 = s1.getSelectedItem().toString();
final String toCurrencyCode3 = s2.getSelectedItem().toString();
if (fromCurrencyCode3.equals(toCurrencyCode3)) {
txt2.setText(txt1.getText());
}
else
{
double UAE1=txtvalue*UAE;
txt2.setText(String.valueOf(UAE1));
}
break;
case 4:
final String fromCurrencyCode4 = s1.getSelectedItem().toString();
final String toCurrencyCode4 = s2.getSelectedItem().toString();
if (fromCurrencyCode4.equals(toCurrencyCode4)) {
txt2.setText(txt1.getText());
}
else
{
double dollar1=txtvalue*dollar;
txt2.setText(String.valueOf(dollar1));
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//i want to put the code here which show result only after click that button
}
});
clear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txt2.setText(" ");
}
});
}
}
here is the xml
<?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"
android:background="#cad1d9"
>
<TableLayout android:layout_width="match_parent" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="from" android:textColor="#5c6471"></TextView>
</TableLayout>
<Spinner android:id="#+id/sp1" android:layout_height="50dip" android:layout_width="match_parent"></Spinner>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Amount" android:textColor="#5c6471"></TextView>
<EditText android:text="1" android:id="#+id/txt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="decimal"></EditText>
<TextView android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To" android:textColor="#5c6471"></TextView>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/sp2"></Spinner>
<TextView android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value" android:textColor="#5c6471"></TextView>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/txt2" android:text="1" android:editable="false"></EditText>
<Button android:editable="false" android:width="100px" android:gravity="center" android:id="#+id/btn1" android:text="Convert" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="#string/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minWidth="400px" android:id="#+id/clear_btn"></Button>
</LinearLayout>
You can do the following to hide txt2 when you first create the activity:
txt2.setVisibility(View.GONE);
Then in the click handler for the convert button:
txt2.setVisibility(View.VISIBLE);
In the click handler for the clear button, presumably you would want to make it GONE again.
Regarding the problem of the clear button being off the screen: wrap your LinearLayout in a ScrollView (making the ScrollView the top view of the hierarchy with a single LinearLayout child). That way the user can scroll down to see all the content of your layout. You need to change the height of the LinearLayout to wrap_content:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cad1d9"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
. . .
</LinearLayout
</ScrollView>
Hi I am developing a app in which alphabets are not fitting for every device. For HCL ME tablet my design won't fit. For samsung it is working. MY XML file is:
<?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:layout_weight="2" android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_weight="1">
<TextView android:id="#+id/letter1" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
<TextView android:id="#+id/letter2" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip"></TextView>
<TextView android:id="#+id/letter3" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_weight="1">
<ImageView android:id="#+id/imag"
android:gravity="center"
android:scaleType = "fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_gravity="bottom"
android:id="#+id/linearLayout2"
android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="match_parent">
<Button android:id="#+id/previous" android:layout_width="wrap_content" android:layout_weight="1" android:text="Previous" android:layout_height="wrap_content" ></Button>
<Button android:id="#+id/practice" android:layout_width="wrap_content" android:layout_weight="1" android:text="Practice" android:layout_height="wrap_content" android:onClick="onClick"></Button>
<Button android:id="#+id/home" android:layout_width="wrap_content" android:layout_weight="1" android:text="Home" android:layout_height="wrap_content"></Button>
<Button android:id="#+id/spell" android:layout_width="wrap_content" android:layout_weight="1" android:text="Spell" android:layout_height="wrap_content" android:onClick="Content"></Button>
<Button android:id="#+id/next" android:layout_width="wrap_content" android:layout_weight="1" android:text="Next" android:layout_height="wrap_content" android:onClick="Content"></Button>
</LinearLayout>
</LinearLayout>
and my java file is:
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.SimpleGestureFilter.SimpleGestureListener;
public class MyAcivity extends Activity implements SimpleGestureListener {
private SimpleGestureFilter detector;
private static int counter=-1;
private String[] mBtn1 ={"C","D","E","F","G","H","IÄ","J","K","L","M","N","O","CA","CB"};
private TextView txtLetter;
private ImageView imgLetter;
private int[] imgArr={R.drawable.w1,R.drawable.w2,R.drawable.w3,R.drawable.w4,R.drawable.w5,R.drawable.w6,R.drawable.w7,R.drawable.w8,R.drawable.w9,R.drawable.w10,R.drawable.w11,R.drawable.w12,
R.drawable.w13,R.drawable.w14,R.drawable.w15};
private TextView txtKannada;
private String[] mBtn2 = {"CgÀ¸À","DªÉÄ","E°","F±À","GqÀ","Hl","IĶ","J¯É","Kr","LzÀÄ","M¯É","N¯É","OµÀzsÀ",
"CAUÀr","CB"};
private String[] mBtn3 = {"ARASA","AME","ILI","ISA","UDA","UTA","RUSHI","ELE","EDI","AIDU","oLE","OLE","AUSHADA",
"ANGADI","AHA"};
private TextView txtEnglish;
private int[] mAudio = {R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f,R.raw.g,R.raw.h,R.raw.i,R.raw.j,
R.raw.k,R.raw.l,R.raw.m,R.raw.n,R.raw.o};
protected MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
detector = new SimpleGestureFilter(this,this);
if(counter == -1)
counter =getIntent().getExtras().getInt("POSITION");
Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/brhknd.ttf");
txtLetter = (TextView)findViewById(R.id.letter1);
txtKannada = (TextView)findViewById(R.id.letter2);
txtEnglish = (TextView)findViewById(R.id.letter3);
imgLetter = (ImageView)findViewById(R.id.imag);
txtLetter.setTypeface(tf);
txtLetter.setText(mBtn1[counter]);
txtLetter.setTextSize(350);
txtKannada.setTypeface(tf);
txtKannada.setText(mBtn2[counter]);
txtKannada.setTextSize(100);
txtEnglish.setText(mBtn3[counter]);
txtEnglish.setTextSize(50);
Button btnNext = (Button)findViewById(R.id.next);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(counter<imgArr.length-1)
counter++;
changeContent();
}
});
Button mPlay = (Button)findViewById(R.id.spell);
mPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MySwara.this, mAudio[counter]);
mp.start();
}
});
Button btnPrvs = (Button)findViewById(R.id.previous);
btnPrvs.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(counter>0)
counter--;
changeContent();
}
});
Button btnPractice = (Button)findViewById(R.id.practice);
btnPractice.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MySwara.this,DrawingActivity.class);
startActivity(intent);
}
});
Button btnHome = (Button)findViewById(R.id.home);
btnHome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MySwara.this,mainClass.class);
startActivity(intent);
}
});
}
public void changeContent()
{
txtLetter.setText(mBtn1[counter]);
txtKannada.setText(mBtn2[counter]);
txtEnglish.setText(mBtn3[counter]);
//imgLetter.setBackgroundResource(imgArr[counter]);
Bitmap bm = BitmapFactory.decodeResource(getResources(), imgArr[counter]);
imgLetter.setImageBitmap(bm);
}
#Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
#Override
public void onSwipe(int direction) {
String str = "";
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
if(counter>0)
counter--;
changeContent();
break;
case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
if(counter<imgArr.length-1)
counter++;
changeContent();
break;
}
}
}
How i can fit to all devices. Can anyone help?? Thanks in advance.
There are couple of possibilities:
use different xml files for different sizes of screen -> check here http://developer.android.com/guide/practices/screens_support.html
you may use scrollview -> http://developer.android.com/reference/android/widget/ScrollView.html
or simply make your layout fit to the smallest screen size you like to support and then just have "a little" unused space on the larger screens.
It really depends on what you need. Probably the most compatible and advanced solution would be 1.
BUT be aware of the fact that EVERY change on the screen layout has to be duplicated for each xml file following this route!
create xml for each device separately. follow this
second way:
create a parent LinearLayout and pass it to the following method
public static boolean isTabletPC(Context context) {
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getWidth() >= 1200) {
isTabletPC = true;
return isTabletPC;
} else {
isTabletPC = false;
return isTabletPC;
}
}
public static void fixUiDeviceDependencies(
Activity act, LinearLayout llParent) {
if (Utilities.isTabletPC(act)) {
Display display = ((WindowManager) act
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
LayoutParams params = (LayoutParams) llParent
.getLayoutParams();
params.width = display.getWidth() / 2;
llParent.setLayoutParams(params);
}
}