Different content same page - android

I have an application to display quotes one after another. The page format is same but every time just the content changes. And accordingly the buttons for previous and next pages. Is there a way to do it using xml?

See Bellow Example:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<Button android:onClick="onClickNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click"
/>
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
Your ACtivity:
public class MyActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv;
private String data[4];
private int i = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(r.id.extView1)
data[0] = "hello";
data[1] = "hi";
data[2] = "bye";
data[3] = "fine";
}
public void onClickNext(View v)
{
if(i<data.length)
{
tv.setText(data[i]);
i++;
}
}
}

Define your view with a text view and the two buttons, and use the setText method on the text view to change the text.
Inside your activity, you want something like this:
public void onNextButtonClicked(View view) {
TextView myView = (TextView)this.findViewById(R.id.myTextView);
myView.setText("My next text");
}
Inside your layout XML, something like this would set up the event binding:
<Button
android:id="#+id/nextButton"
android:onClick="onNextButtonClicked"
android:text="#string/next_button"/>

Related

How to create a ListView of Buttons that play different sounds on click

I need to make an Activity with a ListView that could have more than 50 ImageButtons that each play a different Sound.
This is the main activity (that would have the buttons):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.werewolve.freaksound.sounds"
android:background="#drawable/f_background_fit">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="#drawable/f_logo" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/soundList"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
</RelativeLayout>
This is my custom layout for each line of the listview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/play_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#00000000"
android:src="#drawable/f_button_s"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" />
</RelativeLayout>
Each button will play a different sound with the onClick "play_btn" and a text according to the sound with the string "list_item_string".
Example:
* (Laugh Sound) * (Play Button) ***
You should create a custom adapter like so:
public class PlaySoundsAdapter extends BaseAdapter implements View.OnClickListener {
SoundExample[] sounds;
Activity context;
PlaySoundAlert soundPlayerAlert;
public PlaySoundsAdapter(Activity context, SoundExample[] soundsArray) {
this.context = context;
this.sounds = soundsArray;
// Hooks up the PlaySoundAlert.PlaySound in MainActivity
this.soundPlayerAlert = (PlaySoundAlert)context;
}
#Override
public int getCount() {
return sounds == null ? 0 : sounds.length;
}
#Override
public Object getItem(int i) {
return sounds[i];
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
SoundExample item = (SoundExample)getItem(i);
if (view == null) // reuse existing view
view = context.getLayoutInflater().inflate(R.layout.custom_sound_layout,
viewGroup, false);
// Set the TextView to the name of the sound
TextView t = (TextView)view.findViewById(R.id.txtName);
t.setText(item.getSoundName());
// Set the tag of the button to the sound resource id (uri)
Button b = (Button)view.findViewById(R.id.play_btn);
b.setTag(item.getSoundUri());
// When the button is clicked, play the associated sound
b.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
Button b = (Button) view;
if (b != null) {
int soundUri = (int)b.getTag();
// Notify listener (MainActivity) to play the required sound
if (soundPlayerAlert != null) {
soundPlayerAlert.playSound(soundUri);
}
}
}
}
Next create an interface you can implement in your activity to play a sound like this:
public interface PlaySoundAlert {
public void playSound(int uri);
}
As you can see the Adapter I created above uses this interface to fire off an event to play the required sound.
Your SoundExample class might like something like this:
public class SoundExample {
private int soundUri;
private String soundName;
public String getSoundName() {
return soundName;
}
public void setSoundName(String soundName) {
this.soundName = soundName;
}
public int getSoundUri() {
return soundUri;
}
public void setSoundUri(int soundUri) {
this.soundUri = soundUri;
}
}
And to use this inside your Activity or Fragment use the following:
public class MainActivity extends Activity implements PlaySoundAlert {
ListView lstSounds;
PlaySoundsAdapter soundsAdapter;
SoundExample[] mySounds;
// Media player for playing sounds
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstSounds = (ListView) findViewById(R.id.soundList);
// Create sound list & add two SoundExample objects
mySounds = new SoundExample[2];
SoundExample s1 = new SoundExample();
SoundExample s2 = new SoundExample();
// Set sound one to a beep
s1.setSoundName("Beep Sound");
s1.setSoundUri(R.raw.beep);
// Set sound two to an applause sound
s2.setSoundName("Applause Sound");
// NOTE: I am using a sound titled applause.mp3 inside a folder called "raw"
s2.setSoundUri(R.raw.applause);
// Add sounds to the list
mySounds[0] = s1;
mySounds[1] = s2;
// Instantiate the adapter and apply to the ListView
soundsAdapter = new PlaySoundsAdapter(this, mySounds);
lstSounds.setAdapter(soundsAdapter);
}
#Override
public void playSound(int uri) {
// Play sound
mp = MediaPlayer.create(this, uri);
if (!mp.isPlaying())
mp.start();
}
}
And that should be all you need!
The solution would be to create a custom ListView object that has both a TextView for displaying the name of the button and an ImageButton. You then add these objects to your ListView by using a custom Adapter. An alternative to the ImageButton is to simply use a regular image and make it Clickable. A great example for creating a ListView that can display custom objects (including how to create the custom Adapter) may be found here: http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

How to Move cursor from one EditText to another one and back to the first edit text if a custom button is clicked?

i am basically developing a small mathematics app, in a activity their will be problems like additions subtractions etc. the user has to fill the answers in edittext from the custom buttons from 0-9, a dot, a slash button and a backspace button which i created on the same activity. now i like to add up and down button, so that when the up button is pressed the cursor has to move towards upside edit text and vice versa.
here is a sample code which i used
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView et1;
TextView et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(android.R.id.et1);
et2 = (EditText) findViewById(android.R.id.et2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) et2.getText(), et1.getSelectionStart());
et2.requestFocus();
}
});
}
}
You could create an array of of your EditTexts and a variable containing your current position, defaulted to 0, meaning the first text box. Then if the user presses the up button, if the variable is greater than 0, set the position -1 and then get the textbox object from the array and call focus(). Below is an example piece of code, its not accurate but should get you started
List<EditText> textfields = null;
int currentTextFieldPosition = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textfields = new ArrayList<EditText>();
textfield1 = (EditText)findViewById(R.id.textfield1);
.....
textfields.add(textfield1);
......
}
protected OnClickListener mBtnUpClickListener = new OnClickListener()
{
public boolean onClick()
{
if (currentTextfieldPosition > 0)
{
currentTextfieldPosition--;
textfields.get(currentTextFieldPosition).focus()
}
}
}

How to make a TextView Clickable with a extra ID in android

I am developing a Android Application, where i have multiple textView and I want those textview clickable. When I will click those textView I will get a common empty form but with a static ID in the top for every textview.
For example: If I have 3 product name in 3 text view, if I click on textView1, a previously made layout will be shown and there will be a static ID with my product name in the top, then there will be form to filling up the details of the product.If I click on the textView2 then the form will be same only the Static ID and the product name will change.
I hope you guys understood what I wanted to explain.
I am new in application development so I need some simple solution.
So far I have made a layout of my form and I have also made a clickable Layout. So I need to know how I would make the class and function to call the layout and plus the static ID.
Layout of the product name and the Clickable TextView:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/p85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:textStyle="bold"
android:layout_marginTop="12dp"
android:text="#string/p85"
android:ems="7"
android:textSize="35sp"
android:textColor="#375C34"
android:clickable="true"
android:background="#drawable/product_list_view"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
ProductList.java file that call the product_list.xml
import android.app.Activity;
import android.os.Bundle;
public class ProductList extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.product_list);
}
}
In xml in your textview put :
android:onClick="onClick"
And have this
public void onClick(View v) {
switch(v.getId()){
case R.id.TextViewFromXml):
// do something
break;
}
}
Or Remove this
Textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
First set Tag(i.e your Static ID) to TextView and then set Clicklistner
tv.setTag("1234");
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myId=tv.getTag();
}
});

Click on TextView inside inflated Linear Layout

I have an inflated Linear Layout that contains 2 TextViews inside it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_m"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="m"
android:text="m" />
<TextView
android:id="#+id/tv_q"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="q"
android:text="q" />
</LinearLayout>
All i want is that when this Linear Layout is inflated then i want to get the only TEXTVIEW on which i click. For example if i click on "tv_m" then it shall only return me the text of tv_m.
May b its simple but i am not getting a way to it. So i need help.
Thanks
After inflating the layout get the textview objects as below
LinearLayout layout = inflater.inflate(<your layout name>, null);
TextView textView1 = layout.findViewById(R.id.tv_m));
TextView textView2 = layout.findViewById(R.id.tv_q));
String selectedText;
textView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedText = textView1.getText().toString();
}
});
Similarly you can put listener for textView2 also. The selectedText will be the final string which you want.
You need to set up on click listeners for the text views. Then when one is clicked, it will call a function in your code passing it the view that was touched. Then you can call getText on it.
here is the code just check this out :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(your xml to be inflate, null, false);
lt.addView(view);
TextView tv_m = (TextView)view.findViewById( R.id.tv_m);
TextView tv_l = (TextView)view.findViewById( R.id.tv_l);
tv_m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv_m.getText(); // to get the value written on text view
} });
}

Edit text not receiving text

I have tried to solve it for hours but been unable to. What is going on is that I setted this editText, called the layout on the activity, and connect it to my variable on the activity, when you click on the field it opens the visual keyboard, but when you press a key, it takes you to a browser-like search screen, instead of just updating the edittext´s text.
on my xml the editText is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "#drawable/splash"
android:orientation = "vertical">
<EditText
android:id="#+id/searchEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/hintGray"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="10dp"
android:layout_marginTop="300dp"
android:background="#drawable/a01_text_box"
android:hint="Hint"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a01_boton_buscar"
android:layout_gravity="center_horizontal" />
</LinearLayout>
on the activity I do this
public class SubscribeActivity {
private EditText searchEdit;
private ImageButton searchButton;
private Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
this.context = this;
findAndInitViews();
}
protected void findAndInitViews() {
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(context, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
I still can't find a way for it not to happen. Can you help me?
Seems like you are not actually playing with activities
This is wrong:
public class SubscribeActivity
It should be something like:
public class SubscribeActivity extends Activity
first I would suggesting to use the getApplicationContext() Method of Activity - instead of casting the Activity down to your Context-Object. Beside you must extend Activity. Im wondering that your code is running... But I assume it's an copy and paste mistake.
Your onCreate(Bundle b) should be public instead of protected.
If the fault is still there I would comment the code not needed for displaying the EditText. And then step by step including the code back again.
Try it different way.
public class SubscribeActivity extends Activity{
private EditText searchEdit;
private ImageButton searchButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
}
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(SubscribeActivity.this, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
Please try to do in this way and let me know the result.

Categories

Resources