In my android project, whenever I insert an hyperlink in my text, the hyperlink works, but the rest of my text just disappears. The hyperlink is shown in the middle of the screen, there is no text visible around it, while I clearly have a lot of text written in my strings.xml.
This is a screenshot on how it looks like:
This is how it looks like in my Graphic layout. Underneath that text is the link:
This is my MainActivity code:
package com.example.rodekruis;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
public class BWCActivity extends Activity {
TextView HyperLink;
Spanned Text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bwc);
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='https://www.rkz.nl/het_kinderbrandwondencentrum'> Kinderbrandwondencentrum </a>";
textView.setText(Html.fromHtml(text));
}
}
This is my layout activity code:
<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.rodekruis.Bezoek" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="#drawable/rkz_logo"
android:layout_gravity="left"
android:layout_marginLeft="38dp"/>
<ScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="364dp" >
<TextView
android:id="#+id/textView"
android:layout_width="244dp"
android:layout_height="276dp"
android:layout_gravity="center"
android:text="#string/title_activity_bwc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</ScrollView>
</LinearLayout>
In my strings I just have a lot of text, it doesn't fit in my screen, that's why I have added a Scrollview. Does anyone have a solution for this?
Replace your ScrollView with this:
<ScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_activity_bwc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</ScrollView>
Related
I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. Here is my code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.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:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}
In my graphic layout, I have a lot of text, with at the very end a hyperlink. Cause the text doesn't completely fit in my layout, I've added a scrollview. Now when I start my emulator, the only thing that gets shown is the hyperlink. No text at all.
This is what I see in my graphic layout before I run my project:
This is when I run my project:
Here is my Main activity code:
package com.example.rodekruis;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
public class BWCActivity extends Activity {
TextView HyperLink;
Spanned Text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bwc);
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='https://www.rkz.nl/het_kinderbrandwondencentrum'> Kinderbrandwondencentrum </a>";
textView.setText(Html.fromHtml(text));
}
}
And this is my activity_main code:
<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.rodekruis.Bezoek" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="#drawable/rkz_logo"
android:layout_gravity="left"
android:layout_marginLeft="38dp"/>
<ScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="364dp" >
<TextView
android:id="#+id/textView"
android:layout_width="244dp"
android:layout_height="276dp"
android:layout_gravity="center"
android:text="#string/title_activity_bwc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
</ScrollView>
</LinearLayout>
You only set the link in the TextView
String text = "<a href='https://www.rkz.nl/het_kinderbrandwondencentrum'> Kinderbrandwondencentrum </a>";
textView.setText(Html.fromHtml(text));
If the text already contains the link use this:
String text = getResources().getString(R.string.title_activity_bwc);
textView.setText(Html.fromHtml(text));
Or if you want to add the link use this:
String text = getResources().getString(R.string.title_activity_bwc);
text += "<a href='https://www.rkz.nl/het_kinderbrandwondencentrum'> Kinderbrandwondencentrum </a>";
textView.setText(Html.fromHtml(text));
I'm trying to make the app show 5 pictures that are include in the drawable file of project, I need when the user tap the "Next" button for first time show the first picture when he tap second show the second picture and so on. but when he tap the "Back" button the previous picture show up, for example the user click 4 times "next" button then when he tap "Back" button it shows the third picture and so on.
JAVA CODE:
package com.example.tables;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory{
ImageSwitcher imgS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgS=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
imgS.setFactory(this);
Animation inShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
imgS.setInAnimation(inShow);
Animation outShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
imgS.setOutAnimation(outShow);
}
#Override
public View makeView() {
ImageView tableImage=new ImageView(getApplicationContext());
tableImage.setScaleType(ImageView.ScaleType.FIT_CENTER);///mal2 alcenter
tableImage.setLayoutParams(new ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT , ActionBar.LayoutParams.WRAP_CONTENT));
return tableImage;
}
public void buttonNext(View view){
imgS.setImageResource(R.drawable.i);
}
public void buttonBack(View view){
imgS.setImageResource(R.drawable.ii);
}
}
XML CODE:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tables.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reserve table" />
<TextView
android:id="#+id/selectTableViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#f00"
android:layout_marginLeft="30dp"
android:text="Please check the button up to Reserve the table">
</TextView>
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="buttonNext"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="buttonBack"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Make an array of the images then add that to your image switcher. You would also need to add some type of current index constant variable so you can index the switching
I have searched high and low to find a simple way to turn buttons into a menu (any menu style) I currently have buttons in my app and I would like to turn them into a menu (i.e. the 3 dots in the right corner) for a more simple UI
I would like the following to appear in a menu:
bCheckOnline
bSettings
bLogout1
If this is alot more complicated than i thought please contact me and I will pay someone to modify my code. (however, any help is appreciated!)
I have included my res/layout/activity_main.xml below:
<?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"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#color/black"
android:paddingBottom="#dimen/sixteen"
android:paddingTop="#dimen/sixteen"
android:weightSum="10" >
<TextView
android:id="#+id/tvSyncDate_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone" />
<TextView
android:id="#+id/tvVersion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9.06"
android:gravity="center_vertical"
android:paddingLeft="13dp"
android:singleLine="true"
android:textColor="#color/white"
android:text="TAP+REPORT"
android:textSize="#dimen/twentysize"
android:textStyle="bold" />
<Button
android:id="#+id/bSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.55"
android:background="#drawable/btn2"
android:text="Sync"
android:textColor="#color/white"
android:textSize="#dimen/twentysize" />
</LinearLayout>
<ScrollView
android:id="#+id/svMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:background="#color/white"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/thirteen"
android:paddingRight="#dimen/thirteen"
android:paddingTop="5dp"
android:text="Tap tag now with top-back of phone and hold to report. Upon completion tap sync."
android:textColor="#color/darkgrey"
android:textSize="#dimen/seventeen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="4.76"
android:gravity="top|center_vertical|center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/myanimation"
android:layout_width="match_parent"
android:layout_height="338dp"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="#anim/anim_android" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="#dimen/twentysize"
android:paddingLeft="#dimen/twentysize"
android:paddingRight="#dimen/twentysize" >
<Button
android:id="#+id/bCheckOnline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8.55"
android:background="#drawable/btn3"
android:text="View Online"
android:textColor="#color/darkgrey"
android:textSize="#dimen/seventeen" />
<Button
android:id="#+id/bLogout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:background="#null"
android:gravity="right|center_horizontal"
android:paddingRight="5dp"
android:text="Logout"
android:textColor="#color/holobluelight"
android:textSize="#dimen/seventeen" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
I have included the java file:
package com.test.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//import android.provider.Contacts.Intents;
import android.widget.TextView;
import java.util.Random;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.net.Uri;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import java.io.IOException;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import android.util.Log;
//import org.ndeftools.Message;
//import org.ndeftools.Record;
//import org.ndeftools.wellknown.SmartPosterRecord;
//import org.ndeftools.wellknown.TextRecord;
//import org.ndeftools.wellknown.UriRecord;
import android.app.PendingIntent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class ScanInputActivity extends Activity implements OnClickListener {
SharedPreferences app_preferences;
TextView tvSyncDate_temp;
Vehicle vehicle;
private NfcAdapter mNfcAdapter;
public static final String MIME_TEXT_PLAIN = "text/plain";
public static final String TAG = "NfcDemo";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
final AnimationDrawable myAnimationDrawable
= (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
Button bSettings= (Button) findViewById(R.id.bSettings);
bSettings.setOnClickListener(this);
Button bCheckOnline= (Button) findViewById(R.id.bCheckOnline);
bCheckOnline.setOnClickListener(this);
Button bLogout1 = (Button) findViewById(R.id.bLogout1);
bLogout1.setOnClickListener(this);
You need to extends ActionBarActivity insted of Activity.
Then make a function for onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Then make onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
// Do something when Settings button is clicked.
return true;
}
return super.onOptionsItemSelected(item);
}
and in you Resourses folder there must be a menu folder with main.xml something like
<menu 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"
tools:context="com.example.test.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
You can play with the above code to experiment with it and modify the same to get your desired results. I hope the information was helpful. Read more about it here.
This is so simple but I have no idea why it's crashing when I click a button.... It's meant to just change the TextView on the activity.
Code is below:::::
MainActivity:
package com.example.databasetut;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void displayText(String message) {
TextView textView = (TextView) findViewById(R.id.textDisplay);
textView.setText(message);
}
public void onClick_AddRecord(View v) {
displayText("Clicked add record");
}
public void onClick_ClearAll(View v) {
displayText("Clicked clear all");
}
public void onClick_DisplayRecords(View v) {
displayText("Clicked display record");
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/textDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.databasetut.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:onClick="onClick_DisplayRecords"
android:text="Display" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onClick_ClearAll"
android:text="Clear" />
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:onClick="onClick_AddRecord"
android:text="Add" />
<TextView
android:id="#+id/textDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
What's the problem here?!
You have given android:id="#+id/textDisplay" to both RelativeLayout and to TextView so error occurs.
Change the id of RelativeLayout or TextView your problem will be solved.
I dont think that you're alowed to use uppercase letters in view Id's in your xml layout. So the fix will be to change android:id="#+id/textDisplay" to ex. android:id="#+id/text_display". Plus remove android:id="#+id/textDisplay" from your RelativeLayout keep this id only on your textview