DialogFragment RadioGroup onClick NullPointerException - android

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();
}
}

Related

I need popup message like map info window

I am unable to find the way to create a popup like info window in map . I have to use it to show the three button inside
Yes
later
No
these three options are clickable.
The pop up will show above three button in vertical manner in recycler view . I have created recycler view and pop up view but how to show it like a Tooltip with clickabe
you can use PopupWindow, its starting with API +23
<?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="wrap_content" android:background="#FFBBFFBB" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Hello My Window" android:textSize="20sp" /> <Button android:id="#+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> </LinearLayout>
class
package com.example.hellopopupwindow;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context mContext = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopupWindow(view);
}
});
}
private void showPopupWindow(View view) {
// A custom layout, as the display content
View contentView = LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);
// Set the button click event
Button button = (Button) contentView.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "button is pressed",
Toast.LENGTH_SHORT).show();
}
});
final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("mengdd", "onTouch : ");
return false;
// It returns true if the words, the touch event will be blocked
// PopupWindow onTouchEvent interception is not called, so click on the external area cannot be dismiss
}
});
// If you do not set the PopupWindow background, both the external region click or Back keys are not dismiss box
// I think there is a bug API
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
// After setting the parameter to show
popupWindow.showAsDropDown(view);
}
}

radiobutton is not selected while select radiobutton in listview

I created custom listview with two radiobutton in radiogroup.
My code getter and setter file
Item_radio.java
package com.example.customlistview_radio;
public class Item_radio
{
public boolean isSelected_radio1;
public boolean isSelected_radio2;
public Item_radio(boolean radio1_status,boolean radio2_status)
{
this.isSelected_radio1=radio1_status;
this.isSelected_radio2=radio2_status;
}
public boolean isSelected_radio1() {
return isSelected_radio1;
}
public void setSelected_radio1(boolean isSelected_radio1) {
this.isSelected_radio1 = isSelected_radio1;
}
public boolean isSelected_radio2() {
return isSelected_radio2;
}
public void setSelected_radio2(boolean isSelected_radio2) {
this.isSelected_radio2 = isSelected_radio2;
}
}
MyRadioAdapter.java
package com.example.customlistview_radio;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
public class MyRadioAdapter extends BaseAdapter
{
Context context;
List<Item_radio> radioList;
public MyRadioAdapter(Context context,List<Item_radio> radiolist)
{
this.context=context;
this.radioList=new ArrayList<Item_radio>();
this.radioList.addAll(radiolist);
}
#Override
public int getCount() {
return radioList.size();
}
#Override
public Object getItem(int position) {
return radioList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
RadioButton radio_button1;
RadioButton radio_button2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View view = convertView;
if(view==null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.radio_button1=(RadioButton)view.findViewById(R.id.my_radio_but1);
holder.radio_button2=(RadioButton)view.findViewById(R.id.my_radio_but2);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
if (position % 2 == 1)
{
view.setBackgroundColor(Color.CYAN);
}
else
{
view.setBackgroundColor(Color.WHITE);
}
final Item_radio radio = (Item_radio) getItem(position);
holder.radio_button1.setChecked(radio.isSelected_radio1());
holder.radio_button2.setChecked(radio.isSelected_radio2());
holder.radio_button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.e("button1","Clicked:button1");
RadioButton rb=(RadioButton)v;
radio.setSelected_radio1(rb.isChecked());
Log.e("button1","Clicked:button1:"+rb.isChecked());
}
});
holder.radio_button2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton rb=(RadioButton)v;
Log.e("button2","Clicked:button2"+rb.isChecked());
radio.setSelected_radio2(rb.isChecked());
}
});
return view;
}
}
MainActivity.java
package com.example.customlistview_radio;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
ListView mylist;
Button submit_but;
ArrayList<Item_radio> radio_list;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylist=(ListView)findViewById(R.id.mylist_demo);
submit_but=(Button)findViewById(R.id.submit_but);
radio_list=new ArrayList<Item_radio>();
for(int i=0;i<20;i++)
{
radio_list.add(new Item_radio(false,false));
}
mylist.setAdapter(new MyRadioAdapter(MainActivity.this, radio_list));
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/my_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="#+id/my_radio_but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio1" />
<RadioButton
android:id="#+id/my_radio_but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio2" />
</RadioGroup>
</RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customlistview_radio.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
/>
<ListView
android:id="#+id/mylist_demo"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
tools:listitem="#layout/list_item"
android:layout_weight="0.3" >
</ListView>
<Button
android:id="#+id/submit_but"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="submit"
android:layout_weight="1.5"/>
</LinearLayout>
While selecting the radiobutton in list row some radiobutton is selected.
If i scroll the listview and select the radio button between last 5 or 6 items it will not selected.
Problem
That is suppose radio button1 is not selected then i choose radiobutton2 it gets selected afterwards radiobutton1 is selected.
My logCat
03-04 10:22:17.221: E/button2(4665): Clicked:button2true
03-04 10:22:20.201: E/button2(4665): Clicked:button2true
03-04 10:22:21.221: E/button2(4665): Clicked:button2true
03-04 10:22:22.101: E/button2(4665): Clicked:button2true
03-04 10:22:23.711: E/button2(4665): Clicked:button2true
03-04 10:22:24.671: E/button2(4665): Clicked:button2true
03-04 10:22:25.711: E/button2(4665): Clicked:button2true
03-04 10:22:40.770: E/button2(4665): Clicked:button2true
03-04 10:22:42.371: E/button2(4665): Clicked:button2false
03-04 10:22:52.271: E/button2(4665): Clicked:button2false
Please see my locat the first 8 rows working properly.
When i click radiobutton2 in 9 th row is not getting selected print checked status false but i select the button and also the checked appearance is not changed still in unselected mode.
Am i doing anything wrong?
Please help me to solve the problem.
Thank you...
One works sometime and same doesn't after, It's all because of you have used setChecked before rest of the codes. so what I can suggest to you is just remove these lines
holder.radio_button1.setChecked(radio.isSelected_radio1());
holder.radio_button2.setChecked(radio.isSelected_radio2());
UPDATE : If this harms your list, then do the following
Just reverse your logic here, After setting the radio_1 checked, set the radio_2 unchecked. Your code looks like this
holder.radio_button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("button1", "Clicked:button1");
RadioButton rb = (RadioButton) v;
radio.setSelected_radio1(rb.isChecked());
radio.setSelected_radio2(!rb.isSelected());
Log.e("button1", "Clicked:button1:" + rb.isChecked());
}
});
holder.radio_button2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton rb=(RadioButton)v;
Log.e("button2","Clicked:button2"+rb.isChecked());
radio.setSelected_radio2(rb.isChecked());
radio.setSelected_radio1(!rb.isSelected());
}
});
UPDATE 2
Whatever currently happening is all because of your view is recycled, The final option you can use is, Remove this if(view==null) condition and matching else clause with it's code as well.
You need to change your code little bit.
Step 1:
Create HashMap in your adapter like this
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();
Step 2:
And inside of getView add the following
if ( selectedItems.get( position ) != null ) {
holder.radio_button1.setChecked(radio.isSelected_radio1()); selectedItems.get( position ) );
}
Step 3:
Inside of your radio button onlick add the following
selectedItems.put( position, your_value);
Note:
Here your_value is 0 or 1. If 1 means radio button selected 0 means not seleced.
Try this let me know if you face any issues.
I am adding this only for radio button 1. First try with this. If it is working then modify your radio button 2
Edit 1:
Why don't you try with Radiogroup like the following?????
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);
}
});

Eclipse Android Checkbox

I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

displaying a string on the textview when clicking a button in android

I'm very new to Android development and just started to study.
What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.
With the help of some experts I created the button and text view.
So the button is showing in the simulator but when I click that button nothing happens.
Can anyone please help me with how can I display the text when pressing the button?
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtView"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="#string/hello" />
<Button
android:id="#+id/mybtn"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
String hello;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn= new Button(this);
txtView=new TextView(this);
mybtn.setOnClickListener(this);
printmyname();
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtView = (TextView)findViewById(R.id.viewwidth);
txtView = (TextView)findViewById(R.id.viewheight);
hello="This is my first project";
//setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
txtView.setText(hello);
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();
}
private void printmyname(){
System.out.println("coming");
}
}
You can do like this:
String hello;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
}
public void onClick(View view){
txtView.setText(hello);
}
Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file.
Hope this will help you.
First create xml file as follows. Create one textview and a button:
main.xml
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The first TextView is created by default. You can leave or remove it if you want.
Next one is to create a button
The next one is TextView where you want to display text.
Now coming to the main activity code...
package com.android.example.simple;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView=(TextView)findViewById(R.id.textView1);
final Button button1 = (Button)findViewById(R.id.mybutton);
//Implement listener for your button so that when you click the
//button, android will listen to it.
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
textView.setText("You clicked the button");
} });
}
}
This is because you DON'T associated the OnClickListener() on the button retrieve from the XML layout.
You don't need to create object because they are already created by the Android system when you inflate XML file (with the Activity.setContentLayout( int resourceLayoutId ) method).
Just retrieve them with the findViewById(...) method.
Just check your code in .java class
You had written below line
mybtn.setOnClickListener(this);
before initializing the mybtn object I mean
mybtn = (Button)findViewById(R.id.mybtn);
just switch this two line or put that line "mybtn.setOnClickListener(this)" after initializing your mybtn object and you will get the answer what you want..
Try this
public void onClick(View view){
txtView.setText("hello");
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show();
}
Also in toast use "Hello"
you use this as txtView.setText("hello");
MainActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
textView1=(TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("TextView displayed Successfully");
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
mybtn .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtView.SetText("Your Message");
}
});
}
Check this:
hello.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
text.setText("hello");
}
});

Android 2.2 requestFocus() and spinners

I'm having problems using s.requestFocus() when s is a spinner. Is there a special treatment to get it to work when it's a spinner ?
Thanks
try this code..
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
I'm guessing you aren't literally just looking to "give focus" to the spinner. When you give focus to an EditText, the keyboard pops up, so you may be expecting the spinner selection to "open up" on focus - but it doesn't work that way (don't ask me why). Use s.performClick() to do this - it will act just as if the user clicked on the spinner control.
use this
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
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;
import android.widget.Toast;
public class dynamic_spinner_main extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_spinner);
///////////////////////////////////////////////////////////////
//grab our UI elements so we can manipulate them (in the case of the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
////////////////////////////////////////////////////////////////
//create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class);
mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position));
System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString());
startActivity(mIntent);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
clearButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
main_spinner.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">
<EditText android:layout_height="wrap_content"
android:layout_margin="4px"
android:id="#+id/newSpinnerItemText"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:id="#+id/AddBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Add To Spinner"></Button>
<Button android:layout_height="wrap_content"
android:id="#+id/ClearBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Clear Spinner Items"></Button>
<Spinner android:layout_height="wrap_content"
android:id="#+id/dynamicSpinner"
android:layout_margin="4px"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>
Spinner isn't focusable by default (source). So you have to make it focusable either in xml
<Spinner
android:id="#+id/spinner"
android:focusable="true"
android:layout_width="wrap_conten"
android:layout_height="wrap_content" />
or in code
findViewById(R.id.spinner).setFocusable(true);

Categories

Resources